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