Coverage Report - org.kuali.rice.ken.deliverer.impl.KEWActionListMessageDeliverer
 
Classes in this File Line Coverage Branch Coverage Complexity
KEWActionListMessageDeliverer
0%
0/61
0%
0/12
4.8
 
 1  
 /*
 2  
  * Copyright 2007 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.ken.deliverer.impl;
 17  
 
 18  
 import java.io.ByteArrayOutputStream;
 19  
 import java.io.IOException;
 20  
 import java.util.HashMap;
 21  
 import java.util.LinkedHashMap;
 22  
 import java.util.Properties;
 23  
 
 24  
 import org.kuali.rice.ken.bo.NotificationMessageDelivery;
 25  
 import org.kuali.rice.ken.core.GlobalNotificationServiceLocator;
 26  
 import org.kuali.rice.ken.deliverer.NotificationMessageDeliverer;
 27  
 import org.kuali.rice.ken.document.kew.NotificationWorkflowDocument;
 28  
 import org.kuali.rice.ken.exception.ErrorList;
 29  
 import org.kuali.rice.ken.exception.NotificationAutoRemoveException;
 30  
 import org.kuali.rice.ken.exception.NotificationMessageDeliveryException;
 31  
 import org.kuali.rice.ken.service.NotificationWorkflowDocumentService;
 32  
 import org.kuali.rice.ken.util.NotificationConstants;
 33  
 import org.kuali.rice.ken.util.Util;
 34  
 import org.kuali.rice.kew.exception.WorkflowException;
 35  
 import org.kuali.rice.kew.service.WorkflowDocument;
 36  
 
 37  
 
 38  
 /**
 39  
  * This class is responsible for describing the default delivery mechanism for
 40  
  * the system - the KEW Action List.
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  
 public class KEWActionListMessageDeliverer implements NotificationMessageDeliverer {
 44  0
     private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KEWActionListMessageDeliverer.class);
 45  
     
 46  
     /**
 47  
      * Property set in the attribute content that indicates the action received by workflow was initiated by the Notification System itself
 48  
      * (and not an end user)
 49  
      */
 50  
     public static final String INTERNAL_COMMAND_FLAG = "internal_command";
 51  
 
 52  
     private NotificationWorkflowDocumentService notificationWorkflowDocumentService;
 53  
 
 54  
     /**
 55  
      * Constructs a KEWActionListMessageDeliverer.java.
 56  
      */
 57  0
     public KEWActionListMessageDeliverer() {
 58  0
         this.notificationWorkflowDocumentService = GlobalNotificationServiceLocator.getInstance().getNotificationWorkflowDocumentService();
 59  0
     }
 60  
 
 61  
     /**
 62  
      * This implementation leverages the workflow integration services to push this notification into the KEW action list.
 63  
      * @see org.kuali.rice.ken.deliverer.NotificationMessageDeliverer#deliverMessage(org.kuali.rice.ken.bo.NotificationMessageDelivery)
 64  
      */
 65  
     public void deliverMessage(NotificationMessageDelivery messageDelivery) throws NotificationMessageDeliveryException {
 66  
         try {
 67  
             // make the call to actually generate and ad-hoc route a workflow document
 68  0
             String documentId = notificationWorkflowDocumentService.createAndAdHocRouteNotificationWorkflowDocument(
 69  
                     messageDelivery,
 70  
                     Util.getNotificationSystemUser(),
 71  
                     messageDelivery.getUserRecipientId(),
 72  
                     NotificationConstants.KEW_CONSTANTS.GENERIC_DELIVERY_ANNOTATION);
 73  
 
 74  
             // now set the workflow doc id into the message delivery's delivery system id
 75  0
             messageDelivery.setDeliverySystemId(documentId);
 76  0
             LOG.debug("Message Delivery: " + messageDelivery.toString());
 77  0
         } catch (WorkflowException we) {
 78  0
             LOG.error(we.getStackTrace());
 79  0
             throw new NotificationMessageDeliveryException("Workflow exception delivering message", we);
 80  0
         }
 81  0
     }
 82  
 
 83  
     /**
 84  
      * This implementation does an auto-remove by "canceling" the workflow document associated with the message delivery record.  This 
 85  
      * prevents the user from seeing the item in their list anymore.
 86  
      * @see org.kuali.rice.ken.deliverer.NotificationMessageDeliverer#autoRemoveMessageDelivery(org.kuali.rice.ken.bo.NotificationMessageDelivery)
 87  
      */
 88  
     public void autoRemoveMessageDelivery(NotificationMessageDelivery messageDelivery) throws NotificationAutoRemoveException {
 89  
         // first retrieve the appropriate notification workflow document to "auto-remove" and proxy as the recipient
 90  0
         NotificationWorkflowDocument workflowDoc = null;
 91  0
         String sysId = messageDelivery.getDeliverySystemId();
 92  0
         if (sysId == null) {
 93  0
             LOG.error("NotificationMessageDelivery " + messageDelivery.getId() + " is missing delivery system id (workflow document id");
 94  
             // there is no possibility for recovery, so since there is no id, we'll just log an error and return successfully instead
 95  
             // of throwing an exception
 96  0
             return;
 97  
         }
 98  
         
 99  
         try {
 100  0
             workflowDoc = notificationWorkflowDocumentService.getNotificationWorkflowDocumentByDocumentId(messageDelivery.getUserRecipientId(), sysId);
 101  0
         } catch(WorkflowException we) {
 102  0
             throw new NotificationAutoRemoveException(we);
 103  0
         }
 104  
 
 105  0
         flagWorkflowDocument(workflowDoc);
 106  
 
 107  
         try {
 108  0
             notificationWorkflowDocumentService.clearAllFyisAndAcknowledgeNotificationWorkflowDocument(messageDelivery.getUserRecipientId(), workflowDoc, 
 109  
                     NotificationConstants.KEW_CONSTANTS.GENERIC_AUTO_REMOVE_ANNOTATION);
 110  0
         } catch(WorkflowException we) {
 111  0
             throw new NotificationAutoRemoveException(we);
 112  0
         }
 113  0
     }
 114  
 
 115  
     /**
 116  
      * @see org.kuali.rice.ken.deliverer.NotificationMessageDeliverer#dismissMessageDelivery(org.kuali.rice.ken.bo.NotificationMessageDelivery, java.lang.String, java.lang.String)
 117  
      */
 118  
     public void dismissMessageDelivery(NotificationMessageDelivery messageDelivery, String user, String cause) {
 119  
         // TODO: move hardcoded web controller actions here...
 120  0
         LOG.info("Dismissing as user '" + user + "' workflow document '" + messageDelivery.getDeliverySystemId() + "' corresponding to message delivery #" + messageDelivery.getId() + " due to cause: " + cause);
 121  0
         if (NotificationConstants.AUTO_REMOVE_CAUSE.equals(cause)) {
 122  
             // perform an auto-remove
 123  
             // XXX: currently auto-removes are going through autoremove method
 124  
         } else {
 125  
             NotificationWorkflowDocument nwd;
 126  
             try {
 127  0
                 nwd = notificationWorkflowDocumentService.getNotificationWorkflowDocumentByDocumentId(user, messageDelivery.getDeliverySystemId());
 128  0
             } catch (WorkflowException we) {
 129  0
                 LOG.error("Could not get workflow document with docId");
 130  0
                 throw new RuntimeException("Could not get workflow document with docId", we);
 131  0
             }
 132  
             
 133  0
             flagWorkflowDocument(nwd);
 134  
 
 135  
             try {
 136  0
                 if (NotificationConstants.ACK_CAUSE.equals(cause)) {
 137  
                     // moved from NotificationController, ack command
 138  
                     /*
 139  
                      * acknowledge using workflow docId
 140  
                      */
 141  0
                     if (nwd.isAcknowledgeRequested()) {
 142  0
                         nwd.acknowledge("This notification has been acknowledged.");
 143  0
                         LOG.debug("acknowledged "+nwd.getTitle());                      
 144  0
                         LOG.debug("status display value: "+nwd.getStatusDisplayValue());
 145  
                     } else {
 146  0
                         LOG.debug("Acknowledgement was not needed for document " + nwd.getDocumentId());
 147  
                     }
 148  0
                 } else if (NotificationConstants.FYI_CAUSE.equals(cause)) {
 149  
                     // moved from NotificationController, fyi command
 150  
                     /*
 151  
                      * FYI using workflow docId
 152  
                      */
 153  0
                     if (nwd.isFYIRequested()) {
 154  0
                         nwd.fyi();
 155  0
                         LOG.debug("fyi "+nwd.getTitle());                      
 156  0
                         LOG.debug("status display value: "+nwd.getStatusDisplayValue());
 157  
                     } else {
 158  0
                         LOG.debug("FYI was not needed for document " + nwd.getDocumentId());
 159  
                     }
 160  
                 }
 161  0
             } catch (WorkflowException we) {
 162  0
                 LOG.error("Error performing action on workflow document");
 163  0
                 throw new RuntimeException("Error performing action on workflow document", we);
 164  0
             }
 165  
         }
 166  0
     }
 167  
 
 168  
     /**
 169  
      * Marks the workflow document as originating from the Notification System, so that the Notification
 170  
      * post-processor does not route the action back through the Notification System.
 171  
      * @param doc the doc to monogram
 172  
      */
 173  
     protected void flagWorkflowDocument(WorkflowDocument doc) {
 174  0
         Properties p = new Properties();
 175  0
         p.setProperty(INTERNAL_COMMAND_FLAG, "true");
 176  0
         ByteArrayOutputStream baos = new ByteArrayOutputStream(100);
 177  
         try {
 178  0
             p.store(baos, null);
 179  0
         } catch (IOException ioe) {
 180  0
             throw new RuntimeException("Could not store properties", ioe);
 181  0
         }
 182  0
         doc.getDocumentContent().setAttributeContent("<whatever>" + new String(baos.toByteArray()) + "</whatever>");
 183  0
     }
 184  
 }