use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class GraphEntityMapper method mapOneToMany.
/**
* Map many values to an instance based on the relationship type.
* See DATAGRAPH-637
*
* @param instance the instance to map values onto
* @param valueType the type of each value
* @param values the values to map
* @param relationshipType the relationship type associated with these values
*/
private void mapOneToMany(Object instance, Class<?> valueType, Object values, String relationshipType, Direction relationshipDirection) {
ClassInfo classInfo = metadata.classInfo(instance);
FieldInfo writer = EntityAccessManager.getIterableField(classInfo, valueType, relationshipType, relationshipDirection);
if (writer != null) {
if (writer.type().isArray() || Iterable.class.isAssignableFrom(writer.type())) {
FieldInfo reader = EntityAccessManager.getIterableField(classInfo, valueType, relationshipType, relationshipDirection);
Object currentValues;
if (reader != null) {
currentValues = reader.read(instance);
if (writer.type().isArray()) {
values = EntityAccessManager.merge(writer.type(), values, (Object[]) currentValues, valueType);
} else {
values = EntityAccessManager.merge(writer.type(), values, (Collection) currentValues, valueType);
}
}
}
writer.write(instance, values);
return;
}
// this is not necessarily an error. but we can't tell.
logger.debug("Unable to map iterable of type: {} onto property of {}", valueType, classInfo.name());
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class GraphEntityMapper method setLabels.
private void setLabels(Node nodeModel, Object instance) {
ClassInfo classInfo = metadata.classInfo(instance);
FieldInfo labelFieldInfo = classInfo.labelFieldOrNull();
if (labelFieldInfo != null) {
Collection<String> staticLabels = classInfo.staticLabels();
Set<String> dynamicLabels = new HashSet<>();
for (String label : nodeModel.getLabels()) {
if (!staticLabels.contains(label)) {
dynamicLabels.add(label);
}
}
writeProperty(classInfo, instance, PropertyModel.with(labelFieldInfo.getName(), dynamicLabels));
}
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class IdentityMap method hash.
private static long hash(Object object, ClassInfo classInfo) {
List<FieldInfo> hashFields = new ArrayList<>(classInfo.propertyFields());
if (classInfo.labelFieldOrNull() != null) {
hashFields.add(classInfo.labelFieldOrNull());
}
long hash = SEED;
for (FieldInfo fieldInfo : hashFields) {
Object value = fieldInfo.read(object);
if (value != null) {
if (value.getClass().isArray()) {
hash = hash * 31L + hashArray(value);
} else {
hash = hash * 31L + value.hashCode();
}
}
}
return hash;
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class MappingContext method optionalNativeId.
/**
* This method does not trigger a possible {@link IdStrategy} while accessing the identiy- or primary index field.
*
* @param entity The entity in question
* @return An optional native id.
*/
public Optional<Long> optionalNativeId(Object entity) {
ClassInfo classInfo = metaData.classInfo(entity);
if (classInfo == null) {
throw new IllegalArgumentException("Class " + entity.getClass() + " is not a valid entity class. " + "Please check the entity mapping.");
}
if (classInfo.hasIdentityField()) {
FieldInfo identityField = classInfo.identityField();
Object value = identityField.readProperty(entity);
return Optional.ofNullable((Long) value);
} else {
Object primaryIndexValue = classInfo.readPrimaryIndexValueOf(entity);
return Optional.ofNullable(primaryIndexValue).map(v -> primaryIdToNativeId.get(LabelPrimaryId.of(classInfo, v)));
}
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class MappingContext method deregisterDependentRelationshipEntity.
/**
* Deregister a relationship entity if it has either start or end node equal to the supplied startOrEndEntity
*
* @param startOrEndEntity the entity that might be the start or end node of a relationship entity
*/
private void deregisterDependentRelationshipEntity(Object startOrEndEntity) {
Iterator<Long> relationshipEntityIdIterator = relationshipEntityRegister.keySet().iterator();
while (relationshipEntityIdIterator.hasNext()) {
Long relationshipEntityId = relationshipEntityIdIterator.next();
Object relationshipEntity = relationshipEntityRegister.get(relationshipEntityId);
final ClassInfo classInfo = metaData.classInfo(relationshipEntity);
FieldInfo startNodeReader = classInfo.getStartNodeReader();
FieldInfo endNodeReader = classInfo.getEndNodeReader();
if (startOrEndEntity == startNodeReader.read(relationshipEntity) || startOrEndEntity == endNodeReader.read(relationshipEntity)) {
relationshipEntityIdIterator.remove();
}
}
}
Aggregations