use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class MappingContext method generateIdIfNecessary.
private static void generateIdIfNecessary(Object entity, ClassInfo classInfo) {
if (classInfo.idStrategyClass() == null || InternalIdStrategy.class.equals(classInfo.idStrategyClass())) {
return;
}
if (classInfo.idStrategy() == null) {
throw new MappingException("Id strategy " + classInfo.idStrategyClass() + " could not be instantiated " + "and wasn't registered. Either provide no argument constructor or register instance " + "with SessionFactory");
}
FieldInfo primaryIndexField = classInfo.primaryIndexField();
Object existingUuid = classInfo.readPrimaryIndexValueOf(entity);
if (existingUuid == null) {
IdStrategy strategy = classInfo.idStrategy();
Object id = strategy.generateId(entity);
if (strategy instanceof UuidStrategy && primaryIndexField.isTypeOf(String.class)) {
id = id.toString();
}
primaryIndexField.writeDirect(entity, id);
}
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class MappingContext method neighbours.
/**
* Get related objects of an entity / relationship. Used in deletion scenarios.
*
* @param entity The entity to look neighbours for.
* @return If entity is a relationship, end and start nodes. If entity is a node, the relations pointing to it.
*/
public Set<Object> neighbours(Object entity) {
return optionalNativeId(entity).filter(id -> id >= 0).map(id -> {
Set<Object> neighbours = new HashSet<>();
Class<?> type = entity.getClass();
if (!metaData.isRelationshipEntity(type.getName())) {
if (getNodeEntity(id) != null) {
// todo: refactor to create a list of mappedRelationships from a nodeEntity id.
for (MappedRelationship mappedRelationship : relationshipRegister) {
if (mappedRelationship.getStartNodeId() == id || mappedRelationship.getEndNodeId() == id) {
Object affectedObject = mappedRelationship.getEndNodeId() == id ? getNodeEntity(mappedRelationship.getStartNodeId()) : getNodeEntity(mappedRelationship.getEndNodeId());
if (affectedObject != null) {
neighbours.add(affectedObject);
}
}
}
}
} else if (relationshipEntityRegister.containsKey(id)) {
ClassInfo classInfo = metaData.classInfo(type.getName());
FieldInfo startNodeReader = classInfo.getStartNodeReader();
FieldInfo endNodeReader = classInfo.getEndNodeReader();
neighbours.add(startNodeReader.read(entity));
neighbours.add(endNodeReader.read(entity));
}
return neighbours;
}).orElseGet(Collections::emptySet);
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class SingleUseEntityMapper method writeProperty.
private void writeProperty(ClassInfo classInfo, Object instance, Map.Entry<String, Object> property) {
String key = property.getKey();
FieldInfo writer = classInfo.getFieldInfo(key);
if (writer == null) {
// Check relationships
FieldInfo fieldInfo = classInfo.relationshipFieldByName(key);
if (fieldInfo != null) {
writer = fieldInfo;
}
}
boolean isComposite = false;
if (writer == null) {
// Check property maps
Optional<FieldInfo> optionalMatchingComposite = findMatchingCompositeField(classInfo, key);
if (optionalMatchingComposite.isPresent()) {
writer = optionalMatchingComposite.get();
isComposite = true;
}
}
if (writer == null) {
logger.warn("Unable to find property: {} on class: {} for writing", key, classInfo.name());
} else {
// That's what we're gonna write too
Class<?> effectiveFieldType = writer.type();
// This takes attribute and composite converters into consideration.
Class<?> elementType = writer.convertedType();
if (elementType == null) {
// If it is not a converted type, we retrieve the element type (not the field type, which maybe a collection)
elementType = DescriptorMappings.getType(writer.getTypeDescriptor());
}
Predicate<Class<?>> isCollectionLike = c -> c != null && (c.isArray() || Iterable.class.isAssignableFrom(c));
boolean targetIsCollection = isCollectionLike.test(effectiveFieldType);
Object value = property.getValue();
// the field as a collection anyway.
if (!targetIsCollection && GenericUtils.isGenericField(writer.getField()) && value != null && isCollectionLike.test(value.getClass())) {
targetIsCollection = true;
}
if (metadata.classInfo(elementType) != null) {
value = mapKnownEntityType(elementType, key, value, targetIsCollection);
} else if (isComposite) {
value = getAndMergeExistingCompositeValue(instance, key, writer, value);
}
// merge iterable / arrays and co-erce to the correct attribute type
if (targetIsCollection) {
if (value == null) {
value = Collections.emptyList();
} else if (value.getClass().isArray()) {
value = Arrays.asList((Object[]) value);
}
if (effectiveFieldType.isArray()) {
value = EntityAccessManager.merge(effectiveFieldType, value, new Object[] {}, elementType);
} else {
value = EntityAccessManager.merge(effectiveFieldType, value, Collections.emptyList(), elementType);
}
}
writer.write(instance, value);
}
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class Neo4jSession method queryStatementsFor.
//
// These helper methods for the delegates are deliberately NOT defined on the Session interface
//
public <T, ID extends Serializable> QueryStatements<ID> queryStatementsFor(Class<T> type, int depth) {
final FieldInfo fieldInfo = metaData.classInfo(type.getName()).primaryIndexField();
String primaryIdName = fieldInfo != null ? fieldInfo.property() : null;
if (metaData.isRelationshipEntity(type.getName())) {
return new RelationshipQueryStatements<>(primaryIdName, loadRelationshipClauseBuilder(depth));
} else {
return new NodeQueryStatements<>(primaryIdName, loadNodeClauseBuilder(depth));
}
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class AnnotatedFieldAndNonAnnotatedSetterTest method shouldPreferAnnotatedFieldWithNonAnnotatedSetterForRelationshipEntity.
@Test
public void shouldPreferAnnotatedFieldWithNonAnnotatedSetterForRelationshipEntity() {
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());
}
Aggregations