Coverage Report - org.kuali.rice.krad.maintenance.MaintenanceUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MaintenanceUtils
0%
0/64
0%
0/30
6.8
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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.maintenance;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.util.RiceKeyConstants;
 20  
 import org.kuali.rice.kew.api.WorkflowDocument;
 21  
 import org.kuali.rice.krad.document.MaintenanceDocument;
 22  
 import org.kuali.rice.krad.exception.KualiExceptionIncident;
 23  
 import org.kuali.rice.krad.exception.ValidationException;
 24  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 25  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 26  
 import org.kuali.rice.krad.util.GlobalVariables;
 27  
 import org.kuali.rice.krad.util.KRADConstants;
 28  
 import org.kuali.rice.krad.util.UrlFactory;
 29  
 
 30  
 import java.util.HashMap;
 31  
 import java.util.Map;
 32  
 import java.util.Properties;
 33  
 
 34  
 /**
 35  
  * Provides static utility methods for use within the maintenance framework
 36  
  *
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  */
 39  0
 public class MaintenanceUtils {
 40  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MaintenanceUtils.class);
 41  
 
 42  
     /**
 43  
      * Determines if there is another maintenance document that has a lock on the same key as the given document, and
 44  
      * therefore will block the maintenance document from being submitted
 45  
      *
 46  
      * @param document - maintenance document instance to check locking for
 47  
      * @param throwExceptionIfLocked - indicates if an exception should be thrown in the case of found locking document,
 48  
      * if false only an error will be added
 49  
      */
 50  
     public static void checkForLockingDocument(MaintenanceDocument document, boolean throwExceptionIfLocked) {
 51  0
         LOG.info("starting checkForLockingDocument (by MaintenanceDocument)");
 52  
 
 53  
         // get the docHeaderId of the blocking docs, if any are locked and blocking
 54  
         //String blockingDocId = getMaintenanceDocumentService().getLockingDocumentId(document);
 55  0
         String blockingDocId = document.getNewMaintainableObject().getLockingDocumentId();
 56  0
         checkDocumentBlockingDocumentId(blockingDocId, throwExceptionIfLocked);
 57  0
     }
 58  
 
 59  
     public static void checkDocumentBlockingDocumentId(String blockingDocId, boolean throwExceptionIfLocked) {
 60  
         // if we got nothing, then no docs are blocking, and we're done
 61  0
         if (StringUtils.isBlank(blockingDocId)) {
 62  0
             return;
 63  
         }
 64  
 
 65  0
         if (MaintenanceUtils.LOG.isInfoEnabled()) {
 66  0
             MaintenanceUtils.LOG.info("Locking document found:  docId = " + blockingDocId + ".");
 67  
         }
 68  
 
 69  
         // load the blocking locked document
 70  0
         WorkflowDocument lockedDocument = null;
 71  
         try {
 72  
             // need to perform this check to prevent an exception from being thrown by the
 73  
             // createWorkflowDocument call - the throw itself causes transaction rollback problems to
 74  
             // occur, even though the exception would be caught here
 75  0
             if (KRADServiceLocatorWeb.getWorkflowDocumentService().workflowDocumentExists(blockingDocId)) {
 76  0
                 lockedDocument = KRADServiceLocatorWeb.getWorkflowDocumentService()
 77  
                         .loadWorkflowDocument(blockingDocId, GlobalVariables.getUserSession().getPerson());
 78  
             }
 79  0
         } catch (Exception ex) {
 80  
             // clean up the lock and notify the admins
 81  0
             MaintenanceUtils.LOG.error("Unable to retrieve locking document specified in the maintenance lock table: " +
 82  
                     blockingDocId, ex);
 83  
 
 84  0
             cleanOrphanLocks(blockingDocId, ex);
 85  0
             return;
 86  0
         }
 87  0
         if (lockedDocument == null) {
 88  0
             MaintenanceUtils.LOG.warn("Locking document header for " + blockingDocId + "came back null.");
 89  0
             cleanOrphanLocks(blockingDocId, null);
 90  
         }
 91  
 
 92  
         // if we can ignore the lock (see method notes), then exit cause we're done
 93  0
         if (lockCanBeIgnored(lockedDocument)) {
 94  0
             return;
 95  
         }
 96  
 
 97  
         // build the link URL for the blocking document
 98  0
         Properties parameters = new Properties();
 99  0
         parameters.put(KRADConstants.PARAMETER_DOC_ID, blockingDocId);
 100  0
         parameters.put(KRADConstants.PARAMETER_COMMAND, KRADConstants.METHOD_DISPLAY_DOC_SEARCH_VIEW);
 101  0
         String blockingUrl = UrlFactory.parameterizeUrl(
 102  
                 KRADServiceLocator.getKualiConfigurationService().getPropertyString(KRADConstants.WORKFLOW_URL_KEY) +
 103  
                         "/" + KRADConstants.DOC_HANDLER_ACTION, parameters);
 104  0
         if (MaintenanceUtils.LOG.isDebugEnabled()) {
 105  0
             MaintenanceUtils.LOG.debug("blockingUrl = '" + blockingUrl + "'");
 106  0
             MaintenanceUtils.LOG.debug("Maintenance record: " + lockedDocument.getApplicationDocumentId() + "is locked.");
 107  
         }
 108  0
         String[] errorParameters = {blockingUrl, blockingDocId};
 109  
 
 110  
         // If specified, add an error to the ErrorMap and throw an exception; otherwise, just add a warning to the ErrorMap instead.
 111  0
         if (throwExceptionIfLocked) {
 112  
             // post an error about the locked document
 113  0
             GlobalVariables.getMessageMap()
 114  
                     .putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_MAINTENANCE_LOCKED, errorParameters);
 115  0
             throw new ValidationException("Maintenance Record is locked by another document.");
 116  
         } else {
 117  
             // Post a warning about the locked document.
 118  0
             GlobalVariables.getMessageMap()
 119  
                     .putWarning(KRADConstants.GLOBAL_MESSAGES, RiceKeyConstants.WARNING_MAINTENANCE_LOCKED,
 120  
                             errorParameters);
 121  
         }
 122  0
     }
 123  
 
 124  
     /**
 125  
      * Guesses whether the current user should be allowed to change a document even though it is locked. It
 126  
      * probably should use Authorization instead? See KULNRVSYS-948
 127  
      *
 128  
      * @param lockedDocument
 129  
      * @return
 130  
      * @throws org.kuali.rice.kew.exception.WorkflowException
 131  
      *
 132  
      */
 133  
     private static boolean lockCanBeIgnored(WorkflowDocument lockedDocument) {
 134  
         // TODO: implement real authorization for Maintenance Document Save/Route - KULNRVSYS-948
 135  0
         if (lockedDocument == null) {
 136  0
             return true;
 137  
         }
 138  
 
 139  
         // get the user-id. if no user-id, then we can do this test, so exit
 140  0
         String userId = GlobalVariables.getUserSession().getPrincipalId().trim();
 141  0
         if (StringUtils.isBlank(userId)) {
 142  0
             return false; // dont bypass locking
 143  
         }
 144  
 
 145  
         // if the current user is not the initiator of the blocking document
 146  0
         if (!userId.equalsIgnoreCase(lockedDocument.getInitiatorPrincipalId().trim())) {
 147  0
             return false;
 148  
         }
 149  
 
 150  
         // if the blocking document hasn't been routed, we can ignore it
 151  0
         return lockedDocument.isInitiated();
 152  
     }
 153  
 
 154  
     protected static void cleanOrphanLocks(String lockingDocumentNumber, Exception workflowException) {
 155  
         // put a try/catch around the whole thing - the whole reason we are doing this is to prevent data errors
 156  
         // from stopping a document
 157  
         try {
 158  
             // delete the locks for this document since it does not seem to exist
 159  0
             KRADServiceLocatorWeb.getMaintenanceDocumentService().deleteLocks(lockingDocumentNumber);
 160  
             // notify the incident list
 161  0
             Map<String, String> parameters = new HashMap<String, String>(1);
 162  0
             parameters.put(KRADConstants.PARAMETER_DOC_ID, lockingDocumentNumber);
 163  0
             KualiExceptionIncident kei = KRADServiceLocatorWeb.getKualiExceptionIncidentService()
 164  
                     .getExceptionIncident(workflowException, parameters);
 165  0
             KRADServiceLocatorWeb.getKualiExceptionIncidentService().report(kei);
 166  0
         } catch (Exception ex) {
 167  0
             MaintenanceUtils.LOG.error("Unable to delete and notify upon locking document retrieval failure.", ex);
 168  0
         }
 169  0
     }
 170  
 
 171  
     public static boolean isMaintenanceDocumentCreatingNewRecord(String maintenanceAction) {
 172  0
         if (KRADConstants.MAINTENANCE_EDIT_ACTION.equalsIgnoreCase(maintenanceAction)) {
 173  0
             return false;
 174  0
         } else if (KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION.equalsIgnoreCase(maintenanceAction)) {
 175  0
             return false;
 176  0
         } else if (KRADConstants.MAINTENANCE_DELETE_ACTION.equalsIgnoreCase(maintenanceAction)) {
 177  0
             return false;
 178  0
         } else if (KRADConstants.MAINTENANCE_NEW_ACTION.equalsIgnoreCase(maintenanceAction)) {
 179  0
             return true;
 180  0
         } else if (KRADConstants.MAINTENANCE_COPY_ACTION.equalsIgnoreCase(maintenanceAction)) {
 181  0
             return true;
 182  
         } else {
 183  0
             return true;
 184  
         }
 185  
     }
 186  
 }