001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.feedback.web;
018:
019: import java.util.Date;
020:
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpServletResponse;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.apache.struts.action.ActionForm;
026: import org.apache.struts.action.ActionForward;
027: import org.apache.struts.action.ActionMapping;
028: import org.apache.struts.action.ActionMessages;
029: import org.kuali.rice.core.Core;
030:
031: import edu.iu.uis.eden.KEWServiceLocator;
032: import edu.iu.uis.eden.doctype.DocumentType;
033: import edu.iu.uis.eden.mail.EmailBody;
034: import edu.iu.uis.eden.mail.EmailContent;
035: import edu.iu.uis.eden.mail.EmailContentService;
036: import edu.iu.uis.eden.mail.EmailFrom;
037: import edu.iu.uis.eden.mail.EmailService;
038: import edu.iu.uis.eden.mail.EmailSubject;
039: import edu.iu.uis.eden.mail.EmailTo;
040: import edu.iu.uis.eden.user.WorkflowUser;
041: import edu.iu.uis.eden.web.WorkflowAction;
042: import edu.iu.uis.eden.web.session.UserSession;
043:
044: /**
045: * Struts action which handles the Feedback screen.
046: *
047: * @author temay
048: * @author rkirkend
049: * @author ewestfal
050: */
051: public class FeedbackAction extends WorkflowAction {
052:
053: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
054: .getLogger(FeedbackAction.class);
055:
056: public ActionForward start(ActionMapping mapping, ActionForm form,
057: HttpServletRequest request, HttpServletResponse response)
058: throws Exception {
059:
060: // load fixed properties in Form elements
061: FeedbackForm feedbackForm = (FeedbackForm) form;
062: feedbackForm.setTimeDate(new Date().toString());
063:
064: // load EDEN User properties in Form elements
065: feedbackForm.setComments("");
066:
067: // load application properties from Request in Form elements
068: String documentType = request.getParameter("docType");
069: if (documentType == null) {
070: documentType = "";
071: }
072: feedbackForm.setDocumentType(documentType);
073:
074: String pageUrl = request.getParameter("pageUrl");
075: if (pageUrl == null) {
076: pageUrl = "";
077: }
078: feedbackForm.setPageUrl(pageUrl);
079:
080: String routeHeaderId = request.getParameter("routeHeaderId");
081: if (routeHeaderId == null) {
082: routeHeaderId = "";
083: }
084: feedbackForm.setRouteHeaderId(routeHeaderId);
085:
086: String exception = request.getParameter("exception");
087: if (exception == null) {
088: feedbackForm.setException("");
089: feedbackForm.setEdenCategory("");
090: } else {
091: feedbackForm.setEdenCategory("problem");
092: feedbackForm.setException(exception);
093: }
094:
095: UserSession uSession = getUserSession(request);
096: WorkflowUser workflowUser = uSession.getWorkflowUser();
097: if (workflowUser != null) {
098: feedbackForm.setNetworkId(workflowUser
099: .getAuthenticationUserId().getAuthenticationId());
100: feedbackForm.setUserEmail(workflowUser.getEmailAddress());
101: String name = workflowUser.getDisplayName().trim();
102: feedbackForm.setUserName(name);
103: String firstName = name.substring(0, name.indexOf(" "));
104: String lastName = name.substring(name.lastIndexOf(" ") + 1,
105: name.length());
106: feedbackForm.setFirstName(firstName);
107: feedbackForm.setLastName(lastName);
108: } else {
109: feedbackForm.setNetworkId("");
110: feedbackForm.setUserEmail("");
111: feedbackForm.setUserName("");
112: feedbackForm.setFirstName("");
113: feedbackForm.setLastName("");
114: }
115:
116: return mapping.findForward("start");
117: }
118:
119: public ActionForward sendFeedback(ActionMapping mapping,
120: ActionForm form, HttpServletRequest request,
121: HttpServletResponse response) throws Exception {
122: FeedbackForm feedbackForm = (FeedbackForm) form;
123: EmailService emailService = KEWServiceLocator.getEmailService();
124: EmailContentService emailContentService = KEWServiceLocator
125: .getEmailContentService();
126: String fromAddress = determineFromAddress(emailContentService,
127: feedbackForm);
128: String toAddress = emailContentService
129: .getApplicationEmailAddress();
130: EmailContent content = emailContentService
131: .generateFeedback(feedbackForm);
132: emailService.sendEmail(new EmailFrom(fromAddress), new EmailTo(
133: toAddress), new EmailSubject(content.getSubject()),
134: new EmailBody(content.getBody()), content.isHtml());
135: return mapping.findForward("sent");
136: }
137:
138: private String determineFromAddress(
139: EmailContentService emailContentService, FeedbackForm form) {
140: DocumentType docType = null;
141: if (!StringUtils.isEmpty(form.getDocumentType())) {
142: docType = KEWServiceLocator.getDocumentTypeService()
143: .findByName(form.getDocumentType());
144: if (docType == null) {
145: LOG
146: .warn("Couldn't locate document type for the given name to determine feedback from address! "
147: + form.getDocumentType());
148: }
149: }
150: // if we pass null to this method it will return us the application email address
151: return emailContentService.getDocumentTypeEmailAddress(docType);
152: }
153:
154: private String constructSubject(FeedbackForm form) {
155: String subject = "Feedback from " + form.getNetworkId();
156: if (form.getRouteHeaderId() != null) {
157: subject += (" for document " + form.getRouteHeaderId());
158: }
159: return subject;
160: }
161:
162: private String constructEmailBody(FeedbackForm form) {
163: StringBuffer buffer = new StringBuffer();
164: buffer.append("\n");
165: buffer.append("Network ID: " + form.getNetworkId())
166: .append("\n");
167: buffer.append("Name: " + form.getUserName()).append("\n");
168: buffer.append("Email: " + form.getUserEmail()).append("\n");
169: buffer.append("Phone: " + form.getPhone()).append("\n");
170: buffer.append("Time: " + form.getTimeDate()).append("\n");
171: buffer.append(
172: "Environment: "
173: + Core.getCurrentContextConfig()
174: .getEnvironment()).append("\n\n");
175:
176: buffer.append("Document type: " + form.getDocumentType())
177: .append("\n");
178: buffer.append("Document id: " + form.getRouteHeaderId())
179: .append("\n\n");
180:
181: buffer.append("Category: " + form.getEdenCategory()).append(
182: "\n");
183: buffer.append("Comments: \n" + form.getComments()).append(
184: "\n\n");
185:
186: buffer.append("Exception: \n" + form.getException());
187: return buffer.toString();
188: }
189:
190: public ActionMessages establishRequiredState(
191: HttpServletRequest request, ActionForm form)
192: throws Exception {
193: return null;
194: }
195:
196: }
|