1 | |
package org.apache.ojb.broker.util.collections; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import org.apache.ojb.broker.PersistenceBroker; |
19 | |
import org.apache.ojb.broker.PersistenceBrokerException; |
20 | |
import org.apache.ojb.broker.metadata.ClassDescriptor; |
21 | |
|
22 | |
import java.util.Iterator; |
23 | |
import java.util.Vector; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class RemovalAwareCollection extends ManageableVector implements IRemovalAwareCollection |
36 | |
{ |
37 | |
private Vector allObjectsToBeRemoved = new Vector(); |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public void afterStore(PersistenceBroker broker) throws PersistenceBrokerException |
43 | |
{ |
44 | |
|
45 | |
|
46 | |
allObjectsToBeRemoved.removeAll(this); |
47 | |
|
48 | |
Iterator iter = allObjectsToBeRemoved.iterator(); |
49 | |
while (iter.hasNext()) |
50 | |
{ |
51 | |
Object obj = iter.next(); |
52 | |
ClassDescriptor cld = broker.getClassDescriptor(obj.getClass()); |
53 | |
if (broker.serviceBrokerHelper().assertValidPkForDelete(cld, obj)) |
54 | |
{ |
55 | |
broker.delete(obj); |
56 | |
} |
57 | |
} |
58 | |
allObjectsToBeRemoved.clear(); |
59 | |
} |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public Object remove(int index) |
65 | |
{ |
66 | |
Object toBeRemoved = super.remove(index); |
67 | |
registerForDeletion(toBeRemoved); |
68 | |
return toBeRemoved; |
69 | |
} |
70 | |
|
71 | |
protected void registerForDeletion(Object toBeRemoved) |
72 | |
{ |
73 | |
|
74 | |
if (!allObjectsToBeRemoved.contains(toBeRemoved)) |
75 | |
{ |
76 | |
this.allObjectsToBeRemoved.add(toBeRemoved); |
77 | |
} |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public boolean remove(Object o) |
84 | |
{ |
85 | |
boolean result = super.remove(o); |
86 | |
registerForDeletion(o); |
87 | |
return result; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public synchronized void clear() |
94 | |
{ |
95 | |
removeAllElements(); |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public synchronized void removeAllElements() |
102 | |
{ |
103 | |
for (int i = 0; i < this.size(); i++) |
104 | |
{ |
105 | |
registerForDeletion(this.get(i)); |
106 | |
} |
107 | |
super.removeAllElements(); |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public synchronized void removeElementAt(int index) |
115 | |
{ |
116 | |
Object toBeDeleted = this.get(index); |
117 | |
registerForDeletion(toBeDeleted); |
118 | |
super.removeElementAt(index); |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
protected void removeRange(int fromIndex, int toIndex) |
125 | |
{ |
126 | |
for (int i = fromIndex; i < toIndex; i++) |
127 | |
{ |
128 | |
registerForDeletion(this.get(i)); |
129 | |
} |
130 | |
super.removeRange(fromIndex, toIndex); |
131 | |
} |
132 | |
|
133 | |
} |