1 | package com.tourMate.dao.impl;
|
---|
2 |
|
---|
3 | import com.tourMate.dao.HotelDao;
|
---|
4 | import com.tourMate.entities.*;
|
---|
5 | import jakarta.persistence.EntityManager;
|
---|
6 | import jakarta.persistence.PersistenceContext;
|
---|
7 | import jakarta.transaction.Transactional;
|
---|
8 | import org.springframework.stereotype.Service;
|
---|
9 |
|
---|
10 | import java.util.Calendar;
|
---|
11 | import java.util.Date;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | @Service
|
---|
15 | public class HotelDaoImpl implements HotelDao {
|
---|
16 |
|
---|
17 | @PersistenceContext
|
---|
18 | EntityManager em;
|
---|
19 |
|
---|
20 | @Override
|
---|
21 | @Transactional
|
---|
22 | public void createHotel(Hotels hotel) {
|
---|
23 | em.persist(hotel);
|
---|
24 | }
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public List<Hotels> getHotels() {
|
---|
28 | return em.createQuery("select h from Hotels h order by h.hotelId").getResultList();
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public List<Hotels> getHotelsForUser(User u) {
|
---|
33 | return em.createQuery("select h from Hotels h where h.owner = :u").setParameter("u", u).getResultList();
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public List<Hotels> getHotelsByLocation(String hotelLocation) {
|
---|
38 | return em.createQuery("select h from Hotels h where h.hotelLocation = hotelLocation").getResultList();
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Transactional
|
---|
42 | @Override
|
---|
43 | public void editHotel(Hotels hotel) {
|
---|
44 | em.persist(hotel);
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Transactional
|
---|
48 | @Override
|
---|
49 | public void deleteHotel(Hotels hotel) {
|
---|
50 | em.remove(hotel);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public Hotels findHotelByID(long hotelId) {
|
---|
55 | return em.find(Hotels.class, hotelId);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public List<HotelsImages> getHotelImages(Hotels hotel) {
|
---|
60 | return em.createQuery("select hi from HotelsImages hi where hi.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | @Transactional
|
---|
65 | public void addHotelImage(HotelsImages image) {
|
---|
66 | em.persist(image);
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Transactional
|
---|
70 | @Override
|
---|
71 | public void addRoomImage(HotelRoomImages x) {
|
---|
72 | em.persist(x);
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | @Transactional
|
---|
77 | public void saveReservation(HotelRoomReservations hotelRoomReservations) {
|
---|
78 | em.persist(hotelRoomReservations);
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | @Transactional
|
---|
83 | public HotelsImages findHotelImageById(long hotelImageId) {
|
---|
84 | return em.find(HotelsImages.class, hotelImageId);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | @Transactional
|
---|
89 | public void deleteHotelImage(HotelsImages hotelsImages) {
|
---|
90 | em.remove(hotelsImages);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public List<HotelRoom> getRoomsOfHotel(Hotels hotel) {
|
---|
95 | return em.createQuery("SELECT hr from HotelRoom hr where hr.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public List<HotelRoomAvailable> getRoomsAvailable(Long id) {
|
---|
100 | return em.createQuery("SELECT hra from HotelRoomAvailable hra WHERE hra.hotelRoom.id = :hotelRoomId").setParameter("hotelRoomId", id).getResultList();
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public HotelRoom findRoomById(long hotelRoomId) {
|
---|
105 | return em.find(HotelRoom.class, hotelRoomId);
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public List<HotelRoomImages> getRoomImages(HotelRoom hotelRoom) {
|
---|
110 | return em.createQuery("select i from HotelRoomImages i where i.room = :hotelRoom").setParameter("hotelRoom", hotelRoom).getResultList();
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Transactional
|
---|
114 | @Override
|
---|
115 | public void createRoom(HotelRoom hotelRoom) {
|
---|
116 | em.persist(hotelRoom);
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Transactional
|
---|
120 | @Override
|
---|
121 | public void editRoom(HotelRoom hr) {
|
---|
122 | em.persist(hr);
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Transactional
|
---|
126 | @Override
|
---|
127 | public void deleteRoom(HotelRoom hr) {
|
---|
128 | em.remove(hr);
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Transactional
|
---|
132 | @Override
|
---|
133 | public void createRoomAvailible(HotelRoomAvailable hra) {
|
---|
134 | em.persist(hra);
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Transactional
|
---|
138 | @Override
|
---|
139 | public void editRoomAvailible(HotelRoomAvailable hr) {
|
---|
140 | em.persist(hr);
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Transactional
|
---|
144 | @Override
|
---|
145 | public void deleteRoomAvailible(HotelRoomAvailable hra) {
|
---|
146 | em.remove(hra);
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public HotelRoomAvailable findAvailibleRoomById(long hotelRoomAvailableId) {
|
---|
151 | return em.find(HotelRoomAvailable.class, hotelRoomAvailableId);
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public List<HotelRoomAvailable> getRoomsAvailibility() {
|
---|
156 | return em.createQuery("select hra from HotelRoomAvailable hra").getResultList();
|
---|
157 | }
|
---|
158 |
|
---|
159 | @Override
|
---|
160 | public List<HotelRoomAvailable> getRoomsAvailibilityByHotel(Hotels hotel) {
|
---|
161 | return em.createQuery("select hr from HotelRoomAvailable hr where hr.hotelRoom.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | public List<HotelRoomAvailable> getRoomsAvailibilityByDateAndLocation(String hotelLocation, Date dateFrom, Date dateTo, int numberOfBeds, Boolean flexible) {
|
---|
166 | if(flexible)
|
---|
167 | {
|
---|
168 | Calendar calendar = Calendar.getInstance();
|
---|
169 | Calendar calendar1 = Calendar.getInstance();
|
---|
170 | calendar.setTime(dateTo);
|
---|
171 | calendar1.setTime(dateFrom);
|
---|
172 | return em.createQuery("SELECT hr FROM HotelRoomAvailable hr WHERE " +
|
---|
173 | "((hr.dateFrom <= :dateTo AND hr.dateTo >= :dateFrom) OR " +
|
---|
174 | "(hr.dateFrom <= :dateToMinus1 AND hr.dateTo >= :dateFromMinus1) OR " +
|
---|
175 | "(hr.dateFrom <= :dateToMinus2 AND hr.dateTo >= :dateFromMinus2) OR " +
|
---|
176 | "(hr.dateFrom <= :dateToMinus3 AND hr.dateTo >= :dateFromMinus3) OR " +
|
---|
177 | "(hr.dateFrom <= :dateToPlus1 AND hr.dateTo >= :dateFromPlus1) OR " +
|
---|
178 | "(hr.dateFrom <= :dateToPlus2 AND hr.dateTo >= :dateFromPlus2) OR " +
|
---|
179 | "(hr.dateFrom <= :dateToPlus3 AND hr.dateTo >= :dateFromPlus3)) " +
|
---|
180 | "AND hr.hotelRoom.hotel.hotelLocation LIKE :hotelLocation " +
|
---|
181 | "AND hr.hotelRoom.numOfBeds >= :numBeds")
|
---|
182 | .setParameter("hotelLocation", hotelLocation)
|
---|
183 | .setParameter("dateFrom", dateFrom)
|
---|
184 | .setParameter("dateTo", dateTo)
|
---|
185 | .setParameter("dateToMinus1", subtractDays(calendar, 1))
|
---|
186 | .setParameter("dateToMinus2", subtractDays(calendar, 2))
|
---|
187 | .setParameter("dateToMinus3", subtractDays(calendar, 3))
|
---|
188 | .setParameter("dateToPlus1", addDays(calendar, 1))
|
---|
189 | .setParameter("dateToPlus2", addDays(calendar, 2))
|
---|
190 | .setParameter("dateToPlus3", addDays(calendar, 3))
|
---|
191 | .setParameter("dateFromMinus1", subtractDays(calendar1, 1))
|
---|
192 | .setParameter("dateFromMinus2", subtractDays(calendar1, 2))
|
---|
193 | .setParameter("dateFromMinus3", subtractDays(calendar1, 3))
|
---|
194 | .setParameter("dateFromPlus1", addDays(calendar1, 1))
|
---|
195 | .setParameter("dateFromPlus2", addDays(calendar1, 2))
|
---|
196 | .setParameter("dateFromPlus3", addDays(calendar1, 3))
|
---|
197 | .setParameter("numBeds", numberOfBeds)
|
---|
198 | .getResultList();
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | return em.createQuery("SELECT hr FROM HotelRoomAvailable hr WHERE " +
|
---|
203 | "((hr.dateFrom <= :dateTo AND hr.dateTo >= :dateFrom)) " +
|
---|
204 | "AND hr.hotelRoom.hotel.hotelLocation LIKE :hotelLocation " +
|
---|
205 | "AND hr.hotelRoom.numOfBeds >= :numBeds")
|
---|
206 | .setParameter("hotelLocation", hotelLocation)
|
---|
207 | .setParameter("dateFrom", dateFrom)
|
---|
208 | .setParameter("dateTo", dateTo)
|
---|
209 | .setParameter("numBeds", numberOfBeds)
|
---|
210 | .getResultList();
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
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 | }
|
---|
235 |
|
---|
236 | @Override
|
---|
237 | @Transactional
|
---|
238 | public void editReservation(HotelRoomReservations hr) {
|
---|
239 | em.persist(hr);
|
---|
240 | }
|
---|
241 |
|
---|
242 | @Transactional
|
---|
243 | @Override
|
---|
244 | public void deleteReservation(HotelRoomReservations hr) {
|
---|
245 |
|
---|
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 | } |
---|