View Javadoc

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