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