Example usage for org.springframework.data.mongodb.core.mapping MongoPersistentEntity getIdProperty

List of usage examples for org.springframework.data.mongodb.core.mapping MongoPersistentEntity getIdProperty

Introduction

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

Prototype

@Nullable
P getIdProperty();

Source Link

Document

Returns the id property of the PersistentEntity .

Usage

From source file:io.github.carlomicieli.springbooks.test.AbstractMongoDbTests.java

protected void ensureDocumentId(Object savedObject, Object id) {
    ConversionService conversionService = mongoTemplate.getConverter().getConversionService();
    MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext = mongoTemplate
            .getConverter().getMappingContext();
    MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(savedObject.getClass());

    MongoPersistentProperty idProp = persistentEntity == null ? null : persistentEntity.getIdProperty();
    if (idProp == null) {
        return;//from  w  w  w . ja va2  s.c  om
    }

    BeanWrapper<PersistentEntity<Object, ?>, Object> wrapper = BeanWrapper.create(savedObject,
            conversionService);
    Object idValue = wrapper.getProperty(idProp, idProp.getType(), true);

    if (idValue != null) {
        return;
    }

    wrapper.setProperty(idProp, id);
}

From source file:com.sangupta.jerry.mongodb.MongoTemplateBasicOperations.java

/**
 * Get the basic services from mongo template
 *///from   ww  w .j  a  va 2  s  .  c  o m
private synchronized void fetchMappingContextAndConversionService() {
    if (mappingContext == null) {
        MongoConverter mongoConverter = this.mongoTemplate.getConverter();
        mappingContext = mongoConverter.getMappingContext();
        conversionService = mongoConverter.getConversionService();

        MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entityClass);
        MongoPersistentProperty idProperty = persistentEntity.getIdProperty();
        this.idKey = idProperty == null ? "_id" : idProperty.getName();
    }
}

From source file:things.mongo.MongoConnector.java

private MongoPersistentProperty getIdField(Class valueClass) {
    MongoPersistentEntity<?> mongoPersistentEntity = mongoTemplate.getConverter().getMappingContext()
            .getPersistentEntity(valueClass);
    MongoPersistentProperty id = mongoPersistentEntity.getIdProperty();
    return id;/*from   w w  w  .j av  a2 s. c o  m*/
}

From source file:com.sangupta.jerry.mongodb.MongoTemplateBasicOperations.java

/**
 * Extract the value of the primary ID of the entity object
 * //from   www .ja  v  a  2  s  .  c  o m
 * @param entity
 * @return
 */
public X getPrimaryID(T entity) {
    if (mappingContext == null || conversionService == null) {
        fetchMappingContextAndConversionService();
    }

    MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entity.getClass());
    MongoPersistentProperty idProperty = persistentEntity.getIdProperty();
    if (idProperty == null) {
        return null;
    }

    X idValue = BeanWrapper.create(entity, conversionService).getProperty(idProperty, this.primaryIDClass,
            true);
    return idValue;
}

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 ww  .  j  ava2s.  c  o m*/
    }

    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()) {
            
       }
    }*/
}

From source file:com.epam.ta.reportportal.database.dao.ReportPortalRepositoryImpl.java

@Override
public void partialUpdate(T t) {
    ID id = getEntityInformation().getId(t);
    if (null == id) {
        throw new IllegalArgumentException("ID property should not be null");
    }/*from  w w w  . ja  v  a 2s .co  m*/

    Update update = new Update();
    final MongoPersistentEntity<?> persistentEntity = mongoOperations.getConverter().getMappingContext()
            .getPersistentEntity(getEntityInformation().getJavaType());
    persistentEntity.doWithProperties((PropertyHandler<MongoPersistentProperty>) persistentProperty -> {
        if (!persistentEntity.isIdProperty(persistentProperty)) {
            Object value = Accessible.on(t).field(persistentProperty.getField()).getValue();
            if (null != value) {
                update.set(persistentProperty.getFieldName(), value);
            }
        }
    });

    WriteResult writeResult = mongoOperations.updateFirst(
            query(where(persistentEntity.getIdProperty().getFieldName()).is(id)), update,
            getEntityInformation().getCollectionName());
    if (1 != writeResult.getN()) {
        throw new IncorrectResultSizeDataAccessException(1, writeResult.getN());
    }
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

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

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

    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);

    // Write the ID
    final MongoPersistentProperty idProperty = entity.getIdProperty();
    if (!dbo.containsField("_id") && null != idProperty) {

        try {
            Object id = wrapper.getProperty(idProperty, Object.class, useFieldAccessOnly);
            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;
            }

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

            if (null != propertyObj) {
                if (!conversions.isSimpleType(propertyObj.getClass())) {
                    writePropertyInternal(propertyObj, dbo, prop);
                } else {
                    writeSimpleInternal(propertyObj, dbo, prop.getFieldName());
                }
            }
        }
    });

    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) {
                writePropertyInternal(propertyObj, dbo, inverseProp);
            }
        }
    });
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

protected DBRef createDBRef(Object target, org.springframework.data.mongodb.core.mapping.DBRef dbref) {

    MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass());
    if (null == targetEntity || null == targetEntity.getIdProperty()) {
        return null;
    }/*w w  w . ja v  a 2 s.com*/

    MongoPersistentProperty idProperty = targetEntity.getIdProperty();
    BeanWrapper<MongoPersistentEntity<Object>, Object> wrapper = BeanWrapper.create(target, conversionService);
    Object id = wrapper.getProperty(idProperty, Object.class, useFieldAccessOnly);

    if (null == id) {
        throw new MappingException("Cannot create a reference to an object with a NULL id.");
    }

    String collection = dbref.collection();
    if ("".equals(collection)) {
        collection = targetEntity.getCollection();
    }

    String dbname = dbref.db();
    DB db = StringUtils.hasText(dbname) ? mongoDbFactory.getDb(dbname) : mongoDbFactory.getDb();

    return new DBRef(db, collection, idMapper.convertId(id));
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

public <T> T findById(Object id, Class<T> entityClass, String collectionName) {
    MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entityClass);
    MongoPersistentProperty idProperty = persistentEntity.getIdProperty();
    String idKey = idProperty == null ? ID : idProperty.getName();
    return doFindOne(collectionName, new BasicDBObject(idKey, id), null, entityClass);
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

/**
 * Returns a {@link Query} for the given entity by its id.
 * //from  w w w.jav  a2  s .c om
 * @param object must not be {@literal null}.
 * @return
 */
private Query getIdQueryFor(Object object) {

    Assert.notNull(object);

    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(object.getClass());
    MongoPersistentProperty idProp = entity.getIdProperty();

    if (idProp == null) {
        throw new MappingException("No id property found for object of type " + entity.getType().getName());
    }

    ConversionService service = mongoConverter.getConversionService();
    Object idProperty = null;

    idProperty = BeanWrapper.create(object, service).getProperty(idProp, Object.class, true);
    return new Query(where(idProp.getFieldName()).is(idProperty));
}