use of org.springframework.data.mapping.InstanceCreatorMetadata in project spring-data-commons by spring-projects.
the class DtoInstantiatingConverter method convert.
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@NonNull
@Override
public Object convert(Object source) {
if (targetType.isInterface()) {
return source;
}
PersistentEntity<?, ? extends PersistentProperty<?>> sourceEntity = context.getRequiredPersistentEntity(source.getClass());
PersistentPropertyAccessor<Object> sourceAccessor = sourceEntity.getPropertyAccessor(source);
PersistentEntity<?, ? extends PersistentProperty<?>> targetEntity = context.getRequiredPersistentEntity(targetType);
@SuppressWarnings({ "rawtypes", "unchecked" }) Object dto = instantiator.createInstance(targetEntity, new ParameterValueProvider() {
@Override
@Nullable
public Object getParameterValue(Parameter parameter) {
String name = parameter.getName();
if (name == null) {
throw new IllegalArgumentException(String.format("Parameter %s does not have a name", parameter));
}
return sourceAccessor.getProperty(sourceEntity.getRequiredPersistentProperty(name));
}
});
PersistentPropertyAccessor<Object> targetAccessor = targetEntity.getPropertyAccessor(dto);
InstanceCreatorMetadata<? extends PersistentProperty<?>> creator = targetEntity.getInstanceCreatorMetadata();
targetEntity.doWithProperties((SimplePropertyHandler) property -> {
if ((creator != null) && creator.isCreatorParameter(property)) {
return;
}
targetAccessor.setProperty(property, sourceAccessor.getProperty(sourceEntity.getRequiredPersistentProperty(property.getName())));
});
return dto;
}
use of org.springframework.data.mapping.InstanceCreatorMetadata in project spring-data-neo4j by spring-projects.
the class DtoInstantiatingConverter method convert.
@Override
public Object convert(@Nullable EntityInstanceWithSource entityInstanceAndSource) {
if (entityInstanceAndSource == null) {
return null;
}
Object entityInstance = entityInstanceAndSource.getEntityInstance();
if (targetType.isInterface() || targetType.isInstance(entityInstance)) {
return entityInstance;
}
Neo4jPersistentEntity<?> sourceEntity = context.getRequiredPersistentEntity(entityInstance.getClass());
PersistentPropertyAccessor<Object> sourceAccessor = sourceEntity.getPropertyAccessor(entityInstance);
Neo4jPersistentEntity<?> targetEntity = context.addPersistentEntity(ClassTypeInformation.from(targetType)).orElseThrow(() -> new MappingException("Could not add a persistent entity for the projection target type '" + targetType.getName() + "'."));
InstanceCreatorMetadata<? extends PersistentProperty<?>> creator = targetEntity.getInstanceCreatorMetadata();
Object dto = context.getInstantiatorFor(targetEntity).createInstance(targetEntity, getParameterValueProvider(targetEntity, targetProperty -> getPropertyValueFor(targetProperty, sourceEntity, sourceAccessor, entityInstanceAndSource)));
PersistentPropertyAccessor<Object> dtoAccessor = targetEntity.getPropertyAccessor(dto);
targetEntity.doWithAll(property -> setPropertyOnDtoObject(entityInstanceAndSource, sourceEntity, sourceAccessor, creator, dtoAccessor, property));
return dto;
}
Aggregations