Coverage Report - org.kuali.rice.kns.service.impl.MaintenanceDocumentDictionaryServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
MaintenanceDocumentDictionaryServiceImpl
0%
0/322
0%
0/202
4.375
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kns.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collection;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.kuali.rice.core.util.RiceKeyConstants;
 25  
 import org.kuali.rice.kew.dto.DocumentTypeDTO;
 26  
 import org.kuali.rice.kew.exception.WorkflowException;
 27  
 import org.kuali.rice.kns.bo.PersistableBusinessObject;
 28  
 import org.kuali.rice.kns.datadictionary.DataDictionary;
 29  
 import org.kuali.rice.kns.datadictionary.MaintainableCollectionDefinition;
 30  
 import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
 31  
 import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition;
 32  
 import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
 33  
 import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
 34  
 import org.kuali.rice.kns.document.MaintenanceDocument;
 35  
 import org.kuali.rice.kns.lookup.valuefinder.ValueFinder;
 36  
 import org.kuali.rice.kns.maintenance.Maintainable;
 37  
 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
 38  
 import org.kuali.rice.kns.rule.BusinessRule;
 39  
 import org.kuali.rice.kns.service.DataDictionaryService;
 40  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 41  
 import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
 42  
 import org.kuali.rice.kns.util.GlobalVariables;
 43  
 import org.kuali.rice.kns.util.ObjectUtils;
 44  
 
 45  
 /**
 46  
  * This class is the service implementation for the MaintenanceDocumentDictionary structure. Defines the API for the interacting
 47  
  * with Document-related entries in the data dictionary. This is the default implementation, that is delivered with Kuali.
 48  
  */
 49  0
 public class MaintenanceDocumentDictionaryServiceImpl implements MaintenanceDocumentDictionaryService {
 50  0
     protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MaintenanceDocumentDictionaryServiceImpl.class);
 51  
 
 52  
     private DataDictionaryService dataDictionaryService;
 53  
 
 54  
     /**
 55  
      * This method gets the workflow document type for the given documentTypeName
 56  
      * 
 57  
      * @param documentTypeName
 58  
      * @return
 59  
      */
 60  
     protected DocumentTypeDTO getDocumentType(String documentTypeName) {
 61  
         try {
 62  0
             return KNSServiceLocatorWeb.getWorkflowInfoService().getDocType(documentTypeName);
 63  0
         } catch (WorkflowException e) {
 64  0
             throw new RuntimeException("Caught exception attempting to get document type for doc type name '" + documentTypeName + "'", e);
 65  
         }
 66  
     }
 67  
 
 68  
     /**
 69  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getMaintenanceLabel(java.lang.String)
 70  
      */
 71  
     public String getMaintenanceLabel(String docTypeName) {
 72  0
         String label = null;
 73  
 
 74  0
         DocumentTypeDTO docType = getDocumentType(docTypeName);
 75  0
         if (docType != null) {
 76  0
             label = docType.getDocTypeLabel();
 77  
         }
 78  
 
 79  0
         return label;
 80  
     }
 81  
 
 82  
     /**
 83  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getMaintenanceDescription(java.lang.String)
 84  
      */
 85  
     public String getMaintenanceDescription(String docTypeName) {
 86  0
         String description = null;
 87  
 
 88  0
         DocumentTypeDTO docType = getDocumentType(docTypeName);
 89  0
         if (docType != null) {
 90  0
             description = docType.getDocTypeDescription();
 91  
         }
 92  
 
 93  0
         return description;
 94  
     }
 95  
 
 96  
     /**
 97  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getMaintainableClass(java.lang.String)
 98  
      */
 99  
     public Class getMaintainableClass(String docTypeName) {
 100  0
         Class maintainableClass = null;
 101  
 
 102  0
         MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(docTypeName);
 103  0
         if (entry != null) {
 104  0
             LOG.debug("suppling a generic Rule to insure basic validation");
 105  0
             maintainableClass = entry.getMaintainableClass();
 106  
         }
 107  
 
 108  0
         return maintainableClass;
 109  
     }
 110  
 
 111  
     /**
 112  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getBusinessObjectClass(java.lang.String)
 113  
      */
 114  
     public Class getBusinessObjectClass(String docTypeName) {
 115  0
         Class businessObjectClass = null;
 116  
 
 117  0
         MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(docTypeName);
 118  0
         if (entry != null) {
 119  0
             businessObjectClass = entry.getBusinessObjectClass();
 120  
         }
 121  
 
 122  0
         return businessObjectClass;
 123  
     }
 124  
 
 125  
     /**
 126  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getDocumentTypeName(java.lang.Class)
 127  
      */
 128  
     public String getDocumentTypeName(Class businessObjectClass) {
 129  0
         String documentTypeName = null;
 130  
 
 131  0
         MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(businessObjectClass);
 132  0
         if (entry != null) {
 133  0
             documentTypeName = entry.getDocumentTypeName();
 134  
         }
 135  
 
 136  0
         return documentTypeName;
 137  
     }
 138  
 
 139  
     /**
 140  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getMaintainableSections(java.lang.String)
 141  
      */
 142  
     public List getMaintainableSections(String docTypeName) {
 143  0
         List sections = null;
 144  
 
 145  0
         MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(docTypeName);
 146  0
         if (entry != null) {
 147  0
             sections = entry.getMaintainableSections();
 148  
         }
 149  
 
 150  0
         return sections;
 151  
     }
 152  
 
 153  
     /**
 154  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getBusinessRulesClass(MaintenanceDocument)
 155  
      */
 156  
     public Class<? extends BusinessRule> getBusinessRulesClass(MaintenanceDocument document) {
 157  0
         Maintainable maintainable = document.getOldMaintainableObject();
 158  0
         if (maintainable == null) {
 159  0
             throw new IllegalArgumentException("unable to determine documentType for maintenanceDocument with no oldMaintainableObject");
 160  
         }
 161  
 
 162  0
         Class<? extends BusinessRule> businessRulesClass = null;
 163  
 
 164  0
         MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(maintainable.getBoClass());
 165  0
         if (entry != null) {
 166  0
             businessRulesClass = entry.getBusinessRulesClass();
 167  
         }
 168  
 
 169  0
         if (businessRulesClass == null) {
 170  0
             return MaintenanceDocumentRuleBase.class; // default to a generic rule that will enforce Required fields
 171  
         }
 172  
 
 173  0
         LOG.info("return class: " + businessRulesClass.getName());
 174  
 
 175  0
         return businessRulesClass;
 176  
     }
 177  
 
 178  
     /**
 179  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getDefaultExistenceChecks(java.lang.Class)
 180  
      */
 181  
     public Collection getDefaultExistenceChecks(Class businessObjectClass) {
 182  0
         return getDefaultExistenceChecks(getDocumentTypeName(businessObjectClass));
 183  
     }
 184  
 
 185  
     /**
 186  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getDefaultExistenceChecks(java.lang.String)
 187  
      */
 188  
     public Collection getDefaultExistenceChecks(String docTypeName) {
 189  
 
 190  0
         Collection defaultExistenceChecks = null;
 191  
 
 192  0
         MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(docTypeName);
 193  0
         if (entry != null) {
 194  0
             defaultExistenceChecks = entry.getDefaultExistenceChecks();
 195  
         }
 196  
 
 197  0
         return defaultExistenceChecks;
 198  
     }
 199  
 
 200  
     /**
 201  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getLockingKeys(java.lang.String)
 202  
      */
 203  
     public List getLockingKeys(String docTypeName) {
 204  0
         List lockingKeys = null;
 205  
 
 206  0
         MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(docTypeName);
 207  0
         if (entry != null) {
 208  0
             lockingKeys = entry.getLockingKeyFieldNames();
 209  
         }
 210  
 
 211  0
         return lockingKeys;
 212  
     }
 213  
 
 214  
     /**
 215  
      * @param dataDictionaryService
 216  
      */
 217  
     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
 218  0
         this.dataDictionaryService = dataDictionaryService;
 219  0
     }
 220  
 
 221  
     /**
 222  
      * @return
 223  
      */
 224  
     public DataDictionary getDataDictionary() {
 225  0
         return this.dataDictionaryService.getDataDictionary();
 226  
     }
 227  
 
 228  
     /**
 229  
      * @param docTypeName
 230  
      * @return
 231  
      */
 232  
     public MaintenanceDocumentEntry getMaintenanceDocumentEntry(String docTypeName) {
 233  0
         if (StringUtils.isBlank(docTypeName)) {
 234  0
             throw new IllegalArgumentException("invalid (blank) docTypeName");
 235  
         }
 236  
 
 237  0
         MaintenanceDocumentEntry entry = (MaintenanceDocumentEntry)getDataDictionary().getDocumentEntry(docTypeName);
 238  0
         return entry;
 239  
     }
 240  
 
 241  
     private MaintenanceDocumentEntry getMaintenanceDocumentEntry(Class businessObjectClass) {
 242  0
         if (businessObjectClass == null) {
 243  0
             throw new IllegalArgumentException("invalid (blank) businessObjectClass");
 244  
         }
 245  
 
 246  0
         MaintenanceDocumentEntry entry = getDataDictionary().getMaintenanceDocumentEntryForBusinessObjectClass(businessObjectClass);
 247  0
         return entry;
 248  
     }
 249  
 
 250  
     /**
 251  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getFieldDefaultValue(java.lang.Class, java.lang.String)
 252  
      */
 253  
     public String getFieldDefaultValue(Class boClass, String fieldName) {
 254  
 
 255  
         // input parameter validation
 256  0
         if (boClass == null) {
 257  0
             throw new IllegalArgumentException("The boClass parameter value specified was " + "null.  A valid class representing the boClass must " + "be specified.");
 258  
         }
 259  
 
 260  
         // call the twin
 261  0
         return getFieldDefaultValue(getDocumentTypeName(boClass), fieldName);
 262  
     }
 263  
 
 264  
     /**
 265  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getFieldDefaultValue(java.lang.String, java.lang.String)
 266  
      */
 267  
     public String getFieldDefaultValue(String docTypeName, String fieldName) {
 268  
 
 269  
         // input parameter validation
 270  0
         if (StringUtils.isBlank(docTypeName)) {
 271  0
             throw new IllegalArgumentException("The docTypeName parameter value specified was  " + "blank, whitespace, or null.  A valid string representing the docTypeName must " + "be specified.");
 272  
         }
 273  0
         if (StringUtils.isBlank(fieldName)) {
 274  0
             throw new IllegalArgumentException("The fieldName parameter value specified was  " + "blank, whitespace, or null.  A valid string representing the fieldName must " + "be specified.");
 275  
         }
 276  
 
 277  
         // walk through the sections
 278  0
         List sections = getMaintainableSections(docTypeName);
 279  0
         for (Iterator sectionIterator = sections.iterator(); sectionIterator.hasNext();) {
 280  0
             MaintainableSectionDefinition section = (MaintainableSectionDefinition) sectionIterator.next();
 281  
 
 282  
             // walk through the fields
 283  0
             Collection fields = section.getMaintainableItems();
 284  0
             String defaultValue = getFieldDefaultValue(fields, fieldName);
 285  
             // need to keep trying sections until a match is found
 286  0
             if (defaultValue != null) {
 287  0
                 return defaultValue;
 288  
             }
 289  0
         }
 290  0
         return null;
 291  
     }
 292  
 
 293  
     private String getFieldDefaultValue(Collection maintainableFields, String fieldName) {
 294  0
         for (Iterator iterator = maintainableFields.iterator(); iterator.hasNext();) {
 295  0
             MaintainableItemDefinition item = (MaintainableItemDefinition) iterator.next();
 296  
             // only check fields...skip subcollections
 297  0
             if (item instanceof MaintainableFieldDefinition) {
 298  
 
 299  0
                 MaintainableFieldDefinition field = (MaintainableFieldDefinition) item;
 300  
 
 301  
                 // if the field name matches
 302  0
                 if (field.getName().endsWith(fieldName)) {
 303  
 
 304  
                     // preferentially take the raw default value
 305  0
                     if (StringUtils.isNotBlank(field.getDefaultValue())) {
 306  0
                         return field.getDefaultValue();
 307  
                     }
 308  
 
 309  
                     // take the valuefinder
 310  0
                     else if (field.getDefaultValueFinderClass() != null) {
 311  
 
 312  
                         // attempt to get an instance of the defaultValueFinderClass
 313  0
                         ValueFinder valueFinder = null;
 314  
                         try {
 315  0
                             valueFinder = (ValueFinder) field.getDefaultValueFinderClass().newInstance();
 316  
                         }
 317  0
                         catch (Exception e) {
 318  0
                             LOG.info("Exception obtaining valueFinder for collection field default value", e);
 319  0
                             valueFinder = null;
 320  0
                         }
 321  
 
 322  
                         // get the value
 323  0
                         if (valueFinder != null) {
 324  0
                             return valueFinder.getValue();
 325  
                         }
 326  0
                     }
 327  
                     // if we found the field, but no default anything, then we're done
 328  
                     else {
 329  0
                         return null;
 330  
                     }
 331  
                 }
 332  
             }
 333  0
         }
 334  0
         return null;
 335  
     }
 336  
 
 337  
     /**
 338  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getCollectionFieldDefaultValue(java.lang.String,
 339  
      *      java.lang.String, java.lang.String)
 340  
      */
 341  
     public String getCollectionFieldDefaultValue(String docTypeName, String collectionName, String fieldName) {
 342  
         // input parameter validation
 343  0
         if (StringUtils.isBlank(docTypeName)) {
 344  0
             throw new IllegalArgumentException("The docTypeName parameter value specified was blank, whitespace, or null.  A valid string representing the docTypeName must be specified.");
 345  
         }
 346  0
         if (StringUtils.isBlank(fieldName)) {
 347  0
             throw new IllegalArgumentException("The fieldName parameter value specified was blank, whitespace, or null.  A valid string representing the fieldName must be specified.");
 348  
         }
 349  0
         if (StringUtils.isBlank(collectionName)) {
 350  0
             throw new IllegalArgumentException("The collectionName parameter value specified was null.  A valid string representing the collectionName must be specified.");
 351  
         }
 352  
 
 353  0
         MaintainableCollectionDefinition coll = getMaintainableCollection(docTypeName, collectionName);
 354  0
         if (coll != null) {
 355  0
             Collection collectionFields = coll.getMaintainableFields();
 356  0
             return getFieldDefaultValue(collectionFields, fieldName);
 357  
         }
 358  0
         return null;
 359  
     }
 360  
 
 361  
     /**
 362  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getAllowsCopy(MaintenanceDocument)
 363  
      */
 364  
     public Boolean getAllowsCopy(MaintenanceDocument document) {
 365  0
         Boolean allowsCopy = Boolean.FALSE;
 366  0
         if (document != null && document.getNewMaintainableObject() != null) {
 367  0
             MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(document.getNewMaintainableObject().getBoClass());
 368  0
             if (entry != null) {
 369  0
                 allowsCopy = Boolean.valueOf(entry.getAllowsCopy());
 370  
             }
 371  
         }
 372  
 
 373  0
         return allowsCopy;
 374  
     }
 375  
 
 376  
     /**
 377  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getAllowsNewOrCopy(java.lang.String)
 378  
      */
 379  
     public Boolean getAllowsNewOrCopy(String docTypeName) {
 380  0
         Boolean allowsNewOrCopy = Boolean.FALSE;
 381  
 
 382  0
         if (docTypeName != null) {
 383  0
             MaintenanceDocumentEntry entry = getMaintenanceDocumentEntry(docTypeName);
 384  0
             if (entry != null) {
 385  0
                 allowsNewOrCopy = Boolean.valueOf(entry.getAllowsNewOrCopy());
 386  
             }
 387  
         }
 388  
 
 389  0
         return allowsNewOrCopy;
 390  
     }
 391  
 
 392  
     public MaintainableItemDefinition getMaintainableItem(String docTypeName, String itemName) {
 393  
         // input parameter validation
 394  0
         if (StringUtils.isBlank(docTypeName)) {
 395  0
             throw new IllegalArgumentException("The docTypeName parameter value specified was  " + "blank, whitespace, or null.  A valid string representing the docTypeName must " + "be specified.");
 396  
         }
 397  0
         if (StringUtils.isBlank(itemName)) {
 398  0
             throw new IllegalArgumentException("The itemName parameter value specified was  " + "blank, whitespace, or null.  A valid string representing the itemName must " + "be specified.");
 399  
         }
 400  
 
 401  
         // split name for subcollections
 402  0
         String[] subItems = {};
 403  0
         subItems = StringUtils.split(itemName, ".");
 404  
 
 405  
 
 406  
         // walk through the sections
 407  0
         List sections = getMaintainableSections(docTypeName);
 408  0
         for (Iterator sectionIterator = sections.iterator(); sectionIterator.hasNext();) {
 409  0
             MaintainableSectionDefinition section = (MaintainableSectionDefinition) sectionIterator.next();
 410  
 
 411  
             // walk through the fields
 412  0
             Collection fields = section.getMaintainableItems();
 413  0
             for (Iterator fieldIterator = fields.iterator(); fieldIterator.hasNext();) {
 414  0
                 MaintainableItemDefinition item = (MaintainableItemDefinition) fieldIterator.next();
 415  
 
 416  0
                 if (item.getName().equals(itemName)) {
 417  0
                     return item;
 418  
                 }
 419  
                 // if collection check to see if it has sub collections
 420  
                 // for now this only allows 1 level (i.e. a.b) it should be expanded at some point
 421  0
                 if (item instanceof MaintainableCollectionDefinition) {
 422  0
                     MaintainableCollectionDefinition col = (MaintainableCollectionDefinition) item;
 423  0
                     if ((subItems.length > 1) && (StringUtils.equals(col.getName(), subItems[0]))) {
 424  0
                         for (Iterator<MaintainableCollectionDefinition> colIterator = col.getMaintainableCollections().iterator(); colIterator.hasNext();) {
 425  0
                             MaintainableCollectionDefinition subCol = (MaintainableCollectionDefinition) colIterator.next();
 426  0
                             if (subCol.getName().equals(subItems[1])) {
 427  0
                                 return subCol;
 428  
                             }
 429  0
                         }
 430  
                     }
 431  
                 }
 432  0
             }
 433  0
         }
 434  0
         return null;
 435  
     }
 436  
 
 437  
     public MaintainableFieldDefinition getMaintainableField(String docTypeName, String fieldName) {
 438  0
         MaintainableItemDefinition item = getMaintainableItem(docTypeName, fieldName);
 439  0
         if (item != null && item instanceof MaintainableFieldDefinition) {
 440  0
             return (MaintainableFieldDefinition) item;
 441  
         }
 442  0
         return null;
 443  
     }
 444  
 
 445  
     public MaintainableCollectionDefinition getMaintainableCollection(String docTypeName, String collectionName) {
 446  
         // strip brackets as they are not needed to get to collection class
 447  
         // Like the other subcollections changes this currently only supports one sub level
 448  0
         if (StringUtils.contains(collectionName, "[")) {
 449  0
             collectionName = StringUtils.substringBefore(collectionName, "[") + StringUtils.substringAfter(collectionName, "]");
 450  
         }
 451  0
         MaintainableItemDefinition item = getMaintainableItem(docTypeName, collectionName);
 452  0
         if (item != null && item instanceof MaintainableCollectionDefinition) {
 453  0
             return (MaintainableCollectionDefinition) item;
 454  
         }
 455  0
         return null;
 456  
     }
 457  
 
 458  
     public Class getCollectionBusinessObjectClass(String docTypeName, String collectionName) {
 459  0
         MaintainableCollectionDefinition coll = getMaintainableCollection(docTypeName, collectionName);
 460  0
         if (coll != null) {
 461  0
             return coll.getBusinessObjectClass();
 462  
         }
 463  0
         return null;
 464  
     }
 465  
 
 466  
     public List<MaintainableCollectionDefinition> getMaintainableCollections(String docTypeName) {
 467  0
         ArrayList<MaintainableCollectionDefinition> collections = new ArrayList<MaintainableCollectionDefinition>();
 468  
 
 469  
         // walk through the sections
 470  0
         List sections = getMaintainableSections(docTypeName);
 471  0
         for (Iterator sectionIterator = sections.iterator(); sectionIterator.hasNext();) {
 472  0
             MaintainableSectionDefinition section = (MaintainableSectionDefinition) sectionIterator.next();
 473  
 
 474  
             // walk through the fields
 475  0
             Collection fields = section.getMaintainableItems();
 476  0
             for (Iterator fieldIterator = fields.iterator(); fieldIterator.hasNext();) {
 477  0
                 MaintainableItemDefinition item = (MaintainableItemDefinition) fieldIterator.next();
 478  
 
 479  0
                 if (item instanceof MaintainableCollectionDefinition) {
 480  0
                     collections.add((MaintainableCollectionDefinition) item);
 481  
                     // collections.addAll( getMaintainableCollections( (MaintainableCollectionDefinition)item ) );
 482  
                 }
 483  0
             }
 484  0
         }
 485  
 
 486  0
         return collections;
 487  
     }
 488  
 
 489  
     public List<MaintainableCollectionDefinition> getMaintainableCollections(MaintainableCollectionDefinition parentCollection) {
 490  0
         ArrayList<MaintainableCollectionDefinition> collections = new ArrayList<MaintainableCollectionDefinition>();
 491  
 
 492  
         // walk through the sections
 493  0
         Collection<MaintainableCollectionDefinition> colls = parentCollection.getMaintainableCollections();
 494  0
         for (MaintainableCollectionDefinition coll : colls) {
 495  0
             collections.add(coll);
 496  0
             collections.addAll(getMaintainableCollections(coll));
 497  
         }
 498  
 
 499  0
         return collections;
 500  
     }
 501  
 
 502  
     /**
 503  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#validateMaintenanceRequiredFields(org.kuali.rice.kns.document.MaintenanceDocument)
 504  
      */
 505  
     public void validateMaintenanceRequiredFields(MaintenanceDocument document) {
 506  0
         Maintainable newMaintainableObject = document.getNewMaintainableObject();
 507  0
         if (newMaintainableObject == null) {
 508  0
             LOG.error("New maintainable is null");
 509  0
             throw new RuntimeException("New maintainable is null");
 510  
         }
 511  
 
 512  0
         List<MaintainableSectionDefinition> maintainableSectionDefinitions = getMaintainableSections(getDocumentTypeName(newMaintainableObject.getBoClass()));
 513  0
         for (MaintainableSectionDefinition maintainableSectionDefinition : maintainableSectionDefinitions) {
 514  0
             for (MaintainableItemDefinition maintainableItemDefinition : maintainableSectionDefinition.getMaintainableItems()) {
 515  
                 // validate fields
 516  0
                 if (maintainableItemDefinition instanceof MaintainableFieldDefinition) {
 517  0
                     validateMaintainableFieldRequiredFields((MaintainableFieldDefinition) maintainableItemDefinition, newMaintainableObject.getBusinessObject(), maintainableItemDefinition.getName());
 518  
                 }
 519  
                 // validate collections
 520  0
                 else if (maintainableItemDefinition instanceof MaintainableCollectionDefinition) {
 521  0
                     validateMaintainableCollectionsRequiredFields(newMaintainableObject.getBusinessObject(), (MaintainableCollectionDefinition) maintainableItemDefinition);
 522  
                 }
 523  
             }
 524  
         }
 525  0
     }
 526  
 
 527  
     /**
 528  
      * generates error message if a field is marked as required but is not filled in
 529  
      * 
 530  
      * @param maintainableFieldDefinition
 531  
      * @param businessObject
 532  
      * @param fieldName
 533  
      */
 534  
     private void validateMaintainableFieldRequiredFields(MaintainableFieldDefinition maintainableFieldDefinition, PersistableBusinessObject businessObject, String fieldName) {
 535  
 
 536  0
         if (StringUtils.isBlank(fieldName)) {
 537  0
             throw new IllegalArgumentException("invalid fieldName parameter.");
 538  
         }
 539  
         // if required check we have a value for this field
 540  0
         if (maintainableFieldDefinition.isRequired() && !maintainableFieldDefinition.isUnconditionallyReadOnly() ) {
 541  
             try {
 542  0
                 Object obj = ObjectUtils.getNestedValue(businessObject, fieldName);
 543  
 
 544  0
                 if (obj == null || StringUtils.isBlank(obj.toString())) {
 545  0
                     String attributeLabel = dataDictionaryService.getAttributeLabel(businessObject.getClass(), fieldName);
 546  0
                     String shortLabel = dataDictionaryService.getAttributeShortLabel(businessObject.getClass(), fieldName);
 547  0
                     GlobalVariables.getMessageMap().putError(fieldName, RiceKeyConstants.ERROR_REQUIRED, attributeLabel + " (" + shortLabel + ")" );
 548  0
                 } else if ( fieldName.endsWith(".principalName") ) {
 549  
                     // special handling to catch when the principalName is not really a valid user
 550  
                     // pull the Person object and test the entity ID.  If that's null, then this
 551  
                     // is just a shell user instance and does not represent a true user
 552  
                     // the main principalId property on the main object would be null at this point
 553  
                     // but it is also unconditionally read only and not tested - checking that would
 554  
                     // require checking the relationships and be more complex than we want to get here
 555  0
                     String personProperty = ObjectUtils.getNestedAttributePrefix(fieldName); 
 556  0
                     if ( StringUtils.isNotBlank(personProperty) ) {
 557  0
                         if ( StringUtils.isBlank( (String)ObjectUtils.getNestedValue(businessObject, personProperty+".entityId") ) ) {
 558  0
                             String attributeLabel = dataDictionaryService.getAttributeLabel(businessObject.getClass(), fieldName);
 559  0
                             GlobalVariables.getMessageMap().putError(fieldName, RiceKeyConstants.ERROR_EXISTENCE, attributeLabel );
 560  
                         }
 561  
                     }
 562  
                 }
 563  0
             } catch( Exception ex ) {
 564  0
                 LOG.error( "unable to read property during doc required field checks", ex );
 565  0
             }
 566  
         }
 567  0
     }
 568  
 
 569  
     
 570  
     private MaintainableCollectionDefinition getCollectionDefinition( String docTypeName, String collectionName ) {
 571  0
         String currentCollection = collectionName;
 572  0
         String nestedCollections = "";
 573  0
             if (StringUtils.contains(collectionName, "[")) {
 574  
                     // strip off any array indexes
 575  0
             currentCollection = StringUtils.substringBefore( collectionName, "[" );
 576  0
             nestedCollections = StringUtils.substringAfter( collectionName, "." );
 577  
             }
 578  
             
 579  
         // loop over all sections to find this collection
 580  0
         List<MaintainableSectionDefinition> maintainableSectionDefinitions = getMaintainableSections( docTypeName );
 581  0
         for (MaintainableSectionDefinition maintainableSectionDefinition : maintainableSectionDefinitions) {
 582  0
             for (MaintainableItemDefinition maintainableItemDefinition : maintainableSectionDefinition.getMaintainableItems()) {
 583  0
                 if (maintainableItemDefinition instanceof MaintainableCollectionDefinition && maintainableItemDefinition.getName().equals( currentCollection ) ) {
 584  0
                     if ( StringUtils.isBlank( nestedCollections ) ) {
 585  0
                         return (MaintainableCollectionDefinition) maintainableItemDefinition;
 586  
                     } 
 587  
                     
 588  0
                     return getCollectionDefinition( (MaintainableCollectionDefinition)maintainableItemDefinition, nestedCollections );
 589  
                 }
 590  
             }
 591  
         }
 592  
         
 593  0
         return null;
 594  
     }
 595  
 
 596  
     private MaintainableCollectionDefinition getCollectionDefinition( MaintainableCollectionDefinition collectionDef, String collectionName ) {
 597  0
         String currentCollection = collectionName;
 598  0
         String nestedCollections = "";
 599  0
             if (StringUtils.contains(collectionName, "[")) {
 600  
                     // strip off any array indexes
 601  0
             currentCollection = StringUtils.substringBefore( collectionName, "[" );
 602  0
             nestedCollections = StringUtils.substringAfter( collectionName, "." );
 603  
             }
 604  
         
 605  
         // loop over all nested collections
 606  0
         for (MaintainableCollectionDefinition maintainableCollectionDefinition : collectionDef.getMaintainableCollections()) {
 607  0
             if ( maintainableCollectionDefinition.getName().equals( currentCollection ) ) {
 608  0
                 if ( StringUtils.isBlank( nestedCollections ) ) {
 609  0
                     return maintainableCollectionDefinition;
 610  
                 } 
 611  0
                 return getCollectionDefinition( maintainableCollectionDefinition, nestedCollections );
 612  
             }
 613  
         }
 614  
         
 615  0
         return null;
 616  
     }
 617  
     
 618  
     public void validateMaintainableCollectionsAddLineRequiredFields(MaintenanceDocument document, PersistableBusinessObject businessObject, String collectionName ) {
 619  0
         MaintainableCollectionDefinition def = getCollectionDefinition( getDocumentTypeName(businessObject.getClass()), collectionName );
 620  0
         if ( def != null ) {
 621  0
             validateMaintainableCollectionsAddLineRequiredFields( document, businessObject, collectionName, def, 0);
 622  
         }
 623  0
     }
 624  
     /**
 625  
      * calls code to generate error messages if maintainableFields within any collections or sub-collections are marked as required
 626  
      * 
 627  
      * @param document
 628  
      * @param businessObject
 629  
      * @param collectionName
 630  
      * @param maintainableCollectionDefinition
 631  
      * @param depth
 632  
      */
 633  
     private void validateMaintainableCollectionsAddLineRequiredFields(MaintenanceDocument document, PersistableBusinessObject businessObject, String collectionName, MaintainableCollectionDefinition maintainableCollectionDefinition, int depth) {
 634  0
         if ( depth == 0 ) {
 635  0
             GlobalVariables.getMessageMap().addToErrorPath("add");
 636  
         }
 637  
         // validate required fields on fields withing collection definition
 638  0
         PersistableBusinessObject element = document.getNewMaintainableObject().getNewCollectionLine( collectionName );
 639  0
         GlobalVariables.getMessageMap().addToErrorPath(collectionName);
 640  0
         for (MaintainableFieldDefinition maintainableFieldDefinition : maintainableCollectionDefinition.getMaintainableFields()) {
 641  0
             final String fieldName = maintainableFieldDefinition.getName();
 642  0
             validateMaintainableFieldRequiredFields(maintainableFieldDefinition, element, fieldName);
 643  
             
 644  0
         }
 645  
 
 646  0
         GlobalVariables.getMessageMap().removeFromErrorPath(collectionName);
 647  0
         if ( depth == 0 ) {
 648  0
             GlobalVariables.getMessageMap().removeFromErrorPath("add");
 649  
         }
 650  0
     }
 651  
 
 652  
     /**
 653  
      * calls code to generate error messages if maintainableFields within any collections or sub-collections are marked as required
 654  
      * 
 655  
      * @param businessObject
 656  
      * @param maintainableCollectionDefinition
 657  
      */
 658  
     private void validateMaintainableCollectionsRequiredFields(PersistableBusinessObject businessObject, MaintainableCollectionDefinition maintainableCollectionDefinition) {
 659  0
         final String collectionName = maintainableCollectionDefinition.getName();
 660  
 
 661  
         // validate required fields on fields withing collection definition
 662  0
         Collection<PersistableBusinessObject> collection = (Collection) ObjectUtils.getPropertyValue(businessObject, collectionName);
 663  0
         if (collection != null && !collection.isEmpty()) {
 664  0
             for (MaintainableFieldDefinition maintainableFieldDefinition : maintainableCollectionDefinition.getMaintainableFields()) {
 665  0
                 int pos = 0;
 666  0
                 final String fieldName = maintainableFieldDefinition.getName();
 667  0
                 for (PersistableBusinessObject element : collection) {
 668  0
                     String parentName = collectionName + "[" + (pos++) + "]";
 669  0
                     GlobalVariables.getMessageMap().addToErrorPath(parentName);
 670  0
                     validateMaintainableFieldRequiredFields(maintainableFieldDefinition, element, fieldName);
 671  0
                     GlobalVariables.getMessageMap().removeFromErrorPath(parentName);
 672  0
                 }
 673  0
             }
 674  
 
 675  
             // recursivley validate required fields on subcollections
 676  0
             GlobalVariables.getMessageMap().addToErrorPath(collectionName);
 677  0
             for (MaintainableCollectionDefinition nestedMaintainableCollectionDefinition : maintainableCollectionDefinition.getMaintainableCollections()) {
 678  0
                 for (PersistableBusinessObject element : collection) {
 679  0
                     validateMaintainableCollectionsRequiredFields(element, nestedMaintainableCollectionDefinition);
 680  
                 }
 681  
             }
 682  0
             GlobalVariables.getMessageMap().removeFromErrorPath(collectionName);
 683  
         }
 684  0
     }
 685  
     
 686  
     /**
 687  
      * default implementation checks for duplicats based on keys of objects only
 688  
      * 
 689  
      * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#validateMaintainableCollectionsForDuplicateEntries(org.kuali.rice.kns.document.MaintenanceDocument)
 690  
      */
 691  
     public void validateMaintainableCollectionsForDuplicateEntries(MaintenanceDocument document) {
 692  0
         Maintainable newMaintainableObject = document.getNewMaintainableObject();
 693  0
         if (newMaintainableObject == null) {
 694  0
             LOG.error("New maintainable is null");
 695  0
             throw new RuntimeException("New maintainable is null");
 696  
         }
 697  
 
 698  0
         List<MaintainableSectionDefinition> maintainableSectionDefinitions = getMaintainableSections(getDocumentTypeName(newMaintainableObject.getBoClass()));
 699  0
         for (MaintainableSectionDefinition maintainableSectionDefinition : maintainableSectionDefinitions) {
 700  0
             for (MaintainableItemDefinition maintainableItemDefinition : maintainableSectionDefinition.getMaintainableItems()) {
 701  
                 // validate collections
 702  0
                 if (maintainableItemDefinition instanceof MaintainableCollectionDefinition) {
 703  0
                     validateMaintainableCollectionsForDuplicateEntries(newMaintainableObject.getBusinessObject(), (MaintainableCollectionDefinition) maintainableItemDefinition);
 704  
                 }
 705  
             }
 706  
         }
 707  0
     }
 708  
 
 709  
     /**
 710  
      * recursivly checks collections for duplicate entries based on key valuse
 711  
      * 
 712  
      * @param businessObject
 713  
      * @param maintainableCollectionDefinition
 714  
      */
 715  
     private void validateMaintainableCollectionsForDuplicateEntries(PersistableBusinessObject businessObject, MaintainableCollectionDefinition maintainableCollectionDefinition) {
 716  0
         final String collectionName = maintainableCollectionDefinition.getName();
 717  
 
 718  0
         if (maintainableCollectionDefinition.dissallowDuplicateKey()) {
 719  0
             final Class maintainableBusinessObjectClass = businessObject.getClass();
 720  
             // validate that no duplicates based on keys exist
 721  0
             Collection<PersistableBusinessObject> collection = (Collection) ObjectUtils.getPropertyValue(businessObject, collectionName);
 722  0
             if (collection != null && !collection.isEmpty()) {
 723  0
                 final String propertyName = maintainableCollectionDefinition.getAttributeToHighlightOnDuplicateKey();
 724  
                 // get collection label for dd
 725  0
                 final String label = dataDictionaryService.getCollectionLabel(maintainableBusinessObjectClass, collectionName);
 726  0
                 final String shortLabel = dataDictionaryService.getCollectionShortLabel(maintainableBusinessObjectClass, collectionName);
 727  0
                 int pos = 0;
 728  0
                 for (PersistableBusinessObject element : collection) {
 729  0
                     String pathToElement = collectionName + "[" + (pos++) + "]";
 730  0
                     if (ObjectUtils.countObjectsWithIdentitcalKey(collection, element) > 1) {
 731  0
                         GlobalVariables.getMessageMap().addToErrorPath(pathToElement);
 732  0
                         GlobalVariables.getMessageMap().putError(propertyName, RiceKeyConstants.ERROR_DUPLICATE_ELEMENT, new String[] { label, shortLabel });
 733  0
                         GlobalVariables.getMessageMap().removeFromErrorPath(pathToElement);
 734  
                     }
 735  0
                 }
 736  
 
 737  
                 // recursivley check for duplicate entries on subcollections
 738  0
                 GlobalVariables.getMessageMap().addToErrorPath(collectionName);
 739  0
                 for (MaintainableCollectionDefinition nestedMaintainableCollectionDefinition : maintainableCollectionDefinition.getMaintainableCollections()) {
 740  0
                     for (PersistableBusinessObject element : collection) {
 741  0
                         validateMaintainableCollectionsForDuplicateEntries(element, nestedMaintainableCollectionDefinition);
 742  
                     }
 743  
                 }
 744  0
                 GlobalVariables.getMessageMap().removeFromErrorPath(collectionName);
 745  
 
 746  
             }
 747  
         }
 748  0
     }
 749  
        
 750  
         /**
 751  
          * for issue KULRice 3072
 752  
          * 
 753  
          * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getgetPreserveLockingKeysOnCopy(java.lang.Class)
 754  
          */
 755  
         public boolean getPreserveLockingKeysOnCopy(Class businessObjectClass) {
 756  
 
 757  0
                 boolean preserveLockingKeysOnCopy = false;
 758  
 
 759  0
                 MaintenanceDocumentEntry docEntry = getMaintenanceDocumentEntry(businessObjectClass);
 760  
                 
 761  0
                 if (docEntry != null) {
 762  0
                         preserveLockingKeysOnCopy = docEntry.getPreserveLockingKeysOnCopy();
 763  
                 }
 764  
                 
 765  0
                 return preserveLockingKeysOnCopy;
 766  
         }
 767  
 
 768  
         /**
 769  
          * for isue KULRice 3070
 770  
          * 
 771  
          * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getAllowsRecordDeletion(java.lang.Class)
 772  
          */
 773  
         public Boolean getAllowsRecordDeletion(Class businessObjectClass) {
 774  
                 
 775  0
                 Boolean allowsRecordDeletion = Boolean.FALSE;
 776  
 
 777  0
                 MaintenanceDocumentEntry docEntry = getMaintenanceDocumentEntry(businessObjectClass);
 778  
                 
 779  0
                 if (docEntry != null) {
 780  0
                         allowsRecordDeletion = Boolean.valueOf(docEntry.getAllowsRecordDeletion());
 781  
                 }
 782  
                 
 783  0
                 return allowsRecordDeletion;
 784  
         }
 785  
 
 786  
         /**
 787  
          *  for issue KULRice3070, see if need delete button
 788  
          * 
 789  
          * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#getAllowsRecordDeletion(org.kuali.rice.kns.document.MaintenanceDocument)
 790  
          */
 791  
         public Boolean getAllowsRecordDeletion(MaintenanceDocument document) {
 792  0
         return document != null ? this.getAllowsRecordDeletion(document.getNewMaintainableObject().getBoClass()) : Boolean.FALSE;
 793  
         }
 794  
 
 795  
         /**
 796  
          * @see org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService#translateCodes(java.lang.Class)
 797  
          */
 798  
         public Boolean translateCodes(Class businessObjectClass) {
 799  0
                 boolean translateCodes = false;
 800  
 
 801  0
                 MaintenanceDocumentEntry docEntry = getMaintenanceDocumentEntry(businessObjectClass);
 802  
 
 803  0
                 if (docEntry != null) {
 804  0
                         translateCodes = docEntry.isTranslateCodes();
 805  
                 }
 806  
 
 807  0
                 return translateCodes;
 808  
         }
 809  
 
 810  
 }