Search in sources :

Example 1 with UniqueEntity

use of org.neo4j.graphdb.index.UniqueFactory.UniqueEntity 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

Relationship (org.neo4j.graphdb.Relationship)1 UniqueEntity (org.neo4j.graphdb.index.UniqueFactory.UniqueEntity)1 IndexedEntityRepresentation (org.neo4j.server.rest.repr.IndexedEntityRepresentation)1 InvalidArgumentsException (org.neo4j.server.rest.repr.InvalidArgumentsException)1 RelationshipIndexRepresentation (org.neo4j.server.rest.repr.RelationshipIndexRepresentation)1