use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class GraphEntityMapper method mapRelationshipEntity.
private void mapRelationshipEntity(List<Edge> oneToMany, Edge edge, Object source, Object target, ClassInfo relationshipEntityClassInfo) {
logger.debug("Found relationship type: {} to map to RelationshipEntity: {}", edge.getType(), relationshipEntityClassInfo.name());
// look to see if this relationship already exists in the mapping context.
Object relationshipEntity = mappingContext.getRelationshipEntity(edge.getId());
// do we know about it?
if (relationshipEntity == null) {
// no, create a new relationship entity
relationshipEntity = createRelationshipEntity(edge, source, target);
}
// If the source has a writer for an outgoing relationship for the rel entity, then write the rel entity on the source if it's a scalar writer
ClassInfo sourceInfo = metadata.classInfo(source);
FieldInfo writer = getRelationalWriter(sourceInfo, edge.getType(), Direction.OUTGOING, relationshipEntity);
if (writer == null) {
logger.debug("No writer for {}", target);
} else {
if (writer.forScalar()) {
writer.write(source, relationshipEntity);
mappingContext.addRelationship(new MappedRelationship(edge.getStartNode(), edge.getType(), edge.getEndNode(), edge.getId(), source.getClass(), DescriptorMappings.getType(writer.getTypeDescriptor())));
} else {
oneToMany.add(edge);
}
}
// If the target has a writer for an incoming relationship for the rel entity, then write the rel entity on the target if it's a scalar writer
ClassInfo targetInfo = metadata.classInfo(target);
writer = getRelationalWriter(targetInfo, edge.getType(), Direction.INCOMING, relationshipEntity);
if (writer == null) {
logger.debug("No writer for {}", target);
} else {
if (writer.forScalar()) {
writer.write(target, relationshipEntity);
} else {
oneToMany.add(edge);
}
}
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class GraphEntityMapper method writeProperty.
private void writeProperty(ClassInfo classInfo, Object instance, Property<?, ?> property) {
FieldInfo writer = classInfo.getFieldInfo(property.getKey().toString());
if (writer == null) {
logger.debug("Unable to find property: {} on class: {} for writing", property.getKey(), classInfo.name());
} else {
Object value = property.getValue();
// merge iterable / arrays and co-erce to the correct attribute type
if (writer.type().isArray() || Iterable.class.isAssignableFrom(writer.type())) {
Class<?> paramType = writer.type();
Class elementType = underlyingElementType(classInfo, property.getKey().toString());
if (paramType.isArray()) {
value = EntityAccessManager.merge(paramType, value, new Object[] {}, elementType);
} else {
value = EntityAccessManager.merge(paramType, value, Collections.emptyList(), elementType);
}
}
writer.write(instance, value);
}
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class GraphEntityMapper method underlyingElementType.
private Class underlyingElementType(ClassInfo classInfo, String propertyName) {
FieldInfo fieldInfo = fieldInfoForPropertyName(propertyName, classInfo);
Class clazz = null;
if (fieldInfo != null) {
clazz = DescriptorMappings.getType(fieldInfo.getTypeDescriptor());
}
return clazz;
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class ExecuteQueriesDelegate method countEntitiesOfType.
public long countEntitiesOfType(Class<?> entity) {
ClassInfo classInfo = session.metaData().classInfo(entity.getName());
if (classInfo == null) {
return 0;
}
CypherQuery countStatement;
if (classInfo.isRelationshipEntity()) {
ClassInfo startNodeInfo = null;
ClassInfo endNodeInfo = null;
for (FieldInfo fieldInfo : classInfo.fieldsInfo().fields()) {
if (fieldInfo.hasAnnotation(StartNode.class)) {
startNodeInfo = session.metaData().classInfo(DescriptorMappings.getType(fieldInfo.getTypeDescriptor()).getName());
} else if (fieldInfo.hasAnnotation(EndNode.class)) {
endNodeInfo = session.metaData().classInfo(DescriptorMappings.getType(fieldInfo.getTypeDescriptor()).getName());
}
if (endNodeInfo != null && startNodeInfo != null) {
break;
}
}
String start = startNodeInfo.neo4jName();
String end = endNodeInfo.neo4jName();
String type = classInfo.neo4jName();
countStatement = new CountStatements().countEdges(start, type, end);
} else {
Collection<String> labels = classInfo.staticLabels();
if (labels.isEmpty()) {
return 0;
}
countStatement = new CountStatements().countNodes(labels);
}
return session.doInTransaction(() -> {
try (Response<RowModel> response = session.requestHandler().execute((RowModelRequest) countStatement)) {
RowModel queryResult = response.next();
return queryResult == null ? 0 : ((Number) queryResult.getValues()[0]).longValue();
}
}, Transaction.Type.READ_ONLY);
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class LoadOneDelegate method lookup.
private <T, U> T lookup(Class<T> type, U id) {
Object ref;
ClassInfo typeInfo = session.metaData().classInfo(type.getName());
FieldInfo primaryIndex = typeInfo.primaryIndexField();
if (typeInfo.annotationsInfo().get(RelationshipEntity.class) == null) {
if (primaryIndex == null) {
ref = session.context().getNodeEntity((Long) id);
} else {
ref = session.context().getNodeEntityById(typeInfo, id);
}
} else {
if (primaryIndex == null) {
// Coercing to Long. identityField.convertedType() yields no parameterised type to call cast() with.
// But we know this will always be Long.
ref = session.context().getRelationshipEntity((Long) id);
} else {
ref = session.context().getRelationshipEntityById(typeInfo, id);
}
}
try {
return type.cast(ref);
} catch (ClassCastException cce) {
logger.warn("Could not cast entity {} for id {} to {}", ref, id, type);
return null;
}
}
Aggregations