1 package de.tivsource.page.user.actions.manual;
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.manual.ManualDaoLocal;
16 import de.tivsource.page.dao.page.PageDaoLocal;
17 import de.tivsource.page.dao.property.PropertyDaoLocal;
18 import de.tivsource.page.entity.manual.Manual;
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="manualList", extend = "userTemplate", putAttributes = {
30 @TilesPutAttribute(name = "content", value = "/WEB-INF/tiles/active/view/manual/manual_list.jsp")
31 })
32 })
33 public class IndexAction extends EmptyAction implements Pagination {
34
35
36
37
38 private static final long serialVersionUID = 3719136247417859740L;
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="ManualDao")
57 private ManualDaoLocal manualDaoLocal;
58
59 private List<Manual> manuals;
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("manual");
91 }
92
93 @Override
94 @Actions({
95 @Action(value = "index", results = {
96 @Result(name = "success", type = "tiles", location = "manualList"),
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.manual").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
129 manuals = manualDaoLocal.findAllVisible(from, TO);
130 return SUCCESS;
131 } else {
132
133 return ERROR;
134 }
135
136
137 }
138
139 @Override
140 public Page getPage() {
141 return page;
142 }
143
144 @Override
145 public Integer getNext() {
146 return next;
147 }
148
149 @Override
150 public Integer getPrevious() {
151 return previous;
152 }
153
154 @Override
155 public Integer getCurrent() {
156 return current;
157 }
158
159 @Override
160 public void setPage(Integer extpage) {
161 pagination = extpage;
162 }
163
164 public List<Manual> getManuals() {
165 return manuals;
166 }
167
168 private void getDBCount() {
169 LOGGER.debug("getDBCount() aufgerufen.");
170 dbQuantity = manualDaoLocal.countAllVisible();
171 LOGGER.debug("DbQuantity: " + dbQuantity);
172
173 maxPages = (dbQuantity % TO == 0) ? (dbQuantity / TO) : (dbQuantity / TO) + 1;
174 LOGGER.debug("MaxPages: " + maxPages);
175 }
176
177
178
179
180
181 private void calculate() {
182 if(pagination == 1) {
183 previous = null;
184 next = (2 <= maxPages) ? 2 : null;
185 from = 0;
186 current = pagination;
187 } else {
188 previous = pagination -1;
189 next = (pagination + 1 <= maxPages) ? pagination + 1 : null;
190 from = (pagination - 1) * TO;
191 current = pagination;
192 }
193 }
194
195
196 }