Example usage for org.springframework.data.mongodb.core CollectionCallback CollectionCallback

List of usage examples for org.springframework.data.mongodb.core CollectionCallback CollectionCallback

Introduction

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

Prototype

CollectionCallback

Source Link

Usage

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

public Object persistState(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException {
    if (cs == null) {
        log.debug("Flush: changeset was null, nothing to flush.");
        return 0L;
    }/*from   www.  j a v  a  2  s .  co m*/

    if (log.isDebugEnabled()) {
        log.debug("Flush: changeset: " + cs.getValues());
    }

    String collName = getCollectionNameForEntity(entity.getClass());
    if (mongoTemplate.getCollection(collName) == null) {
        mongoTemplate.createCollection(collName);
    }

    for (String key : cs.getValues().keySet()) {
        if (key != null && !key.startsWith("_") && !key.equals(ChangeSetPersister.ID_KEY)) {
            Object value = cs.getValues().get(key);
            final DBObject dbQuery = new BasicDBObject();
            dbQuery.put(ENTITY_ID, getPersistentId(entity, cs));
            dbQuery.put(ENTITY_CLASS, entity.getClass().getName());
            dbQuery.put(ENTITY_FIELD_NAME, key);
            DBObject dbId = mongoTemplate.execute(collName, new CollectionCallback<DBObject>() {
                public DBObject doInCollection(DBCollection collection)
                        throws MongoException, DataAccessException {
                    return collection.findOne(dbQuery);
                }
            });
            if (value == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Flush: removing: " + dbQuery);
                }
                mongoTemplate.execute(collName, new CollectionCallback<Object>() {
                    public Object doInCollection(DBCollection collection)
                            throws MongoException, DataAccessException {
                        collection.remove(dbQuery);
                        return null;
                    }
                });
            } else {
                final DBObject dbDoc = new BasicDBObject();
                dbDoc.putAll(dbQuery);
                if (log.isDebugEnabled()) {
                    log.debug("Flush: saving: " + dbQuery);
                }
                mongoTemplate.getConverter().write(value, dbDoc);
                dbDoc.put(ENTITY_FIELD_CLASS, value.getClass().getName());
                if (dbId != null) {
                    dbDoc.put("_id", dbId.get("_id"));
                }
                mongoTemplate.execute(collName, new CollectionCallback<Object>() {
                    public Object doInCollection(DBCollection collection)
                            throws MongoException, DataAccessException {
                        collection.save(dbDoc);
                        return null;
                    }
                });
            }
        }
    }
    return 0L;
}