コード例 #1
0
ファイル: DataAccess.java プロジェクト: saichandu/BoxMonitor
  public User getBooking(final String bookingId) {
    /**
     * Creating 'nextUserEmailInQueue' as string buffer because value cannot be reassigned as it is
     * declared as final
     */
    final User user = new 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.BOOKING_ID, bookingId);
      final ArrayList<Document> lstBkngs = coll.find(findCr).into(new ArrayList<Document>());

      for (final Document document : lstBkngs) {
        user.setEmail(document.getString(DBConstants.EMAIL));
        user.setBookingId(document.getString(DBConstants.BOOKING_ID));
      }
    } 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 user;
  }
コード例 #2
0
ファイル: DataAccess.java プロジェクト: saichandu/BoxMonitor
  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;
  }