Search in sources :

Example 1 with Parameter

use of org.springframework.data.mapping.Parameter 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;
}
Also used : Converter(org.springframework.core.convert.converter.Converter) EntityInstantiators(org.springframework.data.mapping.model.EntityInstantiators) ParameterValueProvider(org.springframework.data.mapping.model.ParameterValueProvider) Parameter(org.springframework.data.mapping.Parameter) MappingContext(org.springframework.data.mapping.context.MappingContext) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PersistentEntity(org.springframework.data.mapping.PersistentEntity) EntityInstantiator(org.springframework.data.mapping.model.EntityInstantiator) NonNull(org.springframework.lang.NonNull) Nullable(org.springframework.lang.Nullable) InstanceCreatorMetadata(org.springframework.data.mapping.InstanceCreatorMetadata) SimplePropertyHandler(org.springframework.data.mapping.SimplePropertyHandler) PersistentProperty(org.springframework.data.mapping.PersistentProperty) Assert(org.springframework.util.Assert) ParameterValueProvider(org.springframework.data.mapping.model.ParameterValueProvider) Parameter(org.springframework.data.mapping.Parameter) Nullable(org.springframework.lang.Nullable) NonNull(org.springframework.lang.NonNull)

Example 2 with Parameter

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

the class InstanceCreatorMetadataDiscoverer method getFactoryMethod.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T, P extends PersistentProperty<P>> FactoryMethod<Object, P> getFactoryMethod(PersistentEntity<T, P> entity, Method method) {
    Parameter<Object, P>[] parameters = new Parameter[method.getParameterCount()];
    Annotation[][] parameterAnnotations = method.getParameterAnnotations();
    List<TypeInformation<?>> types = entity.getTypeInformation().getParameterTypes(method);
    String[] parameterNames = PARAMETER_NAME_DISCOVERER.getParameterNames(method);
    for (int i = 0; i < parameters.length; i++) {
        String name = parameterNames == null || parameterNames.length <= i ? null : parameterNames[i];
        TypeInformation<?> type = types.get(i);
        Annotation[] annotations = parameterAnnotations[i];
        parameters[i] = new Parameter(name, type, annotations, entity);
    }
    return new FactoryMethod<>(method, parameters);
}
Also used : FactoryMethod(org.springframework.data.mapping.FactoryMethod) Parameter(org.springframework.data.mapping.Parameter) TypeInformation(org.springframework.data.util.TypeInformation) Annotation(java.lang.annotation.Annotation)

Aggregations

Parameter (org.springframework.data.mapping.Parameter)2 Annotation (java.lang.annotation.Annotation)1 Converter (org.springframework.core.convert.converter.Converter)1 FactoryMethod (org.springframework.data.mapping.FactoryMethod)1 InstanceCreatorMetadata (org.springframework.data.mapping.InstanceCreatorMetadata)1 PersistentEntity (org.springframework.data.mapping.PersistentEntity)1 PersistentProperty (org.springframework.data.mapping.PersistentProperty)1 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1 SimplePropertyHandler (org.springframework.data.mapping.SimplePropertyHandler)1 MappingContext (org.springframework.data.mapping.context.MappingContext)1 EntityInstantiator (org.springframework.data.mapping.model.EntityInstantiator)1 EntityInstantiators (org.springframework.data.mapping.model.EntityInstantiators)1 ParameterValueProvider (org.springframework.data.mapping.model.ParameterValueProvider)1 TypeInformation (org.springframework.data.util.TypeInformation)1 NonNull (org.springframework.lang.NonNull)1 Nullable (org.springframework.lang.Nullable)1 Assert (org.springframework.util.Assert)1