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
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
36
37 private static final long serialVersionUID = 3876303997272325669L;
38
39
40
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
56
57
58
59 private Integer to;
60
61 private Integer next;
62 private Integer previous;
63 private Integer current;
64
65
66
67
68 private Integer from;
69
70
71
72
73 private Integer pagination;
74
75
76
77
78 private Integer dbQuantity;
79
80
81
82
83 private Integer maxPages;
84
85 @Override
86 public void prepare() {
87
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
103 this.getLanguageFromActionContext();
104
105
106
107
108 boolean galleryPageEnabled = getProperty("gallery.page.enabled").equals("true") ? true : false;
109 if(!galleryPageEnabled) {
110 return ERROR;
111 }
112
113
114 to = Integer.parseInt(getProperty("gallery.list.quantity"));
115
116
117 this.getDBCount();
118
119
120 if(pagination == null) {
121 pagination = 1;
122 }
123
124
125 if(pagination > maxPages) {
126 pagination = 1;
127 }
128
129
130 this.calculate();
131
132
133 gallery = galleryDaoLocal.findAllVisible(from, to);
134 return SUCCESS;
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<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
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 }