View Javadoc

1   /**
2    * Copyright 2005-2012 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.kew.actionlist.service.impl;
17  
18  import java.sql.Timestamp;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.Date;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.rice.core.api.config.property.ConfigContext;
28  import org.kuali.rice.kew.actionitem.ActionItem;
29  import org.kuali.rice.kew.actionitem.OutboxItemActionListExtension;
30  import org.kuali.rice.kew.actionitem.dao.ActionItemDAO;
31  import org.kuali.rice.kew.actionlist.ActionListFilter;
32  import org.kuali.rice.kew.actionlist.dao.ActionListDAO;
33  import org.kuali.rice.kew.actionlist.service.ActionListService;
34  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
35  import org.kuali.rice.kew.actionrequest.Recipient;
36  import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
37  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
38  import org.kuali.rice.kew.doctype.bo.DocumentType;
39  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
40  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
41  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
42  import org.kuali.rice.kew.service.KEWServiceLocator;
43  import org.kuali.rice.kew.useroptions.UserOptions;
44  import org.kuali.rice.kew.useroptions.UserOptionsService;
45  import org.kuali.rice.kew.api.KewApiConstants;
46  import org.kuali.rice.kim.api.group.GroupService;
47  import org.kuali.rice.kim.api.identity.principal.Principal;
48  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
49  
50  /**
51   * Default implementation of the {@link ActionListService}.
52   *
53   * @author Kuali Rice Team (rice.collab@kuali.org)
54   */
55  public class ActionListServiceImpl implements ActionListService {
56  
57      protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
58  
59      private ActionListDAO actionListDAO;
60  
61      private ActionItemDAO actionItemDAO;
62  
63      public Collection<Recipient> findUserSecondaryDelegators(String principalId) {
64          return getActionItemDAO().findSecondaryDelegators(principalId);
65      }
66  
67      public Collection<Recipient> findUserPrimaryDelegations(String principalId) {
68          return getActionItemDAO().findPrimaryDelegationRecipients(principalId);
69      }
70  
71      public Collection<ActionItem> getActionList(String principalId, ActionListFilter filter) {
72           return getActionListDAO().getActionList(principalId, filter);
73      }
74  
75      public Collection<ActionItem> getActionListForSingleDocument(String documentId) {
76           return getActionListDAO().getActionListForSingleDocument(documentId);
77      }
78  
79      public void setActionListDAO(ActionListDAO actionListDAO) {
80          this.actionListDAO = actionListDAO;
81      }
82  
83      public ActionListDAO getActionListDAO() {
84          return actionListDAO;
85      }
86  
87      public void deleteActionItem(ActionItem actionItem) {
88      	deleteActionItem(actionItem, false);
89      }
90      
91      public void deleteActionItem(ActionItem actionItem, boolean forceIntoOutbox) {
92          getActionItemDAO().deleteActionItem(actionItem);
93          // remove notification from KCB
94          KEWServiceLocator.getNotificationService().removeNotification(Collections.singletonList(ActionItem.to(actionItem)));
95          this.saveOutboxItem(actionItem, forceIntoOutbox);
96      }
97  
98      public void deleteByDocumentId(String documentId) {
99          getActionItemDAO().deleteByDocumentId(documentId);
100     }
101 
102     public Collection<ActionItem> findByDocumentId(String documentId) {
103         return getActionItemDAO().findByDocumentId(documentId);
104     }
105 
106     public Collection<ActionItem> findByActionRequestId(String actionRequestId) {
107         return getActionItemDAO().findByActionRequestId(actionRequestId);
108     }
109 
110     public Collection<ActionItem> findByWorkflowUserDocumentId(String workflowUserId, String documentId) {
111         return getActionItemDAO().findByWorkflowUserDocumentId(workflowUserId, documentId);
112     }
113 
114     public Collection<ActionItem> findByDocumentTypeName(String documentTypeName) {
115         return getActionItemDAO().findByDocumentTypeName(documentTypeName);
116     }
117 
118     public ActionItem createActionItemForActionRequest(ActionRequestValue actionRequest) {
119         ActionItem actionItem = new ActionItem();
120 
121         DocumentRouteHeaderValue routeHeader = actionRequest.getRouteHeader();
122         DocumentType docType = routeHeader.getDocumentType();
123 
124         actionItem.setActionRequestCd(actionRequest.getActionRequested());
125         actionItem.setActionRequestId(actionRequest.getActionRequestId());
126         actionItem.setDocName(docType.getName());
127         actionItem.setRoleName(actionRequest.getQualifiedRoleName());
128         actionItem.setPrincipalId(actionRequest.getPrincipalId());
129         actionItem.setDocumentId(actionRequest.getDocumentId());
130         actionItem.setDateAssigned(new Timestamp(new Date().getTime()));
131         actionItem.setDocHandlerURL(docType.getResolvedDocumentHandlerUrl());
132         actionItem.setDocLabel(docType.getLabel());
133         actionItem.setDocTitle(routeHeader.getDocTitle());
134         actionItem.setGroupId(actionRequest.getGroupId());
135         actionItem.setResponsibilityId(actionRequest.getResponsibilityId());
136         actionItem.setDelegationType(actionRequest.getDelegationType());
137         actionItem.setRequestLabel(actionRequest.getRequestLabel());
138 
139         ActionRequestValue delegatorActionRequest = getActionRequestService().findDelegatorRequest(actionRequest);
140         if (delegatorActionRequest != null) {
141             actionItem.setDelegatorPrincipalId(delegatorActionRequest.getPrincipalId());
142             actionItem.setDelegatorGroupId(delegatorActionRequest.getGroupId());
143         }
144 
145         return actionItem;
146     }
147 
148 
149     public void updateActionItemsForTitleChange(String documentId, String newTitle) {
150         Collection<ActionItem> items = getActionItemDAO().findByDocumentId(documentId);
151         for (Iterator<ActionItem> iterator = items.iterator(); iterator.hasNext();) {
152             ActionItem item = iterator.next();
153             item.setDocTitle(newTitle);
154             saveActionItem(item);
155         }
156     }
157 
158     public void saveActionItem(ActionItem actionItem) {
159         getActionItemDAO().saveActionItem(actionItem);
160     }
161 
162     public ActionItemDAO getActionItemDAO() {
163         return actionItemDAO;
164     }
165 
166     public ActionRequestService getActionRequestService() {
167         return (ActionRequestService) KEWServiceLocator.getActionRequestService();
168     }
169 
170     public GroupService getGroupService(){
171     	return KimApiServiceLocator.getGroupService();
172     }
173 
174     public void setActionItemDAO(ActionItemDAO actionItemDAO) {
175         this.actionItemDAO = actionItemDAO;
176     }
177 
178 
179     public void validateActionItem(ActionItem actionItem) {
180         List<WorkflowServiceErrorImpl> errors = new ArrayList<WorkflowServiceErrorImpl>();
181         String principalId = actionItem.getPrincipalId();
182         if (principalId == null || principalId.trim().equals("")) {
183             errors.add(new WorkflowServiceErrorImpl("ActionItem person null.", "actionitem.personid.empty", actionItem
184                     .getId().toString()));
185         } else {
186         	Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
187         	if (principal == null) {
188                 errors.add(new WorkflowServiceErrorImpl("ActionItem person invalid.", "actionitem.personid.invalid",
189                         actionItem.getId().toString()));
190             }
191         }
192 
193         if (actionItem.getDateAssigned() == null) {
194             errors.add(new WorkflowServiceErrorImpl("ActionItem date assigned empty.", "actionitem.dateassigned.empty",
195                     actionItem.getId().toString()));
196         }
197 
198         String actionRequestCd = actionItem.getActionRequestCd();
199         if (actionRequestCd == null || actionRequestCd.trim().equals("")) {
200             errors.add(new WorkflowServiceErrorImpl("ActionItem action request cd empty.",
201                     "actionitem.actionrequestcd.empty", actionItem.getId().toString()));
202         } else if (!KewApiConstants.ACTION_REQUEST_CD.containsKey(actionRequestCd)) {
203             errors.add(new WorkflowServiceErrorImpl("ActionItem action request cd invalid.",
204                     "actionitem.actionrequestcd.invalid", actionItem.getId().toString()));
205         }
206 
207         if (actionItem.getActionRequestId() == null) {
208             errors.add(new WorkflowServiceErrorImpl("ActionItem action request id empty.",
209                     "actionitem.actionrequestid.empty", actionItem.getId().toString()));
210         }
211 
212         if (actionItem.getDocumentId() == null) {
213             errors.add(new WorkflowServiceErrorImpl("ActionItem Document id empty.", "actionitem.documentid.empty",
214                     actionItem.getId().toString()));
215         } else if (KEWServiceLocator.getRouteHeaderService().getRouteHeader(actionItem.getDocumentId()) == null) {
216             errors.add(new WorkflowServiceErrorImpl("ActionItem Document id invalid.", "actionitem.documentid.invalid",
217                     actionItem.getId().toString()));
218         }
219 
220         String docTypeName = actionItem.getDocName();
221         DocumentType docType = null;
222         if (docTypeName == null || docTypeName.trim().equals("")) {
223             errors.add(new WorkflowServiceErrorImpl("ActionItem doctypename empty.", "actionitem.doctypename.empty",
224                     actionItem.getId().toString()));
225         } else {
226             docType = KEWServiceLocator.getDocumentTypeService().findByName(actionItem.getDocName());
227             if (docType == null) {
228                 errors.add(new WorkflowServiceErrorImpl("ActionItem doctypename invalid.", "actionitem.doctypename.invalid",
229                         actionItem.getId().toString()));
230             }
231         }
232 
233         if (actionItem.getDocLabel() == null || actionItem.getDocLabel().trim().equals("")) {
234             errors.add(new WorkflowServiceErrorImpl("ActionItem doctypelabel empty.", "actionitem.doctypelabel.empty",
235                     actionItem.getId().toString()));
236         } else if (docType != null && !docType.getLabel().equals(actionItem.getDocLabel())) {
237             errors.add(new WorkflowServiceErrorImpl("ActionItem doctypelabel no match.", "actionitem.doctypelabel.nomatch",
238                     actionItem.getId().toString()));
239         }
240 
241         // first check to see if the document type has an empty document handler url
242         if (StringUtils.isNotBlank(docType.getResolvedDocumentHandlerUrl())) {
243             if (actionItem.getDocHandlerURL() == null || actionItem.getDocHandlerURL().trim().equals("")) {
244                 errors.add(new WorkflowServiceErrorImpl("ActionItem doc handler url empty.", "actionitem.dochdrurl.empty",
245                         actionItem.getId().toString()));
246             } else if (docType != null && !docType.getResolvedDocumentHandlerUrl().equals(actionItem.getDocHandlerURL())) {
247                 errors.add(new WorkflowServiceErrorImpl("ActionItem doc handler url no match.", "actionitem.dochdrurl.nomatch",
248                         actionItem.getId().toString()));
249             }
250         } else {
251             // if the doc type doc handler url is blank, verify that the action item doc handler url is also blank
252             if (StringUtils.isNotBlank(actionItem.getDocHandlerURL())) {
253                 errors.add(new WorkflowServiceErrorImpl("ActionItem doc handler url not empty.", "actionitem.dochdrurl.not.empty", 
254                         actionItem.getId().toString()));
255             }
256         }
257 
258         if (!errors.isEmpty()) {
259             throw new WorkflowServiceErrorException("ActionItem Validation Error", errors);
260         }
261     }
262 
263     public ActionItem findByActionItemId(String actionItemId) {
264         return getActionItemDAO().findByActionItemId(actionItemId);
265     }
266 
267     public int getCount(String principalId) {
268         return getActionListDAO().getCount(principalId);
269     }
270 
271     public List<Integer> getMaxActionItemIdAndCountForUser(String principalId) {
272         return getActionListDAO().getMaxActionItemIdAndCountForUser(principalId);
273     }
274 
275     /**
276      *
277      * This overridden method ...
278      *
279      * @see org.kuali.rice.kew.actionlist.service.ActionListService#getOutbox(java.lang.String, org.kuali.rice.kew.actionlist.ActionListFilter)
280      */
281     public Collection<ActionItem> getOutbox(String principalId, ActionListFilter filter) {
282         return this.getActionListDAO().getOutbox(principalId, filter);
283     }
284 
285     public Collection<ActionItem> getOutboxItemsByDocumentType(String documentTypeName) {
286         return this.getActionItemDAO().getOutboxItemsByDocumentType(documentTypeName);
287     }
288 
289     /**
290      * This overridden method ...
291      *
292      * @see org.kuali.rice.kew.actionlist.service.ActionListService#removeOutboxItems(String, java.util.List)
293      */
294     public void removeOutboxItems(String principalId, List<String> outboxItems) {
295         this.getActionListDAO().removeOutboxItems(principalId, outboxItems);
296     }
297 
298     public void saveOutboxItem(ActionItem actionItem) {
299     	saveOutboxItem(actionItem, false);
300     }
301     
302     /**
303      *
304      * save the ouboxitem unless the document is saved or the user already has the item in their outbox.
305      *
306      * @see org.kuali.rice.kew.actionlist.service.ActionListService#saveOutboxItem(org.kuali.rice.kew.actionitem.ActionItem, boolean)
307      */
308     public void saveOutboxItem(ActionItem actionItem, boolean forceIntoOutbox) {
309     	UserOptionsService userOptionsService = KEWServiceLocator.getUserOptionsService();
310     	Boolean isUsingOutBox = true;
311     	List<UserOptions> options = userOptionsService.findByUserQualified(actionItem.getPrincipalId(), KewApiConstants.USE_OUT_BOX);
312     	if (options == null || options.isEmpty()){
313     		isUsingOutBox = true;
314     	} else {
315 			for (Iterator iter = options.iterator(); iter.hasNext();) {
316 				UserOptions u = (UserOptions) iter.next();
317 				if (u.getOptionVal() == null || !(u.getOptionVal().equals("yes"))){
318 					isUsingOutBox = false;
319 				}
320 			}
321     	}
322     	
323     	if (isUsingOutBox
324             && ConfigContext.getCurrentContextConfig().getOutBoxOn()
325             && getActionListDAO().getOutboxByDocumentIdUserId(actionItem.getDocumentId(), actionItem.getPrincipalId()) == null
326             && !KEWServiceLocator.getRouteHeaderService().getRouteHeader(actionItem.getDocumentId()).getDocRouteStatus().equals(
327                     		KewApiConstants.ROUTE_HEADER_SAVED_CD)) {
328 
329     		// only create an outbox item if this user has taken action on the document
330     		ActionRequestValue actionRequest = KEWServiceLocator.getActionRequestService().findByActionRequestId(
331     				actionItem.getActionRequestId());
332     		ActionTakenValue actionTaken = actionRequest.getActionTaken();
333     		// if an action was taken...
334     		if (forceIntoOutbox || (actionTaken != null && actionTaken.getPrincipalId().equals(actionItem.getPrincipalId()))) {
335     			this.getActionListDAO().saveOutboxItem(new OutboxItemActionListExtension(actionItem));
336     		}
337        
338     	}
339     }
340     
341 	public Collection<ActionItem> findByPrincipalId(String principalId) {
342 		return getActionItemDAO().findByPrincipalId(principalId);
343 	}
344 
345 }