Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public int occupiedDoubleRooms(Long hotelCode, LocalDate date) throws BookingServiceException {
- if (date == null) {
- throw new BookingServiceException("La fecha no puede ser nula.");
- }
- if (hotelCode == null) {
- throw new BookingServiceException("El código de hotel no puede ser nulo.");
- }
- EntityManager em = emf.createEntityManager();
- EntityTransaction et = em.getTransaction();
- et.begin();
- try {
- HotelJPADAO hotelDAO = daoFactory.generateHotelJPADAO(em);
- Hotel hotel = hotelDAO.findById(hotelCode);
- if (hotel == null) {
- throw new BookingServiceException("Hotel no encontrado con el código: " + hotelCode);
- }
- List<Booking> bookings = hotel.getBookings();
- int occupiedRooms = 0;
- for (Booking booking : bookings) {
- if ((booking.getDepartureDate().isAfter(date)||booking.getDepartureDate().isEqual(date))&&((booking.getArrivalDate().isBefore(date))||booking.getArrivalDate().isEqual(date))&& booking.getNumberOfPeople()==2) {
- occupiedRooms++;
- }
- }
- et.commit();
- return occupiedRooms;
- } catch (RuntimeException e) {
- et.rollback();
- em.close();
- throw new BookingServiceException("Error al obtener las habitaciones dobles ocupadas.", e);
- } finally {
- em.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement