Coverage Report - org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase
 
Classes in this File Line Coverage Branch Coverage Complexity
MaintenanceDocumentRuleBase
0%
0/356
0%
0/170
2.392
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 17  
 package org.kuali.rice.krad.rules;
 18  
 
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.core.api.config.property.ConfigurationService;
 21  
 import org.kuali.rice.core.api.datetime.DateTimeService;
 22  
 import org.kuali.rice.core.util.RiceKeyConstants;
 23  
 import org.kuali.rice.core.web.format.Formatter;
 24  
 import org.kuali.rice.kew.api.WorkflowDocument;
 25  
 import org.kuali.rice.kim.api.role.RoleService;
 26  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 27  
 import org.kuali.rice.krad.bo.GlobalBusinessObject;
 28  
 import org.kuali.rice.core.api.CoreApiServiceLocator;
 29  
 import org.kuali.rice.krad.bo.Inactivatable;
 30  
 import org.kuali.rice.krad.bo.PersistableBusinessObject;
 31  
 import org.kuali.rice.krad.datadictionary.InactivationBlockingMetadata;
 32  
 import org.kuali.rice.krad.document.Document;
 33  
 import org.kuali.rice.krad.document.MaintenanceDocument;
 34  
 import org.kuali.rice.krad.document.authorization.MaintenanceDocumentAuthorizer;
 35  
 import org.kuali.rice.krad.exception.ValidationException;
 36  
 import org.kuali.rice.krad.maintenance.Maintainable;
 37  
 import org.kuali.rice.krad.rule.event.ApproveDocumentEvent;
 38  
 import org.kuali.rice.krad.service.BusinessObjectService;
 39  
 import org.kuali.rice.krad.service.DataDictionaryService;
 40  
 import org.kuali.rice.krad.service.DataObjectAuthorizationService;
 41  
 import org.kuali.rice.krad.service.DataObjectMetaDataService;
 42  
 import org.kuali.rice.krad.service.DictionaryValidationService;
 43  
 import org.kuali.rice.krad.service.DocumentDictionaryService;
 44  
 import org.kuali.rice.krad.service.DocumentHelperService;
 45  
 import org.kuali.rice.krad.service.InactivationBlockingDetectionService;
 46  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 47  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 48  
 import org.kuali.rice.krad.service.PersistenceStructureService;
 49  
 import org.kuali.rice.krad.util.ErrorMessage;
 50  
 import org.kuali.rice.krad.util.ForeignKeyFieldsPopulationState;
 51  
 import org.kuali.rice.krad.util.GlobalVariables;
 52  
 import org.kuali.rice.krad.util.KRADConstants;
 53  
 import org.kuali.rice.krad.util.KRADPropertyConstants;
 54  
 import org.kuali.rice.krad.util.ObjectUtils;
 55  
 import org.kuali.rice.krad.util.UrlFactory;
 56  
 import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
 57  
 import org.springframework.util.AutoPopulatingList;
 58  
 
 59  
 import java.security.GeneralSecurityException;
 60  
 import java.util.ArrayList;
 61  
 import java.util.Iterator;
 62  
 import java.util.List;
 63  
 import java.util.Map;
 64  
 import java.util.Properties;
 65  
 import java.util.Set;
 66  
 
 67  
 /**
 68  
  * Contains all of the business rules that are common to all maintenance documents
 69  
  *
 70  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 71  
  */
 72  
 public class MaintenanceDocumentRuleBase extends DocumentRuleBase implements MaintenanceDocumentRule {
 73  0
     protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MaintenanceDocumentRuleBase.class);
 74  
 
 75  
     // these two constants are used to correctly prefix errors added to
 76  
     // the global errors
 77  
     public static final String MAINTAINABLE_ERROR_PREFIX = KRADConstants.MAINTENANCE_NEW_MAINTAINABLE;
 78  
     public static final String DOCUMENT_ERROR_PREFIX = "document.";
 79  
     public static final String MAINTAINABLE_ERROR_PATH = DOCUMENT_ERROR_PREFIX + "newMaintainableObject";
 80  
 
 81  
     private PersistenceStructureService persistenceStructureService;
 82  
     private DataDictionaryService ddService;
 83  
     private DocumentHelperService documentHelperService;
 84  
     private BusinessObjectService boService;
 85  
     private DictionaryValidationService dictionaryValidationService;
 86  
     private ConfigurationService configService;
 87  
     private WorkflowDocumentService workflowDocumentService;
 88  
     private org.kuali.rice.kim.service.PersonService personService;
 89  
     private RoleService roleService;
 90  
     private DataObjectMetaDataService dataObjectMetaDataService;
 91  
     private DataObjectAuthorizationService dataObjectAuthorizationService;
 92  
     private DocumentDictionaryService documentDictionaryService;
 93  
 
 94  
     private Object oldDataObject;
 95  
     private Object newDataObject;
 96  
     private Class dataObjectClass;
 97  
 
 98  
     protected List priorErrorPath;
 99  
 
 100  
     /**
 101  
      * Default constructor a MaintenanceDocumentRuleBase.java.
 102  
      */
 103  0
     public MaintenanceDocumentRuleBase() {
 104  0
         priorErrorPath = new ArrayList();
 105  0
     }
 106  
 
 107  
     /**
 108  
      * @see org.kuali.rice.krad.maintenance.rules.MaintenanceDocumentRule#processSaveDocument(org.kuali.rice.krad.document.Document)
 109  
      */
 110  
     @Override
 111  
     public boolean processSaveDocument(Document document) {
 112  0
         MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
 113  
 
 114  
         // remove all items from the errorPath temporarily (because it may not
 115  
         // be what we expect, or what we need)
 116  0
         clearErrorPath();
 117  
 
 118  
         // setup convenience pointers to the old & new bo
 119  0
         setupBaseConvenienceObjects(maintenanceDocument);
 120  
 
 121  
         // the document must be in a valid state for saving. this does not include business
 122  
         // rules, but just enough testing that the document is populated and in a valid state
 123  
         // to not cause exceptions when saved. if this passes, then the save will always occur,
 124  
         // regardless of business rules.
 125  0
         if (!isDocumentValidForSave(maintenanceDocument)) {
 126  0
             resumeErrorPath();
 127  0
             return false;
 128  
         }
 129  
 
 130  
         // apply rules that are specific to the class of the maintenance document
 131  
         // (if implemented). this will always succeed if not overloaded by the
 132  
         // subclass
 133  0
         processCustomSaveDocumentBusinessRules(maintenanceDocument);
 134  
 
 135  
         // return the original set of items to the errorPath
 136  0
         resumeErrorPath();
 137  
 
 138  
         // return the original set of items to the errorPath, to ensure no impact
 139  
         // on other upstream or downstream items that rely on the errorPath
 140  0
         return true;
 141  
     }
 142  
 
 143  
     /**
 144  
      * @see org.kuali.rice.krad.maintenance.rules.MaintenanceDocumentRule#processRouteDocument(org.kuali.rice.krad.document.Document)
 145  
      */
 146  
     @Override
 147  
     public boolean processRouteDocument(Document document) {
 148  0
         LOG.info("processRouteDocument called");
 149  
 
 150  0
         MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
 151  
 
 152  
         // get the documentAuthorizer for this document
 153  0
         MaintenanceDocumentAuthorizer documentAuthorizer =
 154  
                 (MaintenanceDocumentAuthorizer) getDocumentHelperService().getDocumentAuthorizer(document);
 155  
 
 156  
         // remove all items from the errorPath temporarily (because it may not
 157  
         // be what we expect, or what we need)
 158  0
         clearErrorPath();
 159  
 
 160  
         // setup convenience pointers to the old & new bo
 161  0
         setupBaseConvenienceObjects(maintenanceDocument);
 162  
 
 163  
         // apply rules that are common across all maintenance documents, regardless of class
 164  0
         processGlobalSaveDocumentBusinessRules(maintenanceDocument);
 165  
 
 166  
         // from here on, it is in a default-success mode, and will route unless one of the
 167  
         // business rules stop it.
 168  0
         boolean success = true;
 169  
 
 170  0
         WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
 171  0
         if (workflowDocument.isInitiated() || workflowDocument.isSaved()){
 172  0
                 success &= documentAuthorizer.canCreateOrMaintain((MaintenanceDocument)document, GlobalVariables.getUserSession().getPerson());
 173  0
             if (success == false) {
 174  0
                 GlobalVariables.getMessageMap()
 175  
                         .putError(KRADConstants.DOCUMENT_ERRORS, RiceKeyConstants.AUTHORIZATION_ERROR_DOCUMENT,
 176  
                                 new String[]{GlobalVariables.getUserSession().getPerson().getPrincipalName(),
 177  
                                         "Create/Maintain", getDocumentDictionaryService()
 178  
                                         .getMaintenanceDocumentTypeName(newDataObject.getClass())});
 179  
             }
 180  
         }
 181  
         // apply rules that are common across all maintenance documents, regardless of class
 182  0
         success &= processGlobalRouteDocumentBusinessRules(maintenanceDocument);
 183  
 
 184  
         // apply rules that are specific to the class of the maintenance document
 185  
         // (if implemented). this will always succeed if not overloaded by the
 186  
         // subclass
 187  0
         success &= processCustomRouteDocumentBusinessRules(maintenanceDocument);
 188  
 
 189  0
         success &= processInactivationBlockChecking(maintenanceDocument);
 190  
 
 191  
         // return the original set of items to the errorPath, to ensure no impact
 192  
         // on other upstream or downstream items that rely on the errorPath
 193  0
         resumeErrorPath();
 194  
 
 195  0
         return success;
 196  
     }
 197  
 
 198  
     /**
 199  
      * Determines whether a document is inactivating the record being maintained
 200  
      *
 201  
      * @param maintenanceDocument
 202  
      * @return true iff the document is inactivating the business object; false otherwise
 203  
      */
 204  
     protected boolean isDocumentInactivatingBusinessObject(MaintenanceDocument maintenanceDocument) {
 205  0
         if (maintenanceDocument.isEdit()) {
 206  0
             Class dataObjectClass = maintenanceDocument.getNewMaintainableObject().getDataObjectClass();
 207  
             // we can only be inactivating a business object if we're editing it
 208  0
             if (dataObjectClass != null && Inactivatable.class.isAssignableFrom(dataObjectClass)) {
 209  0
                 Inactivatable oldInactivateableBO = (Inactivatable) oldDataObject;
 210  0
                 Inactivatable newInactivateableBO = (Inactivatable) newDataObject;
 211  
 
 212  0
                 return oldInactivateableBO.isActive() && !newInactivateableBO.isActive();
 213  
             }
 214  
         }
 215  0
         return false;
 216  
     }
 217  
 
 218  
     /**
 219  
      * Determines whether this document has been inactivation blocked
 220  
      *
 221  
      * @param maintenanceDocument
 222  
      * @return true iff there is NOTHING that blocks this record
 223  
      */
 224  
     protected boolean processInactivationBlockChecking(MaintenanceDocument maintenanceDocument) {
 225  0
         if (isDocumentInactivatingBusinessObject(maintenanceDocument)) {
 226  0
             Class dataObjectClass = maintenanceDocument.getNewMaintainableObject().getDataObjectClass();
 227  0
             Set<InactivationBlockingMetadata> inactivationBlockingMetadatas =
 228  
                     ddService.getAllInactivationBlockingDefinitions(dataObjectClass);
 229  
 
 230  0
             if (inactivationBlockingMetadatas != null) {
 231  0
                 for (InactivationBlockingMetadata inactivationBlockingMetadata : inactivationBlockingMetadatas) {
 232  
                     // for the purposes of maint doc validation, we only need to look for the first blocking record
 233  
 
 234  
                     // we found a blocking record, so we return false
 235  0
                     if (!processInactivationBlockChecking(maintenanceDocument, inactivationBlockingMetadata)) {
 236  0
                         return false;
 237  
                     }
 238  
                 }
 239  
             }
 240  
         }
 241  0
         return true;
 242  
     }
 243  
 
 244  
     /**
 245  
      * Given a InactivationBlockingMetadata, which represents a relationship that may block inactivation of a BO, it
 246  
      * determines whether there
 247  
      * is a record that violates the blocking definition
 248  
      *
 249  
      * @param maintenanceDocument
 250  
      * @param inactivationBlockingMetadata
 251  
      * @return true iff, based on the InactivationBlockingMetadata, the maintenance document should be allowed to route
 252  
      */
 253  
     protected boolean processInactivationBlockChecking(MaintenanceDocument maintenanceDocument,
 254  
             InactivationBlockingMetadata inactivationBlockingMetadata) {
 255  0
         if (newDataObject instanceof PersistableBusinessObject) {
 256  0
             String inactivationBlockingDetectionServiceBeanName =
 257  
                     inactivationBlockingMetadata.getInactivationBlockingDetectionServiceBeanName();
 258  0
             if (StringUtils.isBlank(inactivationBlockingDetectionServiceBeanName)) {
 259  0
                 inactivationBlockingDetectionServiceBeanName =
 260  
                         KRADServiceLocatorWeb.DEFAULT_INACTIVATION_BLOCKING_DETECTION_SERVICE;
 261  
             }
 262  0
             InactivationBlockingDetectionService inactivationBlockingDetectionService = KRADServiceLocatorWeb
 263  
                     .getInactivationBlockingDetectionService(inactivationBlockingDetectionServiceBeanName);
 264  
 
 265  0
             boolean foundBlockingRecord = inactivationBlockingDetectionService
 266  
                     .hasABlockingRecord((PersistableBusinessObject) newDataObject, inactivationBlockingMetadata);
 267  
 
 268  0
             if (foundBlockingRecord) {
 269  0
                 putInactivationBlockingErrorOnPage(maintenanceDocument, inactivationBlockingMetadata);
 270  
             }
 271  
 
 272  0
             return !foundBlockingRecord;
 273  
         }
 274  
 
 275  0
         return true;
 276  
     }
 277  
 
 278  
     /**
 279  
      * If there is a violation of an InactivationBlockingMetadata, it prints out an appropriate error into the error
 280  
      * map
 281  
      *
 282  
      * @param document
 283  
      * @param inactivationBlockingMetadata
 284  
      */
 285  
     protected void putInactivationBlockingErrorOnPage(MaintenanceDocument document,
 286  
             InactivationBlockingMetadata inactivationBlockingMetadata) {
 287  0
         if (!persistenceStructureService.hasPrimaryKeyFieldValues(newDataObject)) {
 288  0
             throw new RuntimeException("Maintenance document did not have all primary key values filled in.");
 289  
         }
 290  0
         Properties parameters = new Properties();
 291  0
         parameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE,
 292  
                 inactivationBlockingMetadata.getBlockedBusinessObjectClass().getName());
 293  0
         parameters
 294  
                 .put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.METHOD_DISPLAY_ALL_INACTIVATION_BLOCKERS);
 295  
 
 296  0
         List keys = new ArrayList();
 297  0
         if (getPersistenceStructureService().isPersistable(newDataObject.getClass())) {
 298  0
             keys = getPersistenceStructureService().listPrimaryKeyFieldNames(newDataObject.getClass());
 299  
         }
 300  
 
 301  
         // build key value url parameters used to retrieve the business object
 302  0
         String keyName = null;
 303  0
         for (Iterator iter = keys.iterator(); iter.hasNext(); ) {
 304  0
             keyName = (String) iter.next();
 305  
 
 306  0
             Object keyValue = null;
 307  0
             if (keyName != null) {
 308  0
                 keyValue = ObjectUtils.getPropertyValue(newDataObject, keyName);
 309  
             }
 310  
 
 311  0
             if (keyValue == null) {
 312  0
                 keyValue = "";
 313  0
             } else if (keyValue instanceof java.sql.Date) { //format the date for passing in url
 314  0
                 if (Formatter.findFormatter(keyValue.getClass()) != null) {
 315  0
                     Formatter formatter = Formatter.getFormatter(keyValue.getClass());
 316  0
                     keyValue = (String) formatter.format(keyValue);
 317  0
                 }
 318  
             } else {
 319  0
                 keyValue = keyValue.toString();
 320  
             }
 321  
 
 322  
             // Encrypt value if it is a secure field
 323  0
             if (getDataObjectAuthorizationService().attributeValueNeedsToBeEncryptedOnFormsAndLinks(
 324  
                     inactivationBlockingMetadata.getBlockedBusinessObjectClass(), keyName)){
 325  
                 try {
 326  0
                     keyValue = CoreApiServiceLocator.getEncryptionService().encrypt(keyValue);
 327  0
                 } catch (GeneralSecurityException e) {
 328  0
                     LOG.error("Exception while trying to encrypted value for inquiry framework.", e);
 329  0
                     throw new RuntimeException(e);
 330  0
                 }
 331  
             }
 332  
 
 333  0
             parameters.put(keyName, keyValue);
 334  0
         }
 335  
 
 336  0
         String blockingUrl =
 337  
                 UrlFactory.parameterizeUrl(KRADConstants.DISPLAY_ALL_INACTIVATION_BLOCKERS_ACTION, parameters);
 338  
 
 339  
         // post an error about the locked document
 340  0
         GlobalVariables.getMessageMap()
 341  
                 .putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_INACTIVATION_BLOCKED, blockingUrl);
 342  0
     }
 343  
 
 344  
     /**
 345  
      * @see org.kuali.rice.krad.maintenance.rules.MaintenanceDocumentRule#processApproveDocument(ApproveDocumentEvent)
 346  
      */
 347  
     @Override
 348  
     public boolean processApproveDocument(ApproveDocumentEvent approveEvent) {
 349  0
         MaintenanceDocument maintenanceDocument = (MaintenanceDocument) approveEvent.getDocument();
 350  
 
 351  
         // remove all items from the errorPath temporarily (because it may not
 352  
         // be what we expect, or what we need)
 353  0
         clearErrorPath();
 354  
 
 355  
         // setup convenience pointers to the old & new bo
 356  0
         setupBaseConvenienceObjects(maintenanceDocument);
 357  
 
 358  
         // apply rules that are common across all maintenance documents, regardless of class
 359  0
         processGlobalSaveDocumentBusinessRules(maintenanceDocument);
 360  
 
 361  
         // from here on, it is in a default-success mode, and will approve unless one of the
 362  
         // business rules stop it.
 363  0
         boolean success = true;
 364  
 
 365  
         // apply rules that are common across all maintenance documents, regardless of class
 366  0
         success &= processGlobalApproveDocumentBusinessRules(maintenanceDocument);
 367  
 
 368  
         // apply rules that are specific to the class of the maintenance document
 369  
         // (if implemented). this will always succeed if not overloaded by the
 370  
         // subclass
 371  0
         success &= processCustomApproveDocumentBusinessRules(maintenanceDocument);
 372  
 
 373  
         // return the original set of items to the errorPath, to ensure no impact
 374  
         // on other upstream or downstream items that rely on the errorPath
 375  0
         resumeErrorPath();
 376  
 
 377  0
         return success;
 378  
     }
 379  
 
 380  
     /**
 381  
      * This method is a convenience method to easily add a Document level error (ie, one not tied to a specific field,
 382  
      * but
 383  
      * applicable to the whole document).
 384  
      *
 385  
      * @param errorConstant - Error Constant that can be mapped to a resource for the actual text message.
 386  
      */
 387  
     protected void putGlobalError(String errorConstant) {
 388  0
         if (!errorAlreadyExists(KRADConstants.DOCUMENT_ERRORS, errorConstant)) {
 389  0
             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.DOCUMENT_ERRORS, errorConstant);
 390  
         }
 391  0
     }
 392  
 
 393  
     /**
 394  
      * This method is a convenience method to easily add a Document level error (ie, one not tied to a specific field,
 395  
      * but
 396  
      * applicable to the whole document).
 397  
      *
 398  
      * @param errorConstant - Error Constant that can be mapped to a resource for the actual text message.
 399  
      * @param parameter - Replacement value for part of the error message.
 400  
      */
 401  
     protected void putGlobalError(String errorConstant, String parameter) {
 402  0
         if (!errorAlreadyExists(KRADConstants.DOCUMENT_ERRORS, errorConstant)) {
 403  0
             GlobalVariables.getMessageMap()
 404  
                     .putErrorWithoutFullErrorPath(KRADConstants.DOCUMENT_ERRORS, errorConstant, parameter);
 405  
         }
 406  0
     }
 407  
 
 408  
     /**
 409  
      * This method is a convenience method to easily add a Document level error (ie, one not tied to a specific field,
 410  
      * but
 411  
      * applicable to the whole document).
 412  
      *
 413  
      * @param errorConstant - Error Constant that can be mapped to a resource for the actual text message.
 414  
      * @param parameters - Array of replacement values for part of the error message.
 415  
      */
 416  
     protected void putGlobalError(String errorConstant, String[] parameters) {
 417  0
         if (!errorAlreadyExists(KRADConstants.DOCUMENT_ERRORS, errorConstant)) {
 418  0
             GlobalVariables.getMessageMap()
 419  
                     .putErrorWithoutFullErrorPath(KRADConstants.DOCUMENT_ERRORS, errorConstant, parameters);
 420  
         }
 421  0
     }
 422  
 
 423  
     /**
 424  
      * This method is a convenience method to add a property-specific error to the global errors list. This method
 425  
      * makes
 426  
      * sure that
 427  
      * the correct prefix is added to the property name so that it will display correctly on maintenance documents.
 428  
      *
 429  
      * @param propertyName - Property name of the element that is associated with the error. Used to mark the field as
 430  
      * errored in
 431  
      * the UI.
 432  
      * @param errorConstant - Error Constant that can be mapped to a resource for the actual text message.
 433  
      */
 434  
     protected void putFieldError(String propertyName, String errorConstant) {
 435  0
         if (!errorAlreadyExists(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant)) {
 436  0
             GlobalVariables.getMessageMap()
 437  
                     .putErrorWithoutFullErrorPath(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant);
 438  
         }
 439  0
     }
 440  
 
 441  
     /**
 442  
      * This method is a convenience method to add a property-specific error to the global errors list. This method
 443  
      * makes
 444  
      * sure that
 445  
      * the correct prefix is added to the property name so that it will display correctly on maintenance documents.
 446  
      *
 447  
      * @param propertyName - Property name of the element that is associated with the error. Used to mark the field as
 448  
      * errored in
 449  
      * the UI.
 450  
      * @param errorConstant - Error Constant that can be mapped to a resource for the actual text message.
 451  
      * @param parameter - Single parameter value that can be used in the message so that you can display specific
 452  
      * values
 453  
      * to the
 454  
      * user.
 455  
      */
 456  
     protected void putFieldError(String propertyName, String errorConstant, String parameter) {
 457  0
         if (!errorAlreadyExists(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant)) {
 458  0
             GlobalVariables.getMessageMap()
 459  
                     .putErrorWithoutFullErrorPath(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant, parameter);
 460  
         }
 461  0
     }
 462  
 
 463  
     /**
 464  
      * This method is a convenience method to add a property-specific error to the global errors list. This method
 465  
      * makes
 466  
      * sure that
 467  
      * the correct prefix is added to the property name so that it will display correctly on maintenance documents.
 468  
      *
 469  
      * @param propertyName - Property name of the element that is associated with the error. Used to mark the field as
 470  
      * errored in
 471  
      * the UI.
 472  
      * @param errorConstant - Error Constant that can be mapped to a resource for the actual text message.
 473  
      * @param parameters - Array of strings holding values that can be used in the message so that you can display
 474  
      * specific values
 475  
      * to the user.
 476  
      */
 477  
     protected void putFieldError(String propertyName, String errorConstant, String[] parameters) {
 478  0
         if (!errorAlreadyExists(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant)) {
 479  0
             GlobalVariables.getMessageMap()
 480  
                     .putErrorWithoutFullErrorPath(MAINTAINABLE_ERROR_PREFIX + propertyName, errorConstant, parameters);
 481  
         }
 482  0
     }
 483  
 
 484  
     /**
 485  
      * Adds a property-specific error to the global errors list, with the DD short label as the single argument.
 486  
      *
 487  
      * @param propertyName - Property name of the element that is associated with the error. Used to mark the field as
 488  
      * errored in
 489  
      * the UI.
 490  
      * @param errorConstant - Error Constant that can be mapped to a resource for the actual text message.
 491  
      */
 492  
     protected void putFieldErrorWithShortLabel(String propertyName, String errorConstant) {
 493  0
         String shortLabel = ddService.getAttributeShortLabel(dataObjectClass, propertyName);
 494  0
         putFieldError(propertyName, errorConstant, shortLabel);
 495  0
     }
 496  
 
 497  
     /**
 498  
      * This method is a convenience method to add a property-specific document error to the global errors list. This
 499  
      * method makes
 500  
      * sure that the correct prefix is added to the property name so that it will display correctly on maintenance
 501  
      * documents.
 502  
      *
 503  
      * @param propertyName - Property name of the element that is associated with the error. Used to mark the field as
 504  
      * errored in
 505  
      * the UI.
 506  
      * @param errorConstant - Error Constant that can be mapped to a resource for the actual text message.
 507  
      * @param parameter - Single parameter value that can be used in the message so that you can display specific
 508  
      * values
 509  
      * to the
 510  
      * user.
 511  
      */
 512  
     protected void putDocumentError(String propertyName, String errorConstant, String parameter) {
 513  0
         if (!errorAlreadyExists(DOCUMENT_ERROR_PREFIX + propertyName, errorConstant)) {
 514  0
             GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + propertyName, errorConstant, parameter);
 515  
         }
 516  0
     }
 517  
 
 518  
     /**
 519  
      * This method is a convenience method to add a property-specific document error to the global errors list. This
 520  
      * method makes
 521  
      * sure that the correct prefix is added to the property name so that it will display correctly on maintenance
 522  
      * documents.
 523  
      *
 524  
      * @param propertyName - Property name of the element that is associated with the error. Used to mark the field as
 525  
      * errored in
 526  
      * the UI.
 527  
      * @param errorConstant - Error Constant that can be mapped to a resource for the actual text message.
 528  
      * @param parameters - Array of String parameters that can be used in the message so that you can display specific
 529  
      * values to the
 530  
      * user.
 531  
      */
 532  
     protected void putDocumentError(String propertyName, String errorConstant, String[] parameters) {
 533  0
         GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + propertyName, errorConstant, parameters);
 534  0
     }
 535  
 
 536  
     /**
 537  
      * Convenience method to determine whether the field already has the message indicated.
 538  
      *
 539  
      * This is useful if you want to suppress duplicate error messages on the same field.
 540  
      *
 541  
      * @param propertyName - propertyName you want to test on
 542  
      * @param errorConstant - errorConstant you want to test
 543  
      * @return returns True if the propertyName indicated already has the errorConstant indicated, false otherwise
 544  
      */
 545  
     protected boolean errorAlreadyExists(String propertyName, String errorConstant) {
 546  0
         if (GlobalVariables.getMessageMap().fieldHasMessage(propertyName, errorConstant)) {
 547  0
             return true;
 548  
         } else {
 549  0
             return false;
 550  
         }
 551  
     }
 552  
 
 553  
     /**
 554  
      * This method specifically doesn't put any prefixes before the error so that the developer can do things specific
 555  
      * to the
 556  
      * globals errors (like newDelegateChangeDocument errors)
 557  
      *
 558  
      * @param propertyName
 559  
      * @param errorConstant
 560  
      */
 561  
     protected void putGlobalsError(String propertyName, String errorConstant) {
 562  0
         if (!errorAlreadyExists(propertyName, errorConstant)) {
 563  0
             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(propertyName, errorConstant);
 564  
         }
 565  0
     }
 566  
 
 567  
     /**
 568  
      * This method specifically doesn't put any prefixes before the error so that the developer can do things specific
 569  
      * to the
 570  
      * globals errors (like newDelegateChangeDocument errors)
 571  
      *
 572  
      * @param propertyName
 573  
      * @param errorConstant
 574  
      * @param parameter
 575  
      */
 576  
     protected void putGlobalsError(String propertyName, String errorConstant, String parameter) {
 577  0
         if (!errorAlreadyExists(propertyName, errorConstant)) {
 578  0
             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(propertyName, errorConstant, parameter);
 579  
         }
 580  0
     }
 581  
 
 582  
     /**
 583  
      * This method is used to deal with error paths that are not what we expect them to be. This method, along with
 584  
      * resumeErrorPath() are used to temporarily clear the errorPath, and then return it to the original state after
 585  
      * the
 586  
      * rule is
 587  
      * executed.
 588  
      *
 589  
      * This method is called at the very beginning of rule enforcement and pulls a copy of the contents of the
 590  
      * errorPath
 591  
      * ArrayList
 592  
      * to a local arrayList for temporary storage.
 593  
      */
 594  
     protected void clearErrorPath() {
 595  
         // add all the items from the global list to the local list
 596  0
         priorErrorPath.addAll(GlobalVariables.getMessageMap().getErrorPath());
 597  
 
 598  
         // clear the global list
 599  0
         GlobalVariables.getMessageMap().getErrorPath().clear();
 600  0
     }
 601  
 
 602  
     /**
 603  
      * This method is used to deal with error paths that are not what we expect them to be. This method, along with
 604  
      * clearErrorPath()
 605  
      * are used to temporarily clear the errorPath, and then return it to the original state after the rule is
 606  
      * executed.
 607  
      *
 608  
      * This method is called at the very end of the rule enforcement, and returns the temporarily stored copy of the
 609  
      * errorPath to
 610  
      * the global errorPath, so that no other classes are interrupted.
 611  
      */
 612  
     protected void resumeErrorPath() {
 613  
         // revert the global errorPath back to what it was when we entered this
 614  
         // class
 615  0
         GlobalVariables.getMessageMap().getErrorPath().addAll(priorErrorPath);
 616  0
     }
 617  
 
 618  
     /**
 619  
      * Executes the DataDictionary Validation against the document.
 620  
      *
 621  
      * @param document
 622  
      * @return true if it passes DD validation, false otherwise
 623  
      */
 624  
     protected boolean dataDictionaryValidate(MaintenanceDocument document) {
 625  0
         LOG.debug("MaintenanceDocument validation beginning");
 626  
 
 627  
         // explicitly put the errorPath that the dictionaryValidationService
 628  
         // requires
 629  0
         GlobalVariables.getMessageMap().addToErrorPath("document.newMaintainableObject");
 630  
 
 631  
         // document must have a newMaintainable object
 632  0
         Maintainable newMaintainable = document.getNewMaintainableObject();
 633  0
         if (newMaintainable == null) {
 634  0
             GlobalVariables.getMessageMap().removeFromErrorPath("document.newMaintainableObject");
 635  0
             throw new ValidationException(
 636  
                     "Maintainable object from Maintenance Document '" + document.getDocumentTitle() +
 637  
                             "' is null, unable to proceed.");
 638  
         }
 639  
 
 640  
         // document's newMaintainable must contain an object (ie, not null)
 641  0
         Object dataObject = newMaintainable.getDataObject();
 642  0
         if (dataObject == null) {
 643  0
             GlobalVariables.getMessageMap().removeFromErrorPath("document.newMaintainableObject.");
 644  0
             throw new ValidationException("Maintainable's component business object is null.");
 645  
         }
 646  
 
 647  0
         GlobalVariables.getMessageMap().addToErrorPath("dataObject");
 648  
 
 649  0
         dictionaryValidationService.validate(newDataObject);
 650  
 
 651  0
         GlobalVariables.getMessageMap().removeFromErrorPath("dataObject");
 652  
 
 653  
         // explicitly remove the errorPath we've added
 654  0
         GlobalVariables.getMessageMap().removeFromErrorPath("document.newMaintainableObject");
 655  
 
 656  0
         LOG.debug("MaintenanceDocument validation ending");
 657  0
         return true;
 658  
     }
 659  
 
 660  
     /**
 661  
      * This method checks the two major cases that may violate primary key integrity.
 662  
      *
 663  
      * 1. Disallow changing of the primary keys on an EDIT maintenance document. Other fields can be changed, but once
 664  
      * the primary
 665  
      * keys have been set, they are permanent.
 666  
      *
 667  
      * 2. Disallow creating a new object whose primary key values are already present in the system on a CREATE NEW
 668  
      * maintenance
 669  
      * document.
 670  
      *
 671  
      * This method also will add new Errors to the Global Error Map.
 672  
      *
 673  
      * @param document - The Maintenance Document being tested.
 674  
      * @return Returns false if either test failed, otherwise returns true.
 675  
      */
 676  
     protected boolean primaryKeyCheck(MaintenanceDocument document) {
 677  
         // default to success if no failures
 678  0
         boolean success = true;
 679  0
         Class<?> dataObjectClass = document.getNewMaintainableObject().getDataObjectClass();
 680  
 
 681  0
         Object oldBo = document.getOldMaintainableObject().getDataObject();
 682  0
         Object newDataObject = document.getNewMaintainableObject().getDataObject();
 683  
 
 684  
         // We dont do primaryKeyChecks on Global Business Object maintenance documents. This is
 685  
         // because it doesnt really make any sense to do so, given the behavior of Globals. When a
 686  
         // Global Document completes, it will update or create a new record for each BO in the list.
 687  
         // As a result, there's no problem with having existing BO records in the system, they will
 688  
         // simply get updated.
 689  0
         if (newDataObject instanceof GlobalBusinessObject) {
 690  0
             return success;
 691  
         }
 692  
 
 693  
         // fail and complain if the person has changed the primary keys on
 694  
         // an EDIT maintenance document.
 695  0
         if (document.isEdit()) {
 696  0
             if (!getDataObjectMetaDataService().equalsByPrimaryKeys(oldBo, newDataObject)) {
 697  
                 // add a complaint to the errors
 698  0
                 putDocumentError(KRADConstants.DOCUMENT_ERRORS,
 699  
                         RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_PRIMARY_KEYS_CHANGED_ON_EDIT,
 700  
                         getHumanReadablePrimaryKeyFieldNames(dataObjectClass));
 701  0
                 success &= false;
 702  
             }
 703  
         }
 704  
 
 705  
         // fail and complain if the person has selected a new object with keys that already exist
 706  
         // in the DB.
 707  0
         else if (document.isNew()) {
 708  
 
 709  
             // TODO: when/if we have standard support for DO retrieval, do this check for DO's
 710  0
             if (newDataObject instanceof PersistableBusinessObject) {
 711  
 
 712  
                 // get a map of the pk field names and values
 713  0
                 Map<String, ?> newPkFields = getDataObjectMetaDataService().getPrimaryKeyFieldValues(newDataObject);
 714  
 
 715  
                 // TODO: Good suggestion from Aaron, dont bother checking the DB, if all of the
 716  
                 // objects PK fields dont have values. If any are null or empty, then
 717  
                 // we're done. The current way wont fail, but it will make a wasteful
 718  
                 // DB call that may not be necessary, and we want to minimize these.
 719  
 
 720  
                 // attempt to do a lookup, see if this object already exists by these Primary Keys
 721  0
                 PersistableBusinessObject testBo = boService
 722  
                         .findByPrimaryKey(dataObjectClass.asSubclass(PersistableBusinessObject.class), newPkFields);
 723  
 
 724  
                 // if the retrieve was successful, then this object already exists, and we need
 725  
                 // to complain
 726  0
                 if (testBo != null) {
 727  0
                     putDocumentError(KRADConstants.DOCUMENT_ERRORS,
 728  
                             RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_KEYS_ALREADY_EXIST_ON_CREATE_NEW,
 729  
                             getHumanReadablePrimaryKeyFieldNames(dataObjectClass));
 730  0
                     success &= false;
 731  
                 }
 732  
             }
 733  
         }
 734  
 
 735  0
         return success;
 736  
     }
 737  
 
 738  
     /**
 739  
      * This method creates a human-readable string of the class' primary key field names, as designated by the
 740  
      * DataDictionary.
 741  
      *
 742  
      * @param dataObjectClass
 743  
      * @return
 744  
      */
 745  
     protected String getHumanReadablePrimaryKeyFieldNames(Class<?> dataObjectClass) {
 746  0
         String delim = "";
 747  0
         StringBuffer pkFieldNames = new StringBuffer();
 748  
 
 749  
         // get a list of all the primary key field names, walk through them
 750  0
         List<String> pkFields = getDataObjectMetaDataService().listPrimaryKeyFieldNames(dataObjectClass);
 751  0
         for (Iterator<String> iter = pkFields.iterator(); iter.hasNext(); ) {
 752  0
             String pkFieldName = (String) iter.next();
 753  
 
 754  
             // TODO should this be getting labels from the view dictionary
 755  
             // use the DataDictionary service to translate field name into human-readable label
 756  0
             String humanReadableFieldName = ddService.getAttributeLabel(dataObjectClass, pkFieldName);
 757  
 
 758  
             // append the next field
 759  0
             pkFieldNames.append(delim + humanReadableFieldName);
 760  
 
 761  
             // separate names with commas after the first one
 762  0
             if (delim.equalsIgnoreCase("")) {
 763  0
                 delim = ", ";
 764  
             }
 765  0
         }
 766  
 
 767  0
         return pkFieldNames.toString();
 768  
     }
 769  
 
 770  
     /**
 771  
      * This method enforces all business rules that are common to all maintenance documents which must be tested before
 772  
      * doing an
 773  
      * approval.
 774  
      *
 775  
      * It can be overloaded in special cases where a MaintenanceDocument has very special needs that would be contrary
 776  
      * to what is
 777  
      * enforced here.
 778  
      *
 779  
      * @param document - a populated MaintenanceDocument instance
 780  
      * @return true if the document can be approved, false if not
 781  
      */
 782  
     protected boolean processGlobalApproveDocumentBusinessRules(MaintenanceDocument document) {
 783  0
         return true;
 784  
     }
 785  
 
 786  
     /**
 787  
      * This method enforces all business rules that are common to all maintenance documents which must be tested before
 788  
      * doing a
 789  
      * route.
 790  
      *
 791  
      * It can be overloaded in special cases where a MaintenanceDocument has very special needs that would be contrary
 792  
      * to what is
 793  
      * enforced here.
 794  
      *
 795  
      * @param document - a populated MaintenanceDocument instance
 796  
      * @return true if the document can be routed, false if not
 797  
      */
 798  
     protected boolean processGlobalRouteDocumentBusinessRules(MaintenanceDocument document) {
 799  0
         boolean success = true;
 800  
 
 801  
         // require a document description field
 802  0
         success &= checkEmptyDocumentField(
 803  
                 KRADPropertyConstants.DOCUMENT_HEADER + "." + KRADPropertyConstants.DOCUMENT_DESCRIPTION,
 804  
                 document.getDocumentHeader().getDocumentDescription(), "Description");
 805  
 
 806  0
         return success;
 807  
     }
 808  
 
 809  
     /**
 810  
      * This method enforces all business rules that are common to all maintenance documents which must be tested before
 811  
      * doing a
 812  
      * save.
 813  
      *
 814  
      * It can be overloaded in special cases where a MaintenanceDocument has very special needs that would be contrary
 815  
      * to what is
 816  
      * enforced here.
 817  
      *
 818  
      * Note that although this method returns a true or false to indicate whether the save should happen or not, this
 819  
      * result may not
 820  
      * be followed by the calling method. In other words, the boolean result will likely be ignored, and the document
 821  
      * saved,
 822  
      * regardless.
 823  
      *
 824  
      * @param document - a populated MaintenanceDocument instance
 825  
      * @return true if all business rules succeed, false if not
 826  
      */
 827  
     protected boolean processGlobalSaveDocumentBusinessRules(MaintenanceDocument document) {
 828  
         // default to success
 829  0
         boolean success = true;
 830  
 
 831  
         // do generic checks that impact primary key violations
 832  0
         primaryKeyCheck(document);
 833  
 
 834  
         // this is happening only on the processSave, since a Save happens in both the
 835  
         // Route and Save events.
 836  0
         this.dataDictionaryValidate(document);
 837  
 
 838  0
         return success;
 839  
     }
 840  
 
 841  
     /**
 842  
      * This method should be overridden to provide custom rules for processing document saving
 843  
      *
 844  
      * @param document
 845  
      * @return boolean
 846  
      */
 847  
     protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
 848  0
         return true;
 849  
     }
 850  
 
 851  
     /**
 852  
      * This method should be overridden to provide custom rules for processing document routing
 853  
      *
 854  
      * @param document
 855  
      * @return boolean
 856  
      */
 857  
     protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
 858  0
         return true;
 859  
     }
 860  
 
 861  
     /**
 862  
      * This method should be overridden to provide custom rules for processing document approval.
 863  
      *
 864  
      * @param document
 865  
      * @return booelan
 866  
      */
 867  
     protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
 868  0
         return true;
 869  
     }
 870  
 
 871  
     // Document Validation Helper Methods
 872  
 
 873  
     /**
 874  
      * This method checks to see if the document is in a state that it can be saved without causing exceptions.
 875  
      *
 876  
      * Note that Business Rules are NOT enforced here, only validity checks.
 877  
      *
 878  
      * This method will only return false if the document is in such a state that routing it will cause
 879  
      * RunTimeExceptions.
 880  
      *
 881  
      * @param maintenanceDocument - a populated MaintenaceDocument instance.
 882  
      * @return boolean - returns true unless the object is in an invalid state.
 883  
      */
 884  
     protected boolean isDocumentValidForSave(MaintenanceDocument maintenanceDocument) {
 885  
 
 886  0
         boolean success = true;
 887  
 
 888  0
         success &= super.isDocumentOverviewValid(maintenanceDocument);
 889  0
         success &= validateDocumentStructure((Document) maintenanceDocument);
 890  0
         success &= validateMaintenanceDocument(maintenanceDocument);
 891  0
         success &= validateGlobalBusinessObjectPersistable(maintenanceDocument);
 892  0
         return success;
 893  
     }
 894  
 
 895  
     /**
 896  
      * This method makes sure the document itself is valid, and has the necessary fields populated to be routable.
 897  
      *
 898  
      * This is not a business rules test, rather its a structure test to make sure that the document will not cause
 899  
      * exceptions
 900  
      * before routing.
 901  
      *
 902  
      * @param document - document to be tested
 903  
      * @return false if the document is missing key values, true otherwise
 904  
      */
 905  
     protected boolean validateDocumentStructure(Document document) {
 906  0
         boolean success = true;
 907  
 
 908  
         // document must have a populated documentNumber
 909  0
         String documentHeaderId = document.getDocumentNumber();
 910  0
         if (documentHeaderId == null || StringUtils.isEmpty(documentHeaderId)) {
 911  0
             throw new ValidationException("Document has no document number, unable to proceed.");
 912  
         }
 913  
 
 914  0
         return success;
 915  
     }
 916  
 
 917  
     /**
 918  
      * This method checks to make sure the document is a valid maintenanceDocument, and has the necessary values
 919  
      * populated such that
 920  
      * it will not cause exceptions in later routing or business rules testing.
 921  
      *
 922  
      * This is not a business rules test.
 923  
      *
 924  
      * @param maintenanceDocument - document to be tested
 925  
      * @return whether maintenance doc passes
 926  
      * @throws ValidationException
 927  
      */
 928  
     protected boolean validateMaintenanceDocument(MaintenanceDocument maintenanceDocument) {
 929  0
         boolean success = true;
 930  0
         Maintainable newMaintainable = maintenanceDocument.getNewMaintainableObject();
 931  
 
 932  
         // document must have a newMaintainable object
 933  0
         if (newMaintainable == null) {
 934  0
             throw new ValidationException(
 935  
                     "Maintainable object from Maintenance Document '" + maintenanceDocument.getDocumentTitle() +
 936  
                             "' is null, unable to proceed.");
 937  
         }
 938  
 
 939  
         // document's newMaintainable must contain an object (ie, not null)
 940  0
         if (newMaintainable.getDataObject() == null) {
 941  0
             throw new ValidationException("Maintainable's component data object is null.");
 942  
         }
 943  
 
 944  0
         return success;
 945  
     }
 946  
 
 947  
     /**
 948  
      * This method checks whether this maint doc contains Global Business Objects, and if so, whether the GBOs are in a
 949  
      * persistable
 950  
      * state. This will return false if this method determines that the GBO will cause a SQL Exception when the
 951  
      * document
 952  
      * is
 953  
      * persisted.
 954  
      *
 955  
      * @param document
 956  
      * @return False when the method determines that the contained Global Business Object will cause a SQL Exception,
 957  
      *         and the
 958  
      *         document should not be saved. It will return True otherwise.
 959  
      */
 960  
     protected boolean validateGlobalBusinessObjectPersistable(MaintenanceDocument document) {
 961  0
         boolean success = true;
 962  
 
 963  0
         if (document.getNewMaintainableObject() == null) {
 964  0
             return success;
 965  
         }
 966  0
         if (document.getNewMaintainableObject().getDataObject() == null) {
 967  0
             return success;
 968  
         }
 969  0
         if (!(document.getNewMaintainableObject().getDataObject() instanceof GlobalBusinessObject)) {
 970  0
             return success;
 971  
         }
 972  
 
 973  0
         PersistableBusinessObject bo = (PersistableBusinessObject) document.getNewMaintainableObject().getDataObject();
 974  0
         GlobalBusinessObject gbo = (GlobalBusinessObject) bo;
 975  0
         return gbo.isPersistable();
 976  
     }
 977  
 
 978  
     /**
 979  
      * This method tests to make sure the MaintenanceDocument passed in is based on the class you are expecting.
 980  
      *
 981  
      * It does this based on the NewMaintainableObject of the MaintenanceDocument.
 982  
      *
 983  
      * @param document - MaintenanceDocument instance you want to test
 984  
      * @param clazz - class you are expecting the MaintenanceDocument to be based on
 985  
      * @return true if they match, false if not
 986  
      */
 987  
     protected boolean isCorrectMaintenanceClass(MaintenanceDocument document, Class clazz) {
 988  
         // disallow null arguments
 989  0
         if (document == null || clazz == null) {
 990  0
             throw new IllegalArgumentException("Null arguments were passed in.");
 991  
         }
 992  
 
 993  
         // compare the class names
 994  0
         if (clazz.toString().equals(document.getNewMaintainableObject().getDataObjectClass().toString())) {
 995  0
             return true;
 996  
         } else {
 997  0
             return false;
 998  
         }
 999  
     }
 1000  
 
 1001  
     /**
 1002  
      * This method accepts an object, and attempts to determine whether it is empty by this method's definition.
 1003  
      *
 1004  
      * OBJECT RESULT null false empty-string false whitespace false otherwise true
 1005  
      *
 1006  
      * If the result is false, it will add an object field error to the Global Errors.
 1007  
      *
 1008  
      * @param valueToTest - any object to test, usually a String
 1009  
      * @param propertyName - the name of the property being tested
 1010  
      * @return true or false, by the description above
 1011  
      */
 1012  
     protected boolean checkEmptyBOField(String propertyName, Object valueToTest, String parameter) {
 1013  0
         boolean success = true;
 1014  
 
 1015  0
         success = checkEmptyValue(valueToTest);
 1016  
 
 1017  
         // if failed, then add a field error
 1018  0
         if (!success) {
 1019  0
             putFieldError(propertyName, RiceKeyConstants.ERROR_REQUIRED, parameter);
 1020  
         }
 1021  
 
 1022  0
         return success;
 1023  
     }
 1024  
 
 1025  
     /**
 1026  
      * This method accepts document field (such as , and attempts to determine whether it is empty by this method's
 1027  
      * definition.
 1028  
      *
 1029  
      * OBJECT RESULT null false empty-string false whitespace false otherwise true
 1030  
      *
 1031  
      * If the result is false, it will add document field error to the Global Errors.
 1032  
      *
 1033  
      * @param valueToTest - any object to test, usually a String
 1034  
      * @param propertyName - the name of the property being tested
 1035  
      * @return true or false, by the description above
 1036  
      */
 1037  
     protected boolean checkEmptyDocumentField(String propertyName, Object valueToTest, String parameter) {
 1038  0
         boolean success = true;
 1039  0
         success = checkEmptyValue(valueToTest);
 1040  0
         if (!success) {
 1041  0
             putDocumentError(propertyName, RiceKeyConstants.ERROR_REQUIRED, parameter);
 1042  
         }
 1043  0
         return success;
 1044  
     }
 1045  
 
 1046  
     /**
 1047  
      * This method accepts document field (such as , and attempts to determine whether it is empty by this method's
 1048  
      * definition.
 1049  
      *
 1050  
      * OBJECT RESULT null false empty-string false whitespace false otherwise true
 1051  
      *
 1052  
      * It will the result as a boolean
 1053  
      *
 1054  
      * @param valueToTest - any object to test, usually a String
 1055  
      */
 1056  
     protected boolean checkEmptyValue(Object valueToTest) {
 1057  0
         boolean success = true;
 1058  
 
 1059  
         // if its not a string, only fail if its a null object
 1060  0
         if (valueToTest == null) {
 1061  0
             success = false;
 1062  
         } else {
 1063  
             // test for null, empty-string, or whitespace if its a string
 1064  0
             if (valueToTest instanceof String) {
 1065  0
                 if (StringUtils.isBlank((String) valueToTest)) {
 1066  0
                     success = false;
 1067  
                 }
 1068  
             }
 1069  
         }
 1070  
 
 1071  0
         return success;
 1072  
     }
 1073  
 
 1074  
     /**
 1075  
      * This method is used during debugging to dump the contents of the error map, including the key names. It is not
 1076  
      * used by the
 1077  
      * application in normal circumstances at all.
 1078  
      */
 1079  
     protected void showErrorMap() {
 1080  0
         if (GlobalVariables.getMessageMap().hasNoErrors()) {
 1081  0
             return;
 1082  
         }
 1083  
 
 1084  0
         for (Iterator i = GlobalVariables.getMessageMap().getAllPropertiesAndErrors().iterator(); i.hasNext(); ) {
 1085  0
             Map.Entry e = (Map.Entry) i.next();
 1086  
 
 1087  0
             AutoPopulatingList errorList = (AutoPopulatingList) e.getValue();
 1088  0
             for (Iterator j = errorList.iterator(); j.hasNext(); ) {
 1089  0
                 ErrorMessage em = (ErrorMessage) j.next();
 1090  
 
 1091  0
                 if (em.getMessageParameters() == null) {
 1092  0
                     LOG.error(e.getKey().toString() + " = " + em.getErrorKey());
 1093  
                 } else {
 1094  0
                     LOG.error(e.getKey().toString() + " = " + em.getErrorKey() + " : " +
 1095  
                             em.getMessageParameters().toString());
 1096  
                 }
 1097  0
             }
 1098  0
         }
 1099  0
     }
 1100  
 
 1101  
     /**
 1102  
      * @see org.kuali.rice.krad.maintenance.rules.MaintenanceDocumentRule#setupBaseConvenienceObjects(MaintenanceDocument)
 1103  
      */
 1104  
     public void setupBaseConvenienceObjects(MaintenanceDocument document) {
 1105  
         // setup oldAccount convenience objects, make sure all possible sub-objects are populated
 1106  0
         oldDataObject = document.getOldMaintainableObject().getDataObject();
 1107  0
         if (oldDataObject != null && oldDataObject instanceof PersistableBusinessObject) {
 1108  0
             ((PersistableBusinessObject) oldDataObject).refreshNonUpdateableReferences();
 1109  
         }
 1110  
 
 1111  
         // setup newAccount convenience objects, make sure all possible sub-objects are populated
 1112  0
         newDataObject = document.getNewMaintainableObject().getDataObject();
 1113  0
         if (newDataObject instanceof PersistableBusinessObject) {
 1114  0
             ((PersistableBusinessObject) newDataObject).refreshNonUpdateableReferences();
 1115  
         }
 1116  
 
 1117  0
         dataObjectClass = document.getNewMaintainableObject().getDataObjectClass();
 1118  
 
 1119  
         // call the setupConvenienceObjects in the subclass, if a subclass exists
 1120  0
         setupConvenienceObjects();
 1121  0
     }
 1122  
 
 1123  
     public void setupConvenienceObjects() {
 1124  
         // should always be overriden by subclass
 1125  0
     }
 1126  
 
 1127  
     /**
 1128  
      * This method checks to make sure that if the foreign-key fields for the given reference attributes have any
 1129  
      * fields filled out,that all fields are filled out.
 1130  
      *
 1131  
      * If any are filled out, but all are not, it will return false and add a global error message about the problem.
 1132  
      *
 1133  
      * @param referenceName - The name of the reference object, whose foreign-key fields must be all-or-none filled
 1134  
      * out.
 1135  
      * @return true if this is the case, false if not
 1136  
      */
 1137  
     protected boolean checkForPartiallyFilledOutReferenceForeignKeys(String referenceName) {
 1138  0
         boolean success = true;
 1139  
 
 1140  0
         if (newDataObject instanceof PersistableBusinessObject) {
 1141  
             ForeignKeyFieldsPopulationState fkFieldsState;
 1142  0
             fkFieldsState = getPersistenceStructureService()
 1143  
                     .getForeignKeyFieldsPopulationState((PersistableBusinessObject) newDataObject, referenceName);
 1144  
 
 1145  
             // determine result
 1146  0
             if (fkFieldsState.isAnyFieldsPopulated() && !fkFieldsState.isAllFieldsPopulated()) {
 1147  0
                 success = false;
 1148  
 
 1149  
                 // add errors if appropriate
 1150  
 
 1151  
                 // get the full set of foreign-keys
 1152  0
                 List fKeys = new ArrayList(getPersistenceStructureService().getForeignKeysForReference(
 1153  
                         newDataObject.getClass().asSubclass(PersistableBusinessObject.class), referenceName).keySet());
 1154  0
                 String fKeysReadable = consolidateFieldNames(fKeys, ", ").toString();
 1155  
 
 1156  
                 // walk through the missing fields
 1157  0
                 for (Iterator iter = fkFieldsState.getUnpopulatedFieldNames().iterator(); iter.hasNext(); ) {
 1158  0
                     String fieldName = (String) iter.next();
 1159  
 
 1160  
                     // get the human-readable name
 1161  0
                     String fieldNameReadable = ddService.getAttributeLabel(newDataObject.getClass(), fieldName);
 1162  
 
 1163  
                     // add a field error
 1164  0
                     putFieldError(fieldName, RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_PARTIALLY_FILLED_OUT_REF_FKEYS,
 1165  
                             new String[]{fieldNameReadable, fKeysReadable});
 1166  0
                 }
 1167  
             }
 1168  
         }
 1169  
 
 1170  0
         return success;
 1171  
     }
 1172  
 
 1173  
     /**
 1174  
      * This method turns a list of field property names, into a delimited string of the human-readable names.
 1175  
      *
 1176  
      * @param fieldNames - List of fieldNames
 1177  
      * @return A filled StringBuffer ready to go in an error message
 1178  
      */
 1179  
     protected StringBuffer consolidateFieldNames(List fieldNames, String delimiter) {
 1180  0
         StringBuffer sb = new StringBuffer();
 1181  
 
 1182  
         // setup some vars
 1183  0
         boolean firstPass = true;
 1184  0
         String delim = "";
 1185  
 
 1186  
         // walk through the list
 1187  0
         for (Iterator iter = fieldNames.iterator(); iter.hasNext(); ) {
 1188  0
             String fieldName = (String) iter.next();
 1189  
 
 1190  
             // get the human-readable name
 1191  
             // add the new one, with the appropriate delimiter
 1192  0
             sb.append(delim + ddService.getAttributeLabel(newDataObject.getClass(), fieldName));
 1193  
 
 1194  
             // after the first item, start using a delimiter
 1195  0
             if (firstPass) {
 1196  0
                 delim = delimiter;
 1197  0
                 firstPass = false;
 1198  
             }
 1199  0
         }
 1200  
 
 1201  0
         return sb;
 1202  
     }
 1203  
 
 1204  
     /**
 1205  
      * This method translates the passed in field name into a human-readable attribute label.
 1206  
      *
 1207  
      * It assumes the existing newDataObject's class as the class to examine the fieldName for.
 1208  
      *
 1209  
      * @param fieldName The fieldName you want a human-readable label for.
 1210  
      * @return A human-readable label, pulled from the DataDictionary.
 1211  
      */
 1212  
     protected String getFieldLabel(String fieldName) {
 1213  0
         return ddService.getAttributeLabel(newDataObject.getClass(), fieldName) + "(" +
 1214  
                 ddService.getAttributeShortLabel(newDataObject.getClass(), fieldName) + ")";
 1215  
     }
 1216  
 
 1217  
     /**
 1218  
      * This method translates the passed in field name into a human-readable attribute label.
 1219  
      *
 1220  
      * It assumes the existing newDataObject's class as the class to examine the fieldName for.
 1221  
      *
 1222  
      * @param dataObjectClass The class to use in combination with the fieldName.
 1223  
      * @param fieldName The fieldName you want a human-readable label for.
 1224  
      * @return A human-readable label, pulled from the DataDictionary.
 1225  
      */
 1226  
     protected String getFieldLabel(Class dataObjectClass, String fieldName) {
 1227  0
         return ddService.getAttributeLabel(dataObjectClass, fieldName) + "(" +
 1228  
                 ddService.getAttributeShortLabel(dataObjectClass, fieldName) + ")";
 1229  
     }
 1230  
 
 1231  
     /**
 1232  
      * Gets the newDataObject attribute.
 1233  
      *
 1234  
      * @return Returns the newDataObject.
 1235  
      */
 1236  
     protected final Object getNewDataObject() {
 1237  0
         return newDataObject;
 1238  
     }
 1239  
 
 1240  
     protected void setNewDataObject(Object newDataObject) {
 1241  0
         this.newDataObject = newDataObject;
 1242  0
     }
 1243  
 
 1244  
     /**
 1245  
      * Gets the oldDataObject attribute.
 1246  
      *
 1247  
      * @return Returns the oldDataObject.
 1248  
      */
 1249  
     protected final Object getOldDataObject() {
 1250  0
         return oldDataObject;
 1251  
     }
 1252  
 
 1253  
     public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName,
 1254  
             PersistableBusinessObject line) {
 1255  0
         return true;
 1256  
     }
 1257  
 
 1258  
     protected final BusinessObjectService getBoService() {
 1259  0
         if (boService == null) {
 1260  0
             this.boService = KRADServiceLocator.getBusinessObjectService();
 1261  
         }
 1262  0
         return boService;
 1263  
     }
 1264  
 
 1265  
     public final void setBoService(BusinessObjectService boService) {
 1266  0
         this.boService = boService;
 1267  0
     }
 1268  
 
 1269  
     protected final ConfigurationService getConfigService() {
 1270  0
         if (configService == null) {
 1271  0
             this.configService = KRADServiceLocator.getKualiConfigurationService();
 1272  
         }
 1273  0
         return configService;
 1274  
     }
 1275  
 
 1276  
     public final void setConfigService(ConfigurationService configService) {
 1277  0
         this.configService = configService;
 1278  0
     }
 1279  
 
 1280  
     protected final DataDictionaryService getDdService() {
 1281  0
         if (ddService == null) {
 1282  0
             this.ddService = KRADServiceLocatorWeb.getDataDictionaryService();
 1283  
         }
 1284  0
         return ddService;
 1285  
     }
 1286  
 
 1287  
     public final void setDdService(DataDictionaryService ddService) {
 1288  0
         this.ddService = ddService;
 1289  0
     }
 1290  
 
 1291  
     protected final DictionaryValidationService getDictionaryValidationService() {
 1292  0
         if (dictionaryValidationService == null) {
 1293  0
             this.dictionaryValidationService = KRADServiceLocatorWeb.getDictionaryValidationService();
 1294  
         }
 1295  0
         return dictionaryValidationService;
 1296  
     }
 1297  
 
 1298  
     public final void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) {
 1299  0
         this.dictionaryValidationService = dictionaryValidationService;
 1300  0
     }
 1301  
     public org.kuali.rice.kim.service.PersonService getPersonService() {
 1302  0
         if (personService == null) {
 1303  0
             this.personService = KimApiServiceLocator.getPersonService();
 1304  
         }
 1305  0
         return personService;
 1306  
     }
 1307  
 
 1308  
     public void setPersonService(org.kuali.rice.kim.service.PersonService personService) {
 1309  0
         this.personService = personService;
 1310  0
     }
 1311  
 
 1312  
     public DateTimeService getDateTimeService() {
 1313  0
         return CoreApiServiceLocator.getDateTimeService();
 1314  
     }
 1315  
 
 1316  
     public DocumentHelperService getDocumentHelperService() {
 1317  0
         if (documentHelperService == null) {
 1318  0
             this.documentHelperService = KRADServiceLocatorWeb.getDocumentHelperService();
 1319  
         }
 1320  0
         return this.documentHelperService;
 1321  
     }
 1322  
 
 1323  
     public void setDocumentHelperService(DocumentHelperService documentHelperService) {
 1324  0
         this.documentHelperService = documentHelperService;
 1325  0
     }
 1326  
 
 1327  
     protected RoleService getRoleService() {
 1328  0
         if (this.roleService == null) {
 1329  0
             this.roleService = KimApiServiceLocator.getRoleService();
 1330  
         }
 1331  0
         return this.roleService;
 1332  
     }
 1333  
 
 1334  
     protected DataObjectMetaDataService getDataObjectMetaDataService() {
 1335  0
         if (dataObjectMetaDataService == null) {
 1336  0
             this.dataObjectMetaDataService = KRADServiceLocatorWeb.getDataObjectMetaDataService();
 1337  
         }
 1338  0
         return dataObjectMetaDataService;
 1339  
     }
 1340  
 
 1341  
     public void setDataObjectMetaDataService(DataObjectMetaDataService dataObjectMetaDataService) {
 1342  0
         this.dataObjectMetaDataService = dataObjectMetaDataService;
 1343  0
     }
 1344  
 
 1345  
     protected final PersistenceStructureService getPersistenceStructureService() {
 1346  0
         if (persistenceStructureService == null) {
 1347  0
             this.persistenceStructureService = KRADServiceLocator.getPersistenceStructureService();
 1348  
         }
 1349  0
         return persistenceStructureService;
 1350  
     }
 1351  
 
 1352  
     public final void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
 1353  0
         this.persistenceStructureService = persistenceStructureService;
 1354  0
     }
 1355  
 
 1356  
     public WorkflowDocumentService getWorkflowDocumentService() {
 1357  0
         if (workflowDocumentService == null) {
 1358  0
             this.workflowDocumentService = KRADServiceLocatorWeb.getWorkflowDocumentService();
 1359  
         }
 1360  0
         return workflowDocumentService;
 1361  
     }
 1362  
 
 1363  
     public void setWorkflowDocumentService(WorkflowDocumentService workflowDocumentService) {
 1364  0
         this.workflowDocumentService = workflowDocumentService;
 1365  0
     }
 1366  
 
 1367  
     public DataObjectAuthorizationService getDataObjectAuthorizationService() {
 1368  0
         if (dataObjectAuthorizationService == null) {
 1369  0
             this.dataObjectAuthorizationService = KRADServiceLocatorWeb.getDataObjectAuthorizationService();
 1370  
         }
 1371  0
         return dataObjectAuthorizationService;
 1372  
     }
 1373  
 
 1374  
     public void setDataObjectAuthorizationService(DataObjectAuthorizationService dataObjectAuthorizationService) {
 1375  0
         this.dataObjectAuthorizationService = dataObjectAuthorizationService;
 1376  0
     }
 1377  
 
 1378  
     public DocumentDictionaryService getDocumentDictionaryService() {
 1379  0
         if (documentDictionaryService == null) {
 1380  0
             this.documentDictionaryService = KRADServiceLocatorWeb.getDocumentDictionaryService();
 1381  
         }
 1382  0
         return documentDictionaryService;
 1383  
     }
 1384  
 
 1385  
     public void setDocumentDictionaryService(DocumentDictionaryService documentDictionaryService) {
 1386  0
         this.documentDictionaryService = documentDictionaryService;
 1387  0
     }
 1388  
 }
 1389