View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.feedback.web;
18  
19  import java.util.Date;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.struts.action.ActionForm;
26  import org.apache.struts.action.ActionForward;
27  import org.apache.struts.action.ActionMapping;
28  import org.kuali.rice.core.mail.EmailBody;
29  import org.kuali.rice.core.mail.EmailContent;
30  import org.kuali.rice.core.mail.EmailFrom;
31  import org.kuali.rice.core.mail.EmailSubject;
32  import org.kuali.rice.core.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         KEWServiceLocator.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