public void upsertTraceStats(Document key, Document doc) { options.upsert(true); try { traceStatsColl.updateOne(key, doc, options); } catch (MongoWriteException e) { if (e.getCode() == 11000) { options.upsert(false); traceStatsColl.updateOne(key, doc, options); } } }
@SuppressWarnings("deprecation") public boolean passBooking(User currentUserBooking, User nextUserInQueue) { try { final MongoDatabase mdb = MongoDBConnManager.getInstance().getConnection(); final MongoCollection<Document> coll = mdb.getCollection(DBConstants.COLL_BOOKING); final Document findCr = new Document(); findCr.put(DBConstants.BOOKING_ID, nextUserInQueue.getBookingId()); final ArrayList<Document> lstBkngs = coll.find(findCr).into(new ArrayList<Document>()); for (final Document document : lstBkngs) { java.util.Date nextUserBookingTime = document.getDate(DBConstants.BOOKED_DATE_N_TIME); nextUserBookingTime.setSeconds(nextUserBookingTime.getSeconds() + 1); currentUserBooking.setBookedDateNTime(nextUserBookingTime); } // update current user booking time final Document filterQuery = new Document(); filterQuery.put(DBConstants.BOOKING_ID, currentUserBooking.getBookingId()); final Document updateQuery = new Document(); final Document updateSet = new Document(); updateSet.put(DBConstants.BOOKED_DATE_N_TIME, currentUserBooking.getBookedDateNTime()); updateQuery.put("$set", updateSet); coll.updateOne(filterQuery, updateQuery); } catch (Exception e) { if (e instanceof com.mongodb.MongoTimeoutException) { throw new ApplicationException(MessagesEnum.MONGODB_IS_DOWN.getMessage(), e); } throw new ApplicationException(MessagesEnum.PASSING_BOOKING_FAILED.getMessage(), e); } return true; }
public void saveOrUpdate(Document widgetDoc) { logger.debug("--> to be save widget -->"); String tableName = "Widget"; MongoDatabase db = mongoClient.getDatabase(widgetDoc.getString("appId")); MongoCollection<Document> collection = db.getCollection(tableName); if (!widgetDoc.containsKey("_id")) { ObjectId objId = new ObjectId(); widgetDoc.put("_id", objId); logger.debug("--> to insert " + tableName + " with " + widgetDoc.toJson()); collection.insertOne(widgetDoc); // 没有数据就执行添加操作 return; } String objectId = widgetDoc.get("_id").toString(); Document matchFields = new Document(); matchFields.put("_id", new ObjectId(objectId)); if (collection.find(matchFields).iterator().hasNext()) { // 有数据就执行更新操作 logger.debug("--> to update " + tableName + " with " + widgetDoc.toJson()); collection.updateOne(matchFields, new Document("$set", widgetDoc)); } else { logger.debug("--> to save " + tableName + " with " + widgetDoc.toJson()); collection.insertOne(widgetDoc); // 没有数据就执行添加操作 } }
@Override public UpdateResult updateOne(Bson arg0, Bson arg1, UpdateOptions arg2) { if (tx.started()) { tx.beforeUpdateOne(coll, arg0); return tx.getTxCollection().updateOne(modifyQuery(arg0), arg1, arg2); } else { return coll.updateOne(arg0, arg1, arg2); } }
public void addPostComment( final String name, final String email, final String body, final String permalink) { BasicDBObject comment = new BasicDBObject("author", name).append("body", body); if (email != null && !email.equals("")) { comment.append("email", email); } postsCollection.updateOne( new BasicDBObject("permalink", permalink), new BasicDBObject("$push", new BasicDBObject("comments", comment))); }
/** * Update status of feature. * * @param uid feature id * @param enable enabler */ private void updateStatus(String uid, boolean enable) { if (uid == null || uid.isEmpty()) { throw new IllegalArgumentException("Feature identifier cannot be null nor empty"); } if (!exist(uid)) { throw new FeatureNotFoundException(uid); } Document target = BUILDER.getFeatUid(uid); Object enabledd = BUILDER.getEnable(enable); collection.updateOne(target, new Document(MONGO_SET, enabledd)); }
public void push(String id, Comment comment) { MongoCollection<Document> commentCollection = mongoDatabase.getCollection("posts"); Document document = new Document(); document.put("user", comment.getUser()); document.put("text", comment.getText()); document.put("date", comment.getDate()); commentCollection.updateOne( new Document("_id", new ObjectId(id)), new Document("$push", new Document("comments", document)), new UpdateOptions().upsert(false)); }
/** {@inheritDoc} */ @Override public void disableGroup(String groupName) { if (groupName == null || groupName.isEmpty()) { throw new IllegalArgumentException("Groupname cannot be null nor empty"); } if (!existGroup(groupName)) { throw new GroupNotFoundException(groupName); } for (Document document : collection.find(BUILDER.getGroupName(groupName))) { Object enabled = BUILDER.getEnable(false); collection.updateOne(document, new Document(MONGO_SET, enabled)); } }
/** {@inheritDoc} */ @Override public void removeRoleFromFeature(String uid, String roleName) { if (uid == null || uid.isEmpty()) { throw new IllegalArgumentException("Feature identifier cannot be null nor empty"); } if (roleName == null || roleName.isEmpty()) { throw new IllegalArgumentException("roleName cannot be null nor empty"); } if (!exist(uid)) { throw new FeatureNotFoundException(uid); } collection.updateOne( BUILDER.getFeatUid(uid), new Document("$pull", BUILDER.getRoles(roleName))); }
/** * Update a directory name * * @param u The owner * @param UID The UID */ public static void updateDirectoryDate(User u, String UID) { if (UID == null) return; // Nothing to update MongoCollection<Document> collection = DataBase.getInstance().getCollection("directories"); Date now = new Date(); BasicDBObject query = new BasicDBObject(); query.put("owner", new ObjectId(u.UID)); query.put("_id", new ObjectId(UID)); BasicDBObject update = new BasicDBObject(); update.append("$set", new BasicDBObject().append("edit", now)); collection.updateOne(query, update); }
/** {@inheritDoc} */ @Override public void addToGroup(String uid, String groupName) { if (uid == null || uid.isEmpty()) { throw new IllegalArgumentException("Feature identifier cannot be null nor empty"); } if (groupName == null || groupName.isEmpty()) { throw new IllegalArgumentException("Groupname cannot be null nor empty"); } if (!exist(uid)) { throw new FeatureNotFoundException(uid); } Document target = BUILDER.getFeatUid(uid); Document nGroupName = BUILDER.getGroupName(groupName); collection.updateOne(target, new Document(MONGO_SET, nGroupName)); }
/** {@inheritDoc} */ @Override public void update(Feature fp) { if (fp == null) { throw new IllegalArgumentException("Feature cannot be null nor empty"); } Feature fpExist = read(fp.getUid()); collection.updateOne( BUILDER.getFeatUid(fp.getUid()), new Document(MONGO_SET, MAPPER.toDocument(fp))); // enable/disable if (fp.isEnable() != fpExist.isEnable()) { if (fp.isEnable()) { enable(fp.getUid()); } else { disable(fp.getUid()); } } }
public void updateBooking(String bookingId) { try { final MongoDatabase mdb = MongoDBConnManager.getInstance().getConnection(); final MongoCollection<Document> coll = mdb.getCollection(DBConstants.COLL_BOOKING); // update user booking time final Document filterQuery = new Document(); filterQuery.put(DBConstants.BOOKING_ID, bookingId); final Document updateQuery = new Document(); final Document updateSet = new Document(); updateSet.put(DBConstants.DATE_N_TIME, new Date()); updateQuery.put("$set", updateSet); coll.updateOne(filterQuery, updateQuery); } catch (Exception e) { if (e instanceof com.mongodb.MongoTimeoutException) { throw new ApplicationException(MessagesEnum.MONGODB_IS_DOWN.getMessage(), e); } throw new ApplicationException(MessagesEnum.UPDATE_USER_FAILED.getMessage(), e); } }
/** * Move a directory * * @param u * @param d * @param newContainer * @return */ public static Directory moveDirectory(User u, Directory d, Directory newContainer) { if (d.UID == null) return d; MongoCollection<Document> collection = DataBase.getInstance().getCollection("directories"); MongoCollection<Document> collectionFiles = DataBase.getInstance().getCollection("files.files"); Date now = new Date(); // Change path BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(d.UID)); query.put("owner", new ObjectId(u.UID)); String oldPath = d.path; String newPath = newContainer.name == null ? "/" : newContainer.path + newContainer.name + "/"; BasicDBObject update = new BasicDBObject(); update.append("$set", new BasicDBObject().append("path", newPath).append("edit", now)); collection.updateOne(query, update); // Update paths query = new BasicDBObject(); query.put("owner", new ObjectId(u.UID)); if (d.getPathList().size() > 0) { String pathRegex = "^/" + String.join("/", d.getPathList()) + "/" + d.name + "/"; query.put("path", new BasicDBObject("$regex", pathRegex)); } else { String pathRegex = "^/" + d.name + "/"; query.put("path", new BasicDBObject("$regex", pathRegex)); } // Update directories for (Document doc : collection.find(query)) { BasicDBObject dirQuery = new BasicDBObject(); dirQuery.put("_id", doc.getObjectId("_id")); update = new BasicDBObject(); update.append( "$set", new BasicDBObject() .append("path", newPath + doc.getString("path").substring(oldPath.length())) .append("edit", now)); collection.updateOne(dirQuery, update); } // Update files for (Document doc : collectionFiles.find(query)) { BasicDBObject fileQuery = new BasicDBObject(); fileQuery.put("_id", doc.getObjectId("_id")); update = new BasicDBObject(); update.append( "$set", new BasicDBObject() .append("path", newPath + doc.getString("path").substring(oldPath.length())) .append("edit", now)); collectionFiles.updateOne(fileQuery, update); } updateDirectoryDate(u, newContainer); // Update local version d.path = newPath; return d; }
/** * Rename a directory * * @param u The owner * @param d The directory * @param newName The new name * @return The renamed directory */ public static Directory renameDirectory(User u, Directory d, String newName) { if (d.name == null) // Root can't be renamed return d; MongoCollection<Document> collection = DataBase.getInstance().getCollection("directories"); MongoCollection<Document> collectionFiles = DataBase.getInstance().getCollection("files.files"); Date now = new Date(); String newPath = d.path + newName; String oldPath = d.path + d.name; // Change name BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(d.UID)); query.put("owner", new ObjectId(u.UID)); BasicDBObject update = new BasicDBObject(); update.append("$set", new BasicDBObject().append("name", newName).append("edit", now)); collection.updateOne(query, update); // Update paths query = new BasicDBObject(); query.put("owner", new ObjectId(u.UID)); if (d.getPathList().size() > 0) { String pathRegex = "^/" + String.join("/", d.getPathList()) + "/" + d.name + "/"; query.put("path", new BasicDBObject("$regex", pathRegex)); } else { String pathRegex = "^/" + d.name + "/"; query.put("path", new BasicDBObject("$regex", pathRegex)); } // Update directories for (Document doc : collection.find(query)) { BasicDBObject dirQuery = new BasicDBObject(); dirQuery.put("_id", doc.getObjectId("_id")); update = new BasicDBObject(); update.append( "$set", new BasicDBObject() .append("path", newPath + doc.getString("path").substring(oldPath.length())) .append("edit", now)); collection.updateOne(dirQuery, update); } // Update files for (Document doc : collectionFiles.find(query)) { BasicDBObject fileQuery = new BasicDBObject(); fileQuery.put("_id", doc.getObjectId("_id")); update = new BasicDBObject(); update.append( "$set", new BasicDBObject() .append("path", newPath + doc.getString("path").substring(oldPath.length())) .append("edit", now)); collectionFiles.updateOne(fileQuery, update); } // Return the modified element d.name = newName; return d; }