001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.ken.postprocessor.kew;
017
018import org.apache.log4j.Logger;
019import org.kuali.rice.core.framework.persistence.dao.GenericDao;
020import org.kuali.rice.ken.bo.NotificationBo;
021import org.kuali.rice.ken.core.GlobalNotificationServiceLocator;
022import org.kuali.rice.ken.document.kew.NotificationWorkflowDocument;
023import org.kuali.rice.ken.service.NotificationMessageContentService;
024import org.kuali.rice.ken.service.NotificationService;
025import org.kuali.rice.ken.util.Util;
026import org.kuali.rice.kew.api.KewApiConstants;
027import org.kuali.rice.kew.api.WorkflowDocument;
028import org.kuali.rice.kew.api.action.ActionType;
029import org.kuali.rice.kew.framework.postprocessor.ActionTakenEvent;
030import org.kuali.rice.kew.framework.postprocessor.AfterProcessEvent;
031import org.kuali.rice.kew.framework.postprocessor.BeforeProcessEvent;
032import org.kuali.rice.kew.framework.postprocessor.DeleteEvent;
033import org.kuali.rice.kew.framework.postprocessor.DocumentLockingEvent;
034import org.kuali.rice.kew.framework.postprocessor.DocumentRouteLevelChange;
035import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
036import org.kuali.rice.kew.framework.postprocessor.PostProcessor;
037import org.kuali.rice.kew.framework.postprocessor.ProcessDocReport;
038import org.kuali.rice.krad.data.DataObjectService;
039import org.kuali.rice.krad.service.KRADServiceLocator;
040
041import java.util.List;
042
043
044/**
045 * This class is the post processor that gets run when the general notification 
046 * message sending form is approved by its reviewers.
047 * @author Kuali Rice Team (rice.collab@kuali.org)
048 */
049public class NotificationSenderFormPostProcessor implements PostProcessor {
050    private static final Logger LOG = Logger.getLogger(NotificationSenderFormPostProcessor.class);
051    
052    NotificationService notificationService;
053    DataObjectService dataObjectService;
054    NotificationMessageContentService messageContentService;
055    
056    /**
057     * Constructs a NotificationSenderFormPostProcessor instance.
058     */
059    public NotificationSenderFormPostProcessor() {
060        this.notificationService = GlobalNotificationServiceLocator.getInstance().getNotificationService();
061        this.dataObjectService = KRADServiceLocator.getDataObjectService();
062        this.messageContentService = GlobalNotificationServiceLocator.getInstance().getNotificationMessageContentService();
063    }
064
065    /**
066     * Constructs a NotificationSenderFormPostProcessor instance.
067     * @param notificationService
068     * @param dataObjectService
069     */
070    public NotificationSenderFormPostProcessor(NotificationService notificationService, DataObjectService dataObjectService) {
071        this.notificationService = notificationService;
072        this.dataObjectService = dataObjectService;
073    }
074    
075    /**
076     * @see org.kuali.rice.kew.framework.postprocessor.PostProcessor#doActionTaken(org.kuali.rice.kew.framework.postprocessor.ActionTakenEvent)
077     */
078    @Override
079    public ProcessDocReport doActionTaken(ActionTakenEvent arg0) throws Exception {
080            return new ProcessDocReport(true, "");
081    }
082
083    /**
084     * @see org.kuali.rice.kew.framework.postprocessor.PostProcessor#afterActionTaken(org.kuali.rice.kew.api.action.ActionType, org.kuali.rice.kew.framework.postprocessor.ActionTakenEvent)
085     */
086    @Override
087    public ProcessDocReport afterActionTaken(ActionType performed, ActionTakenEvent event) throws Exception {
088        return new ProcessDocReport(true, "");
089    }
090
091    /**
092     * @see org.kuali.rice.kew.framework.postprocessor.PostProcessor#doDeleteRouteHeader(org.kuali.rice.kew.framework.postprocessor.DeleteEvent)
093     */
094    @Override
095    public ProcessDocReport doDeleteRouteHeader(DeleteEvent arg0) throws Exception {
096            return new ProcessDocReport(true, "");
097    }
098
099    /**
100     * @see org.kuali.rice.kew.framework.postprocessor.PostProcessor#doRouteLevelChange(org.kuali.rice.kew.framework.postprocessor.DocumentRouteLevelChange)
101     */
102    @Override
103    public ProcessDocReport doRouteLevelChange(DocumentRouteLevelChange arg0) throws Exception {
104            return new ProcessDocReport(true, "");
105    }
106
107    /**
108     * When the EDL simple message sending form is submitted, it is routed straight to FINAL and at that time (when RESOLVED), we 
109     * actually send the notification.
110     * @see org.kuali.rice.kew.framework.postprocessor.PostProcessor#doRouteStatusChange(org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange)
111     */
112    @Override
113    public ProcessDocReport doRouteStatusChange(DocumentRouteStatusChange arg0) throws Exception {
114        LOG.debug("ENTERING NotificationSenderFormPostProcessor.doRouteStatusChange() for Notification Sender Form with document ID: " + arg0.getDocumentId());
115
116        if(arg0.getNewRouteStatus().equals(KewApiConstants.ROUTE_HEADER_PROCESSED_CD)) {
117            LOG.debug("Workflow status has changed to RESOLVED for Notification Sender Form with document ID: " + arg0.getDocumentId() +
118                ".  We are now calling the NotificationService.sendNotification() service.");
119
120            // obtain a workflow user object first
121            //NetworkIdDTO proxyUser = new NetworkIdDTO(Util.getNotificationSystemUser());
122            String proxyUserId = Util.getNotificationSystemUser();
123
124            // now construct the workflow document, which will interact with workflow
125            WorkflowDocument document;
126            try {
127            document = NotificationWorkflowDocument.loadNotificationDocument(proxyUserId, arg0.getDocumentId());
128
129            LOG.debug("XML:" + document.getApplicationContent());
130
131            //parse out the application content into a Notification BO
132                    NotificationBo notification = messageContentService.parseSerializedNotificationXml(document.getApplicationContent().getBytes());
133
134                    LOG.debug("Notification Content: " + notification.getContent());
135
136                    // send the notification
137                    notificationService.sendNotification(notification);
138
139                    LOG.debug("NotificationService.sendNotification() was successfully called for Notification Sender Form with document ID: " + arg0.getDocumentId());
140            } catch(Exception e) {
141            throw new RuntimeException(e);
142            }
143        }
144
145        LOG.debug("LEAVING NotificationSenderFormPostProcessor.doRouteStatusChange() for Notification Sender Form with document ID: " + arg0.getDocumentId());
146        return new ProcessDocReport(true, "");
147    }
148
149    /**
150     * @see org.kuali.rice.kew.framework.postprocessor.PostProcessor#beforeProcess(org.kuali.rice.kew.framework.postprocessor.BeforeProcessEvent)
151     */
152    @Override
153    public ProcessDocReport beforeProcess(BeforeProcessEvent beforeProcessEvent) throws Exception {
154        return new ProcessDocReport(true, "");
155    }
156
157    /**
158     * @see org.kuali.rice.kew.framework.postprocessor.PostProcessor#afterProcess(org.kuali.rice.kew.framework.postprocessor.AfterProcessEvent)
159     */
160    @Override
161    public ProcessDocReport afterProcess(AfterProcessEvent afterProcessEvent) throws Exception {
162        return new ProcessDocReport(true, "");
163    }
164    
165    /**
166     * @see org.kuali.rice.kew.framework.postprocessor.PostProcessor#getDocumentIdsToLock(org.kuali.rice.kew.framework.postprocessor.DocumentLockingEvent)
167     */
168    @Override
169        public List<String> getDocumentIdsToLock(DocumentLockingEvent documentLockingEvent) throws Exception {
170                return null;
171        }
172        
173}