Example #1
0
  @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;
  }
Example #2
0
  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;
  }
Example #3
0
  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;
  }
Example #4
0
 /**
  * 刷新用户信息
  *
  * @param user 要显示的用户
  */
 public void initUser(User user) {
   jLabel7.setText("当前用户:" + user.getUserName());
   SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日HH时mm分");
   jLabel8.setText("登录时间:" + sdf.format(new Date()));
 }