use of org.neo4j.ogm.typeconversion.AttributeConverter in project neo4j-ogm by neo4j.
the class DomainInfo method selectAttributeConverterFor.
/**
* Selects the specialized attribute converter for the given field info, depending wether the field info
* describes an array, iterable or scalar value.
*
* @param source must not be {@literal null}.
* @param from The attribute converters to select from, must not be {@literal null}.
* @return
*/
private static Optional<AttributeConverter<?, ?>> selectAttributeConverterFor(FieldInfo source, AttributeConverters from) {
FieldInfo fieldInfo = Objects.requireNonNull(source, "Need a field info");
AttributeConverters attributeConverters = Objects.requireNonNull(from, "Need the set of attribute converters for the given field info.");
AttributeConverter selectedConverter;
if (fieldInfo.isArray()) {
selectedConverter = attributeConverters.forArray;
} else if (fieldInfo.isIterable()) {
selectedConverter = attributeConverters.forIterable.apply(fieldInfo.getCollectionClassname());
} else {
selectedConverter = attributeConverters.forScalar;
}
return Optional.ofNullable(selectedConverter);
}
use of org.neo4j.ogm.typeconversion.AttributeConverter 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);
}
}
}
}
use of org.neo4j.ogm.typeconversion.AttributeConverter in project neo4j-ogm by neo4j.
the class Filter method parameters.
public Map<String, Object> parameters() {
AttributeConverter applicablePropertyConverter = this.getPropertyConverter();
PropertyValueTransformer valueTransformer;
if (applicablePropertyConverter == null) {
valueTransformer = new NoOpPropertyValueTransformer();
} else {
valueTransformer = value -> {
List<Object> convertedValues = StreamSupport.stream(CollectionUtils.iterableOf(value).spliterator(), false).map((Function<Object, Object>) applicablePropertyConverter::toGraphProperty).collect(Collectors.toList());
if (convertedValues.size() == 1) {
return convertedValues.get(0);
} else {
return convertedValues;
}
};
}
return function.parameters(this::uniqueParameterName, valueTransformer);
}
Aggregations