1 | package com.tourMate.dao.impl;
|
---|
2 |
|
---|
3 | import com.tourMate.dao.HotelDao;
|
---|
4 | import com.tourMate.entities.*;
|
---|
5 | import com.tourMate.events.OnHotelReservationEvent;
|
---|
6 | import com.tourMate.events.OnRegistrationSuccessEvent;
|
---|
7 | import jakarta.persistence.EntityManager;
|
---|
8 | import jakarta.persistence.PersistenceContext;
|
---|
9 | import jakarta.transaction.Transactional;
|
---|
10 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
11 | import org.springframework.context.ApplicationEventPublisher;
|
---|
12 | import org.springframework.stereotype.Service;
|
---|
13 |
|
---|
14 | import java.util.Calendar;
|
---|
15 | import java.util.Date;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | @Service
|
---|
19 | public class HotelDaoImpl implements HotelDao {
|
---|
20 |
|
---|
21 | @PersistenceContext
|
---|
22 | EntityManager em;
|
---|
23 | @Autowired
|
---|
24 | ApplicationEventPublisher eventPublisher;
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | @Transactional
|
---|
28 | public void createHotel(Hotels hotel) {
|
---|
29 | em.persist(hotel);
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public List<Hotels> getHotels() {
|
---|
34 | return em.createQuery("select h from Hotels h order by h.hotelId").getResultList();
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public List<Hotels> getHotelsForUser(User u) {
|
---|
39 | return em.createQuery("select h from Hotels h where h.owner = :u or h.owner in :users")
|
---|
40 | .setParameter("u", u)
|
---|
41 | .setParameter("users", u.getConnectedAccounts())
|
---|
42 | .getResultList();
|
---|
43 | }
|
---|
44 | @Transactional
|
---|
45 | @Override
|
---|
46 | public void editHotel(Hotels hotel) {
|
---|
47 | em.persist(hotel);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Transactional
|
---|
51 | @Override
|
---|
52 | public void deleteHotel(Hotels hotel) {
|
---|
53 | em.remove(hotel);
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public Hotels findHotelByID(long hotelId) {
|
---|
58 | return em.find(Hotels.class, hotelId);
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public List<HotelsImages> getHotelImages(Hotels hotel) {
|
---|
63 | return em.createQuery("select hi from HotelsImages hi where hi.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | @Transactional
|
---|
68 | public void addHotelImage(HotelsImages image) {
|
---|
69 | em.persist(image);
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Transactional
|
---|
73 | @Override
|
---|
74 | public void addRoomImage(HotelRoomImages x) {
|
---|
75 | em.persist(x);
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | @Transactional
|
---|
80 | public void saveReservation(HotelRoomReservations hotelRoomReservations) {
|
---|
81 | em.persist(hotelRoomReservations);
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | @Transactional
|
---|
86 | public HotelsImages findHotelImageById(long hotelImageId) {
|
---|
87 | return em.find(HotelsImages.class, hotelImageId);
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | @Transactional
|
---|
92 | public void deleteHotelImage(HotelsImages hotelsImages) {
|
---|
93 | em.remove(hotelsImages);
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public List<HotelRoom> getRoomsOfHotel(Hotels hotel) {
|
---|
98 | return em.createQuery("SELECT hr from HotelRoom hr where hr.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public List<HotelRoomAvailable> getRoomsAvailable(Long id) {
|
---|
103 | return em.createQuery("SELECT hra from HotelRoomAvailable hra WHERE hra.hotelRoom.id = :hotelRoomId").setParameter("hotelRoomId", id).getResultList();
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public HotelRoom findRoomById(long hotelRoomId) {
|
---|
108 | return em.find(HotelRoom.class, hotelRoomId);
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public List<HotelRoomImages> getRoomImages(HotelRoom hotelRoom) {
|
---|
113 | return em.createQuery("select i from HotelRoomImages i where i.room = :hotelRoom").setParameter("hotelRoom", hotelRoom).getResultList();
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Transactional
|
---|
117 | @Override
|
---|
118 | public void createRoom(HotelRoom hotelRoom) {
|
---|
119 | em.persist(hotelRoom);
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Transactional
|
---|
123 | @Override
|
---|
124 | public void editRoom(HotelRoom hr) {
|
---|
125 | em.persist(hr);
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Transactional
|
---|
129 | @Override
|
---|
130 | public void deleteRoom(HotelRoom hr) {
|
---|
131 | em.remove(hr);
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Transactional
|
---|
135 | @Override
|
---|
136 | public void createRoomAvailible(HotelRoomAvailable hra) {
|
---|
137 | em.persist(hra);
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Transactional
|
---|
141 | @Override
|
---|
142 | public void editRoomAvailible(HotelRoomAvailable hr) {
|
---|
143 | em.persist(hr);
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Transactional
|
---|
147 | @Override
|
---|
148 | public void deleteRoomAvailible(HotelRoomAvailable hra) {
|
---|
149 | em.remove(hra);
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public HotelRoomAvailable findAvailibleRoomById(long hotelRoomAvailableId) {
|
---|
154 | return em.find(HotelRoomAvailable.class, hotelRoomAvailableId);
|
---|
155 | }
|
---|
156 |
|
---|
157 | @Override
|
---|
158 | public List<HotelRoomAvailable> getRoomsAvailibility() {
|
---|
159 | return em.createQuery("select hra from HotelRoomAvailable hra").getResultList();
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public List<HotelRoomAvailable> getRoomsAvailibilityByHotel(Hotels hotel) {
|
---|
164 | return em.createQuery("select hr from HotelRoomAvailable hr where hr.hotelRoom.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
165 | }
|
---|
166 |
|
---|
167 | @Override
|
---|
168 | public List<HotelRoomAvailable> getRoomsAvailibilityByDateAndLocation(String hotelLocation, Date dateFrom, Date dateTo, int numberOfBeds, Boolean flexible) {
|
---|
169 | if(flexible)
|
---|
170 | {
|
---|
171 | Calendar calendar = Calendar.getInstance();
|
---|
172 | Calendar calendar1 = Calendar.getInstance();
|
---|
173 | calendar.setTime(dateTo);
|
---|
174 | calendar1.setTime(dateFrom);
|
---|
175 | return em.createQuery("SELECT hr FROM HotelRoomAvailable hr WHERE " +
|
---|
176 | "((hr.dateFrom <= :dateTo AND hr.dateTo >= :dateFrom) OR " +
|
---|
177 | "(hr.dateFrom <= :dateToMinus1 AND hr.dateTo >= :dateFromMinus1) OR " +
|
---|
178 | "(hr.dateFrom <= :dateToMinus2 AND hr.dateTo >= :dateFromMinus2) OR " +
|
---|
179 | "(hr.dateFrom <= :dateToMinus3 AND hr.dateTo >= :dateFromMinus3) OR " +
|
---|
180 | "(hr.dateFrom <= :dateToPlus1 AND hr.dateTo >= :dateFromPlus1) OR " +
|
---|
181 | "(hr.dateFrom <= :dateToPlus2 AND hr.dateTo >= :dateFromPlus2) OR " +
|
---|
182 | "(hr.dateFrom <= :dateToPlus3 AND hr.dateTo >= :dateFromPlus3)) " +
|
---|
183 | "AND hr.hotelRoom.hotel.hotelLocation LIKE :hotelLocation " +
|
---|
184 | "AND hr.hotelRoom.numOfBeds >= :numBeds")
|
---|
185 | .setParameter("hotelLocation", hotelLocation)
|
---|
186 | .setParameter("dateFrom", dateFrom)
|
---|
187 | .setParameter("dateTo", dateTo)
|
---|
188 | .setParameter("dateToMinus1", subtractDays(calendar, 1))
|
---|
189 | .setParameter("dateToMinus2", subtractDays(calendar, 2))
|
---|
190 | .setParameter("dateToMinus3", subtractDays(calendar, 3))
|
---|
191 | .setParameter("dateToPlus1", addDays(calendar, 1))
|
---|
192 | .setParameter("dateToPlus2", addDays(calendar, 2))
|
---|
193 | .setParameter("dateToPlus3", addDays(calendar, 3))
|
---|
194 | .setParameter("dateFromMinus1", subtractDays(calendar1, 1))
|
---|
195 | .setParameter("dateFromMinus2", subtractDays(calendar1, 2))
|
---|
196 | .setParameter("dateFromMinus3", subtractDays(calendar1, 3))
|
---|
197 | .setParameter("dateFromPlus1", addDays(calendar1, 1))
|
---|
198 | .setParameter("dateFromPlus2", addDays(calendar1, 2))
|
---|
199 | .setParameter("dateFromPlus3", addDays(calendar1, 3))
|
---|
200 | .setParameter("numBeds", numberOfBeds)
|
---|
201 | .getResultList();
|
---|
202 | }
|
---|
203 | else
|
---|
204 | {
|
---|
205 | return em.createQuery("SELECT hr FROM HotelRoomAvailable hr WHERE " +
|
---|
206 | "((hr.dateFrom <= :dateTo AND hr.dateTo >= :dateFrom)) " +
|
---|
207 | "AND hr.hotelRoom.hotel.hotelLocation LIKE :hotelLocation " +
|
---|
208 | "AND hr.hotelRoom.numOfBeds >= :numBeds")
|
---|
209 | .setParameter("hotelLocation", hotelLocation)
|
---|
210 | .setParameter("dateFrom", dateFrom)
|
---|
211 | .setParameter("dateTo", dateTo)
|
---|
212 | .setParameter("numBeds", numberOfBeds)
|
---|
213 | .getResultList();
|
---|
214 | }
|
---|
215 |
|
---|
216 | }
|
---|
217 |
|
---|
218 | private Date addDays(Calendar calendar, int days) {
|
---|
219 | Calendar newCalendar = (Calendar) calendar.clone();
|
---|
220 | newCalendar.add(Calendar.DAY_OF_MONTH, days);
|
---|
221 | return newCalendar.getTime();
|
---|
222 | }
|
---|
223 |
|
---|
224 | private Date subtractDays(Calendar calendar, int days) {
|
---|
225 | Calendar newCalendar = (Calendar) calendar.clone();
|
---|
226 | newCalendar.add(Calendar.DAY_OF_MONTH, -days);
|
---|
227 | return newCalendar.getTime();
|
---|
228 | }
|
---|
229 |
|
---|
230 | @Override
|
---|
231 | @Transactional
|
---|
232 | public void createReservation(HotelRoomReservations r) {
|
---|
233 | em.persist(r);
|
---|
234 | eventPublisher.publishEvent(new OnHotelReservationEvent(r.getUser(), r));
|
---|
235 | }
|
---|
236 |
|
---|
237 | @Override
|
---|
238 | @Transactional
|
---|
239 | public void editReservation(HotelRoomReservations hr) {
|
---|
240 | em.persist(hr);
|
---|
241 | }
|
---|
242 |
|
---|
243 | @Transactional
|
---|
244 | @Override
|
---|
245 | public void deleteReservation(HotelRoomReservations hr) {
|
---|
246 | em.remove(hr);
|
---|
247 | }
|
---|
248 |
|
---|
249 | @Override
|
---|
250 | public HotelRoomReservations findReservationById(long hotelRoomReservedId) {
|
---|
251 | return em.find(HotelRoomReservations.class, hotelRoomReservedId);
|
---|
252 | }
|
---|
253 |
|
---|
254 | @Override
|
---|
255 | public List<HotelRoomReservations> findReservationByUser(User user) {
|
---|
256 | return em.createQuery("select hr from HotelRoomReservations hr where hr.user = :user and hr.dateTo >= now()")
|
---|
257 | .setParameter("user", user).getResultList();
|
---|
258 | }
|
---|
259 |
|
---|
260 | @Override
|
---|
261 | public List<HotelRoomReservations> findReservationByHotel(Hotels hotel) {
|
---|
262 | List<HotelRoom> hotelRooms = getRoomsOfHotel(hotel);
|
---|
263 | return em.createQuery("select hr from HotelRoomReservations hr where hr.hotelRoom.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
264 | }
|
---|
265 |
|
---|
266 | @Override
|
---|
267 | public List<HotelRoomReservations> getReservations() {
|
---|
268 | return em.createQuery("select hr from HotelRoomReservations hr order by hr.user.name").getResultList();
|
---|
269 | }
|
---|
270 |
|
---|
271 | @Override
|
---|
272 | public List<Reviews> findReviewsByHotel(Hotels hotel) {
|
---|
273 | return em.createQuery("select r from Reviews r where r.hotel = :hotel")
|
---|
274 | .setParameter("hotel", hotel)
|
---|
275 | .getResultList();
|
---|
276 | }
|
---|
277 |
|
---|
278 | @Override
|
---|
279 | public List<HotelRoomReservations> findPastReservationByUser(User u) {
|
---|
280 | return em.createQuery("select hr from HotelRoomReservations hr where hr.user = :user and hr.dateTo <= now()")
|
---|
281 | .setParameter("user", u).getResultList();
|
---|
282 | }
|
---|
283 |
|
---|
284 | @Override
|
---|
285 | public List<HotelRoomReservations> getReservationsInPeriod(String hotelLocation, Date dateFrom, Date dateTo) {
|
---|
286 | return em.createQuery("select hr from HotelRoomReservations hr where hr.hotelRoom.hotel.hotelLocation like :hotelLocation and (hr.dateFrom between :dateFrom and :dateTo) or (hr.dateTo between :dateFrom and :dateTo)")
|
---|
287 | .setParameter("hotelLocation", hotelLocation)
|
---|
288 | .setParameter("dateFrom", dateFrom)
|
---|
289 | .setParameter("dateTo", dateTo)
|
---|
290 | .getResultList();
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Override
|
---|
294 | public List<Hotels> getHotelsByLocation(String hotelLocation)
|
---|
295 | {
|
---|
296 | return em.createQuery("select h from Hotels h where h.hotelLocation like :hotelLocation")
|
---|
297 | .setParameter("hotelLocation", hotelLocation)
|
---|
298 | .getResultList();
|
---|
299 | }
|
---|
300 | } |
---|