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