View Javadoc

1   package de.tivsource.page.user.actions.news;
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.news.NewsDaoLocal;
16  import de.tivsource.page.dao.page.PageDaoLocal;
17  import de.tivsource.page.dao.property.PropertyDaoLocal;
18  import de.tivsource.page.entity.news.News;
19  import de.tivsource.page.entity.page.Page;
20  import de.tivsource.page.user.actions.EmptyAction;
21  import de.tivsource.page.user.interfaces.Pagination;
22  
23  /**
24   * 
25   * @author Marc Michele
26   *
27   */
28  @TilesDefinitions({
29    @TilesDefinition(name="newsList", extend = "userTemplate", putAttributes = {
30      @TilesPutAttribute(name = "content",    value = "/WEB-INF/tiles/active/view/news/news_list.jsp")
31    })
32  })
33  public class IndexAction extends EmptyAction implements Pagination {
34  
35      /**
36       * Serial Version UID.
37       */
38  	private static final long serialVersionUID = 3876303997272325669L;
39  
40  	/**
41       * Statischer Logger der Klasse.
42       */
43      private static final Logger LOGGER = LogManager.getLogger(IndexAction.class);
44  
45      /**
46       * Attribut das die maximal Anzahl der Liste enthält. 
47       */
48      private static final Integer TO = 7;
49  
50      @InjectEJB(name = "PageDao")
51      private PageDaoLocal pageDaoLocal;
52  
53      @InjectEJB(name="PropertyDao")
54      private PropertyDaoLocal propertyDaoLocal;
55  
56      @InjectEJB(name="NewsDao")
57      private NewsDaoLocal newsDaoLocal;
58  
59      private List<News> news;
60  
61      private Page page;
62  
63      private Integer next;
64      private Integer previous;
65      private Integer current;
66  
67      /**
68       * Attribut das den Startpunkt der Liste enthält.
69       */
70      private Integer from;
71  
72      /**
73       * Angefordete Seitenzahl (Achtung kann durch den benutzer manipuliert werden). 
74       */
75      private Integer pagination;
76  
77      /**
78       * Attribut das die Anzahl der Objekte in der Datenbank enthält.
79       */
80      private Integer dbQuantity;
81  
82      /**
83       * Attribute das die maximal mögliche Anzahl an Seiten enthält.
84       */
85      private Integer maxPages;
86  
87      @Override
88      public void prepare() {
89          // Lade die Übersichtsseite aus der Datenbank
90          page = pageDaoLocal.findByTechnical("news");
91      }
92  
93      @Override
94      @Actions({
95          @Action(value = "index", results = {
96              @Result(name = "success", type = "tiles", location = "newsList"),
97              @Result(name = "input", type = "redirectAction", location = "index.html", params={"namespace", "/"}),
98              @Result(name = "error", type = "redirectAction", location = "index.html", params={"namespace", "/"})
99          })
100     })
101     public String execute() throws Exception {
102         LOGGER.info("execute() aufgerufen.");
103 
104         // Hole Eigenschaft aus der Datenbank
105         boolean moduleEnabled = propertyDaoLocal.findByKey("module.news").getValue().equals("true") ? true : false;
106 
107         // Prüfe ob das Module aktiviert ist
108         if(moduleEnabled) {
109             // Hole Action Locale
110             this.getLanguageFromActionContext();
111 
112             // Hole die Anzahl aus der Datenbank
113             this.getDBCount();
114 
115             // Wenn page nicht gesetzt wurde
116             if(pagination == null) {
117                 pagination = 1;
118             }
119 
120             //  Wenn page größer als maxPages ist.
121             if(pagination > maxPages) {
122                 pagination = 1;
123             }
124 
125             // Kalkuliere die Seiten
126             this.calculate();
127 
128             news = newsDaoLocal.findAllVisible(from, TO);
129             return SUCCESS;            
130         } else {
131             // Wenn das Module nicht aktiviert ist.
132             return ERROR;
133         }
134 
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<News> getNews() {
164 		return news;
165 	}
166 
167     private void getDBCount() {
168         LOGGER.debug("getDBCount() aufgerufen.");
169         dbQuantity = newsDaoLocal.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