View Javadoc

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   import java.util.UUID;
8   
9   import org.apache.logging.log4j.LogManager;
10  import org.apache.logging.log4j.Logger;
11  import org.apache.struts2.ServletActionContext;
12  import org.apache.struts2.convention.annotation.Action;
13  import org.apache.struts2.convention.annotation.Actions;
14  import org.apache.struts2.convention.annotation.Result;
15  import org.apache.struts2.tiles.annotation.TilesDefinition;
16  import org.apache.struts2.tiles.annotation.TilesDefinitions;
17  import org.apache.struts2.tiles.annotation.TilesPutAttribute;
18  
19  import de.tivsource.ejb3plugin.InjectEJB;
20  import de.tivsource.page.admin.actions.EmptyAction;
21  import de.tivsource.page.common.captcha.Captcha;
22  import de.tivsource.page.dao.captcha.CaptchaDaoLocal;
23  import de.tivsource.page.dao.property.PropertyDaoLocal;
24  import de.tivsource.page.entity.property.Property;
25  
26  /**
27   * 
28   * @author Marc Michele
29   *
30   */
31  @TilesDefinitions({
32    @TilesDefinition(name="captchaAddForm",  extend = "adminTemplate", putAttributes = {
33      @TilesPutAttribute(name = "meta",       value = "/WEB-INF/tiles/active/meta/chosen.jsp"),
34      @TilesPutAttribute(name = "navigation", value = "/WEB-INF/tiles/active/navigation/system.jsp"),
35      @TilesPutAttribute(name = "content",    value = "/WEB-INF/tiles/active/view/captcha/add_form.jsp")
36    }),
37    @TilesDefinition(name="captchaAddError", extend = "adminTemplate", putAttributes = {
38      @TilesPutAttribute(name = "navigation", value = "/WEB-INF/tiles/active/navigation/system.jsp"),
39      @TilesPutAttribute(name = "content",    value = "/WEB-INF/tiles/active/view/captcha/add_error.jsp")
40    })
41  })
42  public class AddAction extends EmptyAction {
43  
44      /**
45       * Serial Version UID.
46       */
47      private static final long serialVersionUID = -1217385198172019511L;
48  
49      /**
50       * Statischer Logger der Klasse.
51       */
52      private static final Logger LOGGER = LogManager.getLogger(AddAction.class);
53  
54      @InjectEJB(name="CaptchaDao")
55      private CaptchaDaoLocal captchaDaoLocal;
56  
57      @InjectEJB(name="PropertyDao")
58      private PropertyDaoLocal propertyDaoLocal;
59  
60      private Captcha captcha;
61  
62      public Captcha getCaptcha() {
63          return captcha;
64      }
65  
66      public void setCaptcha(Captcha captcha) {
67          this.captcha = captcha;
68      }
69  
70      @Override
71      @Actions({
72          @Action(
73                  value = "add",
74                  results = {
75                          @Result(name = "success", type = "redirectAction", location = "index.html"),
76                          @Result(name = "input", type="tiles", location = "captchaAddForm"),
77                          @Result(name = "error", type="tiles", location = "captchaAddError")
78                  }
79          )
80      })
81      public String execute() throws Exception {
82          LOGGER.info("execute() aufgerufen.");
83  
84          String remoteUser    = ServletActionContext.getRequest().getRemoteUser();
85          String remoteAddress = ServletActionContext.getRequest().getRemoteAddr();
86  
87          if(captcha != null) {
88              captcha.getImage().setCaptcha(captcha);
89              captcha.getImage().setUuid(UUID.randomUUID().toString());
90              captcha.getImage().generate();
91              captcha.getImage().setCreated(new Date());
92              captcha.getImage().setModified(new Date());
93              captcha.getImage().setModifiedAddress(remoteAddress);
94              captcha.getImage().setModifiedBy(remoteUser);
95              
96              captcha.setUuid(UUID.randomUUID().toString());
97              captcha.setCreated(new Date());
98              captcha.setModified(new Date());
99              captcha.setModifiedBy(remoteUser);
100             captcha.setModifiedAddress(remoteAddress);
101             captchaDaoLocal.merge(captcha);
102             setLastModified();
103             return SUCCESS;
104         } else {
105             return ERROR;
106         }
107 
108     }// Ende execute()
109 
110     private void setLastModified() {
111         // Lese Daten des Remote Benutzers
112         String remoteUser    = ServletActionContext.getRequest().getRemoteUser();
113         String remoteAddress = ServletActionContext.getRequest().getRemoteAddr();
114 
115         // Erstelle neues Aktualisierungsdatum
116         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
117         simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
118         String newDate = simpleDateFormat.format(new Date());
119 
120         // Hole Schlüssel mit Aktualisierungsdatum aus der Datenbank oder erstelle neues Objekt
121         Property lastModified = propertyDaoLocal.findByKey("captcha.image.lastModified");
122 
123         if(lastModified != null) {
124             lastModified.setModified(new Date());
125             lastModified.setModifiedAddress(remoteAddress);
126             lastModified.setModifiedBy(remoteUser);
127             lastModified.setValue(newDate);
128         } else {
129             lastModified = new Property();
130             lastModified.setCreated(new Date());
131             lastModified.setKey("captcha.image.lastModified");
132             lastModified.setModified(new Date());
133             lastModified.setModifiedAddress(remoteAddress);
134             lastModified.setModifiedBy(remoteUser);
135             lastModified.setValue(newDate);
136         }
137         propertyDaoLocal.merge(lastModified);
138     }// Ende setLastModified()
139 
140 }// Ende class