Search in sources :

Example 11 with IndexedEntityRepresentation

use of org.neo4j.server.rest.repr.IndexedEntityRepresentation in project neo4j by neo4j.

the class DatabaseActions method getOrCreateIndexedRelationship.

public Pair<IndexedEntityRepresentation, Boolean> getOrCreateIndexedRelationship(String indexName, String key, String value, Long relationshipOrNull, Long startNode, String type, Long endNode, Map<String, Object> properties) throws BadInputException, RelationshipNotFoundException, NodeNotFoundException {
    assertIsLegalIndexName(indexName);
    Relationship result;
    boolean created;
    if (relationshipOrNull != null) {
        if (startNode != null || type != null || endNode != null || properties != null) {
            throw new InvalidArgumentsException("Either specify a relationship to index uniquely, " + "or the means for creating it.");
        }
        Relationship relationship = relationship(relationshipOrNull);
        result = graphDb.index().forRelationships(indexName).putIfAbsent(relationship, key, value);
        if (created = result == null) {
            UniqueRelationshipFactory factory = new UniqueRelationshipFactory(indexName, relationship.getStartNode(), relationship.getEndNode(), relationship.getType().name(), properties);
            UniqueEntity<Relationship> entity = factory.getOrCreateWithOutcome(key, value);
            // when given a relationship id, return as created if that relationship was newly added to the index
            created = entity.entity().getId() == relationship.getId() || entity.wasCreated();
            result = entity.entity();
        }
    } else if (startNode == null || type == null || endNode == null) {
        throw new InvalidArgumentsException("Either specify a relationship to index uniquely, " + "or the means for creating it.");
    } else {
        UniqueRelationshipFactory factory = new UniqueRelationshipFactory(indexName, node(startNode), node(endNode), type, properties);
        UniqueEntity<Relationship> entity = factory.getOrCreateWithOutcome(key, value);
        result = entity.entity();
        created = entity.wasCreated();
    }
    return Pair.of(new IndexedEntityRepresentation(result, key, value, new RelationshipIndexRepresentation(indexName, Collections.<String, String>emptyMap())), created);
}
Also used : RelationshipIndexRepresentation(org.neo4j.server.rest.repr.RelationshipIndexRepresentation) UniqueEntity(org.neo4j.graphdb.index.UniqueFactory.UniqueEntity) Relationship(org.neo4j.graphdb.Relationship) IndexedEntityRepresentation(org.neo4j.server.rest.repr.IndexedEntityRepresentation) InvalidArgumentsException(org.neo4j.server.rest.repr.InvalidArgumentsException)

Aggregations

IndexedEntityRepresentation (org.neo4j.server.rest.repr.IndexedEntityRepresentation)11 Transaction (org.neo4j.graphdb.Transaction)5 Test (org.junit.Test)4 NodeIndexRepresentation (org.neo4j.server.rest.repr.NodeIndexRepresentation)4 NodeRepresentationTest (org.neo4j.server.rest.repr.NodeRepresentationTest)4 RelationshipIndexRepresentation (org.neo4j.server.rest.repr.RelationshipIndexRepresentation)4 RelationshipRepresentationTest (org.neo4j.server.rest.repr.RelationshipRepresentationTest)4 Node (org.neo4j.graphdb.Node)3 Relationship (org.neo4j.graphdb.Relationship)3 NotFoundException (org.neo4j.graphdb.NotFoundException)2 IterableWrapper (org.neo4j.helpers.collection.IterableWrapper)2 EndNodeNotFoundException (org.neo4j.server.rest.domain.EndNodeNotFoundException)2 StartNodeNotFoundException (org.neo4j.server.rest.domain.StartNodeNotFoundException)2 ConstraintDefinitionRepresentation (org.neo4j.server.rest.repr.ConstraintDefinitionRepresentation)2 DatabaseRepresentation (org.neo4j.server.rest.repr.DatabaseRepresentation)2 IndexDefinitionRepresentation (org.neo4j.server.rest.repr.IndexDefinitionRepresentation)2 IndexRepresentation (org.neo4j.server.rest.repr.IndexRepresentation)2 InvalidArgumentsException (org.neo4j.server.rest.repr.InvalidArgumentsException)2 ListRepresentation (org.neo4j.server.rest.repr.ListRepresentation)2 NodeIndexRootRepresentation (org.neo4j.server.rest.repr.NodeIndexRootRepresentation)2