Example usage for org.springframework.data.crossstore ChangeSet set

List of usage examples for org.springframework.data.crossstore ChangeSet set

Introduction

In this page you can find the example usage for org.springframework.data.crossstore ChangeSet set.

Prototype

void set(String key, Object o);

Source Link

Usage

From source file:org.springframework.data.mongodb.crossstore.MongoChangeSetPersister.java

public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, Object id,
        final ChangeSet changeSet) throws DataAccessException, NotFoundException {

    if (id == null) {
        log.debug("Unable to load MongoDB data for null id");
        return;/*from w  ww. ja va2  s. co m*/
    }

    String collName = getCollectionNameForEntity(entityClass);

    final DBObject dbk = new BasicDBObject();
    dbk.put(ENTITY_ID, id);
    dbk.put(ENTITY_CLASS, entityClass.getName());
    if (log.isDebugEnabled()) {
        log.debug("Loading MongoDB data for " + dbk);
    }
    mongoTemplate.execute(collName, new CollectionCallback<Object>() {
        public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
            for (DBObject dbo : collection.find(dbk)) {
                String key = (String) dbo.get(ENTITY_FIELD_NAME);
                if (log.isDebugEnabled()) {
                    log.debug("Processing key: " + key);
                }
                if (!changeSet.getValues().containsKey(key)) {
                    String className = (String) dbo.get(ENTITY_FIELD_CLASS);
                    if (className == null) {
                        throw new DataIntegrityViolationException("Unble to convert property " + key
                                + ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available");
                    }
                    Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader());
                    Object value = mongoTemplate.getConverter().read(clazz, dbo);
                    if (log.isDebugEnabled()) {
                        log.debug("Adding to ChangeSet: " + key);
                    }
                    changeSet.set(key, value);
                }
            }
            return null;
        }
    });
}