Coverage Report - org.kuali.rice.krad.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.krad.service.impl;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.kns.service.BusinessObjectMetaDataService;
 20  
 import org.kuali.rice.krad.bo.BusinessObject;
 21  
 import org.kuali.rice.krad.bo.BusinessObjectRelationship;
 22  
 import org.kuali.rice.krad.bo.Inactivatable;
 23  
 import org.kuali.rice.krad.datadictionary.InactivationBlockingMetadata;
 24  
 import org.kuali.rice.krad.service.BusinessObjectService;
 25  
 import org.kuali.rice.krad.service.InactivationBlockingDetectionService;
 26  
 import org.kuali.rice.krad.util.ObjectUtils;
 27  
 import org.springframework.transaction.annotation.Transactional;
 28  
 
 29  
 import java.util.ArrayList;
 30  
 import java.util.Collection;
 31  
 import java.util.HashMap;
 32  
 import java.util.Iterator;
 33  
 import java.util.Map;
 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 org.kuali.rice.krad.bo.Inactivatable} might not have the active field, for example
 50  
          * instances of {@link InactivateableFromTo}
 51  
          * 
 52  
      * @see org.kuali.rice.krad.service.InactivationBlockingDetectionService#listAllBlockerRecords(org.kuali.rice.krad.datadictionary.InactivationBlockingDefinition)
 53  
      * @see org.kuali.rice.krad.bo.Inactivatable
 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
                                 Inactivatable businessObject = (Inactivatable) 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 org.kuali.rice.krad.bo.Inactivatable} might not have the active field, for example
 83  
          * instances of {@link InactivateableFromTo}
 84  
          * 
 85  
          * @see org.kuali.rice.krad.service.InactivationBlockingDetectionService#hasABlockingRecord(org.kuali.rice.krad.bo.BusinessObject,
 86  
          *      org.kuali.rice.krad.datadictionary.InactivationBlockingMetadata)
 87  
          * @see org.kuali.rice.krad.bo.Inactivatable
 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
                                 Inactivatable businessObject = (Inactivatable) 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  
 }