1 package de.tivsource.page.user.actions.gallery;
2
3 import java.util.List;
4 import java.util.regex.Pattern;
5
6 import org.apache.logging.log4j.LogManager;
7 import org.apache.logging.log4j.Logger;
8 import org.apache.struts2.ServletActionContext;
9 import org.apache.struts2.convention.annotation.Action;
10 import org.apache.struts2.convention.annotation.Actions;
11 import org.apache.struts2.convention.annotation.Result;
12 import org.apache.struts2.tiles.annotation.TilesDefinition;
13 import org.apache.struts2.tiles.annotation.TilesDefinitions;
14 import org.apache.struts2.tiles.annotation.TilesPutAttribute;
15
16 import de.tivsource.ejb3plugin.InjectEJB;
17 import de.tivsource.page.dao.gallery.GalleryDaoLocal;
18 import de.tivsource.page.dao.picture.PictureDaoLocal;
19 import de.tivsource.page.entity.gallery.Gallery;
20 import de.tivsource.page.entity.page.Page;
21 import de.tivsource.page.entity.picture.Picture;
22 import de.tivsource.page.user.actions.EmptyAction;
23 import de.tivsource.page.user.interfaces.Pagination;
24
25
26
27
28
29
30 @TilesDefinitions({
31 @TilesDefinition(name="gallery", extend = "userTemplate", putAttributes = {
32 @TilesPutAttribute(name = "content", value = "/WEB-INF/tiles/active/view/gallery/gallery.jsp")
33 })
34 })
35 public class GalleryAction extends EmptyAction implements Pagination {
36
37
38
39
40 private static final long serialVersionUID = -4466409845775558651L;
41
42
43
44
45 private static final Logger LOGGER = LogManager.getLogger(GalleryAction.class);
46
47 @InjectEJB(name="GalleryDao")
48 private GalleryDaoLocal galleryDaoLocal;
49
50 @InjectEJB(name="PictureDao")
51 private PictureDaoLocal pictureDaoLocal;
52
53 private Page page;
54
55 private Gallery gallery;
56
57 private List<Picture> pictures;
58
59
60
61
62
63 private String pathUuid;
64
65
66
67
68
69 private Integer requestedPage;
70
71
72
73
74 private Integer pictureCount;
75
76
77
78
79 private Integer maxElements = 5;
80
81 private Integer next;
82 private Integer previous;
83 private Integer current;
84
85
86
87
88 private Integer from;
89
90
91
92
93 private Integer maxPages;
94
95 @Override
96 @Actions({
97 @Action(value = "*/index", results = {
98 @Result(name = "success", type = "tiles", location = "gallery"),
99 @Result(name = "input", type = "redirectAction", location = "index.html", params={"namespace", "/"}),
100 @Result(name = "error", type = "redirectAction", location = "index.html", params={"namespace", "/"})
101 })
102 })
103 public String execute() throws Exception {
104 LOGGER.info("execute() aufgerufen.");
105
106
107 this.getLanguageFromActionContext();
108
109
110
111
112 boolean galleryPageEnabled = getProperty("gallery.page.enabled").equals("true") ? true : false;
113 if(!galleryPageEnabled) {
114 return ERROR;
115 }
116
117
118
119
120
121 if(getProperty("gallery.overview.list.quantity") != null) {
122 maxElements = Integer.parseInt(getProperty("gallery.overview.list.quantity"));
123 }
124
125
126 pathUuid = ServletActionContext.getRequest().getServletPath();
127 LOGGER.info("UUID from Path: " + pathUuid);
128
129
130 pathUuid = pathUuid.replaceAll("/index.html", "");
131 pathUuid = pathUuid.replaceAll("/gallery/", "");
132
133 LOGGER.info("UUID from Path: " + pathUuid);
134
135
136
137
138
139 if (isValid(pathUuid) && galleryDaoLocal.isGallery(pathUuid)) {
140 LOGGER.info("gültige Galerie UUID.");
141
142
143 gallery = galleryDaoLocal.findByUuid(pathUuid);
144
145
146 pictureCount = pictureDaoLocal.countAllInGallery(gallery.getUuid());
147 LOGGER.info("Inhalt Attribute pictureCount:" + pictureCount);
148
149
150 calculate();
151
152 LOGGER.info("Inhalt Attribute from:" + from);
153
154
155 pictures = pictureDaoLocal.findAll(from, maxElements, gallery.getUuid(), "p.orderNumber, p.uuid", "desc");
156
157 page = new Page();
158 page.setTechnical(gallery.getTechnical());
159 page.setDescriptionMap(gallery.getDescriptionMap());
160 page.setPicture(gallery.getPicture());
161 page.setPictureOnPage(gallery.getPictureOnPage());
162 page.setCssGroup(gallery.getCssGroup());
163
164 return SUCCESS;
165 }
166
167
168
169
170
171 return ERROR;
172 }
173
174 public Page getPage() {
175 return page;
176 }
177
178 public Gallery getGallery() {
179 return gallery;
180 }
181
182 public List<Picture> getPictures() {
183 return pictures;
184 }
185
186 @Override
187 public Integer getNext() {
188 return next;
189 }
190
191 @Override
192 public Integer getPrevious() {
193 return previous;
194 }
195
196 @Override
197 public Integer getCurrent() {
198 return current;
199 }
200
201 @Override
202 public void setPage(Integer page) {
203 this.requestedPage = page;
204 }
205
206 private Boolean isValid(String input) {
207 if (Pattern.matches("[abcdef0-9-]*", input)) {
208 return true;
209 } else {
210 return false;
211 }
212 }
213
214
215
216
217
218 private void calculate() {
219 maxPages = (pictureCount % maxElements == 0) ? (pictureCount / maxElements)
220 : (pictureCount / maxElements) + 1;
221
222
223 if(requestedPage == null) {
224 requestedPage = 1;
225 }
226
227
228 if(requestedPage > maxPages) {
229 requestedPage = 1;
230 }
231
232 if(requestedPage == 1) {
233 previous = null;
234 next = (2 <= maxPages) ? 2 : null;
235 from = 0;
236 current = 1;
237 } else {
238 previous = requestedPage -1;
239 next = (requestedPage + 1 <= maxPages) ? requestedPage + 1 : null;
240 from = (requestedPage - 1) * maxElements;
241 current = requestedPage;
242 }
243 }
244
245 }