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