DATADOC-270 - Removed critical Sonar warnings from codebase.
This commit is contained in:
@@ -2,10 +2,6 @@ package org.springframework.data.persistence.document.mongodb;
|
|||||||
|
|
||||||
import javax.persistence.EntityManagerFactory;
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
|
||||||
import com.mongodb.BasicDBObject;
|
|
||||||
import com.mongodb.DBCollection;
|
|
||||||
import com.mongodb.DBObject;
|
|
||||||
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.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
@@ -18,152 +14,157 @@ import org.springframework.data.persistence.ChangeSetBacked;
|
|||||||
import org.springframework.data.persistence.ChangeSetPersister;
|
import org.springframework.data.persistence.ChangeSetPersister;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
|
import com.mongodb.BasicDBObject;
|
||||||
|
import com.mongodb.DBCollection;
|
||||||
|
import com.mongodb.DBObject;
|
||||||
|
import com.mongodb.MongoException;
|
||||||
|
|
||||||
public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
|
public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
|
||||||
|
|
||||||
private static final String ENTITY_CLASS = "_entity_class";
|
private static final String ENTITY_CLASS = "_entity_class";
|
||||||
|
|
||||||
private static final String ENTITY_ID = "_entity_id";
|
private static final String ENTITY_ID = "_entity_id";
|
||||||
|
|
||||||
private static final String ENTITY_FIELD_NAME = "_entity_field_name";
|
private static final String ENTITY_FIELD_NAME = "_entity_field_name";
|
||||||
|
|
||||||
private static final String ENTITY_FIELD_CLASS = "_entity_field_class";
|
private static final String ENTITY_FIELD_CLASS = "_entity_field_class";
|
||||||
|
|
||||||
protected final Log log = LogFactory.getLog(getClass());
|
protected final Log log = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
private MongoTemplate mongoTemplate;
|
private MongoTemplate mongoTemplate;
|
||||||
|
|
||||||
private EntityManagerFactory entityManagerFactory;
|
private EntityManagerFactory entityManagerFactory;
|
||||||
|
|
||||||
public void setMongoTemplate(MongoTemplate mongoTemplate) {
|
public void setMongoTemplate(MongoTemplate mongoTemplate) {
|
||||||
this.mongoTemplate = mongoTemplate;
|
this.mongoTemplate = mongoTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
|
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
|
||||||
this.entityManagerFactory = entityManagerFactory;
|
this.entityManagerFactory = entityManagerFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void getPersistentState(Class<? extends ChangeSetBacked> entityClass,
|
public void getPersistentState(Class<? extends ChangeSetBacked> entityClass,
|
||||||
Object id, final ChangeSet changeSet)
|
Object id, final ChangeSet changeSet)
|
||||||
throws DataAccessException, NotFoundException {
|
throws DataAccessException, NotFoundException {
|
||||||
|
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
log.debug("Unable to load MongoDB data for null id");
|
log.debug("Unable to load MongoDB data for null id");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String collName = getCollectionNameForEntity(entityClass);
|
String collName = getCollectionNameForEntity(entityClass);
|
||||||
|
|
||||||
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()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Loading MongoDB data for " + dbk);
|
log.debug("Loading MongoDB data for " + dbk);
|
||||||
}
|
}
|
||||||
mongoTemplate.execute(collName, new CollectionCallback<Object>() {
|
mongoTemplate.execute(collName, new CollectionCallback<Object>() {
|
||||||
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()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Processing key: " + key);
|
log.debug("Processing key: " + key);
|
||||||
}
|
}
|
||||||
if (!changeSet.getValues().containsKey(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 = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader());
|
Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader());
|
||||||
Object value = mongoTemplate.getConverter().read(clazz, dbo);
|
Object value = mongoTemplate.getConverter().read(clazz, dbo);
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Adding to ChangeSet: " + key);
|
log.debug("Adding to ChangeSet: " + key);
|
||||||
}
|
}
|
||||||
changeSet.set(key, value);
|
changeSet.set(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException {
|
public Object getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException {
|
||||||
log.debug("getPersistentId called on " + entity);
|
log.debug("getPersistentId called on " + entity);
|
||||||
if (entityManagerFactory == null) {
|
if (entityManagerFactory == null) {
|
||||||
throw new DataAccessResourceFailureException("EntityManagerFactory cannot be null");
|
throw new DataAccessResourceFailureException("EntityManagerFactory cannot be null");
|
||||||
}
|
}
|
||||||
Object o = entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity);
|
Object o = entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity);
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object persistState(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException {
|
public Object persistState(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException {
|
||||||
if (cs == null) {
|
if (cs == null) {
|
||||||
log.debug("Flush: changeset was null, nothing to flush.");
|
log.debug("Flush: changeset was null, nothing to flush.");
|
||||||
return 0L;
|
return 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Flush: changeset: " + cs.getValues());
|
log.debug("Flush: changeset: " + cs.getValues());
|
||||||
}
|
}
|
||||||
|
|
||||||
String collName = getCollectionNameForEntity(entity.getClass());
|
String collName = getCollectionNameForEntity(entity.getClass());
|
||||||
DBCollection dbc = mongoTemplate.getCollection(collName);
|
if (mongoTemplate.getCollection(collName) == null) {
|
||||||
if (dbc == null) {
|
mongoTemplate.createCollection(collName);
|
||||||
dbc = 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getCollectionNameForEntity(Class<? extends ChangeSetBacked> entityClass) {
|
for (String key : cs.getValues().keySet()) {
|
||||||
return ClassUtils.getQualifiedName(entityClass);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getCollectionNameForEntity(Class<? extends ChangeSetBacked> entityClass) {
|
||||||
|
return ClassUtils.getQualifiedName(entityClass);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,10 +35,10 @@ public class CannotGetMongoDbConnectionException extends DataAccessResourceFailu
|
|||||||
super(msg);
|
super(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CannotGetMongoDbConnectionException(String msg, String database, String username, char[] password2) {
|
public CannotGetMongoDbConnectionException(String msg, String database, String username, char[] password) {
|
||||||
super(msg);
|
super(msg);
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password2;
|
this.password = password == null ? null : password.clone();
|
||||||
this.database = database;
|
this.database = database;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1277,7 +1277,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|||||||
|
|
||||||
if (entityClass == null) {
|
if (entityClass == null) {
|
||||||
throw new InvalidDataAccessApiUsageException(
|
throw new InvalidDataAccessApiUsageException(
|
||||||
"No class parameter provided, entity collection can't be determined for " + entityClass);
|
"No class parameter provided, entity collection can't be determined!");
|
||||||
}
|
}
|
||||||
|
|
||||||
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
|
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
|
||||||
|
|||||||
@@ -100,9 +100,8 @@ public class QueryMapper {
|
|||||||
newConditions.add(getMappedObject((DBObject) iter.next(), entity));
|
newConditions.add(getMappedObject((DBObject) iter.next(), entity));
|
||||||
}
|
}
|
||||||
value = newConditions;
|
value = newConditions;
|
||||||
} else {
|
|
||||||
// TODO: Implement other forms of conversion (like @Alias and whatnot)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
newDbo.put(newKey, value);
|
newDbo.put(newKey, value);
|
||||||
}
|
}
|
||||||
return newDbo;
|
return newDbo;
|
||||||
|
|||||||
@@ -200,7 +200,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
|||||||
spelCtx.setBeanResolver(new BeanFactoryResolver(applicationContext));
|
spelCtx.setBeanResolver(new BeanFactoryResolver(applicationContext));
|
||||||
}
|
}
|
||||||
if (!(dbo instanceof BasicDBList)) {
|
if (!(dbo instanceof BasicDBList)) {
|
||||||
String[] keySet = dbo.keySet().toArray(new String[] {});
|
String[] keySet = dbo.keySet().toArray(new String[dbo.keySet().size()]);
|
||||||
for (String key : keySet) {
|
for (String key : keySet) {
|
||||||
spelCtx.setVariable(key, dbo.get(key));
|
spelCtx.setVariable(key, dbo.get(key));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,6 +51,6 @@ public class Circle {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("Circle [center=%s, radius=%d]", center, radius);
|
return String.format("Circle [center=%s, radius=%f]", center, radius);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public class OrCriteria implements CriteriaDefinition {
|
|||||||
|
|
||||||
public OrCriteria(Query[] queries) {
|
public OrCriteria(Query[] queries) {
|
||||||
super();
|
super();
|
||||||
this.queries = queries;
|
this.queries = queries == null ? null : queries.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user