1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30 public class OjbCollectionHelper {
31 private static final Logger LOG = Logger.getLogger(OjbCollectionHelper.class);
32
33
34
35
36
37
38
39
40 public void processCollections(OjbCollectionAware template, PersistableBusinessObject orig, PersistableBusinessObject copy) {
41 if (copy == null) {
42 return;
43 }
44
45 List originalCollections = orig.buildListOfDeletionAwareLists();
46
47 if (originalCollections != null && !originalCollections.isEmpty()) {
48
49
50
51
52 try {
53 List copyCollections = copy.buildListOfDeletionAwareLists();
54 int size = originalCollections.size();
55
56 if (copyCollections.size() != size) {
57 throw new RuntimeException("size mismatch while attempting to process list of Collections to manage");
58 }
59
60 for (int i = 0; i < size; i++) {
61 Collection origSource = (Collection) originalCollections.get(i);
62 Collection copySource = (Collection) copyCollections.get(i);
63 List list = findUnwantedElements(copySource, origSource);
64 cleanse(template, origSource, list);
65 }
66 }
67 catch (ObjectRetrievalFailureException orfe) {
68
69 }
70 }
71 }
72
73
74
75
76
77
78
79
80
81 public void processCollections2(OjbCollectionAware template, PersistableBusinessObject orig, PersistableBusinessObject copy) {
82
83 if (copy == null) {
84 return;
85 }
86
87 List originalCollections = orig.buildListOfDeletionAwareLists();
88
89 if (originalCollections != null && !originalCollections.isEmpty()) {
90
91
92
93
94 try {
95 List copyCollections = copy.buildListOfDeletionAwareLists();
96 int size = originalCollections.size();
97
98 if (copyCollections.size() != size) {
99 throw new RuntimeException("size mismatch while attempting to process list of Collections to manage");
100 }
101
102 for (int i = 0; i < size; i++) {
103 Collection origSource = (Collection) originalCollections.get(i);
104 Collection copySource = (Collection) copyCollections.get(i);
105 List list = findUnwantedElements(copySource, origSource);
106 cleanse(template, origSource, list);
107 }
108 }
109 catch (ObjectRetrievalFailureException orfe) {
110
111 }
112 }
113 }
114
115
116
117
118
119
120
121
122 private void cleanse(OjbCollectionAware template, Collection origSource, List unwantedItems) {
123 if (unwantedItems.size() > 0) {
124 Iterator iter = unwantedItems.iterator();
125 while (iter.hasNext()) {
126 Object obj = iter.next();
127 if ( LOG.isDebugEnabled() ) {
128 LOG.debug( "cleansing " + obj);
129 }
130 template.getPersistenceBrokerTemplate().delete(obj);
131 }
132 }
133
134 }
135
136
137
138
139
140
141
142
143
144 private List findUnwantedElements(Collection fromList, Collection controlList) {
145 List toRemove = new ArrayList();
146
147 Iterator iter = fromList.iterator();
148 while (iter.hasNext()) {
149 PersistableBusinessObject line = (PersistableBusinessObject) iter.next();
150 if (!ObjectUtils.collectionContainsObjectWithIdentitcalKey(controlList, line)) {
151 toRemove.add(line);
152 }
153 }
154 return toRemove;
155 }
156 }