Search in sources :

Example 1 with ProxyAttributeConverter

use of org.neo4j.ogm.typeconversion.ProxyAttributeConverter in project neo4j-ogm by neo4j.

the class DomainInfo method registerDefaultFieldConverters.

private void registerDefaultFieldConverters(ClassInfo classInfo, FieldInfo fieldInfo) {
    if (!fieldInfo.hasPropertyConverter() && !fieldInfo.hasCompositeConverter()) {
        final String typeDescriptor = fieldInfo.getTypeDescriptor();
        // Check if there's a registered set of attribute converters for the given field info and if so,
        // select the correct one based on the features of the field
        Function<AttributeConverters, Optional<AttributeConverter<?, ?>>> selectAttributeConverter = ac -> DomainInfo.selectAttributeConverterFor(fieldInfo, ac);
        Optional<AttributeConverter<?, ?>> registeredAttributeConverter = ConvertibleTypes.REGISTRY.entrySet().stream().filter(e -> typeDescriptor.contains(e.getKey())).sorted(comparingInt((Map.Entry<String, ?> e) -> e.getKey().length()).reversed()).findFirst().map(Map.Entry::getValue).flatMap(selectAttributeConverter);
        boolean isSupportedNativeType = typeSystem.supportsAsNativeType(DescriptorMappings.getType(fieldInfo.getTypeDescriptor()));
        // We can use a registered converter
        if (registeredAttributeConverter.isPresent() && !isSupportedNativeType) {
            fieldInfo.setPropertyConverter(registeredAttributeConverter.get());
        } else {
            // Check if the user configured one through the convert annotation
            if (fieldInfo.getAnnotations().get(Convert.class) != null) {
                // no converter's been set but this method is annotated with @Convert so we need to proxy it
                Class<?> entityAttributeType = DescriptorMappings.getType(typeDescriptor);
                String graphTypeDescriptor = fieldInfo.getAnnotations().get(Convert.class).get(Convert.GRAPH_TYPE, null);
                if (graphTypeDescriptor == null) {
                    throw new MappingException("Found annotation to convert a " + (entityAttributeType != null ? entityAttributeType.getName() : " null object ") + " on " + classInfo.name() + '.' + fieldInfo.getName() + " but no target graph property type or specific AttributeConverter have been specified.");
                }
                fieldInfo.setPropertyConverter(new ProxyAttributeConverter(entityAttributeType, DescriptorMappings.getType(graphTypeDescriptor), this.conversionCallbackRegistry));
            }
            Class fieldType = DescriptorMappings.getType(typeDescriptor);
            if (fieldType == null) {
                throw new RuntimeException("Class " + classInfo.name() + " field " + fieldInfo.getName() + " has null field type.");
            }
            boolean enumConverterSet = false;
            for (Class enumClass : enumTypes) {
                if (fieldType.equals(enumClass)) {
                    setEnumFieldConverter(fieldInfo, enumClass);
                    enumConverterSet = true;
                    break;
                }
            }
            if (!enumConverterSet && isEnum(fieldType)) {
                LOGGER.debug("Setting default enum converter for unscanned class " + classInfo.name() + ", field: " + fieldInfo.getName());
                setEnumFieldConverter(fieldInfo, fieldType);
            }
        }
    }
}
Also used : ClassGraph(io.github.classgraph.ClassGraph) java.util(java.util) RelationshipEntity(org.neo4j.ogm.annotation.RelationshipEntity) LoggerFactory(org.slf4j.LoggerFactory) ConversionCallback(org.neo4j.ogm.typeconversion.ConversionCallback) Function(java.util.function.Function) ConvertibleTypes(org.neo4j.ogm.typeconversion.ConvertibleTypes) AttributeConverters(org.neo4j.ogm.typeconversion.AttributeConverters) ScanResult(io.github.classgraph.ScanResult) NodeEntity(org.neo4j.ogm.annotation.NodeEntity) NoNativeTypes(org.neo4j.ogm.driver.TypeSystem.NoNativeTypes) Convert(org.neo4j.ogm.annotation.typeconversion.Convert) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ProxyAttributeConverter(org.neo4j.ogm.typeconversion.ProxyAttributeConverter) InputStreamReader(java.io.InputStreamReader) ConversionCallbackRegistry(org.neo4j.ogm.typeconversion.ConversionCallbackRegistry) ClassUtils(org.neo4j.ogm.support.ClassUtils) TypeSystem(org.neo4j.ogm.driver.TypeSystem) Configuration(org.neo4j.ogm.config.Configuration) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) MappingException(org.neo4j.ogm.exception.core.MappingException) Comparator(java.util.Comparator) AttributeConverter(org.neo4j.ogm.typeconversion.AttributeConverter) InputStream(java.io.InputStream) Convert(org.neo4j.ogm.annotation.typeconversion.Convert) AttributeConverters(org.neo4j.ogm.typeconversion.AttributeConverters) ProxyAttributeConverter(org.neo4j.ogm.typeconversion.ProxyAttributeConverter) MappingException(org.neo4j.ogm.exception.core.MappingException) ProxyAttributeConverter(org.neo4j.ogm.typeconversion.ProxyAttributeConverter) AttributeConverter(org.neo4j.ogm.typeconversion.AttributeConverter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

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 java.util (java.util)1 Comparator (java.util.Comparator)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Pattern (java.util.regex.Pattern)1 NodeEntity (org.neo4j.ogm.annotation.NodeEntity)1 RelationshipEntity (org.neo4j.ogm.annotation.RelationshipEntity)1 Convert (org.neo4j.ogm.annotation.typeconversion.Convert)1 Configuration (org.neo4j.ogm.config.Configuration)1 TypeSystem (org.neo4j.ogm.driver.TypeSystem)1 NoNativeTypes (org.neo4j.ogm.driver.TypeSystem.NoNativeTypes)1 MappingException (org.neo4j.ogm.exception.core.MappingException)1 ClassUtils (org.neo4j.ogm.support.ClassUtils)1 AttributeConverter (org.neo4j.ogm.typeconversion.AttributeConverter)1