Search in sources :

Example 1 with MappingException

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;
}
Also used : FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) MappingException(org.neo4j.ogm.exception.core.MappingException)

Example 2 with MappingException

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;
}
Also used : InstantLongConverter(org.neo4j.ogm.typeconversion.InstantLongConverter) Convert(org.neo4j.ogm.annotation.typeconversion.Convert) Instant(java.time.Instant) NumberString(org.neo4j.ogm.annotation.typeconversion.NumberString) DateString(org.neo4j.ogm.annotation.typeconversion.DateString) EnumString(org.neo4j.ogm.annotation.typeconversion.EnumString) InstantStringConverter(org.neo4j.ogm.typeconversion.InstantStringConverter) MappingException(org.neo4j.ogm.exception.core.MappingException) MappingException(org.neo4j.ogm.exception.core.MappingException) DateStringConverter(org.neo4j.ogm.typeconversion.DateStringConverter) EnumStringConverter(org.neo4j.ogm.typeconversion.EnumStringConverter) NumberStringConverter(org.neo4j.ogm.typeconversion.NumberStringConverter) DateLongConverter(org.neo4j.ogm.typeconversion.DateLongConverter)

Example 3 with MappingException

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);
    }
}
Also used : MappingException(org.neo4j.ogm.exception.core.MappingException)

Example 4 with MappingException

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);
    }
}
Also used : EntityAccessManager(org.neo4j.ogm.metadata.reflect.EntityAccessManager) EndNode(org.neo4j.ogm.annotation.EndNode) EntityInstantiator(org.neo4j.ogm.session.EntityInstantiator) java.util(java.util) CompositeAttributeConverter(org.neo4j.ogm.typeconversion.CompositeAttributeConverter) BiFunction(java.util.function.BiFunction) MethodInfo(org.neo4j.ogm.metadata.MethodInfo) LoggerFactory(org.slf4j.LoggerFactory) Property(org.neo4j.ogm.model.Property) Response(org.neo4j.ogm.response.Response) Direction(org.neo4j.ogm.annotation.Relationship.Direction) GraphModel(org.neo4j.ogm.model.GraphModel) Edge(org.neo4j.ogm.model.Edge) PropertyModel(org.neo4j.ogm.response.model.PropertyModel) Node(org.neo4j.ogm.model.Node) EntityUtils(org.neo4j.ogm.utils.EntityUtils) EntityFactory(org.neo4j.ogm.metadata.reflect.EntityFactory) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) MetaData(org.neo4j.ogm.metadata.MetaData) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Consumer(java.util.function.Consumer) StartNode(org.neo4j.ogm.annotation.StartNode) DescriptorMappings(org.neo4j.ogm.metadata.DescriptorMappings) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) MappingException(org.neo4j.ogm.exception.core.MappingException) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) InvocationTargetException(java.lang.reflect.InvocationTargetException) MappingException(org.neo4j.ogm.exception.core.MappingException) MappingException(org.neo4j.ogm.exception.core.MappingException)

Example 5 with MappingException

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();
}
Also used : MetaData(org.neo4j.ogm.metadata.MetaData) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) MappingException(org.neo4j.ogm.exception.core.MappingException)

Aggregations

MappingException (org.neo4j.ogm.exception.core.MappingException)10 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)4 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)3 java.util (java.util)2 Predicate (java.util.function.Predicate)2 Pattern (java.util.regex.Pattern)2 Convert (org.neo4j.ogm.annotation.typeconversion.Convert)2 MetaData (org.neo4j.ogm.metadata.MetaData)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ClassGraph (io.github.classgraph.ClassGraph)1 ScanResult (io.github.classgraph.ScanResult)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1