View Javadoc

1   /**
2    * Copyright 2005-2011 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.mail.EmailBody;
28  import org.kuali.rice.core.mail.EmailContent;
29  import org.kuali.rice.core.mail.EmailFrom;
30  import org.kuali.rice.core.mail.EmailSubject;
31  import org.kuali.rice.core.mail.EmailTo;
32  import org.kuali.rice.kew.doctype.bo.DocumentType;
33  import org.kuali.rice.kew.mail.service.EmailContentService;
34  import org.kuali.rice.kew.service.KEWServiceLocator;
35  import org.kuali.rice.kew.web.KewKualiAction;
36  import org.kuali.rice.kim.api.identity.Person;
37  import org.kuali.rice.krad.UserSession;
38  import org.kuali.rice.krad.util.GlobalVariables;
39  
40  
41  /**
42   * Struts action which handles the Feedback screen.
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  public class FeedbackAction extends KewKualiAction {
47  
48  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FeedbackAction.class);
49  
50      @Override
51  	public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
52  
53          // load fixed properties in Form elements
54          FeedbackForm feedbackForm = (FeedbackForm) form;
55          feedbackForm.setTimeDate(new Date().toString());
56  
57          // load User properties in Form elements
58          feedbackForm.setComments("");
59  
60          // load application properties from Request in Form elements
61          String documentType = request.getParameter("docType");
62          if (documentType == null) {
63              documentType = "";
64          }
65          feedbackForm.setDocumentType(documentType);
66  
67          String pageUrl = request.getParameter("pageUrl");
68          if (pageUrl == null) {
69              pageUrl = "";
70          }
71          feedbackForm.setPageUrl(pageUrl);
72  
73          String documentId = request.getParameter("documentId");
74          if (documentId == null) {
75              documentId = "";
76          }
77          feedbackForm.setDocumentId(documentId);
78  
79          String exception = request.getParameter("exception");
80          if (exception == null) {
81              feedbackForm.setException("");
82              feedbackForm.setCategory("");
83          } else {
84              feedbackForm.setCategory("problem");
85              feedbackForm.setException(exception);
86          }
87  
88          UserSession uSession = getUserSession();
89  
90          Person workflowUser = uSession.getPerson();
91          if (workflowUser != null) {
92              feedbackForm.setNetworkId(workflowUser.getPrincipalName());
93              feedbackForm.setUserEmail(workflowUser.getEmailAddress());
94              String name = workflowUser.getName().trim();
95              feedbackForm.setUserName(name);
96              String firstName = name.substring(0, name.indexOf(" "));
97              String lastName = name.substring(name.lastIndexOf(" ") + 1, name.length());
98              feedbackForm.setFirstName(firstName);
99              feedbackForm.setLastName(lastName);
100         } else {
101             feedbackForm.setNetworkId("");
102             feedbackForm.setUserEmail("");
103             feedbackForm.setUserName("");
104             feedbackForm.setFirstName("");
105             feedbackForm.setLastName("");
106         }
107 
108         return mapping.findForward("start");
109     }
110 
111     public ActionForward sendFeedback(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
112     	FeedbackForm feedbackForm = (FeedbackForm)form;    	
113         EmailContentService emailContentService = KEWServiceLocator.getEmailContentService();
114         String fromAddress = determineFromAddress(emailContentService, feedbackForm);
115     	String toAddress = emailContentService.getApplicationEmailAddress();
116         EmailContent content = emailContentService.generateFeedback(feedbackForm);
117         KEWServiceLocator.getMailer().sendEmail(new EmailFrom(fromAddress), new EmailTo(toAddress), new EmailSubject(content.getSubject()), new EmailBody(content.getBody()), content.isHtml());
118     	return mapping.findForward("sent");
119     }
120 
121     private String determineFromAddress(EmailContentService emailContentService, FeedbackForm form) {
122     	DocumentType docType = null;
123     	if (!StringUtils.isEmpty(form.getDocumentType())) {
124     		docType = KEWServiceLocator.getDocumentTypeService().findByName(form.getDocumentType());
125     		if (docType == null) {
126     			LOG.warn("Couldn't locate document type for the given name to determine feedback from address! " + form.getDocumentType());
127     		}
128     	}
129     	// if we pass null to this method it will return us the application email address
130     	return emailContentService.getDocumentTypeEmailAddress(docType);
131     }
132 
133     private static UserSession getUserSession() {
134         return GlobalVariables.getUserSession();
135     }
136 }
137