Coverage Report - org.kuali.rice.kns.workflow.service.impl.WorkflowDocumentServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
WorkflowDocumentServiceImpl
0%
0/152
0%
0/86
3.478
 
 1  
 /*
 2  
  * Copyright 2005-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.kns.workflow.service.impl;
 17  
 
 18  
 import java.text.MessageFormat;
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.commons.lang.StringUtils;
 23  
 import org.apache.commons.lang.time.StopWatch;
 24  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 25  
 import org.kuali.rice.core.util.RiceKeyConstants;
 26  
 import org.kuali.rice.kew.dto.RouteNodeInstanceDTO;
 27  
 import org.kuali.rice.kew.exception.DocumentTypeNotFoundException;
 28  
 import org.kuali.rice.kew.exception.InvalidActionTakenException;
 29  
 import org.kuali.rice.kew.exception.WorkflowException;
 30  
 import org.kuali.rice.kew.service.WorkflowInfo;
 31  
 import org.kuali.rice.kew.util.KEWConstants;
 32  
 import org.kuali.rice.kim.api.entity.principal.Principal;
 33  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 34  
 import org.kuali.rice.kim.api.group.Group;
 35  
 import org.kuali.rice.kim.bo.Person;
 36  
 
 37  
 import org.kuali.rice.kns.bo.AdHocRouteRecipient;
 38  
 import org.kuali.rice.kns.exception.UnknownDocumentIdException;
 39  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 40  
 import org.kuali.rice.kns.util.GlobalVariables;
 41  
 import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
 42  
 import org.kuali.rice.kns.workflow.service.KualiWorkflowInfo;
 43  
 import org.kuali.rice.kns.workflow.service.WorkflowDocumentService;
 44  
 import org.springframework.transaction.annotation.Transactional;
 45  
 
 46  
 
 47  
 /**
 48  
  * This class is the implementation of the WorkflowDocumentService, which makes use of Workflow.
 49  
  */
 50  
 @Transactional
 51  0
 public class WorkflowDocumentServiceImpl implements WorkflowDocumentService {
 52  0
     private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(WorkflowDocumentServiceImpl.class);
 53  
 
 54  
     private KualiWorkflowInfo workflowInfoService;
 55  
 
 56  
     public boolean workflowDocumentExists(String documentId) {
 57  0
         boolean exists = false;
 58  
 
 59  0
         if (StringUtils.isBlank(documentId)) {
 60  0
             throw new IllegalArgumentException("invalid (blank) documentId");
 61  
         }
 62  
 
 63  0
         exists = workflowInfoService.routeHeaderExists(documentId);
 64  
 
 65  0
         return exists;
 66  
     }
 67  
 
 68  
     /**
 69  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#createWorkflowDocument(java.lang.String,
 70  
      *      org.kuali.rice.kew.user.WorkflowUser)
 71  
      */
 72  
     public KualiWorkflowDocument createWorkflowDocument(String documentTypeId, Person person) throws WorkflowException {
 73  0
         String watchName = "createWorkflowDocument";
 74  0
         StopWatch watch = new StopWatch();
 75  0
         watch.start();
 76  0
         if (LOG.isDebugEnabled()) {
 77  0
             LOG.debug(watchName + ": started");
 78  
         }
 79  0
         if (StringUtils.isBlank(documentTypeId)) {
 80  0
             throw new IllegalArgumentException("invalid (blank) documentTypeId");
 81  
         }
 82  0
         if (person == null) {
 83  0
             throw new IllegalArgumentException("invalid (null) person");
 84  
         }
 85  
 
 86  0
         if (StringUtils.isBlank(person.getPrincipalName())) {
 87  0
             throw new IllegalArgumentException("invalid (empty) PrincipalName");
 88  
         }
 89  
 
 90  0
         if (LOG.isDebugEnabled()) {
 91  0
             LOG.debug("creating workflowDoc(" + documentTypeId + "," + person.getPrincipalName() + ")");
 92  
         }
 93  
 
 94  0
         KualiWorkflowDocument document = KualiWorkflowDocumentImpl.createKualiDocumentImpl(person.getPrincipalId(), documentTypeId);
 95  
 
 96  
         // workflow doesn't complain about invalid docTypes until the first call to getDocumentId, but the rest of our code
 97  
         // assumes that you get the exception immediately upon trying to create a document of that invalid type
 98  
         //
 99  
         // and it throws the generic WorkflowException, apparently, instead of the more specific DocumentTypeNotFoundException,
 100  
         // so as long as I'm here I'll do that conversion as well
 101  
         try {
 102  0
             document.getDocumentId();
 103  
         }
 104  0
         catch (WorkflowException e) {
 105  0
             if (e.getMessage().contains("Could not locate the given document type name")) {
 106  0
                 throw new DocumentTypeNotFoundException("unknown document type '" + documentTypeId + "'");
 107  
             }
 108  0
             throw e;
 109  0
         }
 110  
 
 111  0
         watch.stop();
 112  0
         if (LOG.isDebugEnabled()) {
 113  0
             LOG.debug(watchName + ": " + watch.toString());        
 114  
         }
 115  
 
 116  0
         return document;
 117  
     }
 118  
 
 119  
     /**
 120  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#createWorkflowDocument(java.lang.Long,
 121  
      *      org.kuali.rice.kew.user.WorkflowUser)
 122  
      */
 123  
     public KualiWorkflowDocument loadWorkflowDocument(String documentId, Person user) throws WorkflowException {
 124  0
         if (documentId == null) {
 125  0
             throw new IllegalArgumentException("invalid (null) documentHeaderId");
 126  
         }
 127  0
         if (user == null) {
 128  0
             throw new IllegalArgumentException("invalid (null) workflowUser");
 129  
         }
 130  0
         else if (StringUtils.isEmpty(user.getPrincipalName())) {
 131  0
             throw new IllegalArgumentException("invalid (empty) workflowUser");
 132  
         }
 133  
 
 134  0
         if (LOG.isDebugEnabled()) {
 135  0
             LOG.debug("retrieving document(" + documentId + "," + user.getPrincipalName() + ")");
 136  
         }
 137  0
         KualiWorkflowDocument document = KualiWorkflowDocumentImpl.LoadKualiDocumentImpl(user.getPrincipalId(), documentId);
 138  
 
 139  0
         if (document.getRouteHeader() == null) {
 140  0
             throw new UnknownDocumentIdException("unable to locate document with documentHeaderId '" + documentId + "'");
 141  
         }
 142  0
         return document;
 143  
     }
 144  
 
 145  
     /**
 146  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#acknowledge(org.kuali.rice.kew.rule.FlexDoc)
 147  
      */
 148  
     public void acknowledge(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 149  0
         if (LOG.isDebugEnabled()) {
 150  0
             LOG.debug("acknowleding document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 151  
         }
 152  
 
 153  0
         handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ }));
 154  0
         workflowDocument.acknowledge(annotation);
 155  0
     }
 156  
 
 157  
     /**
 158  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#approve(org.kuali.rice.kew.rule.FlexDoc)
 159  
      */
 160  
     public void approve(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 161  0
         if (LOG.isDebugEnabled()) {
 162  0
             LOG.debug("approving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 163  
         }
 164  
 
 165  0
         handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ, KEWConstants.ACTION_REQUEST_APPROVE_REQ }));
 166  0
         workflowDocument.approve(annotation);
 167  0
     }
 168  
 
 169  
 
 170  
     /**
 171  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#superUserApprove(org.kuali.rice.kns.workflow.service.KualiWorkflowDocument,
 172  
      *      java.lang.String)
 173  
      */
 174  
     public void superUserApprove(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 175  0
             if ( LOG.isInfoEnabled() ) {
 176  0
                     LOG.info("super user approve document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 177  
             }
 178  0
         workflowDocument.superUserApprove(annotation);
 179  0
     }
 180  
 
 181  
     /**
 182  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#superUserCancel(org.kuali.rice.kns.workflow.service.KualiWorkflowDocument,
 183  
      *      java.lang.String)
 184  
      */
 185  
     public void superUserCancel(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 186  0
         LOG.info("super user cancel document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 187  0
         workflowDocument.superUserCancel(annotation);
 188  0
     }
 189  
 
 190  
     /**
 191  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#superUserDisapprove(org.kuali.rice.kns.workflow.service.KualiWorkflowDocument,
 192  
      *      java.lang.String)
 193  
      */
 194  
     public void superUserDisapprove(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 195  0
             if ( LOG.isInfoEnabled() ) {
 196  0
                     LOG.info("super user disapprove document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 197  
             }
 198  0
         workflowDocument.superUserDisapprove(annotation);
 199  0
     }
 200  
 
 201  
     /**
 202  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#blanketApprove(org.kuali.rice.kew.rule.FlexDoc)
 203  
      */
 204  
     public void blanketApprove(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 205  0
         if (LOG.isDebugEnabled()) {
 206  0
             LOG.debug("blanket approving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 207  
         }
 208  
 
 209  0
         handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ }));
 210  0
         workflowDocument.blanketApprove(annotation);
 211  0
     }
 212  
 
 213  
     /**
 214  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#cancel(org.kuali.rice.kew.rule.FlexDoc)
 215  
      */
 216  
     public void cancel(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 217  0
         if (LOG.isDebugEnabled()) {
 218  0
             LOG.debug("canceling document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 219  
         }
 220  
 
 221  0
         workflowDocument.cancel(annotation);
 222  0
     }
 223  
 
 224  
     /**
 225  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#clearFyi(org.kuali.rice.kew.rule.FlexDoc)
 226  
      */
 227  
     public void clearFyi(KualiWorkflowDocument workflowDocument, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 228  0
         if (LOG.isDebugEnabled()) {
 229  0
             LOG.debug("clearing FYI for document(" + workflowDocument.getDocumentId() + ")");
 230  
         }
 231  
 
 232  0
         handleAdHocRouteRequests(workflowDocument, "", filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_FYI_REQ }));
 233  0
         workflowDocument.fyi();
 234  0
     }
 235  
 
 236  
     public void sendWorkflowNotification(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 237  0
             sendWorkflowNotification(workflowDocument, annotation, adHocRecipients, null);
 238  0
     }
 239  
     
 240  
     /**
 241  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#sendWorkflowNotification(org.kuali.rice.kns.workflow.service.KualiWorkflowDocument, java.lang.String, java.util.List)
 242  
      */
 243  
     public void sendWorkflowNotification(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients, String notificationLabel) throws WorkflowException {
 244  0
         if (LOG.isDebugEnabled()) {
 245  0
             LOG.debug("sending FYI for document(" + workflowDocument.getDocumentId() + ")");
 246  
         }
 247  
 
 248  0
         handleAdHocRouteRequests(workflowDocument, annotation, adHocRecipients, notificationLabel);
 249  0
     }
 250  
 
 251  
     /**
 252  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#disapprove(org.kuali.rice.kew.rule.FlexDoc)
 253  
      */
 254  
     public void disapprove(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 255  0
         if (LOG.isDebugEnabled()) {
 256  0
             LOG.debug("disapproving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 257  
         }
 258  
 
 259  0
         workflowDocument.disapprove(annotation);
 260  0
     }
 261  
 
 262  
     /**
 263  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#route(org.kuali.rice.kew.rule.FlexDoc)
 264  
      */
 265  
     public void route(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 266  0
         if (LOG.isDebugEnabled()) {
 267  0
             LOG.debug("routing document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 268  
         }
 269  
 
 270  0
         handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ, KEWConstants.ACTION_REQUEST_APPROVE_REQ }));
 271  0
         workflowDocument.routeDocument(annotation);
 272  0
     }
 273  
 
 274  
     /**
 275  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#save(org.kuali.rice.kns.workflow.service.KualiWorkflowDocument, java.lang.String)
 276  
      */
 277  
     public void save(KualiWorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 278  0
         if (workflowDocument.isStandardSaveAllowed()) {
 279  0
         if (LOG.isDebugEnabled()) {
 280  0
             LOG.debug("saving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 281  
         }
 282  
 
 283  0
         workflowDocument.saveDocument(annotation);
 284  
     }
 285  
         else {
 286  0
             this.saveRoutingData(workflowDocument);
 287  
         }
 288  0
     }
 289  
 
 290  
     /**
 291  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#saveRoutingData(org.kuali.rice.kns.workflow.service.KualiWorkflowDocument)
 292  
      */
 293  
     public void saveRoutingData(KualiWorkflowDocument workflowDocument) throws WorkflowException {
 294  0
         if (LOG.isDebugEnabled()) {
 295  0
             LOG.debug("saving document(" + workflowDocument.getDocumentId() + ")");
 296  
         }
 297  
 
 298  0
         workflowDocument.saveRoutingData();
 299  0
     }
 300  
 
 301  
     /**
 302  
      * @see org.kuali.rice.kns.workflow.service.WorkflowDocumentService#getCurrentRouteLevelName(org.kuali.rice.kns.workflow.service.KualiWorkflowDocument)
 303  
      */
 304  
     public String getCurrentRouteLevelName(KualiWorkflowDocument workflowDocument) throws WorkflowException {
 305  0
         if (LOG.isDebugEnabled()) {
 306  0
             LOG.debug("getting current route level name for document(" + workflowDocument.getDocumentId());
 307  
         }
 308  
 //        return KEWServiceLocator.getRouteHeaderService().getRouteHeader(workflowDocument.getDocumentId()).getCurrentRouteLevelName();
 309  0
         KualiWorkflowDocument freshCopyWorkflowDoc = loadWorkflowDocument(workflowDocument.getDocumentId(), GlobalVariables.getUserSession().getPerson());
 310  0
         return freshCopyWorkflowDoc.getCurrentRouteNodeNames();
 311  
     }
 312  
 
 313  
     private void handleAdHocRouteRequests(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 314  0
             handleAdHocRouteRequests(workflowDocument, annotation, adHocRecipients, null);
 315  0
     }
 316  
     
 317  
     /**
 318  
      * Convenience method for generating ad hoc requests for a given document
 319  
      *
 320  
      * @param flexDoc
 321  
      * @param annotation
 322  
      * @param adHocRecipients
 323  
      * @throws InvalidActionTakenException
 324  
      * @throws InvalidRouteTypeException
 325  
      * @throws InvalidActionRequestException
 326  
      */
 327  
     private void handleAdHocRouteRequests(KualiWorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients, String notificationLabel) throws WorkflowException {
 328  
 
 329  0
         if (adHocRecipients != null && adHocRecipients.size() > 0) {
 330  0
             String currentNode = null;
 331  0
             String[] currentNodes = workflowDocument.getNodeNames();
 332  0
             if (currentNodes.length == 0) {
 333  0
                 WorkflowInfo workflowInfo = new WorkflowInfo();
 334  0
                 RouteNodeInstanceDTO[] nodes = workflowInfo.getTerminalNodeInstances(workflowDocument.getDocumentId());
 335  0
                 currentNodes = new String[nodes.length];
 336  0
                 for (int nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) {
 337  0
                     currentNodes[nodeIndex] = nodes[nodeIndex].getName();
 338  
                 }
 339  
             }
 340  
             // for now just pick a node and go with it...
 341  0
             for (int i = 0; i < currentNodes.length; i++) {
 342  0
                 currentNode = currentNodes[i];
 343  
             }
 344  
 
 345  
             
 346  0
             for (AdHocRouteRecipient recipient : adHocRecipients) {
 347  0
                 if (StringUtils.isNotEmpty(recipient.getId())) {
 348  0
                         String newAnnotation = annotation;
 349  0
                         if ( StringUtils.isBlank( annotation ) ) {
 350  
                                 try {
 351  0
                                         String message = KNSServiceLocator.getKualiConfigurationService().getPropertyString(RiceKeyConstants.MESSAGE_ADHOC_ANNOTATION);
 352  0
                                         newAnnotation = MessageFormat.format(message, GlobalVariables.getUserSession().getPrincipalName() );
 353  0
                                 } catch ( Exception ex ) {
 354  0
                                         LOG.warn("Unable to set annotation", ex );
 355  0
                                 }
 356  
                         }
 357  0
                     if (AdHocRouteRecipient.PERSON_TYPE.equals(recipient.getType())) {
 358  
                         // TODO make the 1 a constant
 359  0
                             Principal principal = KimApiServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(recipient.getId());
 360  0
                                 if (principal == null) {
 361  0
                                         throw new RiceRuntimeException("Could not locate principal with name '" + recipient.getId() + "'");
 362  
                                 }
 363  0
                         workflowDocument.adHocRouteDocumentToPrincipal(recipient.getActionRequested(), currentNode, newAnnotation, principal.getPrincipalId(), "", true, notificationLabel);
 364  0
                     }
 365  
                     else {
 366  0
                             Group group = KimApiServiceLocator.getIdentityManagementService().getGroup(recipient.getId());
 367  0
                                 if (group == null) {
 368  0
                                         throw new RiceRuntimeException("Could not locate group with id '" + recipient.getId() + "'");
 369  
                                 }
 370  0
                             workflowDocument.adHocRouteDocumentToGroup(recipient.getActionRequested(), currentNode, newAnnotation, group.getId() , "", true, notificationLabel);
 371  
                     }
 372  0
                 }
 373  
             }
 374  
         }
 375  0
     }
 376  
 
 377  
     /**
 378  
      * Convenience method to filter out any ad hoc recipients that should not be allowed given the action requested of the user that
 379  
      * is taking action on the document
 380  
      *
 381  
      * @param adHocRecipients
 382  
      */
 383  
     private List<AdHocRouteRecipient> filterAdHocRecipients(List<AdHocRouteRecipient> adHocRecipients, String[] validTypes) {
 384  
         // now filter out any but ack or fyi from the ad hoc list
 385  0
         List<AdHocRouteRecipient> realAdHocRecipients = new ArrayList<AdHocRouteRecipient>();
 386  0
         if (adHocRecipients != null) {
 387  0
             for (AdHocRouteRecipient proposedRecipient : adHocRecipients) {
 388  0
                 if (StringUtils.isNotBlank(proposedRecipient.getActionRequested())) {
 389  0
                     for (int i = 0; i < validTypes.length; i++) {
 390  0
                         if (validTypes[i].equals(proposedRecipient.getActionRequested())) {
 391  0
                             realAdHocRecipients.add(proposedRecipient);
 392  
                         }
 393  
                     }
 394  
                 }
 395  
             }
 396  
         }
 397  0
         return realAdHocRecipients;
 398  
     }
 399  
 
 400  
 
 401  
     public void setWorkflowInfoService(KualiWorkflowInfo workflowInfoService) {
 402  0
         this.workflowInfoService = workflowInfoService;
 403  0
     }
 404  
 
 405  
     public KualiWorkflowInfo getWorkflowInfoService() {
 406  0
         return workflowInfoService;
 407  
     }
 408  
 }