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