|
| 1 | +package org.springframework.data.persistence.document.mongo; |
| 2 | + |
| 3 | +import com.mongodb.BasicDBObject; |
| 4 | +import com.mongodb.DBCollection; |
| 5 | +import com.mongodb.DBObject; |
| 6 | +import com.mongodb.MongoException; |
| 7 | +import org.apache.commons.logging.Log; |
| 8 | +import org.apache.commons.logging.LogFactory; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.dao.DataAccessException; |
| 11 | +import org.springframework.dao.DataIntegrityViolationException; |
| 12 | +import org.springframework.data.document.mongodb.CollectionCallback; |
| 13 | +import org.springframework.data.document.mongodb.MongoTemplate; |
| 14 | +import org.springframework.data.persistence.ChangeSet; |
| 15 | +import org.springframework.data.persistence.ChangeSetBacked; |
| 16 | +import org.springframework.data.persistence.ChangeSetPersister; |
| 17 | +import org.springframework.util.ClassUtils; |
| 18 | + |
| 19 | +public class MongoChangeSetPersister implements ChangeSetPersister<Object> { |
| 20 | + |
| 21 | + private static final String ENTITY_CLASS = "_entity_class"; |
| 22 | + |
| 23 | + private static final String ENTITY_ID = "_entity_id"; |
| 24 | + |
| 25 | + private static final String ENTITY_FIELD_NAME = "_entity_field_name"; |
| 26 | + |
| 27 | + private static final String ENTITY_FIELD_CLASS = "_entity_field_class"; |
| 28 | + |
| 29 | + protected final Log log = LogFactory.getLog(getClass()); |
| 30 | + |
| 31 | + private MongoTemplate mongoTemplate; |
| 32 | + |
| 33 | + public void setMongoTemplate(MongoTemplate mongoTemplate) { |
| 34 | + this.mongoTemplate = mongoTemplate; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, |
| 39 | + Object id, final ChangeSet changeSet) throws DataAccessException, |
| 40 | + NotFoundException { |
| 41 | + String collName = getCollectionNameForEntity(entityClass); |
| 42 | + |
| 43 | + final DBObject dbk = new BasicDBObject(); |
| 44 | + dbk.put(ENTITY_ID, id); |
| 45 | + dbk.put(ENTITY_CLASS, entityClass.getName()); |
| 46 | + mongoTemplate.execute(collName, new CollectionCallback<Object>() { |
| 47 | + @Override |
| 48 | + public Object doInCollection(DBCollection collection) |
| 49 | + throws MongoException, DataAccessException { |
| 50 | + for (DBObject dbo : collection.find(dbk)) { |
| 51 | + String key = (String) dbo.get(ENTITY_FIELD_NAME); |
| 52 | + String className = (String) dbo.get(ENTITY_FIELD_CLASS); |
| 53 | + if (className == null) { |
| 54 | + throw new DataIntegrityViolationException( |
| 55 | + "Unble to convert property " + key |
| 56 | + + ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available"); |
| 57 | + } |
| 58 | + Class<?> clazz = null; |
| 59 | + try { |
| 60 | + clazz = Class.forName(className); |
| 61 | + } catch (ClassNotFoundException e) { |
| 62 | + throw new DataIntegrityViolationException( |
| 63 | + "Unble to convert property " + key + " of type " + className, e); |
| 64 | + } |
| 65 | + Object value = mongoTemplate.getConverter().read(clazz, dbo); |
| 66 | + changeSet.set(key, value); |
| 67 | + } |
| 68 | + return null; |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Object getPersistentId(Class<? extends ChangeSetBacked> entityClass, |
| 75 | + ChangeSet cs) throws DataAccessException { |
| 76 | + log.debug("getPersistentId called on " + entityClass); |
| 77 | + if (cs == null) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + if (cs.getValues().get(ChangeSetPersister.ID_KEY) == null) { |
| 81 | + // Not yet persistent |
| 82 | + return null; |
| 83 | + } |
| 84 | + Object o = cs.getValues().get(ChangeSetPersister.ID_KEY); |
| 85 | + return o; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Object persistState(Class<? extends ChangeSetBacked> entityClass, |
| 90 | + ChangeSet cs) throws DataAccessException { |
| 91 | + log.debug("Flush: changeset: " + cs.getValues().keySet()); |
| 92 | + |
| 93 | + String collName = getCollectionNameForEntity(entityClass); |
| 94 | + DBCollection dbc = mongoTemplate.getCollection(collName); |
| 95 | + if (dbc == null) { |
| 96 | + dbc = mongoTemplate.createCollection(collName); |
| 97 | + } |
| 98 | + for (String key : cs.getValues().keySet()) { |
| 99 | + if (key != null && !key.startsWith("_") && !key.equals(ChangeSetPersister.ID_KEY)) { |
| 100 | + Object value = cs.getValues().get(key); |
| 101 | + final DBObject dbQuery = new BasicDBObject(); |
| 102 | + dbQuery.put(ENTITY_ID, cs.getValues().get(ChangeSetPersister.ID_KEY)); |
| 103 | + dbQuery.put(ENTITY_CLASS, entityClass.getName()); |
| 104 | + dbQuery.put(ENTITY_FIELD_NAME, key); |
| 105 | + dbQuery.put(ENTITY_FIELD_CLASS, value.getClass().getName()); |
| 106 | + DBObject dbId = mongoTemplate.execute(collName, |
| 107 | + new CollectionCallback<DBObject>() { |
| 108 | + @Override |
| 109 | + public DBObject doInCollection(DBCollection collection) |
| 110 | + throws MongoException, DataAccessException { |
| 111 | + return collection.findOne(dbQuery); |
| 112 | + } |
| 113 | + }); |
| 114 | + final DBObject dbDoc = new BasicDBObject(); |
| 115 | + mongoTemplate.getConverter().write(value, dbDoc); |
| 116 | + dbDoc.putAll(dbQuery); |
| 117 | + if (dbId != null) { |
| 118 | + dbDoc.put("_id", dbId.get("_id")); |
| 119 | + } |
| 120 | + mongoTemplate.execute(collName, new CollectionCallback<Object>() { |
| 121 | + @Override |
| 122 | + public Object doInCollection(DBCollection collection) |
| 123 | + throws MongoException, DataAccessException { |
| 124 | + collection.save(dbDoc); |
| 125 | + return null; |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + } |
| 130 | + return 0L; |
| 131 | + } |
| 132 | + |
| 133 | + private String getCollectionNameForEntity( |
| 134 | + Class<? extends ChangeSetBacked> entityClass) { |
| 135 | + return ClassUtils.getQualifiedName(entityClass); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments