View Javadoc

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   * @author Marc Michele
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       * Serial Version UID.
43       */
44      private static final long serialVersionUID = 6236431708460575442L;
45  
46      /**
47       * Statischer Logger der Klasse.
48       */
49      private static final Logger LOGGER = LogManager.getLogger(LocationAction.class);
50  
51      /**
52       * Attribut das die maximal Anzahl der Liste enthält. 
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       * Location Uuid im Pfad (Achtung kann duch den Benutzer manipuliert werden).
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       * Attribut das den Startpunkt der Liste enthält.
85       */
86      private Integer from;
87  
88      /**
89       * Angefordete Seitenzahl (Achtung kann durch den benutzer manipuliert werden). 
90       */
91      private Integer pagination;
92  
93      /**
94       * Attribut das die Anzahl der Objekte in der Datenbank enthält.
95       */
96      private Integer dbQuantity;
97  
98      /**
99       * Attribute das die maximal mögliche Anzahl an Seiten enthält.
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         // Hole Eigenschaft aus der Datenbank
119         boolean moduleEnabled = propertyDaoLocal.findByKey("module.reservation").getValue().equals("true") ? true : false;
120 
121         // Prüfe ob das Module aktiviert ist
122         if(moduleEnabled) {
123             // Hole Action Locale
124             this.getLanguageFromActionContext();
125 
126             // Lese UUID aus dem ServletRequest
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             // Setze Daten in ein Page Objekt
134             setUpPage();
135 
136             /*
137              * Wenn die Location Uuid keine nicht erlaubten Zeichen enthält und es
138              * die Location mit der Uuid gibt dann wird der Block ausgeführt.
139              */
140             if (isValid(locationUuid) && locationDaoLocal.isEventLocation(locationUuid)) {
141                 LOGGER.info("gültige Location Uuid.");
142 
143                 // Hole die Anzahl aus der Datenbank
144                 this.getDBCount();
145 
146                 // Wenn page nicht gesetzt wurde
147                 if(pagination == null) {
148                     pagination = 1;
149                 }
150 
151                 //  Wenn page größer als maxPages ist.
152                 if(pagination > maxPages) {
153                     pagination = 1;
154                 }
155 
156                 // Kalkuliere die Seiten
157                 this.calculate();
158 
159                 events = eventDaoLocal.findAll(locationUuid, from, TO);
160                 return SUCCESS;
161             }
162 
163             /*
164              * Wenn es die Seite nicht gibt oder es einen Manipulationsversuch
165              * gab.
166              */
167             return ERROR;
168             
169         } else {
170             /*
171              * Wenn es die Seite nicht gibt oder es einen Manipulationsversuch
172              * gab.
173              */
174              return ERROR;
175         }
176     }// Ende execute()
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         // Berechne die Maximal mögliche Seitenzahl
230         maxPages = (dbQuantity % TO == 0) ? (dbQuantity / TO) : (dbQuantity / TO) + 1;
231         LOGGER.debug("MaxPages: " + maxPages);
232     }// Ende getDBCount()
233 
234     /**
235      * Methode die Start und Enpunkt der Liste und die vorherige beziehungweise
236      * die nächste Seitenzahl berechnet.
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     }// Ende calculate()
251 
252     
253 }// Ende class