1
2
3
4 package de.tivsource.page.dao.location;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import javax.ejb.Stateless;
10 import javax.persistence.EntityManager;
11 import javax.persistence.PersistenceContext;
12 import javax.persistence.Query;
13
14 import org.apache.logging.log4j.LogManager;
15 import org.apache.logging.log4j.Logger;
16
17 import de.tivsource.page.entity.location.Location;
18 import de.tivsource.page.entity.location.OpeningHour;
19
20
21
22
23
24 @Stateless
25 public class LocationDao implements LocationDaoLocal {
26
27
28
29
30 private static final Logger LOGGER = LogManager.getLogger(LocationDao.class);
31
32
33
34
35 @PersistenceContext
36 private EntityManager entityManager;
37
38
39
40
41 @Override
42 public void save(Location location) {
43 LOGGER.info("save(Location location) aufgerufen");
44 entityManager.persist(location);
45 }
46
47
48
49
50 @Override
51 public void merge(Location location) {
52 LOGGER.info("merge(Location location) aufgerufen");
53 entityManager.merge(location);
54 }
55
56
57
58
59 @Override
60 public void delete(Location location) {
61 entityManager.remove(entityManager.find(Location.class, location.getUuid()));
62 }
63
64 @Override
65 public Boolean isLocation(String uuid) {
66 Query query = entityManager.createQuery("select l from Location l where l.uuid = :uuid and l.visible = 'Y' order by l.uuid asc");
67 query.setParameter("uuid", uuid);
68 return (query.getResultList().size() > 0 ? true : false);
69 }
70
71 @Override
72 public Boolean isEventLocation(String uuid) {
73 Query query = entityManager.createQuery("select l from Location l where l.uuid = :uuid and l.visible = 'Y' and l.event = 'Y' order by l.uuid asc");
74 query.setParameter("uuid", uuid);
75 return (query.getResultList().size() > 0 ? true : false);
76 }
77
78
79
80
81 @Override
82 public Location findByUuid(String uuid) {
83 return entityManager.find(Location.class, uuid);
84 }
85
86 @Override
87 public Location findByUuidWidthEvents(String uuid) {
88 Location location = entityManager.find(Location.class, uuid);
89 LOGGER.debug("Größe der Eventliste: " + location.getEvents().size());
90 return location;
91 }
92
93
94
95
96 @SuppressWarnings("unchecked")
97 @Override
98 public List<Location> findAll(Integer start, Integer max) {
99 Query query = entityManager.createQuery("SELECT l FROM Location l");
100 query.setFirstResult(start);
101 query.setMaxResults(max);
102 return query.getResultList();
103 }
104
105
106
107
108 @SuppressWarnings("unchecked")
109 @Override
110 public List<Location> findAll(Integer start, Integer max, String field, String order) {
111 String queryString = "SELECT DISTINCT l FROM Location l JOIN l.descriptionMap dm WHERE dm.language = 'DE' ORDER BY ";
112 queryString = queryString + field + " " + order;
113 Query query = entityManager.createQuery(queryString);
114 query.setFirstResult(start);
115 query.setMaxResults(max);
116 return query.getResultList();
117 }
118
119 @SuppressWarnings("unchecked")
120 @Override
121 public List<Location> findAllEventLocation() {
122 String queryString = "select l from Location l where l.event = 'Y' order by l.order";
123 Query query = entityManager.createQuery(queryString);
124 return query.getResultList();
125 }
126
127 @SuppressWarnings("unchecked")
128 @Override
129 public List<Location> findAllVisible(Integer start, Integer max) {
130 Query query = entityManager.createQuery("FROM Location l WHERE l.visible = 'Y' AND l.inLocationList = 'Y' ORDER BY l.order ASC");
131 query.setFirstResult(start);
132 query.setMaxResults(max);
133 return query.getResultList();
134 }
135
136
137
138
139 @Override
140 public Integer countAll() {
141 Query query = entityManager.createQuery("Select Count(l) from Location l");
142 return Integer.parseInt(query.getSingleResult().toString());
143 }
144
145 @Override
146 public Integer countAllVisible() {
147 Query query = entityManager.createQuery("Select Count(l) from Location l where l.visible = 'Y'");
148 return Integer.parseInt(query.getSingleResult().toString());
149 }
150
151 @Override
152 public void removeOpeningHour(Integer index, String location) {
153
154 Location dbLocation = entityManager.find(Location.class, location);
155
156 List<OpeningHour> openingHours = new ArrayList<OpeningHour>(dbLocation.getOpeningHours());
157 LOGGER.info("Anzahl der OpeningHours in der Liste: " + openingHours.size());
158
159 OpeningHour openingHour = openingHours.get(index);
160 dbLocation.getOpeningHours().remove(openingHour);
161
162 LOGGER.info("Anzahl der OpeningHours im Set: " + dbLocation.getOpeningHours().size());
163
164 entityManager.merge(dbLocation);
165 entityManager.remove(openingHour);
166
167 }
168
169
170
171 }