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
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
37
38 private static final long serialVersionUID = 3876303997272325669L;
39
40
41
42
43 private static final Logger LOGGER = LogManager.getLogger(IndexAction.class);
44
45
46
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
69
70 private Integer from;
71
72
73
74
75 private Integer pagination;
76
77
78
79
80 private Integer dbQuantity;
81
82
83
84
85 private Integer maxPages;
86
87 @Override
88 public void prepare() {
89
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
105 boolean moduleEnabled = propertyDaoLocal.findByKey("module.news").getValue().equals("true") ? true : false;
106
107
108 if(moduleEnabled) {
109
110 this.getLanguageFromActionContext();
111
112
113 this.getDBCount();
114
115
116 if(pagination == null) {
117 pagination = 1;
118 }
119
120
121 if(pagination > maxPages) {
122 pagination = 1;
123 }
124
125
126 this.calculate();
127
128 news = newsDaoLocal.findAllVisible(from, TO);
129 return SUCCESS;
130 } else {
131
132 return ERROR;
133 }
134
135
136 }
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
172 maxPages = (dbQuantity % TO == 0) ? (dbQuantity / TO) : (dbQuantity / TO) + 1;
173 LOGGER.debug("MaxPages: " + maxPages);
174 }
175
176
177
178
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 }
193
194
195 }