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