
org.jnosql.artemis.graph.query.AbstractGraphRepositoryProxy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of artemis-graph Show documentation
Show all versions of artemis-graph Show documentation
Eclipse JNoSQL mapping layer, Artemis, to Graph API
The newest version!
/*
* Copyright (c) 2017 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.jnosql.artemis.graph.query;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.jnosql.artemis.Converters;
import org.jnosql.artemis.DynamicQueryException;
import org.jnosql.artemis.Page;
import org.jnosql.artemis.Pagination;
import org.jnosql.artemis.Repository;
import org.jnosql.artemis.graph.GraphConverter;
import org.jnosql.artemis.graph.GraphTemplate;
import org.jnosql.artemis.query.RepositoryType;
import org.jnosql.artemis.reflection.ClassMapping;
import org.jnosql.artemis.reflection.DynamicQueryMethodReturn;
import org.jnosql.artemis.reflection.DynamicReturn;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import static java.util.stream.Collectors.toList;
/**
* Template method to {@link Repository} proxy on Graph
*
* @param the entity type
* @param the K entity
*/
abstract class AbstractGraphRepositoryProxy implements InvocationHandler {
private final SelectQueryConverter converter = new SelectQueryConverter();
private final DeleteQueryConverter deleteConverter = new DeleteQueryConverter();
protected abstract ClassMapping getClassMapping();
protected abstract Repository getRepository();
protected abstract Graph getGraph();
protected abstract GraphConverter getConverter();
protected abstract GraphTemplate getTemplate();
protected abstract Converters getConverters();
@Override
public Object invoke(Object instance, Method method, Object[] args) throws Throwable {
RepositoryType type = RepositoryType.of(method);
Class> typeClass = getClassMapping().getClassInstance();
switch (type) {
case DEFAULT:
return method.invoke(getRepository(), args);
case FIND_BY:
return findBy(method, args, typeClass);
case FIND_ALL:
return findAll(method, typeClass, args);
case DELETE_BY:
return executeDeleteMethod(method, args);
case OBJECT_METHOD:
return method.invoke(this, args);
case UNKNOWN:
case JNOSQL_QUERY:
DynamicQueryMethodReturn methodReturn = DynamicQueryMethodReturn.builder()
.withArgs(args)
.withMethod(method)
.withTypeClass(typeClass)
.withPrepareConverter(q -> getTemplate().prepare(q))
.withQueryConverter(q -> getTemplate().query(q)).build();
return methodReturn.execute();
default:
return Void.class;
}
}
private Object findAll(Method method, Class> typeClass, Object[] args) {
Supplier> querySupplier = () -> {
GraphTraversal traversal = getGraph().traversal().V().hasLabel(getClassMapping().getName());
SelectQueryConverter.setSort(args, traversal);
SelectQueryConverter.setPagination(args, traversal);
return traversal.toList()
.stream()
.map(getConverter()::toEntity)
.collect(toList());
};
return converter(method, typeClass, querySupplier, args);
}
private Object findBy(Method method, Object[] args, Class> typeClass) {
Supplier> querySupplier = () -> {
GraphQueryMethod queryMethod = new GraphQueryMethod(getClassMapping(),
getGraph().traversal().V(),
getConverters(), method, args);
return converter.apply(queryMethod, args)
.stream()
.map(getConverter()::toEntity)
.collect(toList());
};
return converter(method, typeClass, querySupplier, args);
}
private Object converter(Method method, Class> typeClass,
Supplier> querySupplier,
Object[] args) {
Supplier> singleSupplier =
DynamicReturn.toSingleResult(method).apply(querySupplier);
Function> pageFunction = p -> {
throw new DynamicQueryException("Graph database repository does not support Page as return Type");
};
DynamicReturn> dynamicReturn = DynamicReturn.builder()
.withClassSource(typeClass)
.withMethodSource(method)
.withList(querySupplier)
.withSingleResult(singleSupplier)
.withPagination(DynamicReturn.findPagination(args))
.withListPagination(p -> querySupplier.get())
.withSingleResultPagination(p -> singleSupplier.get())
.withPage(pageFunction)
.build();
return dynamicReturn.execute();
}
private Object executeDeleteMethod(Method method, Object[] args) {
GraphQueryMethod queryMethod = new GraphQueryMethod(getClassMapping(),
getGraph().traversal().V(),
getConverters(), method, args);
List vertices = deleteConverter.apply(queryMethod);
vertices.forEach(Vertex::remove);
return Void.class;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy