Coverage Report - org.kuali.rice.krad.workflow.service.impl.WorkflowDocumentServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
WorkflowDocumentServiceImpl
0%
0/148
0%
0/84
3.545
 
 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.krad.workflow.service.impl;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.commons.lang.time.StopWatch;
 20  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 21  
 import org.kuali.rice.core.api.util.RiceKeyConstants;
 22  
 import org.kuali.rice.kew.api.KewApiServiceLocator;
 23  
 import org.kuali.rice.kew.api.WorkflowDocument;
 24  
 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
 25  
 import org.kuali.rice.kew.api.action.ActionRequestType;
 26  
 import org.kuali.rice.kew.api.action.ActionType;
 27  
 import org.kuali.rice.kew.api.document.node.RouteNodeInstance;
 28  
 import org.kuali.rice.kew.exception.InvalidActionTakenException;
 29  
 import org.kuali.rice.kew.exception.WorkflowException;
 30  
 import org.kuali.rice.kew.util.KEWConstants;
 31  
 import org.kuali.rice.kim.api.group.Group;
 32  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 33  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 34  
 import org.kuali.rice.kim.bo.Person;
 35  
 import org.kuali.rice.krad.bo.AdHocRouteRecipient;
 36  
 import org.kuali.rice.krad.exception.UnknownDocumentIdException;
 37  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 38  
 import org.kuali.rice.krad.util.GlobalVariables;
 39  
 import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
 40  
 import org.springframework.transaction.annotation.Transactional;
 41  
 
 42  
 import java.text.MessageFormat;
 43  
 import java.util.ArrayList;
 44  
 import java.util.HashSet;
 45  
 import java.util.List;
 46  
 import java.util.Set;
 47  
 
 48  
 
 49  
 /**
 50  
  * This class is the implementation of the WorkflowDocumentService, which makes use of Workflow.
 51  
  */
 52  
 @Transactional
 53  0
 public class WorkflowDocumentServiceImpl implements WorkflowDocumentService {
 54  0
     private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(WorkflowDocumentServiceImpl.class);
 55  
 
 56  
     @Override
 57  
     public boolean workflowDocumentExists(String documentId) {
 58  0
         boolean exists = false;
 59  
 
 60  0
         if (StringUtils.isBlank(documentId)) {
 61  0
             throw new IllegalArgumentException("invalid (blank) documentId");
 62  
         }
 63  
 
 64  0
         exists = KewApiServiceLocator.getWorkflowDocumentService().doesDocumentExist(documentId);
 65  
 
 66  0
         return exists;
 67  
     }
 68  
 
 69  
     @Override
 70  
     public WorkflowDocument createWorkflowDocument(String documentTypeName, Person person) {
 71  0
         String watchName = "createWorkflowDocument";
 72  0
         StopWatch watch = new StopWatch();
 73  0
         watch.start();
 74  0
         if (LOG.isDebugEnabled()) {
 75  0
             LOG.debug(watchName + ": started");
 76  
         }
 77  0
         if (StringUtils.isBlank(documentTypeName)) {
 78  0
             throw new IllegalArgumentException("invalid (blank) documentTypeName");
 79  
         }
 80  0
         if (person == null) {
 81  0
             throw new IllegalArgumentException("invalid (null) person");
 82  
         }
 83  
 
 84  0
         if (StringUtils.isBlank(person.getPrincipalName())) {
 85  0
             throw new IllegalArgumentException("invalid (empty) PrincipalName");
 86  
         }
 87  
 
 88  0
         if (LOG.isDebugEnabled()) {
 89  0
             LOG.debug("creating workflowDoc(" + documentTypeName + "," + person.getPrincipalName() + ")");
 90  
         }
 91  
 
 92  0
         WorkflowDocument document = WorkflowDocumentFactory.createDocument(person.getPrincipalId(), documentTypeName);
 93  0
         watch.stop();
 94  0
         if (LOG.isDebugEnabled()) {
 95  0
             LOG.debug(watchName + ": " + watch.toString());        
 96  
         }
 97  
 
 98  0
         return document;
 99  
     }
 100  
 
 101  
     @Override
 102  
     public WorkflowDocument loadWorkflowDocument(String documentId, Person user) {
 103  0
         if (documentId == null) {
 104  0
             throw new IllegalArgumentException("invalid (null) documentHeaderId");
 105  
         }
 106  0
         if (user == null) {
 107  0
             throw new IllegalArgumentException("invalid (null) workflowUser");
 108  
         }
 109  0
         else if (StringUtils.isEmpty(user.getPrincipalName())) {
 110  0
             throw new IllegalArgumentException("invalid (empty) workflowUser");
 111  
         }
 112  
 
 113  0
         if (LOG.isDebugEnabled()) {
 114  0
             LOG.debug("retrieving document(" + documentId + "," + user.getPrincipalName() + ")");
 115  
         }
 116  
         
 117  
         try {
 118  0
             return WorkflowDocumentFactory.loadDocument(user.getPrincipalId(), documentId);
 119  0
         } catch (IllegalArgumentException e) {
 120  
             // TODO do we really need to do this or just let the IllegalArgument propogate?
 121  0
             throw new UnknownDocumentIdException("unable to locate document with documentHeaderId '" + documentId + "'");
 122  
         }
 123  
     }
 124  
 
 125  
     @Override
 126  
     public void acknowledge(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 127  0
         if (LOG.isDebugEnabled()) {
 128  0
             LOG.debug("acknowleding document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 129  
         }
 130  
 
 131  0
         handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ }));
 132  0
         workflowDocument.acknowledge(annotation);
 133  0
     }
 134  
 
 135  
     @Override
 136  
     public void approve(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 137  0
         if (LOG.isDebugEnabled()) {
 138  0
             LOG.debug("approving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 139  
         }
 140  
 
 141  0
         handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ, KEWConstants.ACTION_REQUEST_APPROVE_REQ }));
 142  0
         workflowDocument.approve(annotation);
 143  0
     }
 144  
 
 145  
 
 146  
     @Override
 147  
     public void superUserApprove(WorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 148  0
             if ( LOG.isInfoEnabled() ) {
 149  0
                     LOG.info("super user approve document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 150  
             }
 151  0
         workflowDocument.superUserBlanketApprove(annotation);
 152  0
     }
 153  
 
 154  
     @Override
 155  
     public void superUserCancel(WorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 156  0
         LOG.info("super user cancel document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 157  0
         workflowDocument.superUserCancel(annotation);
 158  0
     }
 159  
 
 160  
     @Override
 161  
     public void superUserDisapprove(WorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 162  0
             if ( LOG.isInfoEnabled() ) {
 163  0
                     LOG.info("super user disapprove document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 164  
             }
 165  0
         workflowDocument.superUserDisapprove(annotation);
 166  0
     }
 167  
 
 168  
     @Override
 169  
     public void blanketApprove(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 170  0
         if (LOG.isDebugEnabled()) {
 171  0
             LOG.debug("blanket approving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 172  
         }
 173  
 
 174  0
         handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ }));
 175  0
         workflowDocument.blanketApprove(annotation);
 176  0
     }
 177  
 
 178  
     @Override
 179  
     public void cancel(WorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 180  0
         if (LOG.isDebugEnabled()) {
 181  0
             LOG.debug("canceling document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 182  
         }
 183  
 
 184  0
         workflowDocument.cancel(annotation);
 185  0
     }
 186  
 
 187  
     @Override
 188  
     public void clearFyi(WorkflowDocument workflowDocument, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 189  0
         if (LOG.isDebugEnabled()) {
 190  0
             LOG.debug("clearing FYI for document(" + workflowDocument.getDocumentId() + ")");
 191  
         }
 192  
 
 193  0
         handleAdHocRouteRequests(workflowDocument, "", filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_FYI_REQ }));
 194  0
         workflowDocument.fyi();
 195  0
     }
 196  
 
 197  
     @Override
 198  
     public void sendWorkflowNotification(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 199  0
             sendWorkflowNotification(workflowDocument, annotation, adHocRecipients, null);
 200  0
     }
 201  
     
 202  
     @Override
 203  
     public void sendWorkflowNotification(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients, String notificationLabel) throws WorkflowException {
 204  0
         if (LOG.isDebugEnabled()) {
 205  0
             LOG.debug("sending FYI for document(" + workflowDocument.getDocumentId() + ")");
 206  
         }
 207  
 
 208  0
         handleAdHocRouteRequests(workflowDocument, annotation, adHocRecipients, notificationLabel);
 209  0
     }
 210  
 
 211  
     @Override
 212  
     public void disapprove(WorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 213  0
         if (LOG.isDebugEnabled()) {
 214  0
             LOG.debug("disapproving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 215  
         }
 216  
 
 217  0
         workflowDocument.disapprove(annotation);
 218  0
     }
 219  
 
 220  
     @Override
 221  
     public void route(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 222  0
         if (LOG.isDebugEnabled()) {
 223  0
             LOG.debug("routing document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 224  
         }
 225  
 
 226  0
         handleAdHocRouteRequests(workflowDocument, annotation, filterAdHocRecipients(adHocRecipients, new String[] { KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, KEWConstants.ACTION_REQUEST_FYI_REQ, KEWConstants.ACTION_REQUEST_APPROVE_REQ }));
 227  0
         workflowDocument.route(annotation);
 228  0
     }
 229  
 
 230  
     @Override
 231  
     public void save(WorkflowDocument workflowDocument, String annotation) throws WorkflowException {
 232  0
         if (workflowDocument.isValidAction(ActionType.SAVE)) {
 233  0
         if (LOG.isDebugEnabled()) {
 234  0
             LOG.debug("saving document(" + workflowDocument.getDocumentId() + ",'" + annotation + "')");
 235  
         }
 236  
 
 237  0
         workflowDocument.saveDocument(annotation);
 238  
     }
 239  
         else {
 240  0
             this.saveRoutingData(workflowDocument);
 241  
         }
 242  0
     }
 243  
 
 244  
     @Override
 245  
     public void saveRoutingData(WorkflowDocument workflowDocument) throws WorkflowException {
 246  0
         if (LOG.isDebugEnabled()) {
 247  0
             LOG.debug("saving document(" + workflowDocument.getDocumentId() + ")");
 248  
         }
 249  
 
 250  0
         workflowDocument.saveDocumentData();
 251  0
     }
 252  
 
 253  
     @Override
 254  
     public String getCurrentRouteLevelName(WorkflowDocument workflowDocument) throws WorkflowException {
 255  0
         if (LOG.isDebugEnabled()) {
 256  0
             LOG.debug("getting current route level name for document(" + workflowDocument.getDocumentId());
 257  
         }
 258  
 //        return KEWServiceLocator.getRouteHeaderService().getRouteHeader(workflowDocument.getDocumentId()).getCurrentRouteLevelName();
 259  0
         WorkflowDocument freshCopyWorkflowDoc = loadWorkflowDocument(workflowDocument.getDocumentId(), GlobalVariables.getUserSession().getPerson());
 260  0
         return getCurrentRouteNodeNames(freshCopyWorkflowDoc);
 261  
     }
 262  
     
 263  
     
 264  
 
 265  
     @Override
 266  
     public String getCurrentRouteNodeNames(WorkflowDocument workflowDocument) {
 267  0
         Set<String> nodeNames = workflowDocument.getNodeNames();
 268  0
         if (nodeNames.isEmpty()) {
 269  0
             return "";
 270  
         }
 271  0
         StringBuilder builder = new StringBuilder();
 272  0
         for (String nodeName : nodeNames) {
 273  0
             builder.append(nodeName).append(", ");
 274  
         }
 275  0
         builder.setLength(builder.length() - 2);
 276  0
         return builder.toString();
 277  
     }
 278  
 
 279  
     private void handleAdHocRouteRequests(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients) throws WorkflowException {
 280  0
             handleAdHocRouteRequests(workflowDocument, annotation, adHocRecipients, null);
 281  0
     }
 282  
     
 283  
     /**
 284  
      * Convenience method for generating ad hoc requests for a given document
 285  
      *
 286  
      * @param flexDoc
 287  
      * @param annotation
 288  
      * @param adHocRecipients
 289  
      * @throws InvalidActionTakenException
 290  
      * @throws InvalidRouteTypeException
 291  
      * @throws InvalidActionRequestException
 292  
      */
 293  
     private void handleAdHocRouteRequests(WorkflowDocument workflowDocument, String annotation, List<AdHocRouteRecipient> adHocRecipients, String notificationLabel) throws WorkflowException {
 294  
 
 295  0
         if (adHocRecipients != null && adHocRecipients.size() > 0) {
 296  0
             String currentNode = null;
 297  0
             Set<String> currentNodes = workflowDocument.getNodeNames();
 298  0
             if (currentNodes.isEmpty()) {
 299  0
                 List<RouteNodeInstance> nodes = KewApiServiceLocator.getWorkflowDocumentService().getTerminalNodeInstances(workflowDocument.getDocumentId());
 300  0
                 currentNodes = new HashSet<String>();
 301  0
                 for (RouteNodeInstance node : nodes) {
 302  0
                     currentNodes.add(node.getName());
 303  
                 }
 304  
             }
 305  
             // for now just pick a node and go with it...
 306  0
             currentNode = currentNodes.iterator().next();
 307  
             
 308  0
             for (AdHocRouteRecipient recipient : adHocRecipients) {
 309  0
                 if (StringUtils.isNotEmpty(recipient.getId())) {
 310  0
                         String newAnnotation = annotation;
 311  0
                         if ( StringUtils.isBlank( annotation ) ) {
 312  
                                 try {
 313  0
                                         String message = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
 314  
                                     RiceKeyConstants.MESSAGE_ADHOC_ANNOTATION);
 315  0
                                         newAnnotation = MessageFormat.format(message, GlobalVariables.getUserSession().getPrincipalName() );
 316  0
                                 } catch ( Exception ex ) {
 317  0
                                         LOG.warn("Unable to set annotation", ex );
 318  0
                                 }
 319  
                         }
 320  0
                     if (AdHocRouteRecipient.PERSON_TYPE.equals(recipient.getType())) {
 321  
                         // TODO make the 1 a constant
 322  0
                             Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(recipient.getId());
 323  0
                                 if (principal == null) {
 324  0
                                         throw new RiceRuntimeException("Could not locate principal with name '" + recipient.getId() + "'");
 325  
                                 }
 326  0
                         workflowDocument.adHocToPrincipal(ActionRequestType.fromCode(recipient.getActionRequested()), currentNode, newAnnotation, principal.getPrincipalId(), "", true, notificationLabel);
 327  0
                     }
 328  
                     else {
 329  0
                             Group group = KimApiServiceLocator.getGroupService().getGroup(recipient.getId());
 330  0
                                 if (group == null) {
 331  0
                                         throw new RiceRuntimeException("Could not locate group with id '" + recipient.getId() + "'");
 332  
                                 }
 333  0
                             workflowDocument.adHocToGroup(ActionRequestType.fromCode(recipient.getActionRequested()), currentNode, newAnnotation, group.getId() , "", true, notificationLabel);
 334  
                     }
 335  0
                 }
 336  
             }
 337  
         }
 338  0
     }
 339  
 
 340  
     /**
 341  
      * Convenience method to filter out any ad hoc recipients that should not be allowed given the action requested of the user that
 342  
      * is taking action on the document
 343  
      *
 344  
      * @param adHocRecipients
 345  
      */
 346  
     private List<AdHocRouteRecipient> filterAdHocRecipients(List<AdHocRouteRecipient> adHocRecipients, String[] validTypes) {
 347  
         // now filter out any but ack or fyi from the ad hoc list
 348  0
         List<AdHocRouteRecipient> realAdHocRecipients = new ArrayList<AdHocRouteRecipient>();
 349  0
         if (adHocRecipients != null) {
 350  0
             for (AdHocRouteRecipient proposedRecipient : adHocRecipients) {
 351  0
                 if (StringUtils.isNotBlank(proposedRecipient.getActionRequested())) {
 352  0
                     for (int i = 0; i < validTypes.length; i++) {
 353  0
                         if (validTypes[i].equals(proposedRecipient.getActionRequested())) {
 354  0
                             realAdHocRecipients.add(proposedRecipient);
 355  
                         }
 356  
                     }
 357  
                 }
 358  
             }
 359  
         }
 360  0
         return realAdHocRecipients;
 361  
     }
 362  
 
 363  
 }