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