Search in sources :

Example 26 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class LoadOneDelegate method load.

public <T, ID extends Serializable> T load(Class<T> type, ID id, int depth) {
    ClassInfo classInfo = session.metaData().classInfo(type.getName());
    if (classInfo == null) {
        throw new IllegalArgumentException(type + " is not a managed entity.");
    }
    final FieldInfo primaryIndexField = classInfo.primaryIndexField();
    if (primaryIndexField != null && !primaryIndexField.isTypeOf(id.getClass())) {
        throw new IllegalArgumentException("Supplied id does not match primary index type on supplied class " + type.getName());
    }
    if (primaryIndexField == null && !(id instanceof Long)) {
        throw new IllegalArgumentException("Supplied id must be of type Long (native graph id) when supplied class " + "does not have primary id " + type.getName());
    }
    Optional<String> labelsOrType = session.determineLabelsOrTypeForLoading(type);
    if (!labelsOrType.isPresent()) {
        logger.warn("Unable to find database label for entity " + type.getName() + " : no results will be returned. Make sure the class is registered, " + "and not abstract without @NodeEntity annotation");
        return null;
    }
    QueryStatements<ID> queryStatements = session.queryStatementsFor(type, depth);
    PagingAndSortingQuery qry = queryStatements.findOneByType(labelsOrType.get(), convertIfNeeded(classInfo, id), depth);
    GraphModelRequest request = new DefaultGraphModelRequest(qry.getStatement(), qry.getParameters());
    return session.doInTransaction(() -> {
        try (Response<GraphModel> response = session.requestHandler().execute(request)) {
            new GraphRowModelMapper(session.metaData(), session.context(), session.getEntityInstantiator()).map(type, response);
            return lookup(type, id);
        }
    }, Transaction.Type.READ_ONLY);
}
Also used : DefaultGraphModelRequest(org.neo4j.ogm.cypher.query.DefaultGraphModelRequest) GraphRowModelMapper(org.neo4j.ogm.context.GraphRowModelMapper) GraphModelRequest(org.neo4j.ogm.request.GraphModelRequest) DefaultGraphModelRequest(org.neo4j.ogm.cypher.query.DefaultGraphModelRequest) GraphModel(org.neo4j.ogm.model.GraphModel) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) PagingAndSortingQuery(org.neo4j.ogm.cypher.query.PagingAndSortingQuery) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 27 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class SaveEventDelegate method children.

// for a given object parent, returns a list of objects referenced by this parent, i.e. its children
// at this stage, these children may or may not represent related nodes in the graph to the parent node
// We'll figure that out later.
private List<Object> children(Object parent) {
    List<Object> children = new ArrayList<>();
    ClassInfo parentClassInfo = this.session.metaData().classInfo(parent);
    if (parentClassInfo != null) {
        for (FieldInfo reader : parentClassInfo.relationshipFields()) {
            Object reference = reader.read(parent);
            if (reference != null) {
                CollectionUtils.iterableOf(reference).forEach(children::add);
            }
        }
    }
    return children;
}
Also used : ArrayList(java.util.ArrayList) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 28 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class SaveEventDelegate method mapInstance.

