Coverage Report - org.kuali.rice.krad.service.impl.BusinessObjectServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
BusinessObjectServiceImpl
0%
0/160
0%
0/86
2.939
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.kuali.rice.krad.service.impl;
 18  
 
 19  
 import org.apache.commons.beanutils.PropertyUtils;
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 22  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 23  
 import org.kuali.rice.kim.bo.Person;
 24  
 import org.kuali.rice.kim.service.PersonService;
 25  
 import org.kuali.rice.kns.service.BusinessObjectMetaDataService;
 26  
 import org.kuali.rice.krad.bo.BusinessObject;
 27  
 import org.kuali.rice.krad.bo.BusinessObjectRelationship;
 28  
 import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
 29  
 import org.kuali.rice.krad.bo.PersistableBusinessObject;
 30  
 import org.kuali.rice.krad.dao.BusinessObjectDao;
 31  
 import org.kuali.rice.krad.exception.ObjectNotABusinessObjectRuntimeException;
 32  
 import org.kuali.rice.krad.exception.ReferenceAttributeDoesntExistException;
 33  
 import org.kuali.rice.krad.service.BusinessObjectService;
 34  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 35  
 import org.kuali.rice.krad.service.ModuleService;
 36  
 import org.kuali.rice.krad.service.PersistenceService;
 37  
 import org.kuali.rice.krad.service.PersistenceStructureService;
 38  
 import org.kuali.rice.krad.util.KRADConstants;
 39  
 import org.kuali.rice.krad.util.ObjectUtils;
 40  
 import org.springframework.transaction.annotation.Transactional;
 41  
 
 42  
 import java.beans.PropertyDescriptor;
 43  
 import java.util.Collection;
 44  
 import java.util.Collections;
 45  
 import java.util.HashMap;
 46  
 import java.util.HashSet;
 47  
 import java.util.List;
 48  
 import java.util.Map;
 49  
 import java.util.Set;
 50  
 
 51  
 /**
 52  
  * This class is the service implementation for the BusinessObjectService structure. This is the default implementation, that is
 53  
  * delivered with Kuali.
 54  
  */
 55  
 
 56  0
 public class BusinessObjectServiceImpl implements BusinessObjectService {
 57  
 
 58  
     private PersistenceService persistenceService;
 59  
     private PersistenceStructureService persistenceStructureService;
 60  
     private BusinessObjectDao businessObjectDao;
 61  
     private PersonService personService;
 62  
     private BusinessObjectMetaDataService businessObjectMetaDataService;
 63  
 
 64  
     private boolean illegalBusinessObjectsForSaveInitialized;
 65  0
     private final Set<String> illegalBusinessObjectsForSave = new HashSet<String>();
 66  
     
 67  
     @Override
 68  
     @Transactional
 69  
     public <T extends PersistableBusinessObject> T save(T bo) {
 70  0
             validateBusinessObjectForSave(bo);
 71  0
         return (T) businessObjectDao.save(bo);
 72  
     }
 73  
 
 74  
     @Override
 75  
     @Transactional
 76  
     public List<? extends PersistableBusinessObject> save(List<? extends PersistableBusinessObject> businessObjects) {
 77  0
         validateBusinessObjectForSave(businessObjects);
 78  0
         return businessObjectDao.save(businessObjects);
 79  
     }
 80  
 
 81  
     @Override
 82  
     @Transactional
 83  
     public PersistableBusinessObject linkAndSave(PersistableBusinessObject bo) {
 84  0
             validateBusinessObjectForSave(bo);
 85  0
         persistenceService.linkObjects(bo);
 86  0
         return businessObjectDao.save(bo);
 87  
     }
 88  
 
 89  
     @Override
 90  
     @Transactional
 91  
     public List<? extends PersistableBusinessObject> linkAndSave(List<? extends PersistableBusinessObject> businessObjects) {
 92  0
         validateBusinessObjectForSave(businessObjects);
 93  0
         return businessObjectDao.save(businessObjects);
 94  
     }
 95  
 
 96  
     protected void validateBusinessObjectForSave(PersistableBusinessObject bo) {
 97  0
             if (bo == null) {
 98  0
             throw new IllegalArgumentException("Object passed in is null");
 99  
         }
 100  0
         if (!isBusinessObjectAllowedForSave(bo)) {
 101  0
                 throw new IllegalArgumentException("Object passed in is a BusinessObject but has been restricted from save operations according to configuration parameter '" + KRADConstants.Config.ILLEGAL_BUSINESS_OBJECTS_FOR_SAVE);
 102  
         }
 103  0
     }
 104  
     
 105  
     protected void validateBusinessObjectForSave(List<? extends PersistableBusinessObject> businessObjects) {
 106  0
             for (PersistableBusinessObject bo : businessObjects) {
 107  0
                      if (bo == null) {
 108  0
                  throw new IllegalArgumentException("One of the objects in the List is null.");
 109  
              }
 110  0
                      if (!isBusinessObjectAllowedForSave(bo)) {
 111  0
                              throw new IllegalArgumentException("One of the objects in the List is a BusinessObject but has been restricted from save operations according to configuration parameter '" + KRADConstants.Config.ILLEGAL_BUSINESS_OBJECTS_FOR_SAVE
 112  
                                              + "  Passed in type was '" + bo.getClass().getName() + "'.");
 113  
                      }
 114  
             }
 115  0
     }
 116  
     
 117  
     
 118  
     /**
 119  
      * Returns true if the BusinessObjectService should be permitted to save instances of the given PersistableBusinessObject.
 120  
      * Implementation checks a configuration parameter for class names of PersistableBusinessObjects that shouldn't be allowed
 121  
      * to be saved.
 122  
      */
 123  
     protected boolean isBusinessObjectAllowedForSave(PersistableBusinessObject bo) {
 124  0
             if (!illegalBusinessObjectsForSaveInitialized) {
 125  0
                     synchronized (this) {
 126  0
                             boolean applyCheck = true;
 127  0
                             String applyCheckValue = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.Config.APPLY_ILLEGAL_BUSINESS_OBJECT_FOR_SAVE_CHECK);
 128  0
                             if (!StringUtils.isEmpty(applyCheckValue)) {
 129  0
                                     applyCheck = Boolean.valueOf(applyCheckValue);
 130  
                             }
 131  0
                             if (applyCheck) {
 132  0
                                     String illegalBos = ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.Config.ILLEGAL_BUSINESS_OBJECTS_FOR_SAVE);
 133  0
                                     if (!StringUtils.isEmpty(illegalBos)) {
 134  0
                                             String[] illegalBosSplit = illegalBos.split(",");
 135  0
                                             for (String illegalBo : illegalBosSplit) {
 136  0
                                                     illegalBusinessObjectsForSave.add(illegalBo.trim());
 137  
                                             }
 138  
                                     }
 139  
                             }
 140  0
                     }
 141  0
                     illegalBusinessObjectsForSaveInitialized = true;
 142  
             }
 143  0
             return !illegalBusinessObjectsForSave.contains(bo.getClass().getName());
 144  
     }
 145  
     
 146  
 
 147  
     @Override
 148  
         public <T extends BusinessObject> T findBySinglePrimaryKey(Class<T> clazz, Object primaryKey) {
 149  0
                 return businessObjectDao.findBySinglePrimaryKey(clazz, primaryKey);
 150  
         }
 151  
     @Override
 152  
     public <T extends BusinessObject> T findByPrimaryKey(Class<T> clazz, Map<String, ?> primaryKeys) {
 153  0
         return businessObjectDao.findByPrimaryKey(clazz, primaryKeys);
 154  
     }
 155  
 
 156  
     @Override
 157  
     public PersistableBusinessObject retrieve(PersistableBusinessObject object) {
 158  0
         return businessObjectDao.retrieve(object);
 159  
     }
 160  
 
 161  
     @Override
 162  
     public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz) {
 163  0
         return businessObjectDao.findAll(clazz);
 164  
     }
 165  
     @Override
 166  
     public <T extends BusinessObject> Collection<T> findAllOrderBy( Class<T> clazz, String sortField, boolean sortAscending ) {
 167  0
             final Map<String, ?> emptyParameters = Collections.emptyMap();
 168  0
             return businessObjectDao.findMatchingOrderBy(clazz, emptyParameters, sortField, sortAscending );
 169  
     }
 170  
     
 171  
     @Override
 172  
     public <T extends BusinessObject> Collection<T> findMatching(Class<T> clazz, Map<String, ?> fieldValues) {
 173  0
         return businessObjectDao.findMatching(clazz, fieldValues);
 174  
     }
 175  
 
 176  
     @Override
 177  
     public int countMatching(Class clazz, Map<String, ?> fieldValues) {
 178  0
         return businessObjectDao.countMatching(clazz, fieldValues);
 179  
     }
 180  
 
 181  
     @Override
 182  
     public int countMatching(Class clazz, Map<String, ?> positiveFieldValues, Map<String, ?> negativeFieldValues) {
 183  0
         return businessObjectDao.countMatching(clazz, positiveFieldValues, negativeFieldValues);
 184  
     }
 185  
     @Override
 186  
     public <T extends BusinessObject> Collection<T> findMatchingOrderBy(Class<T> clazz, Map<String, ?> fieldValues, String sortField, boolean sortAscending) {
 187  0
         return businessObjectDao.findMatchingOrderBy(clazz, fieldValues, sortField, sortAscending);
 188  
     }
 189  
     @Override
 190  
     @Transactional
 191  
     public void delete(PersistableBusinessObject bo) {
 192  0
         businessObjectDao.delete(bo);
 193  0
     }
 194  
 
 195  
     @Override
 196  
     @Transactional
 197  
     public void delete(List<? extends PersistableBusinessObject> boList) {
 198  0
         businessObjectDao.delete(boList);
 199  0
     }
 200  
 
 201  
     @Override
 202  
     @Transactional
 203  
     public void deleteMatching(Class clazz, Map<String, ?> fieldValues) {
 204  0
         businessObjectDao.deleteMatching(clazz, fieldValues);
 205  0
     }
 206  
 
 207  
     @Override
 208  
     public BusinessObject getReferenceIfExists(BusinessObject bo, String referenceName) {
 209  
         // if either argument is null, then we have nothing to do, complain and abort
 210  0
         if (ObjectUtils.isNull(bo)) {
 211  0
             throw new IllegalArgumentException("Passed in BusinessObject was null.  No processing can be done.");
 212  
         }
 213  0
         if (StringUtils.isEmpty(referenceName)) {
 214  0
             throw new IllegalArgumentException("Passed in referenceName was empty or null.  No processing can be done.");
 215  
         }
 216  
 
 217  
         // make sure the attribute exists at all, throw exception if not
 218  
         PropertyDescriptor propertyDescriptor;
 219  
         try {
 220  0
             propertyDescriptor = PropertyUtils.getPropertyDescriptor(bo, referenceName);
 221  
         }
 222  0
         catch (Exception e) {
 223  0
             throw new RuntimeException(e);
 224  0
         }
 225  0
         if (propertyDescriptor == null) {
 226  0
             throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + referenceName + "' does not exist " + "on class: '" + bo.getClass().getName() + "'. GFK");
 227  
         }
 228  
 
 229  
         // get the class of the attribute name
 230  0
         Class referenceClass = ObjectUtils.getPropertyType( bo, referenceName, persistenceStructureService );
 231  0
         if ( referenceClass == null ) {
 232  0
                 referenceClass = propertyDescriptor.getPropertyType();
 233  
         }
 234  
 
 235  
         /*
 236  
          * check for Person or EBO references in which case we can just get the reference through propertyutils
 237  
          */
 238  0
         if (ExternalizableBusinessObject.class.isAssignableFrom(referenceClass)) {
 239  
             try {
 240  0
                     BusinessObject referenceBoExternalizable = (BusinessObject) PropertyUtils.getProperty(bo, referenceName);
 241  0
                     if (referenceBoExternalizable!=null) {
 242  0
                             return referenceBoExternalizable;
 243  
                 }
 244  0
             } catch (Exception ex) {
 245  
                 //throw new RuntimeException("Unable to get property " + referenceName + " from a BO of class: " + bo.getClass().getName(),ex);
 246  
                     //Proceed further - get the BO relationship using responsible module service and proceed further
 247  0
             }
 248  
         }
 249  
 
 250  
         // make sure the class of the attribute descends from BusinessObject,
 251  
         // otherwise throw an exception
 252  0
         if (!ExternalizableBusinessObject.class.isAssignableFrom(referenceClass) && !PersistableBusinessObject.class.isAssignableFrom(referenceClass)) {
 253  0
             throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + referenceName + ") is of class: " + "'" + referenceClass.getName() + "' and is not a " + "descendent of PersistableBusinessObject.  Only descendents of PersistableBusinessObject " + "can be used.");
 254  
         }
 255  
 
 256  
         // get the list of foreign-keys for this reference. if the reference
 257  
         // does not exist, or is not a reference-descriptor, an exception will
 258  
         // be thrown here.
 259  0
         BusinessObjectRelationship boRel = businessObjectMetaDataService.getBusinessObjectRelationship( bo, referenceName );
 260  0
         final Map<String,String> fkMap = boRel != null ? boRel.getParentToChildReferences() : Collections.<String, String>emptyMap();
 261  
 
 262  0
         boolean allFkeysHaveValues = true;
 263  
         // walk through the foreign keys, testing each one to see if it has a value
 264  0
         Map<String,Object> pkMap = new HashMap<String,Object>();
 265  0
         for (Map.Entry<String, String> entry : fkMap.entrySet()) {
 266  0
             String fkFieldName = entry.getKey();
 267  0
             String pkFieldName = entry.getValue();
 268  
 
 269  
             // attempt to retrieve the value for the given field
 270  
             Object fkFieldValue;
 271  
             try {
 272  0
                 fkFieldValue = PropertyUtils.getProperty(bo, fkFieldName);
 273  
             }
 274  0
             catch (Exception e) {
 275  0
                 throw new RuntimeException(e);
 276  0
             }
 277  
 
 278  
             // determine if there is a value for the field
 279  0
             if (ObjectUtils.isNull(fkFieldValue)) {
 280  0
                 allFkeysHaveValues = false;
 281  0
                 break; // no reason to continue processing the fkeys
 282  
             }
 283  0
             else if (String.class.isAssignableFrom(fkFieldValue.getClass())) {
 284  0
                 if (StringUtils.isEmpty((String) fkFieldValue)) {
 285  0
                     allFkeysHaveValues = false;
 286  0
                     break;
 287  
                 }
 288  
                 else {
 289  0
                     pkMap.put(pkFieldName, fkFieldValue);
 290  
                 }
 291  
             }
 292  
 
 293  
             // if there is a value, grab it
 294  
             else {
 295  0
                 pkMap.put(pkFieldName, fkFieldValue);
 296  
             }
 297  0
         }
 298  
 
 299  0
         BusinessObject referenceBo = null;
 300  
         // only do the retrieval if all Foreign Keys have values
 301  0
         if (allFkeysHaveValues) {
 302  0
                 if (ExternalizableBusinessObject.class.isAssignableFrom(referenceClass)) {
 303  0
                         ModuleService responsibleModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(referenceClass);
 304  0
                                 if(responsibleModuleService!=null) {
 305  0
                                         return responsibleModuleService.<ExternalizableBusinessObject>getExternalizableBusinessObject(referenceClass, pkMap);
 306  
                                 }
 307  0
                 } else
 308  0
                         referenceBo = this.<BusinessObject>findByPrimaryKey(referenceClass, pkMap);
 309  
         }
 310  
 
 311  
         // return what we have, it'll be null if it was never retrieved
 312  0
         return referenceBo;
 313  
     }
 314  
     @Override
 315  
     public void linkUserFields(PersistableBusinessObject bo) {
 316  0
         if (bo == null) {
 317  0
             throw new IllegalArgumentException("bo passed in was null");
 318  
         }
 319  
 
 320  0
         bo.linkEditableUserFields();
 321  
        
 322  0
         linkUserFields( Collections.singletonList( bo ) );
 323  0
     }
 324  
 
 325  
     @Override
 326  
     public void linkUserFields(List<PersistableBusinessObject> bos) {
 327  
 
 328  
         // do nothing if there's nothing to process
 329  0
         if (bos == null) {
 330  0
             throw new IllegalArgumentException("List of bos passed in was null");
 331  
         }
 332  0
         else if (bos.isEmpty()) {
 333  0
             return;
 334  
         }
 335  
 
 336  
 
 337  
         Person person;
 338  0
         for (PersistableBusinessObject bo : bos) {
 339  
             // get a list of the reference objects on the BO
 340  0
             List<BusinessObjectRelationship> relationships = businessObjectMetaDataService.getBusinessObjectRelationships( bo );
 341  0
             for ( BusinessObjectRelationship rel : relationships ) {
 342  0
                 if ( Person.class.isAssignableFrom( rel.getRelatedClass() ) ) {
 343  0
                     person = (Person) ObjectUtils.getPropertyValue(bo, rel.getParentAttributeName() );
 344  0
                     if (person != null) {
 345  
                         // find the universal user ID relationship and link the field
 346  0
                         for ( Map.Entry<String,String> entry : rel.getParentToChildReferences().entrySet() ) {
 347  0
                             if ( "principalId".equals(entry.getValue())) {
 348  0
                                 linkUserReference(bo, person, rel.getParentAttributeName(), entry.getKey() );
 349  0
                                 break;
 350  
                             }
 351  
                         }
 352  
                     }                    
 353  
                 }
 354  
             }
 355  0
             if ( persistenceStructureService.isPersistable(bo.getClass())) {
 356  0
                     Map<String, Class> references = persistenceStructureService.listReferenceObjectFields(bo);
 357  
 
 358  
                     // walk through the ref objects, only doing work if they are Person objects
 359  0
                     for ( Map.Entry<String, Class> entry : references.entrySet() ) {
 360  0
                         if (Person.class.isAssignableFrom(entry.getValue())) {
 361  0
                             person = (Person) ObjectUtils.getPropertyValue(bo, entry.getKey());
 362  0
                             if (person != null) {
 363  0
                                 String fkFieldName = persistenceStructureService.getForeignKeyFieldName(bo.getClass(), entry.getKey(), "principalId");
 364  0
                                 linkUserReference(bo, person, entry.getKey(), fkFieldName);
 365  0
                             }
 366  
                         }
 367  
                     }
 368  
             }
 369  0
         }
 370  0
     }
 371  
 
 372  
     /**
 373  
      * 
 374  
      * This method links a single UniveralUser back to the parent BO based on the authoritative principalName.
 375  
      * 
 376  
      * @param bo
 377  
      * @param refFieldName
 378  
      */
 379  
     private void linkUserReference(PersistableBusinessObject bo, Person user, String refFieldName, String fkFieldName) {
 380  
 
 381  
         // if the UserId field is blank, there's nothing we can do, so quit
 382  0
         if (StringUtils.isBlank(user.getPrincipalName())) {
 383  0
             return;
 384  
         }
 385  
 
 386  
         // attempt to load the user from the user-name, exit quietly if the user isnt found
 387  0
         Person userFromService = getPersonService().getPersonByPrincipalName(user.getPrincipalName());
 388  0
         if (userFromService == null) {
 389  0
             return;
 390  
         }
 391  
 
 392  
         // attempt to set the universalId on the parent BO
 393  0
         setBoField(bo, fkFieldName, userFromService.getPrincipalId());
 394  0
     }
 395  
 
 396  
     private void setBoField(PersistableBusinessObject bo, String fieldName, Object fieldValue) {
 397  
         try {
 398  0
             ObjectUtils.setObjectProperty(bo, fieldName, fieldValue.getClass(), fieldValue);
 399  
         }
 400  0
         catch (Exception e) {
 401  0
             throw new RuntimeException("Could not set field [" + fieldName + "] on BO to value: " + fieldValue.toString() + " (see nested exception for details).", e);
 402  0
         }
 403  0
     }
 404  
 
 405  
     @Override
 406  
         public PersistableBusinessObject manageReadOnly(PersistableBusinessObject bo) {
 407  0
                 return getBusinessObjectDao().manageReadOnly(bo);
 408  
         }
 409  
 
 410  
         /**
 411  
      * Gets the businessObjectDao attribute.
 412  
      * 
 413  
      * @return Returns the businessObjectDao.
 414  
      */
 415  
     protected BusinessObjectDao getBusinessObjectDao() {
 416  0
         return businessObjectDao;
 417  
     }
 418  
 
 419  
     /**
 420  
      * Sets the businessObjectDao attribute value.
 421  
      * 
 422  
      * @param businessObjectDao The businessObjectDao to set.
 423  
      */
 424  
     public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
 425  0
         this.businessObjectDao = businessObjectDao;
 426  0
     }
 427  
 
 428  
     /**
 429  
      * Sets the persistenceStructureService attribute value.
 430  
      * 
 431  
      * @param persistenceStructureService The persistenceStructureService to set.
 432  
      */
 433  
     public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
 434  0
         this.persistenceStructureService = persistenceStructureService;
 435  0
     }
 436  
 
 437  
     /**
 438  
      * Sets the kualiUserService attribute value.
 439  
      */
 440  
         public final void setPersonService(PersonService personService) {
 441  0
         this.personService = personService;
 442  0
     }
 443  
 
 444  
         protected PersonService getPersonService() {
 445  0
         return personService != null ? personService : (personService = KimApiServiceLocator.getPersonService());
 446  
     }
 447  
 
 448  
     /**
 449  
      * Sets the persistenceService attribute value.
 450  
      * 
 451  
      * @param persistenceService The persistenceService to set.
 452  
      */
 453  
     public final void setPersistenceService(PersistenceService persistenceService) {
 454  0
         this.persistenceService = persistenceService;
 455  0
     }
 456  
 
 457  
     protected BusinessObjectMetaDataService getBusinessObjectMetaDataService() {
 458  0
         return businessObjectMetaDataService;
 459  
     }
 460  
 
 461  
     public void setBusinessObjectMetaDataService(BusinessObjectMetaDataService boMetadataService) {
 462  0
         this.businessObjectMetaDataService = boMetadataService;
 463  0
     }
 464  
 
 465  
 }