Search in sources :

Example 1 with InstanceCreatorMetadata

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;
}
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 InstanceCreatorMetadata

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;
}
Also used : Converter(org.springframework.core.convert.converter.Converter) MapAccessor(org.neo4j.driver.types.MapAccessor) Collection(java.util.Collection) ParameterValueProvider(org.springframework.data.mapping.model.ParameterValueProvider) Parameter(org.springframework.data.mapping.Parameter) API(org.apiguardian.api.API) LogAccessor(org.springframework.core.log.LogAccessor) Function(java.util.function.Function) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PersistentEntity(org.springframework.data.mapping.PersistentEntity) Value(org.neo4j.driver.Value) CollectionFactory(org.springframework.core.CollectionFactory) List(java.util.List) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) MappingException(org.springframework.data.mapping.MappingException) Nullable(org.springframework.lang.Nullable) LogFactory(org.apache.commons.logging.LogFactory) InstanceCreatorMetadata(org.springframework.data.mapping.InstanceCreatorMetadata) TypeSystem(org.neo4j.driver.types.TypeSystem) PersistentProperty(org.springframework.data.mapping.PersistentProperty) Assert(org.springframework.util.Assert) MappingException(org.springframework.data.mapping.MappingException)

Aggregations

Converter (org.springframework.core.convert.converter.Converter)2 InstanceCreatorMetadata (org.springframework.data.mapping.InstanceCreatorMetadata)2 Parameter (org.springframework.data.mapping.Parameter)2 PersistentEntity (org.springframework.data.mapping.PersistentEntity)2 PersistentProperty (org.springframework.data.mapping.PersistentProperty)2 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)2 ParameterValueProvider (org.springframework.data.mapping.model.ParameterValueProvider)2 Nullable (org.springframework.lang.Nullable)2 Assert (org.springframework.util.Assert)2 Collection (java.util.Collection)1 List (java.util.List)1 Function (java.util.function.Function)1 LogFactory (org.apache.commons.logging.LogFactory)1 API (org.apiguardian.api.API)1 Value (org.neo4j.driver.Value)1 MapAccessor (org.neo4j.driver.types.MapAccessor)1 TypeSystem (org.neo4j.driver.types.TypeSystem)1 CollectionFactory (org.springframework.core.CollectionFactory)1 LogAccessor (org.springframework.core.log.LogAccessor)1 MappingException (org.springframework.data.mapping.MappingException)1