Search in sources :

Example 1 with DateLongConverter

use of org.neo4j.ogm.typeconversion.DateLongConverter 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)

Aggregations

Instant (java.time.Instant)1 Convert (org.neo4j.ogm.annotation.typeconversion.Convert)1 DateString (org.neo4j.ogm.annotation.typeconversion.DateString)1 EnumString (org.neo4j.ogm.annotation.typeconversion.EnumString)1 NumberString (org.neo4j.ogm.annotation.typeconversion.NumberString)1 MappingException (org.neo4j.ogm.exception.core.MappingException)1 DateLongConverter (org.neo4j.ogm.typeconversion.DateLongConverter)1 DateStringConverter (org.neo4j.ogm.typeconversion.DateStringConverter)1 EnumStringConverter (org.neo4j.ogm.typeconversion.EnumStringConverter)1 InstantLongConverter (org.neo4j.ogm.typeconversion.InstantLongConverter)1 InstantStringConverter (org.neo4j.ogm.typeconversion.InstantStringConverter)1 NumberStringConverter (org.neo4j.ogm.typeconversion.NumberStringConverter)1