KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > sessions > DirectMapChangeRecord


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.sessions;
23
24 import java.util.*;
25 import oracle.toplink.essentials.mappings.Association;
26
27 public class DirectMapChangeRecord extends ChangeRecord {
28     protected HashMap addObjectsList;
29     protected HashMap removeObjectsList;
30
31     /**
32      * Used for change tracking when customer sets entire collection
33      */

34     protected transient Object JavaDoc originalCollection;
35
36     /**
37      * Used for change tracking when customer sets entire collection
38      */

39     protected transient Object JavaDoc latestCollection;
40
41     public DirectMapChangeRecord() {
42         super();
43     }
44
45     public DirectMapChangeRecord(ObjectChangeSet owner) {
46         this.owner = owner;
47     }
48
49     /**
50      * INTERNAL:
51      * Use in SDK propject for the mapping of this change record
52      */

53     public Vector getAddAssociations() {
54         Vector addAssociations = new Vector();
55
56         for (Iterator i = getAddObjects().keySet().iterator(); i.hasNext(); ) {
57             Association association = new Association();
58             Object JavaDoc key = i.next();
59             Object JavaDoc value = getAddObjects().get(key);
60
61             association.setKey(key);
62             association.setValue(value);
63             addAssociations.add(association);
64         }
65         if (addAssociations.size() == 0) {
66             return null;
67         }
68         return addAssociations;
69     }
70
71     /**
72      * returns true if the change set has changes
73      */

74     public boolean hasChanges() {
75         return (!(getAddObjects().isEmpty() && getRemoveObjects().isEmpty())) || getOwner().isNew();
76     }
77
78     /**
79      * INTERNAL:
80      * This method will be used to merge one record into another
81      */

82     public void mergeRecord(ChangeRecord mergeFromRecord, UnitOfWorkChangeSet mergeToChangeSet, UnitOfWorkChangeSet mergeFromChangeSet) {
83         Iterator addKeys = ((DirectMapChangeRecord)mergeFromRecord).getAddObjects().keySet().iterator();
84         while (addKeys.hasNext()) {
85             Object JavaDoc key = addKeys.next();
86
87             if (!this.getAddObjects().containsKey(key)) {
88                 if (this.getRemoveObjects().containsKey(key)) {
89                     this.getRemoveObjects().remove(key);
90                 } else {
91                     this.getAddObjects().put(key, ((DirectMapChangeRecord)mergeFromRecord).getAddObjects().get(key));
92                 }
93             }
94         }
95
96         Iterator removeKeys = ((DirectMapChangeRecord)mergeFromRecord).getRemoveObjects().keySet().iterator();
97         while (removeKeys.hasNext()) {
98             Object JavaDoc key = removeKeys.next();
99
100             if (!this.getRemoveObjects().containsKey(key)) {
101                 if (this.getAddObjects().containsKey(key)) {
102                     this.getAddObjects().remove(key);
103                 } else {
104                     this.getRemoveObjects().put(key, ((DirectMapChangeRecord)mergeFromRecord).getRemoveObjects().get(key));
105                 }
106             }
107         }
108     }
109
110     /**
111      * INTERNAL:
112      * Use in SDK propject for the mapping of this change record
113      */

114     public void setAddAssociations(Vector addAssociations) {
115         HashMap addMap = new HashMap();
116
117         for (Enumeration enumtr = addAssociations.elements(); enumtr.hasMoreElements();) {
118             Association association = (Association)enumtr.nextElement();
119             addMap.put(association.getKey(), association.getValue());
120         }
121         if (addMap.isEmpty()) {
122             addObjectsList = null;
123         }
124         addObjectsList = addMap;
125     }
126
127     /**
128      * Used for change tracking when cutomer sets entire collection
129      * This is the last collection that was set on the object
130      */

131     public Object JavaDoc getLatestCollection() {
132         return latestCollection;
133     }
134
135     /**
136      * Used for change tracking when cutomer sets entire collection
137      * This is the original collection that was set on the object when it was cloned
138      */

139     public Object JavaDoc getOriginalCollection() {
140         return originalCollection;
141     }
142
143     /**
144     * INTERNAL:
145     * Use in SDK propject for the mapping of this change record
146     */

147     public Vector getRemoveAssociations() {
148         Vector removeAssociations = new Vector();
149
150         for (Iterator i = getRemoveObjects().keySet().iterator(); i.hasNext(); ) {
151             Association association = new Association();
152             
153             Object JavaDoc key = i.next();
154             Object JavaDoc value = getAddObjects().get(key);
155
156             association.setKey(key);
157             association.setValue(value);
158             removeAssociations.add(association);
159         }
160         if (removeAssociations.size() == 0) {
161             return null;
162         }
163         return removeAssociations;
164     }
165
166     /**
167      * INTERNAL:
168      * Use in SDK propject for the mapping of this change record
169      */

170     public void setRemoveAssociations(Vector removeAssociations) {
171         HashMap removeMap = new HashMap();
172
173         for (Enumeration enumtr = removeAssociations.elements(); enumtr.hasMoreElements();) {
174             Association association = (Association)enumtr.nextElement();
175             removeMap.put(association.getKey(), association.getValue());
176         }
177         if (removeMap.isEmpty()) {
178             removeObjectsList = null;
179         }
180         removeObjectsList = removeMap;
181     }
182
183     /**
184      * ADVANCED:
185      * Adds the items that were added to the collection
186      */

187     public void addAdditionChange(HashMap additions) {
188         if (getAddObjects().size() == 0) {
189             addObjectsList = additions;
190             return;
191         }
192
193         for (Iterator i = additions.keySet().iterator(); i.hasNext(); ) {
194             Object JavaDoc key = i.next();
195             if (getAddObjects().containsKey(key)) {
196                 getAddObjects().put(key, additions.get(key));
197             } else if (additions.get(key).equals(getAddObjects().get(key))) {
198                 getAddObjects().put(key, additions.get(key));
199             }
200         }
201     }
202
203     /**
204     * ADVANCED:
205     * Adds the items that were removed from the collection
206     */

207     public void addRemoveChange(HashMap subtractions) {
208         if (getRemoveObjects().size() == 0) {
209             this.removeObjectsList = subtractions;
210             return;
211         }
212
213         for (Iterator i = subtractions.keySet().iterator(); i.hasNext(); ) {
214             Object JavaDoc key = i.next();
215             if (!getRemoveObjects().containsKey(key)) {
216                 getRemoveObjects().put(key, subtractions.get(key));
217             } else if (subtractions.get(key).equals(getRemoveObjects().get(key))) {
218                 getRemoveObjects().put(key, subtractions.get(key));
219             }
220         }
221     }
222
223     /**
224      * ADVANCED:
225      * Adds the items that were added to the collection
226      */

227     public void addAdditionChange(Object JavaDoc key, Object JavaDoc value) {
228         if ( getRemoveObjects().containsKey(key) ) {
229             if ( value.equals(getRemoveObjects().get(key)) ) {
230                 getRemoveObjects().remove(key);
231             }else {
232                 getAddObjects().put(key, value);
233             }
234         } else {
235             getAddObjects().put(key, value);
236         }
237     }
238
239     /**
240     * ADVANCED:
241     * Adds the items that were removed from the collection
242     */

243     public void addRemoveChange(Object JavaDoc key, Object JavaDoc value) {
244         //if an entry already exists in the remove it must remain untill added
245
// as it contains the original removal.
246
if ( getAddObjects().containsKey(key) ) {
247             getAddObjects().remove(key);
248         }else if ( ! getRemoveObjects().containsKey(key) ) {
249             getRemoveObjects().put(key, value);
250         }
251     }
252     
253     /**
254      * INTERNAL:
255      * Sets the added items list
256      */

257     public void setAddObjects(HashMap addObjects) {
258         this.addObjectsList = addObjects;
259     }
260
261     /**
262      * Used for change tracking when cutomer sets entire collection
263      * This is the last collection that was set on the object
264      */

265     public void setLatestCollection(Object JavaDoc latestCollection) {
266         this.latestCollection = latestCollection;
267     }
268
269     /**
270      * Used for change tracking when cutomer sets entire collection
271      * This is the original collection that was set on the object when it was cloned
272      */

273     public void setOriginalCollection(Object JavaDoc originalCollection) {
274         this.originalCollection = originalCollection;
275     }
276
277     /**
278      * INTERNAL:
279      * Returns the added items list
280      */

281     public HashMap getAddObjects() {
282         if (addObjectsList == null) {
283             // addObjectsList = new Hashtable();
284
addObjectsList = new HashMap();
285         }
286         return addObjectsList;
287     }
288
289     /**
290      * INTERNAL:
291      * Sets the removed items list
292      */

293     public void setRemoveObjects(HashMap removeObjects) {
294         this.removeObjectsList = removeObjects;
295     }
296
297     /**
298      * INTERNAL:
299      * Returns the removed items list
300      */

301     public HashMap getRemoveObjects() {
302         if (removeObjectsList == null) {
303             removeObjectsList = new HashMap();
304         }
305         
306         return removeObjectsList;
307     }
308
309     /**
310      * INTERNAL:
311      * This method will be used to update the objectsChangeSets references
312      */

313     public void updateReferences(UnitOfWorkChangeSet mergeToChangeSet, UnitOfWorkChangeSet mergeFromChangeSet) {
314         //nothing for this record type to do as it does not reference any changesets
315
}
316 }
317
Popular Tags