1 package de.tivsource.page.user.actions.reservation;
2
3 import java.util.List;
4 import java.util.regex.Pattern;
5
6 import org.apache.logging.log4j.LogManager;
7 import org.apache.logging.log4j.Logger;
8 import org.apache.struts2.ServletActionContext;
9 import org.apache.struts2.convention.annotation.Action;
10 import org.apache.struts2.convention.annotation.Actions;
11 import org.apache.struts2.convention.annotation.Result;
12 import org.apache.struts2.tiles.annotation.TilesDefinition;
13 import org.apache.struts2.tiles.annotation.TilesDefinitions;
14 import org.apache.struts2.tiles.annotation.TilesPutAttribute;
15
16 import de.tivsource.ejb3plugin.InjectEJB;
17 import de.tivsource.page.dao.event.EventDaoLocal;
18 import de.tivsource.page.dao.location.LocationDaoLocal;
19 import de.tivsource.page.dao.page.PageDaoLocal;
20 import de.tivsource.page.dao.property.PropertyDaoLocal;
21 import de.tivsource.page.entity.event.Event;
22 import de.tivsource.page.entity.location.Location;
23 import de.tivsource.page.entity.page.Page;
24 import de.tivsource.page.user.actions.EmptyAction;
25 import de.tivsource.page.user.interfaces.Pagination;
26
27
28
29
30
31
32 @TilesDefinitions({
33 @TilesDefinition(name="reservationLocation", extend = "userTemplate", putAttributes = {
34 @TilesPutAttribute(name = "meta", value = "/WEB-INF/tiles/active/meta/reservation_location.jsp"),
35 @TilesPutAttribute(name = "twitter", value = "/WEB-INF/tiles/active/twitter/reservation_location.jsp"),
36 @TilesPutAttribute(name = "content", value = "/WEB-INF/tiles/active/view/reservation/reservation_location.jsp")
37 })
38 })
39 public class LocationAction extends EmptyAction implements Pagination {
40
41
42
43
44 private static final long serialVersionUID = 6236431708460575442L;
45
46
47
48
49 private static final Logger LOGGER = LogManager.getLogger(LocationAction.class);
50
51
52
53
54 private static final Integer TO = 7;
55
56 @InjectEJB(name = "PageDao")
57 private PageDaoLocal pageDaoLocal;
58
59 @InjectEJB(name="PropertyDao")
60 private PropertyDaoLocal propertyDaoLocal;
61
62 @InjectEJB(name="LocationDao")
63 private LocationDaoLocal locationDaoLocal;
64
65 @InjectEJB(name="EventDao")
66 private EventDaoLocal eventDaoLocal;
67
68
69
70
71 private String locationUuid;
72
73 private List<Event> events;
74
75 private Location location;
76
77 private Page page;
78
79 private Integer next;
80 private Integer previous;
81 private Integer current;
82
83
84
85
86 private Integer from;
87
88
89
90
91 private Integer pagination;
92
93
94
95
96 private Integer dbQuantity;
97
98
99
100
101 private Integer maxPages;
102
103 public Location getLocation() {
104 return location;
105 }
106
107 @Override
108 @Actions({
109 @Action(value = "*/index", results = {
110 @Result(name = "success", type = "tiles", location = "reservationLocation"),
111 @Result(name = "input", type = "redirectAction", location = "index.html", params={"namespace", "/"}),
112 @Result(name = "error", type = "redirectAction", location = "index.html", params={"namespace", "/"})
113 })
114 })
115 public String execute() throws Exception {
116 LOGGER.info("execute() aufgerufen.");
117
118
119 boolean moduleEnabled = propertyDaoLocal.findByKey("module.reservation").getValue().equals("true") ? true : false;
120
121
122 if(moduleEnabled) {
123
124 this.getLanguageFromActionContext();
125
126
127 locationUuid = ServletActionContext.getRequest().getServletPath();
128 LOGGER.info("LocationUuid: " + locationUuid);
129 locationUuid = locationUuid.replaceAll("/index.html", "");
130 locationUuid = locationUuid.replaceAll("/reservation/", "");
131 LOGGER.info("LocationUuid: " + locationUuid);
132
133
134 setUpPage();
135
136
137
138
139
140 if (isValid(locationUuid) && locationDaoLocal.isEventLocation(locationUuid)) {
141 LOGGER.info("gültige Location Uuid.");
142
143
144 this.getDBCount();
145
146
147 if(pagination == null) {
148 pagination = 1;
149 }
150
151
152 if(pagination > maxPages) {
153 pagination = 1;
154 }
155
156
157 this.calculate();
158
159 events = eventDaoLocal.findAll(locationUuid, from, TO);
160 return SUCCESS;
161 }
162
163
164
165
166
167 return ERROR;
168
169 } else {
170
171
172
173
174 return ERROR;
175 }
176 }
177
178 @Override
179 public Page getPage() {
180 return page;
181 }
182
183 public List<Event> getEvents() {
184 return events;
185 }
186
187 @Override
188 public Integer getNext() {
189 return next;
190 }
191
192 @Override
193 public Integer getPrevious() {
194 return previous;
195 }
196
197 @Override
198 public Integer getCurrent() {
199 return current;
200 }
201
202 @Override
203 public void setPage(Integer extpage) {
204 pagination = extpage;
205 }
206
207 private Boolean isValid(String input) {
208 if (Pattern.matches("[abcdef0-9-]*", input)) {
209 return true;
210 } else {
211 return false;
212 }
213 }
214
215 private void setUpPage() {
216 location = locationDaoLocal.findByUuid(locationUuid);
217 page = new Page();
218 page.setTechnical(location.getTechnical());
219 page.setDescriptionMap(location.getDescriptionMap());
220 page.setPicture(location.getPicture());
221 page.setPictureOnPage(location.getPictureOnPage());
222 page.setCssGroup(location.getCssGroup());
223 }
224
225 private void getDBCount() {
226 LOGGER.debug("getDBCount() aufgerufen.");
227 dbQuantity = this.eventDaoLocal.countAll(locationUuid);
228 LOGGER.debug("DbQuantity: " + dbQuantity);
229
230 maxPages = (dbQuantity % TO == 0) ? (dbQuantity / TO) : (dbQuantity / TO) + 1;
231 LOGGER.debug("MaxPages: " + maxPages);
232 }
233
234
235
236
237
238 private void calculate() {
239 if(pagination == 1) {
240 previous = null;
241 next = (2 <= maxPages) ? 2 : null;
242 from = 0;
243 current = pagination;
244 } else {
245 previous = pagination -1;
246 next = (pagination + 1 <= maxPages) ? pagination + 1 : null;
247 from = (pagination - 1) * TO;
248 current = pagination;
249 }
250 }
251
252
253 }