Search in sources :

Example 16 with PersistentPropertyAccessor

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

the class MongoTemplate method populateIdIfNecessary.

/**
 * Populates the id property of the saved object, if it's not set already.
 *
 * @param savedObject
 * @param id
 */
protected void populateIdIfNecessary(Object savedObject, Object id) {
    if (id == null) {
        return;
    }
    if (savedObject instanceof Document) {
        Document document = (Document) savedObject;
        document.put(ID_FIELD, id);
        return;
    }
    MongoPersistentProperty idProperty = getIdPropertyFor(savedObject.getClass());
    if (idProperty != null) {
        ConversionService conversionService = mongoConverter.getConversionService();
        MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(savedObject.getClass());
        PersistentPropertyAccessor accessor = entity.getPropertyAccessor(savedObject);
        Object value = accessor.getProperty(idProperty);
        if (value == null) {
            new ConvertingPropertyAccessor(accessor, conversionService).setProperty(idProperty, id);
        }
    }
}
Also used : ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ConversionService(org.springframework.core.convert.ConversionService) Document(org.bson.Document)

Example 17 with PersistentPropertyAccessor

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

the class ReactiveMongoTemplate method populateIdIfNecessary.

/**
 * Populates the id property of the saved object, if it's not set already.
 *
 * @param savedObject
 * @param id
 */
private void populateIdIfNecessary(Object savedObject, @Nullable Object id) {
    if (id == null) {
        return;
    }
    if (savedObject instanceof Document) {
        Document Document = (Document) savedObject;
        Document.put(ID_FIELD, id);
        return;
    }
    MongoPersistentProperty idProp = getIdPropertyFor(savedObject.getClass());
    if (idProp == null) {
        return;
    }
    ConversionService conversionService = mongoConverter.getConversionService();
    MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(savedObject.getClass());
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(savedObject);
    if (accessor.getProperty(idProp) != null) {
        return;
    }
    new ConvertingPropertyAccessor(accessor, conversionService).setProperty(idProp, id);
}
Also used : ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ConversionService(org.springframework.core.convert.ConversionService) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument)

Example 18 with PersistentPropertyAccessor

use of org.springframework.data.mapping.PersistentPropertyAccessor 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)

Example 19 with PersistentPropertyAccessor

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

the class BasicPersistentEntityUnitTests method returnsGeneratedPropertyAccessorForPropertyAccessor.

// DATACMNS-809
@Test
public void returnsGeneratedPropertyAccessorForPropertyAccessor() {
    SampleMappingContext context = new SampleMappingContext();
    PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Entity.class);
    Entity value = new Entity();
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value);
    assertThat(accessor).isNotInstanceOf(BeanWrapper.class);
    assertThat(accessor.getClass().getName()).contains("_Accessor_");
    assertThat(accessor.getBean()).isEqualTo(value);
}
Also used : PersistentEntity(org.springframework.data.mapping.PersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) SamplePersistentProperty(org.springframework.data.mapping.context.SamplePersistentProperty) SampleMappingContext(org.springframework.data.mapping.context.SampleMappingContext) Test(org.junit.Test)

Example 20 with PersistentPropertyAccessor

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

the class ClassGeneratingPropertyAccessorFactoryDatatypeTests method shouldSetAndGetProperty.

// DATACMNS-809
@Test
public void shouldSetAndGetProperty() throws Exception {
    assertThat(getProperty(bean, propertyName)).satisfies(property -> {
        PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);
        persistentPropertyAccessor.setProperty(property, value);
        assertThat(persistentPropertyAccessor.getProperty(property)).isEqualTo(value);
    });
}
Also used : PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) Test(org.junit.Test)

Aggregations

PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)29 Test (org.junit.Test)8 PersistentProperty (org.springframework.data.mapping.PersistentProperty)7 List (java.util.List)6 Key (com.google.cloud.spanner.Key)5 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 DatastorePersistentProperty (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)5 Map (java.util.Map)4 BaseEntity (com.google.cloud.datastore.BaseEntity)3 Entity (com.google.cloud.datastore.Entity)3 FullEntity (com.google.cloud.datastore.FullEntity)3 ListValue (com.google.cloud.datastore.ListValue)3 Value (com.google.cloud.datastore.Value)3 DatastorePersistentEntity (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity)3 EntityInstantiator (org.springframework.data.convert.EntityInstantiator)3 PropertyHandler (org.springframework.data.mapping.PropertyHandler)3 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)3 ClassTypeInformation (org.springframework.data.util.ClassTypeInformation)3 ByteArray (com.google.cloud.ByteArray)2