Class RoomService
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final EquipmentServiceUsed to fetch existing EquipmentTypes from the database.private static final @NonNull org.slf4j.Loggerprivate final ReservationServiceprivate final RoomRepositoryProvides database access to actually create, modify and delete Rooms. -
Constructor Summary
ConstructorsConstructorDescriptionRoomService(@NonNull RoomRepository roomRepository, @NonNull ReservationService reservationService, @NonNull EquipmentService equipmentService) Constructs a new RoomService. -
Method Summary
Modifier and TypeMethodDescription@NonNull RoomSchedulebuildSchedule(@NonNull Room room, @NonNull LocalDate date) @NonNull RoomSchedulebuildSchedule(@NonNull Room room, @NonNull LocalDate date, @NonNull MergeOptions mergeOptions) @NonNull RoomSchedulebuildSchedule(@NonNull Room room, @NonNull LocalDate date, @NonNull LocalTime startTime, @NonNull LocalTime endTime) @NonNull RoomSchedulebuildScheduleForToday(@NonNull Room room) @NonNull LinkedHashMap<DayOfWeek, RoomSchedule> buildScheduleForWeek(@NonNull Room room, @NonNull LocalDate date) voidcreateRoom(@NonNull Room room) Adds the givenRoomto the database.voiddeleteRoom(@NonNull String roomNumber) Removes theRoomwith the givenRoom.numberfrom the database, if there are no Reservations for the Room that end afterLocalDateTime.now().voidSaves the givenRoomto the database.@NonNull org.springframework.data.domain.Page<Room> findAll(@NonNull org.springframework.data.domain.Pageable pageable) A subset of all Rooms in the database table according to the givenPageable.@NonNull LinkedHashSet<String> Fetches all room numbers present on any Rooms in the database.@NonNull TreeSet<MatchingRoomDTO> findMatchingRooms(@NonNull Reservation reservation) Returns all Rooms that are bookable (i.e.@NonNull RoomfindRoomByNumber(String number) Fetches theRoomwith the specifiedRoom.numberfrom the database table.@NonNull org.springframework.data.domain.Page<Room> findRooms(@Nullable RoomStatus roomStatus, @Nullable Integer floor, @Nullable Accessibility accessibility, @NonNull org.springframework.data.domain.Pageable pageable) @NonNull org.springframework.data.domain.Page<Room> findRooms(@Nullable Integer floor, @Nullable Accessibility accessibility, @NonNull org.springframework.data.domain.Pageable pageable) Finds all Rooms that match the given criteria.@NonNull org.springframework.data.domain.Page<Room> findRoomsByFloor(int floor, @NonNull org.springframework.data.domain.Pageable pageable) @NonNull org.springframework.data.domain.Page<Room> findRoomsByStatus(@NonNull RoomStatus roomStatus, @NonNull LocalDateTime startTime, @NonNull LocalDateTime endTime, @Nullable Integer floor, @Nullable Accessibility accessibility, @NonNull org.springframework.data.domain.Pageable pageable) Finds out which Rooms have the givenRoomStatusfor the specified time period and returns them.@NonNull RoomStatusgetRoomStatus(@NonNull Room room, @NonNull LocalDateTime startTime, @NonNull LocalDateTime endTime) Checks theRoomStatusof the givenRoomfor the specified time period.booleanisRoomFree(@NonNull Room room) private voidsetEquipment(@NonNull Room room) Replaces the givenRoom's IndividualEquipmentTypes with matching existing ones, in order to allow the Room to be saved to the database without adding new equipment entites.
-
Field Details
-
LOG
@NonNull private static final @NonNull org.slf4j.Logger LOG -
roomRepository
Provides database access to actually create, modify and delete Rooms. -
reservationService
-
equipmentService
Used to fetch existing EquipmentTypes from the database.- See Also:
-
-
Constructor Details
-
RoomService
public RoomService(@NonNull @NonNull RoomRepository roomRepository, @Lazy @NonNull @NonNull ReservationService reservationService, @Lazy @NonNull @NonNull EquipmentService equipmentService) Constructs a new RoomService.- Parameters:
roomRepository- TheroomRepositoryto be used for database access.reservationService- ThereservationService.equipmentService- TheequipmentService.
-
-
Method Details
-
findAll
@NonNull public @NonNull org.springframework.data.domain.Page<Room> findAll(@NonNull @NonNull org.springframework.data.domain.Pageable pageable) A subset of all Rooms in the database table according to the givenPageable.- Parameters:
pageable- The pagination information specifying which page of Rooms shall be returned.- Returns:
- All Rooms, but only the specified subset.
-
findRoomsByFloor
@NonNull public @NonNull org.springframework.data.domain.Page<Room> findRoomsByFloor(int floor, @NonNull @NonNull org.springframework.data.domain.Pageable pageable) A subset – according to the givenPageable– of those Rooms in the database table that are located on the specified floor.- Parameters:
floor- The floor whose Rooms are looked for.pageable- The pagination information specifying which page of Rooms shall be returned.- Returns:
- The Rooms on the given floor, but only the specified subset.
-
findRoomByNumber
Fetches theRoomwith the specifiedRoom.numberfrom the database table.- Parameters:
number- The number of the room that is being looked for.- Returns:
- The Room matching the given number, if it exists.
- Throws:
NotFoundException- If no Room has the given room number.
-
findAllRoomNumbers
Fetches all room numbers present on any Rooms in the database.- Returns:
- All room numbers in the database, sorted.
-
isRoomFree
- Parameters:
room- The Room whose status shall be checked.- Returns:
- True if no Reservation is currently active for the Room.
-
getRoomStatus
@NonNull public @NonNull RoomStatus getRoomStatus(@NonNull @NonNull Room room, @NonNull @NonNull LocalDateTime startTime, @NonNull @NonNull LocalDateTime endTime) throws ReversedTimePeriodException, ReservationPeriodTooSmallException Checks the
RoomStatusof the givenRoomfor the specified time period.Returns:
RoomStatus.FREEif there are no Reservations for the Room at all,RoomStatus.PARTIALLY_OCCUPIEDif there are only SeatReservations (at least one),RoomStatus.OCCUPIEDif there is at least one Reservation for the whole Room.
- Parameters:
room- The Room whose status shall be checked.startTime- The status of the Room beginning no earlier than this time is searched for. Must come at least 5 minutes before theendTime.endTime- The status of the Room ending no later than this time is searched for. Must come at least 5 minutes after thestartTime.- Returns:
- The given Room's status during the given period.
- Throws:
ReversedTimePeriodException- If theendTimedoes not come after thestartTime.ReservationPeriodTooSmallException- If the specified time period is shorter than 5 minutes.
-
findRooms
@NonNull public @NonNull org.springframework.data.domain.Page<Room> findRooms(@Nullable @Nullable RoomStatus roomStatus, @Nullable @Nullable Integer floor, @Nullable @Nullable Accessibility accessibility, @NonNull @NonNull org.springframework.data.domain.Pageable pageable) - Parameters:
roomStatus- If set, only Rooms with this room status will be returned.floor- If set, only Rooms on the specified floor will be returned.accessibility- If set, only Rooms with at least the specifiedAccessibilityas theRoom.accessibilitywill be returned.pageable- Pagination information.- Returns:
- A subset of the Rooms with the given status. The
Page.getTotalElements()represents the total number of Rooms. The purpose of the pagination is to avoid the very costly operation to compute every Room's status, so – by definition – it is not possible to “look ahead” how many of the remaining Rooms have the given room status.
-
findRooms
@NonNull public @NonNull org.springframework.data.domain.Page<Room> findRooms(@Nullable @Nullable Integer floor, @Nullable @Nullable Accessibility accessibility, @NonNull @NonNull org.springframework.data.domain.Pageable pageable) Finds all Rooms that match the given criteria.- Parameters:
floor- If set, only Rooms on the specified floor will be returned.accessibility- If set, only Rooms with at least the specifiedAccessibilityas theRoom.accessibilitywill be returned.pageable- Pagination information.- Returns:
- The specified subset of all Rooms.
-
findRoomsByStatus
@NonNull public @NonNull org.springframework.data.domain.Page<Room> findRoomsByStatus(@NonNull @NonNull RoomStatus roomStatus, @NonNull @NonNull LocalDateTime startTime, @NonNull @NonNull LocalDateTime endTime, @Nullable @Nullable Integer floor, @Nullable @Nullable Accessibility accessibility, @NonNull @NonNull org.springframework.data.domain.Pageable pageable) throws ReversedTimePeriodException, ReservationPeriodTooSmallException Finds out which Rooms have the givenRoomStatusfor the specified time period and returns them.- Parameters:
roomStatus- The room status searched for.startTime- The status of the Room beginning no earlier than this time is searched for. Must come at least 5 minutes before theendTime.endTime- The status of the Room ending no later than this time is searched for. Must come at least 5 minutes after thestartTime.floor- If set only Rooms on the specified floor will be returned.accessibility- If set, only Rooms with at least the specifiedAccessibilityas theRoom.accessibilitywill be returned.pageable- Pagination information.- Returns:
- A subset of the Rooms with the given status. The
Page.getTotalElements()represents the total number of Rooms. The purpose of the pagination is to avoid the very costly operation to compute every Room's status, so – by definition – it is not possible to “look ahead” how many of the remaining Rooms have the given room status. - Throws:
ReversedTimePeriodException- If theendTimedoes not come after thestartTime.ReservationPeriodTooSmallException- If the specified time period is shorter than 5 minutes.
-
findMatchingRooms
@NonNull public @NonNull TreeSet<MatchingRoomDTO> findMatchingRooms(@NonNull @NonNull Reservation reservation) throws ReservationPeriodTooSmallException, ReversedTimePeriodException Returns all Rooms that are bookable (i.e.RoomStatus.FREEfor room Reservations and at leastRoomStatus.PARTIALLY_OCCUPIEDfor SeatReservations) by the given Reservation and sorts them in descending order byReservation.matches(Room).- Parameters:
reservation- The Reservation for which Rooms shall be looked up.- Returns:
- Rooms matching the given Reservation.
- Throws:
ReservationPeriodTooSmallException- If the period betweenReservation.startDateandReservation.endDateis smaller thanReservationService.MINIMUM_RESERVATION_TIMESPAN.ReversedTimePeriodException- If theReservation.startDatedoes not come before theReservation.endDate.
-
buildScheduleForToday
Creates a
RoomSchedulefor the givenRoomfor today.The schedule will include
- one
RoomScheduleItemfor eachReservationmatching the given parameters; - one schedule item for each time period during which there is no Reservation, this includes the period before the first reservation and the period after the last reservation.
If there is no Reservation for the Room today, the whole period will be a free period.
- Parameters:
room- The Room for which the schedule shall be built.- Returns:
- The schedule for the given Room today.
- one
-
buildScheduleForWeek
@NonNull public @NonNull LinkedHashMap<DayOfWeek, RoomSchedule> buildScheduleForWeek(@NonNull @NonNull Room room, @NonNull @NonNull LocalDate date) Creates a
RoomSchedulefor the givenRoomfor every day in the given date's week. The week begins on aDayOfWeek.MONDAY, so either- on the last Monday or
- on the given date if it happens to be a Monday.
The schedule will include for each day:
- one
RoomScheduleItemfor eachReservationmatching the given parameters; - one schedule item for each time period during which there is no Reservation, this includes the period before the first reservation and the period after the last reservation.
If there is no Reservation for the Room on a day, the whole period will be a free period.
Consecutive RoomScheduleItems whose durations are no longer than the
ReservationService.MINIMUM_RESERVATION_TIMESPANwill be merged recursively.- Parameters:
room- The identifier of the Room for which the schedule shall be built.date- Any day in the week that shall be considered.- Returns:
- The schedule for the given Room for every day in the given date's week.
-
buildSchedule
@NonNull public @NonNull RoomSchedule buildSchedule(@NonNull @NonNull Room room, @NonNull @NonNull LocalDate date) Creates a
RoomSchedulefor the givenRoomfor the givenLocalDate.The schedule will include
- one
RoomScheduleItemfor eachReservationmatching the given parameters; - one schedule item for each time period during which there is no Reservation, this includes the period before the first reservation and the period after the last reservation.
If there is no Reservation for the Room today, the whole period will be a free period.
- Parameters:
room- The Room for which the schedule shall be built.date- The schedule will cover this whole day.- Returns:
- The schedule for the given Room today.
- one
-
buildSchedule
@NonNull public @NonNull RoomSchedule buildSchedule(@NonNull @NonNull Room room, @NonNull @NonNull LocalDate date, @NonNull @NonNull MergeOptions mergeOptions) -
buildSchedule
@NonNull public @NonNull RoomSchedule buildSchedule(@NonNull @NonNull Room room, @NonNull @NonNull LocalDate date, @NonNull @NonNull LocalTime startTime, @NonNull @NonNull LocalTime endTime) throws ReversedTimePeriodException, ReservationPeriodTooSmallException Alias for
ReservationService.buildSchedule(Room, LocalDate, LocalTime, LocalTime).Creates a
RoomSchedulefor the givenRoomfor today.- Parameters:
room- The Room for which the schedule shall be built.date- The day for which the schedule shall be valid.startTime- Ignore Reservations that come before this point in time. Must come >5 minutes before theendTime!endTime- Ignore Reservations that come after this point in time. Must come >5 minutes after thestartTime!- Returns:
- The schedule for the given Room between the given
startTimeandendTimeon the given day. - Throws:
ReversedTimePeriodException- If the start time does not come before the end time.ReservationPeriodTooSmallException- If thestartTimedoes not come >5 minutes before theendTime.
-
createRoom
@PreAuthorize("hasRole('FACULTY_ADMINISTRATION')") public void createRoom(@NonNull @NonNull Room room) throws AlreadyExistsException, InvalidIndividualEquipmentException, InvalidCommonEquipmentException Adds the given
Roomto the database.Replaces the Room's IndividualEquipmentTypes with matching existing ones, in order not to add new ones to the database. Does the same for the CommonEquipmentTypes.
- Parameters:
room- The new Room that shall be newly inserted into the database table. Must have a uniqueRoom.id.- Throws:
AlreadyExistsException- If the Room's number is already present in the database.InvalidIndividualEquipmentException- If one of the Room's IndividualEquipmentTypes does not exist in the database.InvalidCommonEquipmentException- If one of the Room's CommonEquipmentTypes does not exist in the database.
-
editRoom
@PreAuthorize("hasRole('FACULTY_ADMINISTRATION')") public void editRoom(@NonNull @NonNull Room room) throws InvalidIndividualEquipmentException, InvalidCommonEquipmentException, AlreadyExistsException Saves the given
Roomto the database.If a Room with the same
Room.idalready exists, then the given object must be the exact reference returned by the database (i.e. Hibernate)! (Otherwise, the operation will fail.)Replaces the Room's IndividualEquipmentTypes with matching existing ones, in order not to add new ones to the database. Does the same for the CommonEquipmentTypes.
- Parameters:
room- The Room that shall be edited the database table.- Throws:
AlreadyExistsException- If the Room's number is already occupied by a different Room in the database.InvalidIndividualEquipmentException- If one of the Room's IndividualEquipmentTypes does not exist in the database.InvalidCommonEquipmentException- If one of the Room's CommonEquipmentTypes does not exist in the database.
-
setEquipment
private void setEquipment(@NonNull @NonNull Room room) throws InvalidIndividualEquipmentException, InvalidCommonEquipmentException Replaces the givenRoom's IndividualEquipmentTypes with matching existing ones, in order to allow the Room to be saved to the database without adding new equipment entites. Does the same for the CommonEquipmentTypes.- Parameters:
room- The Room whose equipment shall be prepared for being saved to the database.- Throws:
InvalidIndividualEquipmentException- If one of the Room's IndividualEquipmentTypes does not exist in the database.InvalidCommonEquipmentException- If one of the Room's CommonEquipmentTypes does not exist in the database.
-
deleteRoom
@PreAuthorize("hasRole('FACULTY_ADMINISTRATION')") public void deleteRoom(@NonNull @NonNull String roomNumber) throws NotFoundException, RoomStillHasReservationsException Removes the
Roomwith the givenRoom.numberfrom the database, if there are no Reservations for the Room that end afterLocalDateTime.now().Deletes all past Reservations.
- Parameters:
roomNumber- The number of the Room that shall no longer be part of the system.- Throws:
NotFoundException- If no Room with the given number exists.RoomStillHasReservationsException- If there are still future Reservations for the given Room. In this case, no past Reservations are deleted.
-