Search in sources :

Example 1 with Parameter

use of org.springframework.data.mapping.PreferredConstructor.Parameter in project spring-data-mongodb by spring-projects.

the class DtoInstantiatingConverter method convert.

/*
	 * (non-Javadoc)
	 * @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
	 */
@Override
public Object convert(Object source) {
    if (targetType.isInterface()) {
        return source;
    }
    final PersistentEntity<?, ?> sourceEntity = context.getRequiredPersistentEntity(source.getClass());
    final PersistentPropertyAccessor sourceAccessor = sourceEntity.getPropertyAccessor(source);
    final PersistentEntity<?, ?> targetEntity = context.getRequiredPersistentEntity(targetType);
    final PreferredConstructor<?, ? extends PersistentProperty<?>> constructor = targetEntity.getPersistenceConstructor();
    @SuppressWarnings({ "rawtypes", "unchecked" }) Object dto = instantiator.createInstance(targetEntity, new ParameterValueProvider() {

        @Override
        public Object getParameterValue(Parameter parameter) {
            return sourceAccessor.getProperty(sourceEntity.getPersistentProperty(parameter.getName().toString()));
        }
    });
    final PersistentPropertyAccessor dtoAccessor = targetEntity.getPropertyAccessor(dto);
    targetEntity.doWithProperties(new SimplePropertyHandler() {

        @Override
        public void doWithPersistentProperty(PersistentProperty<?> property) {
            if (constructor.isConstructorParameter(property)) {
                return;
            }
            dtoAccessor.setProperty(property, sourceAccessor.getProperty(sourceEntity.getPersistentProperty(property.getName())));
        }
    });
    return dto;
}
Also used : ParameterValueProvider(org.springframework.data.mapping.model.ParameterValueProvider) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) Parameter(org.springframework.data.mapping.PreferredConstructor.Parameter) SimplePropertyHandler(org.springframework.data.mapping.SimplePropertyHandler)

Aggregations

PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1 Parameter (org.springframework.data.mapping.PreferredConstructor.Parameter)1 SimplePropertyHandler (org.springframework.data.mapping.SimplePropertyHandler)1 ParameterValueProvider (org.springframework.data.mapping.model.ParameterValueProvider)1