001    /**
002     * Copyright 2005-2014 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.feedback.web;
017    
018    import java.util.Date;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.apache.struts.action.ActionForm;
025    import org.apache.struts.action.ActionForward;
026    import org.apache.struts.action.ActionMapping;
027    import org.kuali.rice.core.api.CoreApiServiceLocator;
028    import org.kuali.rice.core.api.mail.EmailBody;
029    import org.kuali.rice.core.api.mail.EmailContent;
030    import org.kuali.rice.core.api.mail.EmailFrom;
031    import org.kuali.rice.core.api.mail.EmailSubject;
032    import org.kuali.rice.core.api.mail.EmailTo;
033    import org.kuali.rice.kew.doctype.bo.DocumentType;
034    import org.kuali.rice.kew.mail.service.EmailContentService;
035    import org.kuali.rice.kew.service.KEWServiceLocator;
036    import org.kuali.rice.kew.web.KewKualiAction;
037    import org.kuali.rice.kim.api.identity.Person;
038    import org.kuali.rice.krad.UserSession;
039    import org.kuali.rice.krad.util.GlobalVariables;
040    
041    
042    /**
043     * Struts action which handles the Feedback screen.
044     *
045     * @author Kuali Rice Team (rice.collab@kuali.org)
046     */
047    public class FeedbackAction extends KewKualiAction {
048    
049            private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FeedbackAction.class);
050    
051        @Override
052            public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
053    
054            // load fixed properties in Form elements
055            FeedbackForm feedbackForm = (FeedbackForm) form;
056            feedbackForm.setTimeDate(new Date().toString());
057    
058            // load User properties in Form elements
059            feedbackForm.setComments("");
060    
061            // load application properties from Request in Form elements
062            String documentType = request.getParameter("docType");
063            if (documentType == null) {
064                documentType = "";
065            }
066            feedbackForm.setDocumentType(documentType);
067    
068            String pageUrl = request.getParameter("pageUrl");
069            if (pageUrl == null) {
070                pageUrl = "";
071            }
072            feedbackForm.setPageUrl(pageUrl);
073    
074            String documentId = request.getParameter("documentId");
075            if (documentId == null) {
076                documentId = "";
077            }
078            feedbackForm.setDocumentId(documentId);
079    
080            String exception = request.getParameter("exception");
081            if (exception == null) {
082                feedbackForm.setException("");
083                feedbackForm.setCategory("");
084            } else {
085                feedbackForm.setCategory("problem");
086                feedbackForm.setException(exception);
087            }
088    
089            UserSession uSession = getUserSession();
090    
091            Person workflowUser = uSession.getPerson();
092            if (workflowUser != null) {
093                feedbackForm.setNetworkId(workflowUser.getPrincipalName());
094                feedbackForm.setUserEmail(workflowUser.getEmailAddress());
095                String name = workflowUser.getName().trim();
096                feedbackForm.setUserName(name);
097                String firstName = name.substring(0, name.indexOf(" "));
098                String lastName = name.substring(name.lastIndexOf(" ") + 1, name.length());
099                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