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.Date;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | @Service
|
---|
14 | public class HotelDaoImpl implements HotelDao {
|
---|
15 |
|
---|
16 | @PersistenceContext
|
---|
17 | EntityManager em;
|
---|
18 |
|
---|
19 | @Override
|
---|
20 | @Transactional
|
---|
21 | public void createHotel(Hotels hotel, long userId) {
|
---|
22 | User u = em.find(User.class, userId);
|
---|
23 | Hotels h = new Hotels(hotel.getHotelName(), hotel.getHotelDescripiton(), hotel.getHotelLocation(), hotel.getHotelEDBS(), hotel.getParking(), hotel.getPetFriendly(), hotel.getInternetAvailable(), u);
|
---|
24 | em.persist(h);
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public List<Hotels> getHotels() {
|
---|
29 | List<Hotels> hoteli = em.createQuery("select h from Hotels h order by h.hotelId").getResultList();
|
---|
30 | return hoteli;
|
---|
31 | //return em.createQuery("select h from Hotels h order by h.hotelId").getResultList();
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public List<Hotels> getHotelsForUser(long userId) {
|
---|
36 | User u = em.find(User.class, userId);
|
---|
37 | return em.createQuery("select h from Hotels h where h.owner = :u").setParameter("u", u).getResultList();
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public List<Hotels> getHotelsByLocation(String hotelLocation) {
|
---|
42 | return em.createQuery("select h from Hotels h where h.hotelLocation = hotelLocation").getResultList();
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Transactional
|
---|
46 | @Override
|
---|
47 | public void editHotel(long hotelId, String hotelName, String hotelDescripiton, String hotelLocation, String hotelEDBS, Boolean parking, Boolean petFriendly, Boolean internetAvailable) {
|
---|
48 | Hotels hotel = findHotelByID(hotelId);
|
---|
49 | hotel.setHotelName(hotelName);
|
---|
50 | hotel.setHotelDescripiton(hotelDescripiton);
|
---|
51 | hotel.setHotelLocation(hotelLocation);
|
---|
52 | hotel.setHotelEDBS(hotelEDBS);
|
---|
53 | hotel.setParking(parking);
|
---|
54 | hotel.setPetFriendly(petFriendly);
|
---|
55 | hotel.setInternetAvailable(internetAvailable);
|
---|
56 | em.persist(hotel);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Transactional
|
---|
60 | @Override
|
---|
61 | public void deleteHotel(long hotelId) {
|
---|
62 | Hotels h = findHotelByID(hotelId);
|
---|
63 | em.remove(h);
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public Hotels findHotelByID(long hotelId) {
|
---|
68 | return em.find(Hotels.class, hotelId);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public List<HotelsImages> getHotelImages(Hotels hotel) {
|
---|
73 | return em.createQuery("select hi from HotelsImages hi where hi.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | @Transactional
|
---|
78 | public void addHotelImage(Hotels hotel, String url)
|
---|
79 | {
|
---|
80 | HotelsImages hotelsImages = new HotelsImages(hotel, url);
|
---|
81 | em.persist(hotelsImages);
|
---|
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(long hotelImageId){
|
---|
93 | HotelsImages hotelsImages = findHotelImageById(hotelImageId);
|
---|
94 | em.remove(hotelsImages);
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public List<HotelRoom> getRoomsOfHotel(long hotelId) {
|
---|
99 | Hotels h = findHotelByID(hotelId);
|
---|
100 | return em.createQuery("SELECT hr from HotelRoom hr where hr.hotel = :hotel").setParameter("hotel", h).getResultList();
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public List<HotelRoomAvailable> getRoomsAvailable(Long id) {
|
---|
105 | return em.createQuery("SELECT hra from HotelRoomAvailable hra WHERE hra.hotelRoom.id = :hotelRoomId").setParameter("hotelRoomId", id).getResultList();
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public HotelRoom findRoomById(long hotelRoomId) {
|
---|
110 | return em.find(HotelRoom.class, hotelRoomId);
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | public List<HotelRoomImages> getRoomImages(HotelRoom hotelRoom) {
|
---|
115 | return em.createQuery("select i.url from HotelRoomImages i where i.room = :hotelRoom").setParameter("hotelRoom", hotelRoom).getResultList();
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Transactional
|
---|
119 | @Override
|
---|
120 | public void createRoom(Hotels hotel, String hotelRoomDescription, String hotelRoomName, Boolean kitchenAvailable, Boolean airConditioning, Boolean balcony, double price, int numOfBeds) {
|
---|
121 | HotelRoom hotelRoom = new HotelRoom(hotel, hotelRoomDescription, hotelRoomName, kitchenAvailable, airConditioning, balcony, price, numOfBeds);
|
---|
122 | em.persist(hotelRoom);
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Transactional
|
---|
126 | @Override
|
---|
127 | public void editRoom(long hotelRoomId, Hotels hotel, String hotelRoomDescription, String hotelRoomName, Boolean kitchenAvailable, Boolean airConditioning, Boolean balcony, double price) {
|
---|
128 | HotelRoom hr = findRoomById(hotelRoomId);
|
---|
129 | hr.setHotel(hotel);
|
---|
130 | hr.setHotelRoomDescription(hotelRoomDescription);
|
---|
131 | hr.setHotelRoomName(hotelRoomName);
|
---|
132 | hr.setKitchenAvailable(kitchenAvailable);
|
---|
133 | hr.setAirConditioning(airConditioning);
|
---|
134 | hr.setBalcony(balcony);
|
---|
135 | hr.setPrice(price);
|
---|
136 | em.persist(hr);
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Transactional
|
---|
140 | @Override
|
---|
141 | public void deleteRoom(long hotelRoomId) {
|
---|
142 | HotelRoom hr = findRoomById(hotelRoomId);
|
---|
143 | em.remove(hr);
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Transactional
|
---|
147 | @Override
|
---|
148 | public void createRoomAvailible(HotelRoom hotelRoom, Date dateFrom, Date dateTo, int numberOfBeds) {
|
---|
149 | HotelRoomAvailable hra = new HotelRoomAvailable(hotelRoom, dateFrom, dateTo, numberOfBeds);
|
---|
150 | em.persist(hra);
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Transactional
|
---|
154 | @Override
|
---|
155 | public void editRoomAvailible(long hotelRoomAvailableId, HotelRoom hotelRoom, Date dateFrom, Date dateTo, int numberOfBeds) {
|
---|
156 | HotelRoomAvailable hr = findAvailibleRoomById(hotelRoomAvailableId);
|
---|
157 | hr.setHotelRoom(hotelRoom);
|
---|
158 | hr.setDateFrom(dateFrom);
|
---|
159 | hr.setDateTo(dateTo);
|
---|
160 | hr.setNumberOfBeds(numberOfBeds);
|
---|
161 | em.persist(hr);
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Transactional
|
---|
165 | @Override
|
---|
166 | public void deleteRoomAvailible(long hotelRoomAvailableId) {
|
---|
167 | HotelRoomAvailable hra = findAvailibleRoomById(hotelRoomAvailableId);
|
---|
168 | em.remove(hra);
|
---|
169 | }
|
---|
170 |
|
---|
171 | @Override
|
---|
172 | public HotelRoomAvailable findAvailibleRoomById(long hotelRoomAvailableId) {
|
---|
173 | return em.find(HotelRoomAvailable.class, hotelRoomAvailableId);
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | public List<HotelRoomAvailable> getRoomsAvailibility() {
|
---|
178 | return em.createQuery("select hra from HotelRoomAvailable hra").getResultList();
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Override
|
---|
182 | public List<HotelRoomAvailable> getRoomsAvailibilityByHotel(Hotels hotel) {
|
---|
183 | return em.createQuery("select hr from HotelRoomAvailable hr where hr.hotelRoom.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override
|
---|
187 | public List<HotelRoomAvailable> getRoomsAvailibilityByDateAndLocation(String hotelLocation, Date dateFrom, Date dateTo, int numberOfBeds) {
|
---|
188 | return em.createQuery("select hr from HotelRoomAvailable hr where hr.dateFrom <= :dateFrom and hr.dateTo >= :dateTo " +
|
---|
189 | "and hr.hotelRoom.hotel.hotelLocation LIKE :hotelLocation and hr.hotelRoom.numOfBeds >= :numBeds")
|
---|
190 | .setParameter("hotelLocation", hotelLocation)
|
---|
191 | .setParameter("dateFrom", dateFrom)
|
---|
192 | .setParameter("dateTo", dateTo)
|
---|
193 | .setParameter("numBeds", numberOfBeds)
|
---|
194 | .getResultList();
|
---|
195 | }
|
---|
196 |
|
---|
197 | @Override
|
---|
198 | @Transactional
|
---|
199 | public void createReservation(User user, HotelRoom hotelRoom, Date dateFrom, Date dateTo, Integer numberOfBeds) {
|
---|
200 | HotelRoomReservations r = new HotelRoomReservations(user, hotelRoom, dateFrom, dateTo, numberOfBeds);
|
---|
201 | em.persist(r);
|
---|
202 | }
|
---|
203 |
|
---|
204 | @Override
|
---|
205 | @Transactional
|
---|
206 | public void editReservation(long hotelRoomReservedId, User user, HotelRoom hotelRoom, Date dateFrom, Date dateTo, Integer numberOfBeds) {
|
---|
207 | HotelRoomReservations hr = findReservationById(hotelRoomReservedId);
|
---|
208 | hr.setUser(user);
|
---|
209 | hr.setHotelRoom(hotelRoom);
|
---|
210 | hr.setDateFrom(dateFrom);
|
---|
211 | hr.setDateTo(dateTo);
|
---|
212 | hr.setNumberOfBeds(numberOfBeds);
|
---|
213 | em.persist(hr);
|
---|
214 |
|
---|
215 | }
|
---|
216 |
|
---|
217 | @Transactional
|
---|
218 | @Override
|
---|
219 | public void deleteReservation(long hotelRoomReservedId) {
|
---|
220 | HotelRoomReservations r = findReservationById(hotelRoomReservedId);
|
---|
221 | em.remove(hotelRoomReservedId);
|
---|
222 | }
|
---|
223 |
|
---|
224 | @Override
|
---|
225 | public HotelRoomReservations findReservationById(long hotelRoomReservedId) {
|
---|
226 | return em.find(HotelRoomReservations.class, hotelRoomReservedId);
|
---|
227 | }
|
---|
228 |
|
---|
229 | @Override
|
---|
230 | public List<HotelRoomReservations> findReservationByUser(User user) {
|
---|
231 | return em.createQuery("select hr from HotelRoomReservations hr where hr.user = :user").setParameter("user", user).getResultList();
|
---|
232 | }
|
---|
233 |
|
---|
234 | @Override
|
---|
235 | public List<HotelRoomReservations> findReservationByHotel(Hotels hotel) {
|
---|
236 | List<HotelRoom> hotelRooms = getRoomsOfHotel(hotel.getHotelId());
|
---|
237 | return em.createQuery("select hr from HotelRoomReservations hr where hr.hotelRoom.hotel = :hotel").setParameter("hotel", hotel).getResultList();
|
---|
238 | }
|
---|
239 |
|
---|
240 | @Override
|
---|
241 | public List<HotelRoomReservations> getReservations() {
|
---|
242 | return em.createQuery("select hr from HotelRoomReservations hr order by hr.user.name").getResultList();
|
---|
243 | }
|
---|
244 | } |
---|