DATADOC-48 adding checks not to overwrite existing keys in changeset
This commit is contained in:
@@ -6,7 +6,6 @@ import com.mongodb.DBObject;
|
|||||||
import com.mongodb.MongoException;
|
import com.mongodb.MongoException;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.data.document.mongodb.CollectionCallback;
|
import org.springframework.data.document.mongodb.CollectionCallback;
|
||||||
@@ -43,28 +42,33 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
|
|||||||
final DBObject dbk = new BasicDBObject();
|
final DBObject dbk = new BasicDBObject();
|
||||||
dbk.put(ENTITY_ID, id);
|
dbk.put(ENTITY_ID, id);
|
||||||
dbk.put(ENTITY_CLASS, entityClass.getName());
|
dbk.put(ENTITY_CLASS, entityClass.getName());
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Loading MongoDB data for " + dbk);
|
||||||
|
}
|
||||||
mongoTemplate.execute(collName, new CollectionCallback<Object>() {
|
mongoTemplate.execute(collName, new CollectionCallback<Object>() {
|
||||||
@Override
|
@Override
|
||||||
public Object doInCollection(DBCollection collection)
|
public Object doInCollection(DBCollection collection)
|
||||||
throws MongoException, DataAccessException {
|
throws MongoException, DataAccessException {
|
||||||
for (DBObject dbo : collection.find(dbk)) {
|
for (DBObject dbo : collection.find(dbk)) {
|
||||||
String key = (String) dbo.get(ENTITY_FIELD_NAME);
|
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);
|
String className = (String) dbo.get(ENTITY_FIELD_CLASS);
|
||||||
if (className == null) {
|
if (className == null) {
|
||||||
throw new DataIntegrityViolationException(
|
throw new DataIntegrityViolationException(
|
||||||
"Unble to convert property " + key
|
"Unble to convert property " + key
|
||||||
+ ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available");
|
+ ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available");
|
||||||
}
|
}
|
||||||
Class<?> clazz = null;
|
Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader());
|
||||||
try {
|
|
||||||
clazz = Class.forName(className);
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
throw new DataIntegrityViolationException(
|
|
||||||
"Unble to convert property " + key + " of type " + className, e);
|
|
||||||
}
|
|
||||||
Object value = mongoTemplate.getConverter().read(clazz, dbo);
|
Object value = mongoTemplate.getConverter().read(clazz, dbo);
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Adding to ChangeSet: " + key);
|
||||||
|
}
|
||||||
changeSet.set(key, value);
|
changeSet.set(key, value);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user