Search in sources :

Example 26 with FieldInfo

use of org.neo4j.ogm.metadata.FieldInfo 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 FieldInfo

use of org.neo4j.ogm.metadata.FieldInfo 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 FieldInfo

use of org.neo4j.ogm.metadata.FieldInfo 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 29 with FieldInfo

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

the class RelationshipDeleteStatements method delete.

@Override
public CypherQuery delete(Long id, Object object, ClassInfo classInfo) {
    FieldInfo versionField = classInfo.getVersionField();
    Long version = (Long) versionField.read(object);
    OptimisticLockingConfig optimisticLockingConfig = new OptimisticLockingConfig(1, classInfo.staticLabels().toArray(new String[] {}), versionField.property());
    Map<String, Object> params = new HashMap<>();
    params.put("id", id);
    params.put("version", version);
    params.put("type", "rel");
    return new DefaultRowModelRequest("MATCH (n)-[r0]->() " + "  WHERE ID(r0) = $id AND r0.`" + versionField.property() + "` = $version " + "SET " + " r0.`" + versionField.property() + "` = r0.`" + versionField.property() + "` + 1 " + "WITH r0 " + " WHERE r0.`" + versionField.property() + "` = $version + 1 " + "DELETE r0 " + // Use DISTINCT because node may have multiple relationships
    "RETURN DISTINCT ID(r0) AS id", params, optimisticLockingConfig);
}
Also used : HashMap(java.util.HashMap) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest)

Example 30 with FieldInfo

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

the class AnnotatedFieldNonAnnotatedSetterAndNonSetterTest method shouldPreferAnnotatedFieldOverNonAnnotatedSetterAndNonSetter.

@Test
public void shouldPreferAnnotatedFieldOverNonAnnotatedSetterAndNonSetter() {
    ClassInfo classInfo = this.domainInfo.getClass(End.class.getName());
    RelEntity relEntity = new RelEntity();
    Set<RelEntity> parameter = new HashSet();
    parameter.addAll(Arrays.asList(relEntity));
    FieldInfo objectAccess = EntityAccessManager.getRelationalWriter(classInfo, "REL_ENTITY_TYPE", Relationship.Direction.INCOMING, relEntity);
    assertThat(objectAccess).as("The resultant object accessor shouldn't be null").isNotNull();
    assertThat(objectAccess instanceof FieldInfo).as("The access mechanism should be via the field").isTrue();
    End end = new End();
    objectAccess.write(end, parameter);
    assertThat(parameter).isEqualTo(end.getRelEntities());
}
Also used : FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

FieldInfo (org.neo4j.ogm.metadata.FieldInfo)142 Test (org.junit.Test)102 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)100 ArrayList (java.util.ArrayList)12 Date (java.util.Date)7 HashSet (java.util.HashSet)6 HashMap (java.util.HashMap)5 Collection (java.util.Collection)4 Satellite (org.neo4j.ogm.domain.satellites.Satellite)4 MappingException (org.neo4j.ogm.exception.core.MappingException)4 CompileContext (org.neo4j.ogm.cypher.compiler.CompileContext)3 Person (org.neo4j.ogm.domain.convertible.enums.Person)3 Member (org.neo4j.ogm.domain.forum.Member)3 Topic (org.neo4j.ogm.domain.forum.Topic)3 Post (org.neo4j.ogm.domain.forum.activity.Post)3 MetaData (org.neo4j.ogm.metadata.MetaData)3 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2