Coverage Report - org.kuali.rice.krad.util.OjbCollectionHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
OjbCollectionHelper
0%
0/45
0%
0/30
6.25
 
 1  
 /*
 2  
  * Copyright 2005-2007 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.util;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collection;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.log4j.Logger;
 23  
 import org.kuali.rice.krad.bo.PersistableBusinessObject;
 24  
 import org.springframework.orm.ObjectRetrievalFailureException;
 25  
 
 26  
 /**
 27  
  * Helper object to deal with persisting collections.
 28  
  */
 29  0
 public class OjbCollectionHelper {
 30  0
         private static final Logger LOG = Logger.getLogger(OjbCollectionHelper.class);
 31  
     /**
 32  
      * OJB RemovalAwareLists do not survive through the response/request lifecycle. This method is a work-around to forcibly remove
 33  
      * business objects that are found in Collections stored in the database but not in memory.
 34  
      * 
 35  
      * @param orig
 36  
      * @param id
 37  
      * @param template
 38  
      */
 39  
     public void processCollections(OjbCollectionAware template, PersistableBusinessObject orig, PersistableBusinessObject copy) {
 40  0
         if (copy == null) {
 41  0
             return;
 42  
         }
 43  
         
 44  0
         List<Collection<PersistableBusinessObject>> originalCollections = orig.buildListOfDeletionAwareLists();
 45  
 
 46  0
         if (originalCollections != null && !originalCollections.isEmpty()) {
 47  
             /*
 48  
              * Prior to being saved, the version in the database will not yet reflect any deleted collections. So, a freshly
 49  
              * retrieved version will contain objects that need to be removed:
 50  
              */
 51  
             try {
 52  0
                 List<Collection<PersistableBusinessObject>> copyCollections = copy.buildListOfDeletionAwareLists();
 53  0
                 int size = originalCollections.size();
 54  
 
 55  0
                 if (copyCollections.size() != size) {
 56  0
                     throw new RuntimeException("size mismatch while attempting to process list of Collections to manage");
 57  
                 }
 58  
 
 59  0
                 for (int i = 0; i < size; i++) {
 60  0
                     Collection<PersistableBusinessObject> origSource = originalCollections.get(i);
 61  0
                     Collection<PersistableBusinessObject> copySource = copyCollections.get(i);
 62  0
                     List<PersistableBusinessObject> list = findUnwantedElements(copySource, origSource);
 63  0
                     cleanse(template, origSource, list);
 64  
                 }
 65  
             }
 66  0
             catch (ObjectRetrievalFailureException orfe) {
 67  
                 // object wasn't found, must be pre-save
 68  0
             }
 69  
         }
 70  0
     }
 71  
     
 72  
     /**
 73  
      * OJB RemovalAwareLists do not survive through the response/request lifecycle. This method is a work-around to forcibly remove
 74  
      * business objects that are found in Collections stored in the database but not in memory.
 75  
      * 
 76  
      * @param orig
 77  
      * @param id
 78  
      * @param template
 79  
      */
 80  
     public void processCollections2(OjbCollectionAware template, PersistableBusinessObject orig, PersistableBusinessObject copy) {
 81  
         // if copy is null this is the first time we are saving the object, don't have to worry about updating collections
 82  0
         if (copy == null) {
 83  0
             return;
 84  
         }
 85  
         
 86  0
         List<Collection<PersistableBusinessObject>> originalCollections = orig.buildListOfDeletionAwareLists();
 87  
 
 88  0
         if (originalCollections != null && !originalCollections.isEmpty()) {
 89  
             /*
 90  
              * Prior to being saved, the version in the database will not yet reflect any deleted collections. So, a freshly
 91  
              * retrieved version will contain objects that need to be removed:
 92  
              */
 93  
             try {
 94  0
                 List<Collection<PersistableBusinessObject>> copyCollections = copy.buildListOfDeletionAwareLists();
 95  0
                 int size = originalCollections.size();
 96  
 
 97  0
                 if (copyCollections.size() != size) {
 98  0
                     throw new RuntimeException("size mismatch while attempting to process list of Collections to manage");
 99  
                 }
 100  
 
 101  0
                 for (int i = 0; i < size; i++) {
 102  0
                     Collection<PersistableBusinessObject> origSource = originalCollections.get(i);
 103  0
                     Collection<PersistableBusinessObject> copySource = copyCollections.get(i);
 104  0
                     List<PersistableBusinessObject> list = findUnwantedElements(copySource, origSource);
 105  0
                     cleanse(template, origSource, list);
 106  
                 }
 107  
             }
 108  0
             catch (ObjectRetrievalFailureException orfe) {
 109  
                 // object wasn't found, must be pre-save
 110  0
             }
 111  
         }
 112  0
     }
 113  
 
 114  
     /**
 115  
      * This method deletes unwanted objects from the database as well as from the given input List
 116  
      * 
 117  
      * @param origSource - list containing unwanted business objects
 118  
      * @param unwantedItems - business objects to be permanently removed
 119  
      * @param template
 120  
      */
 121  
     private void cleanse(OjbCollectionAware template, Collection<PersistableBusinessObject> origSource, List<PersistableBusinessObject> unwantedItems) {
 122  0
         if (unwantedItems.size() > 0) {
 123  0
                 for (PersistableBusinessObject unwantedItem : unwantedItems) {
 124  0
                     if ( LOG.isDebugEnabled() ) {
 125  0
                             LOG.debug( "cleansing " + unwantedItem);
 126  
                     }
 127  0
                 template.getPersistenceBrokerTemplate().delete(unwantedItem);
 128  
             }
 129  
         }
 130  
 
 131  0
     }
 132  
 
 133  
     /**
 134  
      * This method identifies items in the first List that are not contained in the second List. It is similar to the (optional)
 135  
      * java.util.List retainAll method.
 136  
      * 
 137  
      * @param fromList
 138  
      * @param controlList
 139  
      * @return true iff one or more items were removed
 140  
      */
 141  
     private List<PersistableBusinessObject> findUnwantedElements(Collection<PersistableBusinessObject> fromList, Collection<PersistableBusinessObject> controlList) {
 142  0
         List<PersistableBusinessObject> toRemove = new ArrayList<PersistableBusinessObject>();
 143  
 
 144  0
         for (PersistableBusinessObject fromObject : fromList) {
 145  0
                 if (!ObjectUtils.collectionContainsObjectWithIdentitcalKey(controlList, fromObject)) {
 146  0
                 toRemove.add(fromObject);
 147  
             }
 148  
         }
 149  0
         return toRemove;
 150  
     }
 151  
 }