Coverage Report - org.kuali.rice.kns.service.impl.InactivationBlockingDetectionServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
InactivationBlockingDetectionServiceImpl
0%
0/46
0%
0/22
3.6
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kns.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collection;
 20  
 import java.util.HashMap;
 21  
 import java.util.Iterator;
 22  
 import java.util.Map;
 23  
 
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 import org.kuali.rice.kns.bo.BusinessObject;
 26  
 import org.kuali.rice.kns.bo.BusinessObjectRelationship;
 27  
 import org.kuali.rice.kns.bo.Inactivateable;
 28  
 import org.kuali.rice.kns.datadictionary.InactivationBlockingMetadata;
 29  
 import org.kuali.rice.kns.service.BusinessObjectMetaDataService;
 30  
 import org.kuali.rice.kns.service.BusinessObjectService;
 31  
 import org.kuali.rice.kns.service.InactivationBlockingDetectionService;
 32  
 import org.kuali.rice.kns.util.ObjectUtils;
 33  
 import org.springframework.transaction.annotation.Transactional;
 34  
 
 35  
 /**
 36  
  * Performs checking of inactivation blocking 
 37  
  * 
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  */
 40  
 @Transactional
 41  0
 public class InactivationBlockingDetectionServiceImpl implements InactivationBlockingDetectionService {
 42  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InactivationBlockingDetectionServiceImpl.class);
 43  
 
 44  
     protected BusinessObjectMetaDataService businessObjectMetaDataService;
 45  
     protected BusinessObjectService businessObjectService;
 46  
     
 47  
     /**
 48  
      * Note we are checking the active getting after retrieving potential blocking records instead of setting criteria on the
 49  
          * active field. This is because some implementations of {@link Inactivateable} might not have the active field, for example
 50  
          * instances of {@link InactivateableFromTo}
 51  
          * 
 52  
      * @see org.kuali.rice.kns.service.InactivationBlockingDetectionService#listAllBlockerRecords(org.kuali.rice.kns.datadictionary.InactivationBlockingDefinition)
 53  
      * @see org.kuali.rice.kns.bo.Inactivateable
 54  
      */
 55  
     @SuppressWarnings("unchecked")
 56  
         public Collection<BusinessObject> listAllBlockerRecords(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) {
 57  0
                 Collection<BusinessObject> blockingRecords = new ArrayList<BusinessObject>();
 58  
 
 59  0
                 Map<String, String> queryMap = buildInactivationBlockerQueryMap(blockedBo, inactivationBlockingMetadata);
 60  0
                 if (LOG.isDebugEnabled()) {
 61  0
                         LOG.debug("Checking for blocker records for object: " + blockedBo);
 62  0
                         LOG.debug("    With Metadata: " + inactivationBlockingMetadata);
 63  0
                         LOG.debug("    Resulting Query Map: " + queryMap);
 64  
                 }
 65  
 
 66  0
                 if (queryMap != null) {
 67  0
                         Collection potentialBlockingRecords = businessObjectService.findMatching(
 68  
                                         inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass(), queryMap);
 69  0
                         for (Iterator iterator = potentialBlockingRecords.iterator(); iterator.hasNext();) {
 70  0
                                 Inactivateable businessObject = (Inactivateable) iterator.next();
 71  0
                                 if (businessObject.isActive()) {
 72  0
                                         blockingRecords.add((BusinessObject) businessObject);
 73  
                                 }
 74  0
                         }
 75  
                 }
 76  
 
 77  0
                 return blockingRecords;
 78  
         }
 79  
 
 80  
         /**
 81  
          * Note we are checking the active getting after retrieving potential blocking records instead of setting criteria on the
 82  
          * active field. This is because some implementations of {@link Inactivateable} might not have the active field, for example
 83  
          * instances of {@link InactivateableFromTo}
 84  
          * 
 85  
          * @see org.kuali.rice.kns.service.InactivationBlockingDetectionService#hasABlockingRecord(org.kuali.rice.kns.bo.BusinessObject,
 86  
          *      org.kuali.rice.kns.datadictionary.InactivationBlockingMetadata)
 87  
          * @see org.kuali.rice.kns.bo.Inactivateable
 88  
          */
 89  
         public boolean hasABlockingRecord(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) {
 90  0
                 boolean hasBlockingRecord = false;
 91  
 
 92  0
                 Map<String, String> queryMap = buildInactivationBlockerQueryMap(blockedBo, inactivationBlockingMetadata);
 93  0
                 if (queryMap != null) {
 94  0
                         Collection potentialBlockingRecords = businessObjectService.findMatching(
 95  
                                         inactivationBlockingMetadata.getBlockingReferenceBusinessObjectClass(), queryMap);
 96  0
                         for (Iterator iterator = potentialBlockingRecords.iterator(); iterator.hasNext();) {
 97  0
                                 Inactivateable businessObject = (Inactivateable) iterator.next();
 98  0
                                 if (businessObject.isActive()) {
 99  0
                                         hasBlockingRecord = true;
 100  0
                                         break;
 101  
                                 }
 102  0
                         }
 103  
                 }
 104  
 
 105  
                 // if queryMap were null, means that we couldn't perform a query, and hence, need to return false
 106  0
                 return hasBlockingRecord;
 107  
         }
 108  
 
 109  
         protected Map<String, String> buildInactivationBlockerQueryMap(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) {
 110  0
                 BusinessObject blockingBo = (BusinessObject) ObjectUtils.createNewObjectFromClass(inactivationBlockingMetadata
 111  
                                 .getBlockingReferenceBusinessObjectClass());
 112  
 
 113  0
                 BusinessObjectRelationship businessObjectRelationship = businessObjectMetaDataService
 114  
                                 .getBusinessObjectRelationship(blockingBo,
 115  
                                                 inactivationBlockingMetadata.getBlockedReferencePropertyName());
 116  
 
 117  
                 // note, this method assumes that all PK fields of the blockedBo have a non-null and, for strings, non-blank values
 118  0
                 if (businessObjectRelationship != null) {
 119  0
                         Map<String, String> parentToChildReferences = businessObjectRelationship.getParentToChildReferences();
 120  0
                         Map<String, String> queryMap = new HashMap<String, String>();
 121  0
                         for (Map.Entry<String, String> parentToChildReference : parentToChildReferences.entrySet()) {
 122  0
                                 String fieldName = parentToChildReference.getKey();
 123  0
                                 Object fieldValue = ObjectUtils.getPropertyValue(blockedBo, parentToChildReference.getValue());
 124  0
                                 if (fieldValue != null && StringUtils.isNotBlank(fieldValue.toString())) {
 125  0
                                         queryMap.put(fieldName, fieldValue.toString());
 126  
                                 } else {
 127  0
                                         LOG.error("Found null value for foreign key field " + fieldName
 128  
                                                         + " while building inactivation blocking query map.");
 129  0
                                         throw new RuntimeException("Found null value for foreign key field '" + fieldName
 130  
                                                         + "' while building inactivation blocking query map.");
 131  
                                 }
 132  0
                         }
 133  
 
 134  0
                         return queryMap;
 135  
                 }
 136  
 
 137  0
                 return null;
 138  
         }
 139  
     
 140  
     public void setBusinessObjectMetaDataService(BusinessObjectMetaDataService businessObjectMetaDataService) {
 141  0
         this.businessObjectMetaDataService = businessObjectMetaDataService;
 142  0
     }
 143  
 
 144  
     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
 145  0
         this.businessObjectService = businessObjectService;
 146  0
     }
 147  
 
 148  
 }