1 package de.tivsource.page.admin.actions.system.captcha;
2
3 import java.text.SimpleDateFormat;
4 import java.util.Date;
5 import java.util.Locale;
6 import java.util.TimeZone;
7
8 import org.apache.logging.log4j.LogManager;
9 import org.apache.logging.log4j.Logger;
10 import org.apache.struts2.ServletActionContext;
11 import org.apache.struts2.convention.annotation.Action;
12 import org.apache.struts2.convention.annotation.Actions;
13 import org.apache.struts2.convention.annotation.Result;
14 import org.apache.struts2.tiles.annotation.TilesDefinition;
15 import org.apache.struts2.tiles.annotation.TilesDefinitions;
16 import org.apache.struts2.tiles.annotation.TilesPutAttribute;
17
18 import de.tivsource.ejb3plugin.InjectEJB;
19 import de.tivsource.page.admin.actions.EmptyAction;
20 import de.tivsource.page.common.captcha.Captcha;
21 import de.tivsource.page.dao.captcha.CaptchaDaoLocal;
22 import de.tivsource.page.dao.property.PropertyDaoLocal;
23 import de.tivsource.page.entity.property.Property;
24
25
26
27
28
29
30 @TilesDefinitions({
31 @TilesDefinition(name="captchaEditForm", extend = "adminTemplate", putAttributes = {
32 @TilesPutAttribute(name = "meta", value = "/WEB-INF/tiles/active/meta/chosen.jsp"),
33 @TilesPutAttribute(name = "navigation", value = "/WEB-INF/tiles/active/navigation/system.jsp"),
34 @TilesPutAttribute(name = "content", value = "/WEB-INF/tiles/active/view/captcha/edit_form.jsp")
35 }),
36 @TilesDefinition(name="captchaEditError", extend = "adminTemplate", putAttributes = {
37 @TilesPutAttribute(name = "navigation", value = "/WEB-INF/tiles/active/navigation/system.jsp"),
38 @TilesPutAttribute(name = "content", value = "/WEB-INF/tiles/active/view/captcha/edit_error.jsp")
39 })
40 })
41 public class EditAction extends EmptyAction {
42
43
44
45
46 private static final long serialVersionUID = 8810065390227066052L;
47
48
49
50
51 private static final Logger LOGGER = LogManager.getLogger(EditAction.class);
52
53 @InjectEJB(name="CaptchaDao")
54 private CaptchaDaoLocal captchaDaoLocal;
55
56 @InjectEJB(name="PropertyDao")
57 private PropertyDaoLocal propertyDaoLocal;
58
59 private Captcha captcha;
60
61 public Captcha getCaptcha() {
62 return captcha;
63 }
64
65 public void setCaptcha(Captcha captcha) {
66 this.captcha = captcha;
67 }
68
69 @Override
70 @Actions({
71 @Action(
72 value = "edit",
73 results = {
74 @Result(name = "success", type = "redirectAction", location = "index.html"),
75 @Result(name = "input", type = "tiles", location = "captchaEditForm"),
76 @Result(name = "error", type = "tiles", location = "captchaEditError")
77 }
78 )
79 })
80 public String execute() throws Exception {
81 LOGGER.info("execute() aufgerufen.");
82
83 String remoteUser = ServletActionContext.getRequest().getRemoteUser();
84 String remoteAddress = ServletActionContext.getRequest().getRemoteAddr();
85
86 if(captcha != null) {
87 LOGGER.info(captcha.getUuid());
88 Captcha dbCaptcha = captchaDaoLocal.findByUuid(captcha.getUuid());
89 if(captcha.getImage() != null && captcha.getImage().getUploadFile() != null) {
90 dbCaptcha.getImage().setUploadFile(captcha.getImage().getUploadFile());
91 dbCaptcha.getImage().generate();
92 }
93 dbCaptcha.setContent(captcha.getContent());
94 dbCaptcha.setModified(new Date());
95 dbCaptcha.setModifiedBy(remoteUser);
96 dbCaptcha.setModifiedAddress(remoteAddress);
97 captchaDaoLocal.merge(dbCaptcha);
98 setLastModified();
99 return SUCCESS;
100 } else {
101 return ERROR;
102 }
103
104 }
105
106 private void setLastModified() {
107
108 String remoteUser = ServletActionContext.getRequest().getRemoteUser();
109 String remoteAddress = ServletActionContext.getRequest().getRemoteAddr();
110
111
112 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
113 simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
114 String newDate = simpleDateFormat.format(new Date());
115
116
117 Property lastModified = propertyDaoLocal.findByKey("captcha.image.lastModified");
118
119 if(lastModified != null) {
120 lastModified.setModified(new Date());
121 lastModified.setModifiedAddress(remoteAddress);
122 lastModified.setModifiedBy(remoteUser);
123 lastModified.setValue(newDate);
124 } else {
125 lastModified = new Property();
126 lastModified.setCreated(new Date());
127 lastModified.setKey("captcha.image.lastModified");
128 lastModified.setModified(new Date());
129 lastModified.setModifiedAddress(remoteAddress);
130 lastModified.setModifiedBy(remoteUser);
131 lastModified.setValue(newDate);
132 }
133 propertyDaoLocal.merge(lastModified);
134 }
135
136 }