use of org.neo4j.ogm.metadata.ClassInfo 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.ClassInfo in project neo4j-ogm by neo4j.
the class GraphEntityMapper method mapRelationships.
private Set<Long> mapRelationships(GraphModel graphModel) {
Set<Long> mappedRelationshipIds = new HashSet<>();
List<Edge> oneToMany = new ArrayList<>();
for (Edge edge : graphModel.getRelationships()) {
Object source = mappingContext.getNodeEntity(edge.getStartNode());
Object target = mappingContext.getNodeEntity(edge.getEndNode());
if (source == null || target == null) {
String messageFormat = "Relationship {} cannot be fully hydrated because one or more required node entities have not been part of the result set.";
logger.warn(messageFormat, edge);
} else {
mappedRelationshipIds.add(edge.getId());
// check whether this edge should in fact be handled as a relationship entity
ClassInfo relationshipEntityClassInfo = getRelationshipEntity(edge);
if (relationshipEntityClassInfo != null) {
mapRelationshipEntity(oneToMany, edge, source, target, relationshipEntityClassInfo);
} else {
oneToMany.add(edge);
}
}
}
if (!oneToMany.isEmpty()) {
mapOneToMany(oneToMany);
}
return mappedRelationshipIds;
}
use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.
the class GraphEntityMapper method setProperties.
private void setProperties(List<Property<String, Object>> propertyList, Object instance) {
ClassInfo classInfo = metadata.classInfo(instance);
getCompositeProperties(propertyList, classInfo).forEach((field, v) -> field.write(instance, v));
for (Property<?, ?> property : propertyList) {
writeProperty(classInfo, instance, property);
}
}
use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.
the class GraphEntityMapper method executePostLoad.
private void executePostLoad(Object instance) {
ClassInfo classInfo = metadata.classInfo(instance);
MethodInfo postLoadMethod = classInfo.postLoadMethodOrNull();
if (postLoadMethod == null) {
return;
}
try {
postLoadMethod.invoke(instance);
} catch (SecurityException e) {
logger.warn("Cannot call PostLoad annotated method {} on class {}, " + "security manager denied access.", postLoadMethod.getMethod().getName(), classInfo.name(), e);
} catch (IllegalAccessException | InvocationTargetException e) {
logger.warn("Cannot call PostLoad annotated method {} on class {}. " + "Make sure it is public and has no arguments", postLoadMethod.getMethod().getName(), classInfo.name(), e);
}
}
use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.
the class GraphEntityMapper method mapNodes.
private Set<Long> mapNodes(GraphModel graphModel) {
Set<Long> mappedNodeIds = new LinkedHashSet<>();
for (Node node : graphModel.getNodes()) {
Object entity = mappingContext.getNodeEntity(node.getId());
if (entity == null) {
ClassInfo clsi = metadata.resolve(node.getLabels());
if (clsi == null) {
logger.debug("Could not find a class to map for labels " + Arrays.toString(node.getLabels()));
continue;
}
Map<String, Object> allProps = new HashMap<>(toMap(node.getPropertyList()));
getCompositeProperties(node.getPropertyList(), clsi).forEach((k, v) -> {
allProps.put(k.getName(), v);
});
entity = entityFactory.newObject(clsi.getUnderlyingClass(), allProps);
EntityUtils.setIdentity(entity, node.getId(), metadata);
setProperties(node.getPropertyList(), entity);
setLabels(node, entity);
mappingContext.addNodeEntity(entity, node.getId());
}
mappedNodeIds.add(node.getId());
}
return mappedNodeIds;
}
Aggregations