// creates a MappedRelationship between the parent object and the reference. In the case that the reference
// object is a RE, the relationship is created from the start node and the end node of the RE.
// a MappedRelationship therefore represents a directed edge between two nodes in the graph.
private void mapInstance(Set<MappedRelationship> mappedRelationships, ClassInfo parentInfo, long parentId, FieldInfo reader, Object reference) {
    String type = reader.relationshipType();
    Direction direction = reader.relationshipDirection();
    ClassInfo referenceInfo = this.session.metaData().classInfo(reference);
    if (referenceInfo == null) {
        return;
    }
    if (referenceInfo.isRelationshipEntity()) {
        // The relationship entity might just get created and therefore we must be careful not to
        // trigger the creation of it's id place holder, otherwise we can't check wether it's new or not.
        Optional<Long> optionalReferenceId = session.context().optionalNativeId(reference);
        // graph relationship is transitive across the RE domain object
        Object startNode = referenceInfo.getStartNodeReader().read(reference);
        ClassInfo startNodeInfo = this.session.metaData().classInfo(startNode);
        Long startNodeId = session.context().nativeId(startNode);
        Object endNode = referenceInfo.getEndNodeReader().read(reference);
        ClassInfo endNodeInfo = this.session.metaData().classInfo(endNode);
        Long endNodeId = session.context().nativeId(endNode);
        MappedRelationship edge = new MappedRelationship(startNodeId, type, endNodeId, optionalReferenceId.orElse(null), startNodeInfo.getUnderlyingClass(), endNodeInfo.getUnderlyingClass());
        mappedRelationships.add(edge);
    } else {
        // We assume the existence of the reference here
        Long referenceId = session.context().nativeId(reference);
        if (direction == Direction.OUTGOING) {
            MappedRelationship edge = new MappedRelationship(parentId, type, referenceId, null, parentInfo.getUnderlyingClass(), referenceInfo.getUnderlyingClass());
            mappedRelationships.add(edge);
        } else {
            MappedRelationship edge = new MappedRelationship(referenceId, type, parentId, null, referenceInfo.getUnderlyingClass(), parentInfo.getUnderlyingClass());
            mappedRelationships.add(edge);
        }
    }
}
Also used : MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Direction(org.neo4j.ogm.annotation.Relationship.Direction) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 29 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class SessionDelegate method resolvePropertyName.

private String resolvePropertyName(Class entityType, String propertyName) {
    ClassInfo classInfo = session.metaData().classInfo(entityType.getName());
    FieldInfo fieldInfo = classInfo.propertyFieldByName(propertyName);
    if (fieldInfo != null && fieldInfo.getAnnotations() != null) {
        AnnotationInfo annotation = fieldInfo.getAnnotations().get(Property.class);
        if (annotation != null) {
            return annotation.get(Property.NAME, propertyName);
        }
    }
    return propertyName;
}
Also used : FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) AnnotationInfo(org.neo4j.ogm.metadata.AnnotationInfo)

Example 30 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class RequestExecutor method initialiseNewEntity.

/**
 * Register entities in the {@link MappingContext}
 *
 * @param persisted entity created as part of the request
 */
private void initialiseNewEntity(Long identity, Object persisted) {
    MappingContext mappingContext = session.context();
    Transaction tx = session.getTransaction();
    if (persisted != null) {
        // it will be null if the variable represents a simple relationship.
        // set the id field of the newly created domain object
        EntityUtils.setIdentity(persisted, identity, session.metaData());
        ClassInfo classInfo = session.metaData().classInfo(persisted);
        if (tx != null) {
            ((AbstractTransaction) tx).registerNew(persisted);
        }
        registerEntity(mappingContext, classInfo, identity, persisted);
    }
}
Also used : MappingContext(org.neo4j.ogm.context.MappingContext) AbstractTransaction(org.neo4j.ogm.transaction.AbstractTransaction) Transaction(org.neo4j.ogm.transaction.Transaction) AbstractTransaction(org.neo4j.ogm.transaction.AbstractTransaction) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Aggregations

ClassInfo (org.neo4j.ogm.metadata.ClassInfo)145 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)100 Test (org.junit.Test)76 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)8 MetaData (org.neo4j.ogm.metadata.MetaData)8 CompileContext (org.neo4j.ogm.cypher.compiler.CompileContext)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 NodeBuilder (org.neo4j.ogm.cypher.compiler.NodeBuilder)5 MappingException (org.neo4j.ogm.exception.core.MappingException)5 RowModel (org.neo4j.ogm.model.RowModel)5 Collection (java.util.Collection)4 LinkedHashSet (java.util.LinkedHashSet)4 List (java.util.List)4 Optional (java.util.Optional)4 CypherQuery (org.neo4j.ogm.cypher.query.CypherQuery)4 Member (org.neo4j.ogm.domain.forum.Member)4 Satellite (org.neo4j.ogm.domain.satellites.Satellite)4 Collections (java.util.Collections)3