View Javadoc

1   package de.tivsource.page.user.actions.gallery;
2   
3   import java.util.List;
4   
5   import org.apache.logging.log4j.LogManager;
6   import org.apache.logging.log4j.Logger;
7   import org.apache.struts2.convention.annotation.Action;
8   import org.apache.struts2.convention.annotation.Actions;
9   import org.apache.struts2.convention.annotation.Result;
10  import org.apache.struts2.tiles.annotation.TilesDefinition;
11  import org.apache.struts2.tiles.annotation.TilesDefinitions;
12  import org.apache.struts2.tiles.annotation.TilesPutAttribute;
13  
14  import de.tivsource.ejb3plugin.InjectEJB;
15  import de.tivsource.page.dao.gallery.GalleryDaoLocal;
16  import de.tivsource.page.dao.page.PageDaoLocal;
17  import de.tivsource.page.entity.gallery.Gallery;
18  import de.tivsource.page.entity.page.Page;
19  import de.tivsource.page.user.actions.EmptyAction;
20  import de.tivsource.page.user.interfaces.Pagination;
21  
22  /**
23   * 
24   * @author Marc Michele
25   *
26   */
27  @TilesDefinitions({
28    @TilesDefinition(name="galleryList", extend = "userTemplate", putAttributes = {
29      @TilesPutAttribute(name = "content",    value = "/WEB-INF/tiles/active/view/gallery/gallery_list.jsp")
30    })
31  })
32  public class IndexAction extends EmptyAction implements Pagination {
33  
34      /**
35       * Serial Version UID.
36       */
37  	private static final long serialVersionUID = 3876303997272325669L;
38  
39  	/**
40       * Statischer Logger der Klasse.
41       */
42      private static final Logger LOGGER = LogManager.getLogger(IndexAction.class);
43  
44      @InjectEJB(name = "PageDao")
45      private PageDaoLocal pageDaoLocal;
46  
47      @InjectEJB(name="GalleryDao")
48      private GalleryDaoLocal galleryDaoLocal;
49  
50      private List<Gallery> gallery;
51  
52      private Page page;
53  
54      /**
55       * Attribut das die maximale Anzahl von Objekten enthält, die in der Liste
56       * enthalten seien sollen. Dieser Wert ist mit 7 vorbelegt und sollte aus
57       * der Datenbank geholt werden.
58       */
59      private Integer to;
60  
61      private Integer next;
62      private Integer previous;
63      private Integer current;
64  
65      /**
66       * Attribut das den Startpunkt der Liste enthält.
67       */
68      private Integer from;
69  
70      /**
71       * Angefordete Seitenzahl (Achtung kann durch den benutzer manipuliert werden). 
72       */
73      private Integer pagination;
74  
75      /**
76       * Attribut das die Anzahl der Objekte in der Datenbank enthält.
77       */
78      private Integer dbQuantity;
79  
80      /**
81       * Attribute das die maximal mögliche Anzahl an Seiten enthält.
82       */
83      private Integer maxPages;
84  
85      @Override
86      public void prepare() {
87          // Lade die Galerie Seite aus der Datenbank
88          page = pageDaoLocal.findByTechnical("gallery");
89      }
90  
91      @Override
92      @Actions({
93          @Action(value = "index", results = {
94              @Result(name = "success", type = "tiles", location = "galleryList"),
95              @Result(name = "input", type = "redirectAction", location = "index.html", params={"namespace", "/"}),
96              @Result(name = "error", type = "redirectAction", location = "index.html", params={"namespace", "/"})
97          })
98      })
99      public String execute() throws Exception {
100         LOGGER.info("execute() aufgerufen.");
101 
102         // Hole Action Locale
103         this.getLanguageFromActionContext();
104 
105         /*
106          * Ermittle ob die Galeriefunktion der Webseite angeschaltet wurde.
107          */
108         boolean galleryPageEnabled = getProperty("gallery.page.enabled").equals("true") ? true : false;
109         if(!galleryPageEnabled) {
110             return ERROR;
111         }
112 
113         // Hole attribute to aus Datenbank 
114         to = Integer.parseInt(getProperty("gallery.list.quantity"));
115 
116         // Hole die Anzahl aus der Datenbank
117         this.getDBCount();
118 
119         // Wenn page nicht gesetzt wurde
120         if(pagination == null) {
121         	pagination = 1;
122         }
123 
124         //  Wenn page größer als maxPages ist.
125         if(pagination > maxPages) {
126         	pagination = 1;
127         }
128 
129         // Kalkuliere die Seiten
130         this.calculate();
131 
132         // TODO: neue Methode die das Datum und das Attribute visible berücksichtigt 
133         gallery = galleryDaoLocal.findAllVisible(from, to);
134         return SUCCESS;
135         
136     }// Ende execute()
137 
138     @Override
139     public Page getPage() {
140         return page;
141     }
142 
143     @Override
144     public Integer getNext() {
145         return next;
146     }
147 
148     @Override
149     public Integer getPrevious() {
150         return previous;
151     }
152 
153     @Override
154     public Integer getCurrent() {
155         return current;
156     }
157 
158     @Override
159     public void setPage(Integer extpage) {
160         pagination = extpage;
161     }
162 
163     public List<Gallery> getGallery() {
164 		return gallery;
165 	}
166 
167     private void getDBCount() {
168         LOGGER.debug("getDBCount() aufgerufen.");
169         dbQuantity = galleryDaoLocal.countAllVisible();
170         LOGGER.debug("DbQuantity: " + dbQuantity);
171         // Berechne die Maximal mögliche Seitenzahl
172         maxPages = (dbQuantity % to == 0) ? (dbQuantity / to) : (dbQuantity / to) + 1;
173         LOGGER.debug("MaxPages: " + maxPages);
174     }// Ende getDBCount()
175 
176     /**
177      * Methode die Start und Enpunkt der Liste und die vorherige beziehungweise
178      * die nächste Seitenzahl berechnet.
179      */
180     private void calculate() {
181         if(pagination == 1) {
182             previous = null;
183             next = (2 <= maxPages) ? 2 : null;
184             from = 0;
185             current = pagination;
186         } else {
187             previous = pagination -1;
188             next = (pagination + 1 <= maxPages) ? pagination + 1 : null;
189             from = (pagination - 1) * to;
190             current = pagination;
191         }
192     }// Ende calculate()
193 
194     
195 }// Ende class