Search in sources :

Example 6 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 7 with PersistentPropertyAccessor

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

the class MappingMongoConverter method writeInternal.

protected void writeInternal(@Nullable Object obj, final Bson bson, MongoPersistentEntity<?> entity) {
    if (obj == null) {
        return;
    }
    if (null == entity) {
        throw new MappingException("No mapping metadata found for entity of type " + obj.getClass().getName());
    }
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(obj);
    DocumentAccessor dbObjectAccessor = new DocumentAccessor(bson);
    MongoPersistentProperty idProperty = entity.getIdProperty();
    if (idProperty != null && !dbObjectAccessor.hasValue(idProperty)) {
        Object value = idMapper.convertId(accessor.getProperty(idProperty));
        if (value != null) {
            dbObjectAccessor.put(idProperty, value);
        }
    }
    writeProperties(bson, entity, accessor, dbObjectAccessor, idProperty);
}
Also used : PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.springframework.data.mapping.MappingException)

Example 8 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)

Aggregations

PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)8 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)6 Document (org.bson.Document)4 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)4 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 ConversionService (org.springframework.core.convert.ConversionService)2 DefaultSpELExpressionEvaluator (org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator)2 DBRef (com.mongodb.DBRef)1 ReturnDocument (com.mongodb.client.model.ReturnDocument)1 FullDocument (com.mongodb.client.model.changestream.FullDocument)1 ObjectId (org.bson.types.ObjectId)1 Test (org.junit.Test)1 EntityInstantiator (org.springframework.data.convert.EntityInstantiator)1 JdbcPersistentEntity (org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity)1 JdbcPersistentProperty (org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty)1 MappingException (org.springframework.data.mapping.MappingException)1 Parameter (org.springframework.data.mapping.PreferredConstructor.Parameter)1 SimplePropertyHandler (org.springframework.data.mapping.SimplePropertyHandler)1 ParameterValueProvider (org.springframework.data.mapping.model.ParameterValueProvider)1