use of com.creditease.uav.datastore.mongo.MongodbImplStrategy.UpdateStrategy in project uavstack by uavorg.
the class MongoDBDataStore method update.
@SuppressWarnings("rawtypes")
@Override
protected boolean update(DataStoreMsg msg) {
boolean isSuccess = false;
String collectionName = (String) msg.get(DataStoreProtocol.MONGO_COLLECTION_NAME);
MongoCollection<Document> collection = this.datasource.getSourceConnect().getCollection(collectionName);
// collection no exist
if (null == collection) {
if (log.isTraceEnable()) {
log.warn(this, "MongoDB[" + this.datasource.getDataStoreConnection().getDbName() + "] Collection[" + collectionName + "] NO EXIST.");
}
return adaptor.handleUpdateResult(isSuccess, msg, this.datasource.getDataStoreConnection());
}
// prepare query
String updateObj = (String) adaptor.prepareUpdateObj(msg, this.datasource.getDataStoreConnection());
// @SuppressWarnings("rawtypes")
Map params = JSONHelper.toObject(DataStoreHelper.decorateInForMongoDB(updateObj), Map.class);
BasicDBObject condition = new BasicDBObject();
BasicDBObject update = new BasicDBObject();
boolean isRemove = true;
for (Object keyObj : params.keySet()) {
if (keyObj.toString().equals(DataStoreProtocol.WHERE)) {
QueryStrategy qry = new QueryStrategy();
qry.concretProcessor(DataStoreProtocol.WHERE, params, condition);
} else if (keyObj.toString().equals(DataStoreProtocol.UPDATE)) {
isRemove = false;
UpdateStrategy up = new UpdateStrategy();
up.concretProcessor(DataStoreProtocol.UPDATE, params, update);
} else {
log.err(this, "can not figure out, please check it out " + keyObj.toString());
}
}
if (isRemove) {
log.info(this, "condition: " + condition.toString());
DeleteResult res = collection.deleteMany(condition);
log.info(this, "DeletedCount:" + res.getDeletedCount());
isSuccess = true;
} else {
log.info(this, "condition: " + condition.toString());
log.info(this, "update: " + update.toString());
UpdateResult res = collection.updateMany(condition, update);
log.info(this, "ModifiedCount:" + res.getModifiedCount());
isSuccess = true;
}
return adaptor.handleUpdateResult(isSuccess, msg, this.datasource.getDataStoreConnection());
}
Aggregations