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