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