1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
28 | |
|
29 | 0 | public class OjbCollectionHelper { |
30 | 0 | private static final Logger LOG = Logger.getLogger(OjbCollectionHelper.class); |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
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 | |
|
49 | |
|
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 | |
|
68 | 0 | } |
69 | |
} |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public void processCollections2(OjbCollectionAware template, PersistableBusinessObject orig, PersistableBusinessObject copy) { |
81 | |
|
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 | |
|
91 | |
|
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 | |
|
110 | 0 | } |
111 | |
} |
112 | 0 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
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 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
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 | |
} |