/** * 取得指定住院号的记录 * * @param admissionNum 住院号 * @return 住院记录 */ public record GetRecord(String admissionNum) { try { MongoDAO dao = MongoDAO.GetInstance(); BasicDBObject cond = new BasicDBObject(); cond.append("admission_number", new BasicDBObject("$eq", admissionNum)); FindIterable<Document> result = dao.GetCollection("records").find(cond); MongoCursor<Document> it = result.iterator(); if (it.hasNext()) { Document doc = it.next(); record fol = new record(); fol.setAdmission_number(doc.getString("admission_number")); fol.setName(doc.getString("name")); fol.setWeixin_openid(doc.getString("weixin_openid")); fol.setLeaveTime(doc.getDate("leaveTime")); fol.setInTime(doc.getDate("inTime")); fol.setDisease(doc.getString("disease")); return fol; } return null; } catch (Exception e) { e.printStackTrace(); logger.error(e.toString()); return null; } }
public List<User> getAllBookings(final String boxName) { /** * Creating 'nextUserEmailInQueue' as string buffer because value cannot be reassigned as it is * declared as final */ final List<User> users = new ArrayList<User>(); try { final MongoDatabase mdb = MongoDBConnManager.getInstance().getConnection(); final MongoCollection<Document> coll = mdb.getCollection(DBConstants.COLL_BOOKING); final Document findCr = new Document(); findCr.put(DBConstants.BOX_NAME, boxName); final Document sortCr = new Document(); sortCr.put(DBConstants.BOOKED_DATE_N_TIME, 1); final ArrayList<Document> lstBkngs = coll.find(findCr).sort(sortCr).into(new ArrayList<Document>()); for (final Document document : lstBkngs) { final User user = new User(); user.setEmail(document.getString(DBConstants.EMAIL)); user.setBookedDateNTime(document.getDate(DBConstants.BOOKED_DATE_N_TIME)); user.setDateNTime(document.getDate(DBConstants.DATE_N_TIME)); user.setBookingId(document.getString(DBConstants.BOOKING_ID)); users.add(user); } } catch (Exception e) { if (e instanceof com.mongodb.MongoTimeoutException) { throw new ApplicationException(MessagesEnum.MONGODB_IS_DOWN.getMessage(), e); } throw new ApplicationException(MessagesEnum.BOOKINGS_RETRIVAL_FAILED.getMessage(), e); } return users; }
/** * Relock trigger if its lock has expired. * * @param key trigger to lock * @return true when successfully relocked */ public boolean relockExpired(TriggerKey key) { Document existingLock = locksDao.findTriggerLock(key); if (existingLock != null) { if (expiryCalculator.isTriggerLockExpired(existingLock)) { // When a scheduler is defunct then its triggers become expired // after sometime and can be recovered by other schedulers. // To check that a trigger is owned by a defunct scheduler we evaluate // its LOCK_TIME and try to reassign it to this scheduler. // Relock may not be successful when some other scheduler has done // it first. log.info("Trigger {} is expired - re-locking", key); return locksDao.relock(key, existingLock.getDate(Constants.LOCK_TIME)); } else { log.info( "Trigger {} hasn't expired yet. Lock time: {}", key, existingLock.getDate(Constants.LOCK_TIME)); } } else { log.warn( "Error retrieving expired lock from the database for trigger {}. Maybe it was deleted", key); } return false; }
@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; }
/** * 取得已出院未回访的用户 * * @return */ public List<record> GetleavehospitalRecords() { try { List<record> list = new ArrayList<record>(); MongoDAO dao = MongoDAO.GetInstance(); BasicDBObject cond = new BasicDBObject(); cond.append("leaveHospital", new BasicDBObject("$eq", true)); cond.append("followup", new BasicDBObject("$eq", false)); FindIterable<Document> result = dao.GetCollection("records").find(cond); MongoCursor<Document> it = result.iterator(); while (it.hasNext()) { Document doc = it.next(); record fol = new record(); fol.setAdmission_number(doc.getString("admission_number")); fol.setName(doc.getString("name")); fol.setWeixin_openid(doc.getString("weixin_openid")); fol.setLeaveTime(doc.getDate("leaveTime")); fol.setDisease(doc.getString("disease")); list.add(fol); } return list; } catch (Exception e) { e.printStackTrace(); logger.error(e.toString()); return null; } }
/** * Map a Mongo Document object to an Order object. * * @param d Document * @return An Order object with all the information of the Document object. */ public static final Order toOrder(Document d) { ObjectId id = d.get("_id", ObjectId.class); Document c = d.get("customer", Document.class); @SuppressWarnings("unchecked") List<Document> itemsDoc = (List<Document>) d.get("items"); List<Item> items = new ArrayList<>(itemsDoc.size()); for (Document itemDoc : itemsDoc) { items.add(ItemAdapter.toItem(itemDoc)); } Order o = new Order( id == null ? "" : id.toString(), d.getDouble("total"), d.getDate("date"), OrderStatus.findOrderStatus(d.getString("status")), CustomerAdapter.toCustomer(c), items); return o; }
@SuppressWarnings("unchecked") public List<Comment> get(String id) { MongoCollection<Document> commentCollection = mongoDatabase.getCollection("posts"); List<Comment> comments = new ArrayList<Comment>(); Document query = new Document(); query.put("_id", new ObjectId(id)); Document document = commentCollection.find(query).first(); if (document != null) { ArrayList<Document> list = (ArrayList<Document>) document.get("comments"); for (int i = 0; i < list.size(); i++) { Document com = (Document) list.get(i); Comment comment = new Comment(); comment.setUser(com.getString("user")); comment.setText(com.getString("text")); comment.setDate(com.getDate("date")); comments.add(comment); } } return comments; }
public Map<String, Booking> getAllBookings() { final Map<String, Booking> bookings = new LinkedHashMap<String, Booking>(); try { final MongoDatabase mdb = MongoDBConnManager.getInstance().getConnection(); final MongoCollection<Document> coll = mdb.getCollection(DBConstants.COLL_BOOKING); List<Document> aggregates = new ArrayList<Document>(); Document join = new Document(); Document sortCr = new Document(); /*Document lookup = new Document(); lookup.put("from", DBConstants.COLL_BOXES); lookup.put("localField", DBConstants.BOX_NAME); lookup.put("foreignField", DBConstants.BOX_NAME); lookup.put("as", "join_for_teamname");*/ Document lookup2 = new Document(); lookup2.put("from", DBConstants.COLL_USERS); lookup2.put("localField", DBConstants.EMAIL); lookup2.put("foreignField", DBConstants.EMAIL); lookup2.put("as", "join_for_username"); // join.put("$lookup", lookup); join.put("$lookup", lookup2); sortCr.put("$sort", new Document(DBConstants.TEAM_NAME, 1)); aggregates.add(join); aggregates.add(sortCr); final ArrayList<Document> bookingsFromDB = coll.aggregate(aggregates).into(new ArrayList<Document>()); for (final Document document : bookingsFromDB) { final User user = new User(); user.setUserName( ((Document) document.get("join_for_username", List.class).get(0)) .getString(DBConstants.USER_NAME)); user.setEmail(document.getString(DBConstants.EMAIL)); user.setTeamName( ((Document) document.get("join_for_username", List.class).get(0)) .getString(DBConstants.TEAM_NAME)); user.setBookedDateNTime(document.getDate(DBConstants.BOOKED_DATE_N_TIME)); user.setDateNTime(document.getDate(DBConstants.DATE_N_TIME)); user.setEstimatedUsage(document.getInteger(DBConstants.ESTIMATED_USAGE)); user.setBookingId(document.getString(DBConstants.BOOKING_ID)); if (!bookings.containsKey(document.getString(DBConstants.BOX_NAME))) { Booking bkng = new Booking(); bkng.setBoxName(document.getString(DBConstants.BOX_NAME)); bkng.setBoxOwner(getBoxOwner(document.getString(DBConstants.BOX_NAME))); bkng.addUserToQueue(user); bookings.put(document.getString(DBConstants.BOX_NAME), bkng); } else { Booking bkng = bookings.get(document.getString(DBConstants.BOX_NAME)); bkng.addUserToQueue(user); } } // sort users based on timestamps for (Booking bkng : bookings.values()) { Collections.sort(bkng.getUsersInQueue()); } } catch (Exception e) { if (e instanceof com.mongodb.MongoTimeoutException) { throw new ApplicationException(MessagesEnum.MONGODB_IS_DOWN.getMessage(), e); } throw new ApplicationException(MessagesEnum.BOOKINGS_RETRIVAL_FAILED.getMessage(), e); } return bookings; }