KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > ObjectStoreGraphDiff


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.cayenne.ObjectId;
30 import org.apache.cayenne.PersistenceState;
31 import org.apache.cayenne.Persistent;
32 import org.apache.cayenne.Validating;
33 import org.apache.cayenne.graph.CompoundDiff;
34 import org.apache.cayenne.graph.GraphChangeHandler;
35 import org.apache.cayenne.graph.GraphDiff;
36 import org.apache.cayenne.validation.ValidationException;
37 import org.apache.cayenne.validation.ValidationResult;
38
39 /**
40  * A GraphDiff facade for the ObjectStore changes. Provides a way for the lower layers of
41  * the access stack to speed up processing of presorted ObjectStore diffs.
42  *
43  * @author Andrus Adamchik
44  * @since 1.2
45  */

46 class ObjectStoreGraphDiff implements GraphDiff {
47
48     private ObjectStore objectStore;
49     private GraphDiff resolvedDiff;
50
51     ObjectStoreGraphDiff(ObjectStore objectStore) {
52         this.objectStore = objectStore;
53         preprocess(objectStore);
54     }
55
56     Map JavaDoc getChangesByObjectId() {
57         return objectStore.getChangesByObjectId();
58     }
59
60     /**
61      * Requires external synchronization on ObjectStore.
62      */

63     boolean validateAndCheckNoop() {
64         if (getChangesByObjectId().isEmpty()) {
65             return true;
66         }
67
68         boolean noop = true;
69
70         // build a new collection for validation as validation methods may result in
71
// ObjectStore modifications
72

73         Collection JavaDoc objectsToValidate = null;
74
75         Iterator JavaDoc it = getChangesByObjectId().values().iterator();
76         while (it.hasNext()) {
77
78             ObjectDiff diff = (ObjectDiff) it.next();
79
80             if (!diff.isNoop()) {
81
82                 noop = false;
83
84                 if (diff.getObject() instanceof Validating) {
85                     if (objectsToValidate == null) {
86                         objectsToValidate = new ArrayList JavaDoc();
87                     }
88
89                     objectsToValidate.add(diff.getObject());
90                 }
91
92             }
93         }
94
95         if (objectsToValidate != null) {
96             ValidationResult result = new ValidationResult();
97
98             Iterator JavaDoc validationIt = objectsToValidate.iterator();
99             while (validationIt.hasNext()) {
100                 Validating object = (Validating) validationIt.next();
101                 switch (((Persistent) object).getPersistenceState()) {
102                     case PersistenceState.NEW:
103                         object.validateForInsert(result);
104                         break;
105                     case PersistenceState.MODIFIED:
106                         object.validateForUpdate(result);
107                         break;
108                     case PersistenceState.DELETED:
109                         object.validateForDelete(result);
110                         break;
111                 }
112             }
113
114             if (result.hasFailures()) {
115                 throw new ValidationException(result);
116             }
117         }
118
119         return noop;
120     }
121
122     public boolean isNoop() {
123         if (getChangesByObjectId().isEmpty()) {
124             return true;
125         }
126
127         Iterator JavaDoc it = getChangesByObjectId().values().iterator();
128         while (it.hasNext()) {
129             if (!((ObjectDiff) it.next()).isNoop()) {
130                 return false;
131             }
132         }
133
134         return true;
135     }
136
137     public void apply(GraphChangeHandler handler) {
138         resolveDiff();
139         resolvedDiff.apply(handler);
140     }
141
142     public void undo(GraphChangeHandler handler) {
143         resolveDiff();
144         resolvedDiff.undo(handler);
145     }
146
147     /**
148      * Converts diffs organized by ObjectId in a collection of diffs sorted by diffId
149      * (same as creation order).
150      */

151     private void resolveDiff() {
152         if (resolvedDiff == null) {
153
154             CompoundDiff diff = new CompoundDiff();
155             Map JavaDoc changes = getChangesByObjectId();
156
157             if (!changes.isEmpty()) {
158                 List JavaDoc allChanges = new ArrayList JavaDoc(changes.size() * 2);
159
160                 Iterator JavaDoc it = changes.values().iterator();
161                 while (it.hasNext()) {
162                     ((ObjectDiff) it.next()).appendDiffs(allChanges);
163                 }
164
165                 Collections.sort(allChanges);
166                 diff.addAll(allChanges);
167             }
168
169             this.resolvedDiff = diff;
170         }
171     }
172
173     private void preprocess(ObjectStore objectStore) {
174
175         Map JavaDoc changes = getChangesByObjectId();
176         if (!changes.isEmpty()) {
177
178             Iterator JavaDoc it = changes.entrySet().iterator();
179             while (it.hasNext()) {
180                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
181
182                 ObjectId id = (ObjectId) entry.getKey();
183
184                 Persistent object = (Persistent) objectStore.getNode(id);
185
186                 // address manual id override.
187
ObjectId objectId = object.getObjectId();
188                 if (!id.equals(objectId)) {
189
190                     if (objectId != null) {
191                         Map JavaDoc replacement = id.getReplacementIdMap();
192                         replacement.clear();
193                         replacement.putAll(objectId.getIdSnapshot());
194                     }
195
196                     object.setObjectId(id);
197                 }
198             }
199         }
200     }
201 }
202
Popular Tags