1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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.api.WorkflowDocument;
25  import org.kuali.rice.kew.api.WorkflowRuntimeException;
26  import org.kuali.rice.kew.api.action.ActionRequest;
27  import org.kuali.rice.kew.api.action.ActionRequestType;
28  import org.kuali.rice.kim.api.identity.principal.Principal;
29  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30  
31  import java.util.List;
32  
33  
34  
35  
36  
37  
38  public class NotificationWorkflowDocumentServiceImpl implements NotificationWorkflowDocumentService {
39      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
40              .getLogger(NotificationWorkflowDocumentServiceImpl.class);
41  
42      private NotificationMessageContentService messageContentService;
43  
44      
45  
46  
47  
48      public NotificationWorkflowDocumentServiceImpl(NotificationMessageContentService messageContentService) {
49          this.messageContentService = messageContentService;
50      }
51  
52      
53  
54  
55  
56  
57  
58      public String createAndAdHocRouteNotificationWorkflowDocument(NotificationMessageDelivery messageDelivery,
59              String initiatorUserId,
60              String recipientUserId, String annotation) {
61          
62          
63  
64          
65          WorkflowDocument document;
66          if (StringUtils.isNotBlank(messageDelivery.getNotification().getDocTypeName())) {
67              document = NotificationWorkflowDocument.createNotificationDocument(initiatorUserId,
68                      messageDelivery.getNotification().getDocTypeName());
69          } else {
70              document = NotificationWorkflowDocument.createNotificationDocument(initiatorUserId);
71          }
72  
73          
74          document.setApplicationDocumentId(messageDelivery.getId().toString());
75          
76  
77          
78          document.setApplicationContent(messageContentService.generateNotificationMessage(
79                  messageDelivery.getNotification(), messageDelivery.getUserRecipientId()));
80  
81          if (!StringUtils.isBlank(messageDelivery.getNotification().getTitle())) {
82              document.setTitle(messageDelivery.getNotification().getTitle());
83          } else {
84              LOG.error("Encountered notification with no title set: Message Delivery #" + messageDelivery.getId()
85                      + ", Notification #" + messageDelivery.getNotification().getId());
86          }
87  
88          
89          String actionRequested;
90          if (NotificationConstants.DELIVERY_TYPES.ACK.equals(messageDelivery.getNotification().getDeliveryType())) {
91              actionRequested = NotificationConstants.KEW_CONSTANTS.ACK_AD_HOC_ROUTE;
92          } else {
93              actionRequested = NotificationConstants.KEW_CONSTANTS.FYI_AD_HOC_ROUTE;
94          }
95  
96          
97          
98          
99          
100         
101         
102         
103         
104         
105         
106         Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(recipientUserId);
107         
108         document.adHocToPrincipal(ActionRequestType.fromCode(actionRequested), annotation, principal.getPrincipalId(),
109                 messageDelivery.getNotification().getProducer().getName(), true);
110 
111         
112         document.route(annotation);
113 
114         return document.getDocumentId();
115     }
116 
117     
118 
119 
120 
121 
122 
123     public WorkflowDocument getNotificationWorkflowDocumentByDocumentId(String initiatorUserId,
124             String workflowDocumentId) {
125         
126         
127 
128         
129         
130         return NotificationWorkflowDocument.loadNotificationDocument(initiatorUserId, workflowDocumentId);
131     }
132 
133     
134 
135 
136 
137     public void clearAllFyisAndAcknowledgeNotificationWorkflowDocument(String initiatorUserId,
138             WorkflowDocument workflowDocument, String annotation) {
139         List<ActionRequest> reqs = workflowDocument.getRootActionRequests();
140         for (int i = 0; i < reqs.size(); i++) {
141             LOG.info("Action Request[" + i + "] = " + reqs.get(i).getActionRequested());
142             if (reqs.get(i).getActionRequested().equals(ActionRequestType.ACKNOWLEDGE)) {
143                 workflowDocument.acknowledge(annotation);
144             } else if (reqs.get(i).getActionRequested().equals(ActionRequestType.FYI)) {
145                 workflowDocument.logAnnotation(annotation);
146                 workflowDocument.fyi();
147             } else {
148                 throw new WorkflowRuntimeException("Invalid notification action request in workflow document ("
149                         + workflowDocument.getDocumentId()
150                         + ") was encountered.  Should be either an acknowledge or fyi and was not.");
151             }
152         }
153     }
154 
155     
156 
157 
158     public void terminateWorkflowDocument(WorkflowDocument document) {
159         document.superUserCancel("terminating document: documentId=" + document.getDocumentId() + ", appDocId="
160                 + document.getApplicationDocumentId());
161     }
162 }