Search in sources :

Example 1 with EntityNotFoundException

use of jakarta.nosql.mapping.EntityNotFoundException in project jnosql-diana by eclipse.

the class AbstractGraphTemplate method edge.

@Override
public <O, I> EdgeEntity edge(O outgoing, String label, I incoming) {
    requireNonNull(incoming, "incoming is required");
    requireNonNull(label, "label is required");
    requireNonNull(outgoing, "outgoing is required");
    checkId(outgoing);
    checkId(incoming);
    if (isIdNull(outgoing)) {
        throw new IllegalStateException("outgoing Id field is required");
    }
    if (isIdNull(incoming)) {
        throw new IllegalStateException("incoming Id field is required");
    }
    Vertex outVertex = getVertex(outgoing).orElseThrow(() -> new EntityNotFoundException("Outgoing entity does not found"));
    Vertex inVertex = getVertex(incoming).orElseThrow(() -> new EntityNotFoundException("Incoming entity does not found"));
    final Predicate<Traverser<Edge>> predicate = t -> {
        Edge e = t.get();
        return e.inVertex().id().equals(inVertex.id()) && e.outVertex().id().equals(outVertex.id());
    };
    Optional<Edge> edge = getTraversal().V(outVertex.id()).out(label).has(id, inVertex.id()).inE(label).filter(predicate).tryNext();
    return edge.<EdgeEntity>map(edge1 -> new DefaultEdgeEntity<>(edge1, incoming, outgoing)).orElseGet(() -> new DefaultEdgeEntity<>(getEdge(label, outVertex, inVertex), incoming, outgoing));
}
Also used : NonUniqueResultException(jakarta.nosql.NonUniqueResultException) Graph(org.apache.tinkerpop.gremlin.structure.Graph) EntityNotFoundException(jakarta.nosql.mapping.EntityNotFoundException) UnaryOperator(java.util.function.UnaryOperator) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ConverterUtil(org.eclipse.jnosql.mapping.util.ConverterUtil) Duration(java.time.Duration) Objects.requireNonNull(java.util.Objects.requireNonNull) StreamSupport(java.util.stream.StreamSupport) Objects.isNull(java.util.Objects.isNull) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Transaction(org.apache.tinkerpop.gremlin.structure.Transaction) Iterator(java.util.Iterator) ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) Predicate(java.util.function.Predicate) Collection(java.util.Collection) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping) Converters(jakarta.nosql.mapping.Converters) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) IdNotFoundException(jakarta.nosql.mapping.IdNotFoundException) Objects(java.util.Objects) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) Stream(java.util.stream.Stream) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) ClassMappings(org.eclipse.jnosql.mapping.reflection.ClassMappings) PreparedStatement(jakarta.nosql.mapping.PreparedStatement) Optional(java.util.Optional) T.id(org.apache.tinkerpop.gremlin.structure.T.id) Collections(java.util.Collections) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) EntityNotFoundException(jakarta.nosql.mapping.EntityNotFoundException) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 2 with EntityNotFoundException

use of jakarta.nosql.mapping.EntityNotFoundException in project jnosql-diana by eclipse.

the class AbstractGraphTemplate method update.

@Override
public <T> T update(T entity) {
    requireNonNull(entity, "entity is required");
    checkId(entity);
    if (isIdNull(entity)) {
        throw new IllegalStateException("to update a graph id cannot be null");
    }
    getVertex(entity).orElseThrow(() -> new EntityNotFoundException("Entity does not find in the update"));
    UnaryOperator<Vertex> update = e -> {
        final Vertex vertex = getConverter().toVertex(entity);
        GraphTransactionUtil.transaction(getGraph().tx());
        return vertex;
    };
    return getFlow().flow(entity, update);
}
Also used : NonUniqueResultException(jakarta.nosql.NonUniqueResultException) Graph(org.apache.tinkerpop.gremlin.structure.Graph) EntityNotFoundException(jakarta.nosql.mapping.EntityNotFoundException) UnaryOperator(java.util.function.UnaryOperator) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ConverterUtil(org.eclipse.jnosql.mapping.util.ConverterUtil) Duration(java.time.Duration) Objects.requireNonNull(java.util.Objects.requireNonNull) StreamSupport(java.util.stream.StreamSupport) Objects.isNull(java.util.Objects.isNull) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Transaction(org.apache.tinkerpop.gremlin.structure.Transaction) Iterator(java.util.Iterator) ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) Predicate(java.util.function.Predicate) Collection(java.util.Collection) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping) Converters(jakarta.nosql.mapping.Converters) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) IdNotFoundException(jakarta.nosql.mapping.IdNotFoundException) Objects(java.util.Objects) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) Stream(java.util.stream.Stream) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) ClassMappings(org.eclipse.jnosql.mapping.reflection.ClassMappings) PreparedStatement(jakarta.nosql.mapping.PreparedStatement) Optional(java.util.Optional) T.id(org.apache.tinkerpop.gremlin.structure.T.id) Collections(java.util.Collections) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) EntityNotFoundException(jakarta.nosql.mapping.EntityNotFoundException)

Aggregations

NonUniqueResultException (jakarta.nosql.NonUniqueResultException)2 Converters (jakarta.nosql.mapping.Converters)2 EntityNotFoundException (jakarta.nosql.mapping.EntityNotFoundException)2 IdNotFoundException (jakarta.nosql.mapping.IdNotFoundException)2 PreparedStatement (jakarta.nosql.mapping.PreparedStatement)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Objects (java.util.Objects)2 Objects.isNull (java.util.Objects.isNull)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Optional (java.util.Optional)2 Function (java.util.function.Function)2 Predicate (java.util.function.Predicate)2 Supplier (java.util.function.Supplier)2 UnaryOperator (java.util.function.UnaryOperator)2 Collectors (java.util.stream.Collectors)2