use of org.neo4j.ogm.exception.core.MappingException in project neo4j-ogm by neo4j.
the class GraphEntityMapper method createRelationshipEntity.
private Object createRelationshipEntity(Edge edge, Object startEntity, Object endEntity) {
ClassInfo relationClassInfo = getRelationshipEntity(edge);
if (relationClassInfo == null) {
throw new MappingException("Could not find a class to map for relation " + edge);
}
Map<String, Object> allProps = new HashMap<>(toMap(edge.getPropertyList()));
getCompositeProperties(edge.getPropertyList(), relationClassInfo).forEach((k, v) -> {
allProps.put(k.getName(), v);
});
// also add start and end node as valid constructor values
allProps.put(relationClassInfo.getStartNodeReader().getName(), startEntity);
allProps.put(relationClassInfo.getEndNodeReader().getName(), endEntity);
// create and hydrate the new RE
Object relationshipEntity = entityFactory.newObject(relationClassInfo.getUnderlyingClass(), allProps);
EntityUtils.setIdentity(relationshipEntity, edge.getId(), metadata);
// REs also have properties
setProperties(edge.getPropertyList(), relationshipEntity);
// register it in the mapping context
mappingContext.addRelationshipEntity(relationshipEntity, edge.getId());
// set the start and end entities
ClassInfo relEntityInfo = metadata.classInfo(relationshipEntity);
FieldInfo startNodeWriter = relEntityInfo.getStartNodeReader();
if (startNodeWriter != null) {
startNodeWriter.write(relationshipEntity, startEntity);
} else {
throw new RuntimeException("Cannot find a writer for the StartNode of relational entity " + relEntityInfo.name());
}
FieldInfo endNodeWriter = relEntityInfo.getEndNodeReader();
if (endNodeWriter != null) {
endNodeWriter.write(relationshipEntity, endEntity);
} else {
throw new RuntimeException("Cannot find a writer for the EndNode of relational entity " + relEntityInfo.name());
}
return relationshipEntity;
}
use of org.neo4j.ogm.exception.core.MappingException in project neo4j-ogm by neo4j.
the class ObjectAnnotations method getConverter.
Object getConverter(Class<?> fieldType) {
// try to get a custom type converter
AnnotationInfo customType = get(Convert.class);
if (customType != null) {
String classDescriptor = customType.get(Convert.CONVERTER, null);
if (classDescriptor == null || Convert.Unset.class.getName().equals(classDescriptor)) {
// will have a default proxy converter applied later on
return null;
}
try {
Class<?> clazz = Class.forName(classDescriptor, false, Configuration.getDefaultClassLoader());
return clazz.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// try to find a pre-registered type annotation. this is very clumsy, but at least it is done only once
AnnotationInfo dateLongConverterInfo = get(DateLong.class);
if (dateLongConverterInfo != null) {
if (fieldType.equals(Instant.class)) {
return new InstantLongConverter();
}
return new DateLongConverter();
}
AnnotationInfo dateStringConverterInfo = get(DateString.class);
if (dateStringConverterInfo != null) {
String format = dateStringConverterInfo.get(DateString.FORMAT, DateString.ISO_8601);
if (fieldType == Date.class) {
return new DateStringConverter(format, isLenientConversion(dateStringConverterInfo));
} else if (fieldType == Instant.class) {
return new InstantStringConverter(format, dateStringConverterInfo.get("zoneId"), isLenientConversion(dateStringConverterInfo));
} else {
throw new MappingException("Cannot use @DateString with attribute of type " + fieldType);
}
}
AnnotationInfo enumStringConverterInfo = get(EnumString.class);
if (enumStringConverterInfo != null) {
String classDescriptor = enumStringConverterInfo.get(EnumString.TYPE, null);
try {
Class clazz = Class.forName(classDescriptor, false, Configuration.getDefaultClassLoader());
return new EnumStringConverter(clazz, isLenientConversion(enumStringConverterInfo));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
AnnotationInfo numberStringConverterInfo = get(NumberString.class);
if (numberStringConverterInfo != null) {
String classDescriptor = numberStringConverterInfo.get(NumberString.TYPE, null);
try {
Class clazz = Class.forName(classDescriptor, false, Configuration.getDefaultClassLoader());
return new NumberStringConverter(clazz, isLenientConversion(numberStringConverterInfo));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return null;
}
use of org.neo4j.ogm.exception.core.MappingException in project neo4j-ogm by neo4j.
the class ReflectionEntityInstantiator method createInstance.
/**
* {@inheritDoc}
*/
@Override
public <T> T createInstance(Class<T> clazz, Map<String, Object> propertyValues) {
try {
Constructor<T> defaultConstructor = clazz.getDeclaredConstructor();
defaultConstructor.setAccessible(true);
return defaultConstructor.newInstance();
} catch (SecurityException | IllegalArgumentException | ReflectiveOperationException e) {
throw new MappingException("Unable to find default constructor to instantiate " + clazz, e);
}
}
use of org.neo4j.ogm.exception.core.MappingException in project neo4j-ogm by neo4j.
the class GraphEntityMapper method mapContentOf.
private void mapContentOf(GraphModel graphModel, BiFunction<GraphModel, Long, Boolean> additionalNodeFilter, Set<Long> returnedNodeIds, Set<Long> mappedRelationshipIds, Set<Long> returnedRelationshipIds, Set<Long> mappedNodeIds) {
Predicate<Long> includeInResult = id -> additionalNodeFilter.apply(graphModel, id);
try {
Set<Long> newNodeIds = mapNodes(graphModel);
returnedNodeIds.addAll(newNodeIds.stream().filter(includeInResult).collect(toList()));
mappedNodeIds.addAll(newNodeIds);
newNodeIds = mapRelationships(graphModel);
returnedRelationshipIds.addAll(newNodeIds.stream().filter(includeInResult).collect(toList()));
mappedRelationshipIds.addAll(newNodeIds);
} catch (MappingException e) {
throw e;
} catch (Exception e) {
throw new MappingException("Error mapping GraphModel", e);
}
}
use of org.neo4j.ogm.exception.core.MappingException in project neo4j-ogm by neo4j.
the class LoadByInstancesDelegate method findCommonClassInfo.
/**
* @param objects
* @param <T>
* @return A class info that is part of the hierarchy of the distinct object types contained in {@code objects}.
*/
private <T> ClassInfo findCommonClassInfo(Collection<T> objects) {
MetaData metaData = session.metaData();
Set<ClassInfo> infos = objects.stream().map(//
Object::getClass).distinct().map(//
metaData::classInfo).map(//
LoadByInstancesDelegate::getRootClassInfo).collect(toSet());
if (infos.size() != 1) {
throw new MappingException("Can't find single supertype for " + infos);
}
return infos.iterator().next();
}
Aggregations