View Javadoc

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.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.ken.bo.NotificationMessageDelivery;
20  import org.kuali.rice.ken.document.kew.NotificationWorkflowDocument;
21  import org.kuali.rice.ken.service.NotificationMessageContentService;
22  import org.kuali.rice.ken.service.NotificationWorkflowDocumentService;
23  import org.kuali.rice.ken.util.NotificationConstants;
24  import org.kuali.rice.kew.dto.ActionRequestDTO;
25  import org.kuali.rice.kew.exception.WorkflowException;
26  import org.kuali.rice.kew.service.WorkflowDocument;
27  import org.kuali.rice.kew.util.KEWConstants;
28  import org.kuali.rice.kim.api.entity.principal.Principal;
29  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30  
31  
32  
33  /**
34   * This class is responsible for interacting with KEW - this is the default implementation that leverages the KEW client API.
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class NotificationWorkflowDocumentServiceImpl implements NotificationWorkflowDocumentService {
38      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
39  	.getLogger(NotificationWorkflowDocumentServiceImpl.class);
40  
41      private NotificationMessageContentService messageContentService;
42  
43      /**
44       * Constructs a NotificationWorkflowDocumentServiceImpl instance.
45       * @param messageContentService
46       */
47      public NotificationWorkflowDocumentServiceImpl(NotificationMessageContentService messageContentService) {
48  	this.messageContentService = messageContentService;
49      }
50  
51      /**
52       * Implements by instantiating a NotificationWorkflowDocument, which in turn interacts with Workflow to set it up with an initiator of the
53       * passed in user id.
54       * @see org.kuali.rice.ken.service.NotificationWorkflowDocumentService#createAndAdHocRouteNotificationWorkflowDocument(org.kuali.rice.ken.bo.NotificationMessageDelivery, java.lang.String, java.lang.String, java.lang.String)
55       */
56      public String createAndAdHocRouteNotificationWorkflowDocument(NotificationMessageDelivery messageDelivery, String initiatorUserId,
57  	    String recipientUserId, String annotation) throws WorkflowException {
58  	// obtain a workflow user object first
59  	//WorkflowIdDTO initiator = new WorkflowIdDTO(initiatorUserId);
60  
61  	// now construct the workflow document, which will interact with workflow
62  	NotificationWorkflowDocument document = NotificationWorkflowDocument.createNotificationDocument(initiatorUserId);
63  
64  	// this is our loose foreign key to our message delivery record in notification
65  	document.getRouteHeader().setAppDocId(messageDelivery.getId().toString());
66  	//document.setAppDocId(messageDelivery.getId().toString());
67  
68  	// now add the content of the notification as XML to the document
69  	document.setApplicationContent(messageContentService.generateNotificationMessage(messageDelivery.getNotification(), messageDelivery.getUserRecipientId()));
70  
71          if (!StringUtils.isBlank(messageDelivery.getNotification().getTitle())) {
72              document.setTitle(messageDelivery.getNotification().getTitle());
73          } else {
74              LOG.error("Encountered notification with no title set: Message Delivery #" + messageDelivery.getId() + ", Notification #" + messageDelivery.getNotification().getId());
75          }
76  
77  	// now set up the ad hoc route
78  	String actionRequested;
79  	if(NotificationConstants.DELIVERY_TYPES.ACK.equals(messageDelivery.getNotification().getDeliveryType())) {
80  	    actionRequested = NotificationConstants.KEW_CONSTANTS.ACK_AD_HOC_ROUTE;
81  	} else {
82  	    actionRequested = NotificationConstants.KEW_CONSTANTS.FYI_AD_HOC_ROUTE;
83  	}
84  
85  	// Clarification of ad hoc route call
86  	// param 1 - actionRequested will be either ACK or FYI
87  	// param 2 - annotation is whatever text we pass in to describe the transaction - this will be system generated
88  	// param 3 - recipient is the person who will receive this request
89  	// param 4 - this is the responsibilityParty (a.k.a the system that produced this request), so we'll put the producer name in there
90  	// param 5 - this is the "force action" requests - if set to true, this will be delivered to the recipients list regardless of
91  	//           whether the recipient has already taken action on this request; in our case, this doesn't really apply at this point in time,
92  	//           so we'll set to true just to be safe
93  	Principal principal = KimApiServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(recipientUserId);
94  	document.adHocRouteDocumentToPrincipal(actionRequested, annotation, principal.getPrincipalId(),
95  		messageDelivery.getNotification().getProducer().getName(), true);
96  
97  	// now actually route it along its way
98  	document.routeDocument(annotation);
99  
100 	return document.getDocumentId();
101     }
102 
103     /**
104      * This service method is implemented by constructing a NotificationWorkflowDocument using the pre-existing document Id
105      * that is passed in.
106      * @see org.kuali.rice.ken.service.NotificationWorkflowDocumentService#findNotificationWorkflowDocumentByDocumentId(java.lang.String, java.lang.String)
107      */
108     public NotificationWorkflowDocument getNotificationWorkflowDocumentByDocumentId(String initiatorUserId, String workflowDocumentId) throws WorkflowException {
109 	// construct the workflow id value object
110 	//WorkflowIdDTO initiator = new WorkflowIdDTO(initiatorUserId);
111 
112 
113 	// now return the actual document instance
114 	// this handles going out and getting the workflow document
115 	return NotificationWorkflowDocument.loadNotificationDocument(initiatorUserId, workflowDocumentId);
116     }
117 
118     /**
119      * @see org.kuali.rice.ken.service.NotificationWorkflowDocumentService#clearAllFyisAndAcknowledgeNotificationWorkflowDocument(java.lang.String, org.kuali.rice.ken.document.kew.NotificationWorkflowDocument, java.lang.String)
120      */
121     public void clearAllFyisAndAcknowledgeNotificationWorkflowDocument(String initiatorUserId, NotificationWorkflowDocument workflowDocument, String annotation) throws WorkflowException {
122 	ActionRequestDTO[] reqs = workflowDocument.getActionRequests();
123         for(int i = 0; i < reqs.length; i++) {
124             LOG.info("Action Request[" + i + "] = " + reqs[i].getActionRequested());
125             if(reqs[i].getActionRequested().equals(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ)) {
126                 workflowDocument.acknowledge(annotation);
127             } else if(reqs[i].getActionRequested().equals(KEWConstants.ACTION_REQUEST_FYI_REQ)) {
128                 workflowDocument.logDocumentAction(annotation);
129                 workflowDocument.clearFYI();
130             } else {
131                 throw new WorkflowException("Invalid notification action request in workflow document (" + workflowDocument.getDocumentId() + ") was encountered.  Should be either an acknowledge or fyi and was not.");
132             }
133         }
134     }
135 
136     /**
137      * @see org.kuali.rice.ken.service.NotificationWorkflowDocumentService#terminateWorkflowDocument(org.kuali.rice.kew.service.WorkflowDocument)
138      */
139     public void terminateWorkflowDocument(WorkflowDocument document) throws WorkflowException {
140         document.superUserCancel("terminating document: documentId=" + document.getDocumentId() + ", appDocId=" + document.getAppDocId());
141     }
142 }