View Javadoc

1   package de.tivsource.page.user.actions.feedback;
2   
3   import java.util.Date;
4   import java.util.Iterator;
5   import java.util.List;
6   import java.util.UUID;
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.dao.feedback.FeedbackDaoLocal;
20  import de.tivsource.page.dao.feedback.FeedbackOptionDaoLocal;
21  import de.tivsource.page.dao.page.PageDaoLocal;
22  import de.tivsource.page.dao.property.PropertyDaoLocal;
23  import de.tivsource.page.entity.feedback.Feedback;
24  import de.tivsource.page.entity.feedback.FeedbackOption;
25  import de.tivsource.page.entity.page.Page;
26  import de.tivsource.page.user.actions.EmptyAction;
27  
28  /**
29   * 
30   * @author Marc Michele
31   *
32   */
33  @TilesDefinitions({
34    @TilesDefinition(name="feedbackForm", extend = "userTemplate", putAttributes = {
35      @TilesPutAttribute(name = "meta",       value = "/WEB-INF/tiles/active/meta/feedback_form.jsp"),
36      @TilesPutAttribute(name = "twitter",    value = "/WEB-INF/tiles/active/twitter/feedback_form.jsp"),
37      @TilesPutAttribute(name = "content",    value = "/WEB-INF/tiles/active/view/feedback/feedback_form.jsp")
38    }),
39    @TilesDefinition(name="page", extend = "userTemplate", putAttributes = {
40      @TilesPutAttribute(name = "meta",       value = "/WEB-INF/tiles/active/meta/content.jsp"),
41      @TilesPutAttribute(name = "twitter",    value = "/WEB-INF/tiles/active/twitter/content.jsp"),
42      @TilesPutAttribute(name = "content",    value = "/WEB-INF/tiles/active/view/page/page.jsp")
43    })
44  })
45  public class SentAction extends EmptyAction {
46  
47      /**
48       * Serial Version UID.
49       */
50      private static final long serialVersionUID = -899397629172153047L;
51  
52      /**
53  	 * Statischer Logger der Klasse.
54  	 */
55      private static final Logger LOGGER = LogManager.getLogger(SentAction.class);
56  
57      @InjectEJB(name="PageDao")
58      private PageDaoLocal pageDaoLocal;
59  
60      @InjectEJB(name="PropertyDao")
61      private PropertyDaoLocal propertyDaoLocal;
62  
63      @InjectEJB(name="FeedbackDao")
64      private FeedbackDaoLocal feedbackDaoLocal;
65  
66      @InjectEJB(name = "FeedbackOptionDao")
67      private FeedbackOptionDaoLocal feedbackOptionDaoLocal;
68  
69  	private Feedback feedback;
70  
71  	private Page page;
72  
73  	private List<FeedbackOption> options;
74  	
75  	public Feedback getFeedback() {
76          return feedback;
77      }
78  
79      public void setFeedback(Feedback feedback) {
80          this.feedback = feedback;
81      }
82  
83      public Boolean getCashpoint() {
84          if(feedback.getCashpoint() != null && feedback.getCashpoint().length() > 0) {
85              return true;
86          }
87          return false;
88      }
89  
90      public Boolean getVoucher() {
91          if(feedback.getVoucher() != null && feedback.getVoucher().length() > 0) {
92              return true;
93          }
94          return false;
95      }
96  
97      @Override
98      @Actions({
99          @Action(
100         		value = "sent", 
101         		results = { 
102         				@Result(name = "success", type="tiles", location = "page"), 
103         				@Result(name = "input", type="tiles", location = "feedbackForm")
104         				}
105         )
106     })
107 	public String execute() {
108         LOGGER.info("execute() aufgerufen.");
109 
110         // Hole Eigenschaft aus der Datenbank
111         boolean moduleEnabled = propertyDaoLocal.findByKey("module.feedback").getValue().equals("true") ? true : false;
112 
113         // Prüfe ob das Module aktiviert ist
114         if(moduleEnabled) {
115             // Hole Action Locale
116             this.getLanguageFromActionContext();
117 
118             // Speichere Message Objekt
119             String remoteAddress = ServletActionContext.getRequest().getRemoteAddr();
120             feedback.setUuid(UUID.randomUUID().toString());
121             feedback.setCreated(new Date());
122             feedback.setCreatedAddress(remoteAddress);
123             feedbackDaoLocal.merge(feedback);
124 
125             LOGGER.info("Anzahl der Antworten: " + feedback.getAnswers().size());
126             
127             return SUCCESS;            
128         } else {
129             return ERROR;
130         }
131 	}// Ende execute()
132 
133     @Override
134     public void validate(){
135         if(options == null) {
136             options = feedbackOptionDaoLocal.findAllVisible(0, feedbackOptionDaoLocal.countAllVisible());
137         }
138 
139         // feedback.getCashpoint();
140         // feedback.getVoucher();
141         // feedback.getComment();
142         
143         // Überprüfe Antworten
144         Iterator<FeedbackOption> iteratorFeedbackOption = options.iterator();
145         while(iteratorFeedbackOption.hasNext()) {
146             FeedbackOption feedbackOption = iteratorFeedbackOption.next();
147             LOGGER.info("Check for MapKey " + feedbackOption.getMapKey());
148             Integer value = feedback.getAnswers().get(feedbackOption.getMapKey());
149             // Wenn die Anwort nicht vorhanden ist
150             if(value == null) {
151                 LOGGER.info("Anwort nicht Vorhanden");
152                 addFieldError("feedback.answers['" + feedbackOption.getMapKey() + "']",
153                         getText("feedback.validation."+ feedbackOption.getMapKey() +".required"));
154             }
155             // Wenn die Anwort vorhanden ist und zwischen 0 und Maximum liegt            
156             if((value != null) && !(value > 0) && !(value < (feedbackOption.getMaxStars() + 1))) {
157                 LOGGER.info("Anwort nicht gültig");
158                 addFieldError("feedback.answers['" + feedbackOption.getMapKey() + "']",
159                         getText("feedback.validation."+ feedbackOption.getMapKey() +".notvalid"));
160             }
161         }
162         
163     }// validate()
164 
165     @Override
166     public Page getPage() {
167         if(page == null) {
168             setUpPage();
169         }
170         return page;
171     }// Ende getPage()
172 
173     public List<FeedbackOption> getOptions() {
174         if(options == null) {
175             options = feedbackOptionDaoLocal.findAllVisible(0, feedbackOptionDaoLocal.countAllVisible());
176         }
177         return options;
178     }
179 
180     private void setUpPage() {
181         LOGGER.info("Action Errors: " + this.getFieldErrors().size());
182         if(this.getFieldErrors().size() > 0) {
183             page = pageDaoLocal.findByTechnical("feedback");
184         } else {
185             page = pageDaoLocal.findByTechnical("feedbacksent");
186         }
187     }
188 
189 }// Ende class