/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LinkedList<Booking> bookings = (LinkedList<Booking>) request.getSession().getAttribute("bookings"); if (this.checkBookings(request)) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); } else { Iterator<Booking> iterator = bookings.iterator(); LinkedList<Sale> sales = new LinkedList<Sale>(); LinkedList<Case> cases = new LinkedList<Case>(); while (iterator.hasNext()) { Booking currentBooking = iterator.next(); Case currentCase = new Case(); currentCase.setNumber(this.hashTheBooking(currentBooking)); currentCase.setTicketNumber(currentCase.getNumber()); currentCase.setUserMail(currentBooking.getUserMail()); currentCase.setRoomName(currentBooking.getRoomName()); currentCase.setDate(currentBooking.getDate()); currentCase.setSpectacleName(currentBooking.getSpectacle().getName()); cases.add(currentCase); try { this.daoCase.create(currentCase); Sale currentSale = this.daoSale.createFromBooking(currentBooking, currentCase); sales.add(currentSale); bookings.remove(currentBooking); } catch (Exception e) { this.daoBooking.create(currentBooking); response.sendError(HttpServletResponse.SC_BAD_REQUEST); } } LinkedList<Sale> salesSession = this.getSales(request); LinkedList<Case> casesSession = this.getCases(request); if (salesSession != null) sales.addAll(salesSession); if (casesSession != null) cases.addAll(casesSession); request.getSession().setAttribute("sales", sales); request.getSession().setAttribute("cases", cases); request.getSession().setAttribute("bookings", bookings); } }
private boolean checkBookings(HttpServletRequest request) { Object o = request.getSession().getAttribute("bookings"); if (o == null || ((LinkedList<Booking>) o).isEmpty()) { return true; } LinkedList<Booking> bookings = (LinkedList<Booking>) o; Date now = new Date(); boolean wasDeleted = false; Iterator<Booking> iterator = bookings.iterator(); while (iterator.hasNext()) { Booking booking = iterator.next(); long diff = now.getTime() - booking.getCreationDate().getTime(); if ((diff / (60 * 1000) % 60) > 1) { wasDeleted = true; bookings.remove(booking); this.daoBooking.delete(booking); } } request.getSession().setAttribute("bookings", bookings); return wasDeleted; }
private long hashTheBooking(Booking booking) { String toHash = booking.getRoomName() + booking.getUserMail(); SimpleDateFormat ft = new SimpleDateFormat("HHddMMyyyy"); toHash += ft.format(booking.getDate()); toHash += ft.format(booking.getCreationDate()); toHash += booking.getSpectacle().getName(); toHash += booking.getTotalPrice(); return toHash.hashCode(); }