View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.feedback.web;
17  
18  import java.util.Date;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.struts.action.ActionForm;
25  import org.apache.struts.action.ActionForward;
26  import org.apache.struts.action.ActionMapping;
27  import org.kuali.rice.core.api.CoreApiServiceLocator;
28  import org.kuali.rice.core.api.mail.EmailBody;
29  import org.kuali.rice.core.api.mail.EmailContent;
30  import org.kuali.rice.core.api.mail.EmailFrom;
31  import org.kuali.rice.core.api.mail.EmailSubject;
32  import org.kuali.rice.core.api.mail.EmailTo;
33  import org.kuali.rice.kew.doctype.bo.DocumentType;
34  import org.kuali.rice.kew.mail.service.EmailContentService;
35  import org.kuali.rice.kew.service.KEWServiceLocator;
36  import org.kuali.rice.kew.web.KewKualiAction;
37  import org.kuali.rice.kim.api.identity.Person;
38  import org.kuali.rice.krad.UserSession;
39  import org.kuali.rice.krad.util.GlobalVariables;
40  
41  
42  /**
43   * Struts action which handles the Feedback screen.
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  public class FeedbackAction extends KewKualiAction {
48  
49  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FeedbackAction.class);
50  
51      @Override
52  	public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
53  
54          // load fixed properties in Form elements
55          FeedbackForm feedbackForm = (FeedbackForm) form;
56          feedbackForm.setTimeDate(new Date().toString());
57  
58          // load User properties in Form elements
59          feedbackForm.setComments("");
60  
61          // load application properties from Request in Form elements
62          String documentType = request.getParameter("docType");
63          if (documentType == null) {
64              documentType = "";
65          }
66          feedbackForm.setDocumentType(documentType);
67  
68          String pageUrl = request.getParameter("pageUrl");
69          if (pageUrl == null) {
70              pageUrl = "";
71          }
72          feedbackForm.setPageUrl(pageUrl);
73  
74          String documentId = request.getParameter("documentId");
75          if (documentId == null) {
76              documentId = "";
77          }
78          feedbackForm.setDocumentId(documentId);
79  
80          String exception = request.getParameter("exception");
81          if (exception == null) {
82              feedbackForm.setException("");
83              feedbackForm.setCategory("");
84          } else {
85              feedbackForm.setCategory("problem");
86              feedbackForm.setException(exception);
87          }
88  
89          UserSession uSession = getUserSession();
90  
91          Person workflowUser = uSession.getPerson();
92          if (workflowUser != null) {
93              feedbackForm.setNetworkId(workflowUser.getPrincipalName());
94              feedbackForm.setUserEmail(workflowUser.getEmailAddress());
95              String name = workflowUser.getName().trim();
96              feedbackForm.setUserName(name);
97              String firstName = name.substring(0, name.indexOf(" "));
98              String lastName = name.substring(name.lastIndexOf(" ") + 1, name.length());
99              feedbackForm.setFirstName(firstName);
100             feedbackForm.setLastName(lastName);
101         } else {
102             feedbackForm.setNetworkId("");
103             feedbackForm.setUserEmail("");
104             feedbackForm.setUserName("");
105             feedbackForm.setFirstName("");
106             feedbackForm.setLastName("");
107         }
108 
109         return mapping.findForward("start");
110     }
111 
112     public ActionForward sendFeedback(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
113     	FeedbackForm feedbackForm = (FeedbackForm)form;    	
114         EmailContentService emailContentService = KEWServiceLocator.getEmailContentService();
115         String fromAddress = determineFromAddress(emailContentService, feedbackForm);
116     	String toAddress = emailContentService.getApplicationEmailAddress();
117         EmailContent content = emailContentService.generateFeedback(feedbackForm);
118         CoreApiServiceLocator.getMailer().sendEmail(new EmailFrom(fromAddress), new EmailTo(toAddress), new EmailSubject(content.getSubject()), new EmailBody(content.getBody()), content.isHtml());
119     	return mapping.findForward("sent");
120     }
121 
122     private String determineFromAddress(EmailContentService emailContentService, FeedbackForm form) {
123     	DocumentType docType = null;
124     	if (!StringUtils.isEmpty(form.getDocumentType())) {
125     		docType = KEWServiceLocator.getDocumentTypeService().findByName(form.getDocumentType());
126     		if (docType == null) {
127     			LOG.warn("Couldn't locate document type for the given name to determine feedback from address! " + form.getDocumentType());
128     		}
129     	}
130     	// if we pass null to this method it will return us the application email address
131     	return emailContentService.getDocumentTypeEmailAddress(docType);
132     }
133 
134     private static UserSession getUserSession() {
135         return GlobalVariables.getUserSession();
136     }
137 }
138