Example usage for org.springframework.data.mongodb.core.mapping MongoPersistentProperty usePropertyAccess

List of usage examples for org.springframework.data.mongodb.core.mapping MongoPersistentProperty usePropertyAccess

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.mapping MongoPersistentProperty usePropertyAccess.

Prototype

boolean usePropertyAccess();

Source Link

Document

Returns whether property access shall be used for reading the property value.

Usage

From source file:org.oasis.datacore.sdk.data.spring.DatacoreMappingMongoConverter.java

protected void writeInternal(Object obj, final DBObject dbo, MongoPersistentEntity<?> entity) {

    if (obj == null) {
        return;//from  w  w w  .j  av  a 2s. com
    }

    if (null == entity) {
        throw new MappingException("No mapping metadata found for entity of type " + obj.getClass().getName());
    }

    final BeanWrapper<MongoPersistentEntity<Object>, Object> wrapper = BeanWrapper.create(obj,
            conversionService);
    final MongoPersistentProperty idProperty = entity.getIdProperty();

    if (!dbo.containsField("_id") && null != idProperty) {

        boolean fieldAccessOnly = idProperty.usePropertyAccess() ? false : useFieldAccessOnly;

        try {
            Object id = wrapper.getProperty(idProperty, Object.class, fieldAccessOnly);
            dbo.put("_id", idMapper.convertId(id));
        } catch (ConversionException ignored) {
        }
    }

    // Write the properties
    entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
        public void doWithPersistentProperty(MongoPersistentProperty prop) {

            if (prop.equals(idProperty)) {
                return;
            }

            boolean fieldAccessOnly = prop.usePropertyAccess() ? false : useFieldAccessOnly;

            Object propertyObj = wrapper.getProperty(prop, prop.getType(), fieldAccessOnly);

            //if (null != propertyObj) { // [Ozwillo] HACK to allow unset / set to null at save
            if (/*[Ozwillo] HACK*/null != propertyObj
                    && /*[Ozwillo] END*/!conversions.isSimpleType(propertyObj.getClass())) {
                writePropertyInternal(propertyObj, dbo, prop);
            } else {
                writeSimpleInternal(propertyObj, dbo, prop.getFieldName());
            }
            //} // [Ozwillo] HACK
        }
    });

    entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() {
        public void doWithAssociation(Association<MongoPersistentProperty> association) {
            MongoPersistentProperty inverseProp = association.getInverse();
            Class<?> type = inverseProp.getType();
            Object propertyObj = wrapper.getProperty(inverseProp, type, useFieldAccessOnly);
            //if (null != propertyObj) { // [Ozwillo] HACK to allow unset / set to null at save
            writePropertyInternal(propertyObj, dbo, inverseProp);
            //} // [Ozwillo] HACK
        }
    });

    // [Ozwillo] HACK to persist Datacore model fields at root level NOT FOR NOW
    /*if ("DCEntity".equals(entity.getName())) {
       DCEntity dcEntity = (DCEntity) object;
       DCModel dcModel = dcModelService.getModel(dcEntity.getType());
       for (DCField dcField : dcModel.getAllFields()) {
            
       }
    }*/
}