KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.cayenne.CayenneRuntimeException;
27 import org.apache.cayenne.ObjectId;
28 import org.apache.cayenne.graph.GraphChangeHandler;
29 import org.apache.cayenne.graph.GraphDiff;
30 import org.apache.cayenne.map.DbEntity;
31 import org.apache.cayenne.map.EntityResolver;
32 import org.apache.cayenne.map.ObjEntity;
33 import org.apache.cayenne.map.ObjRelationship;
34
35 /**
36  * A processor of ObjectStore indirect changes, such as flattened relationships and
37  * to-many relationships.
38  *
39  * @since 1.2
40  * @author Andrus Adamchik
41  */

42 final class DataDomainIndirectDiffBuilder implements GraphChangeHandler {
43
44     private final DataDomainFlushAction parent;
45     private final EntityResolver resolver;
46     private final Collection JavaDoc indirectModifications;
47     private final Collection JavaDoc flattenedInserts;
48     private final Collection JavaDoc flattenedDeletes;
49
50     DataDomainIndirectDiffBuilder(DataDomainFlushAction parent) {
51         this.parent = parent;
52         this.indirectModifications = parent.getResultIndirectlyModifiedIds();
53         this.resolver = parent.getDomain().getEntityResolver();
54         this.flattenedInserts = new HashSet JavaDoc();
55         this.flattenedDeletes = new HashSet JavaDoc();
56     }
57
58     void processIndirectChanges(GraphDiff allChanges) {
59         // extract flattened and indirect changes and remove duplicate changes...
60
allChanges.apply(this);
61
62         if (!flattenedInserts.isEmpty()) {
63             Iterator JavaDoc it = flattenedInserts.iterator();
64             while (it.hasNext()) {
65                 FlattenedArcKey key = (FlattenedArcKey) it.next();
66                 DbEntity entity = key.getJoinEntity();
67                 parent.addFlattenedInsert(entity, key);
68             }
69         }
70
71         if (!flattenedDeletes.isEmpty()) {
72             Iterator JavaDoc it = flattenedDeletes.iterator();
73             while (it.hasNext()) {
74                 FlattenedArcKey key = (FlattenedArcKey) it.next();
75                 DbEntity entity = key.getJoinEntity();
76                 parent.addFlattenedDelete(entity, key);
77             }
78         }
79     }
80
81     public void arcCreated(Object JavaDoc nodeId, Object JavaDoc targetNodeId, Object JavaDoc arcId) {
82         ObjEntity entity = resolver.getObjEntity(((ObjectId) nodeId).getEntityName());
83         ObjRelationship relationship = (ObjRelationship) entity.getRelationship(arcId
84                 .toString());
85
86         if (relationship.isSourceIndependentFromTargetChange()) {
87
88             if (!((ObjectId) nodeId).isTemporary()) {
89                 indirectModifications.add(nodeId);
90             }
91
92             if (relationship.isFlattened()) {
93                 if (relationship.isReadOnly()) {
94                     throw new CayenneRuntimeException(
95                             "Cannot set the read-only flattened relationship "
96                                     + relationship.getName());
97                 }
98
99                 // Register this combination (so we can remove it later if an insert
100
// occurs before commit)
101
FlattenedArcKey key = new FlattenedArcKey(
102                         (ObjectId) nodeId,
103                         (ObjectId) targetNodeId,
104                         relationship);
105
106                 // If this combination has already been deleted, simply undelete it.
107
if (!flattenedDeletes.remove(key)) {
108                     flattenedInserts.add(key);
109                 }
110             }
111         }
112     }
113
114     public void arcDeleted(Object JavaDoc nodeId, Object JavaDoc targetNodeId, Object JavaDoc arcId) {
115
116         ObjEntity entity = resolver.getObjEntity(((ObjectId) nodeId).getEntityName());
117         ObjRelationship relationship = (ObjRelationship) entity.getRelationship(arcId
118                 .toString());
119
120         if (relationship.isSourceIndependentFromTargetChange()) {
121             // do not record temporary id mods...
122
if (!((ObjectId) nodeId).isTemporary()) {
123                 indirectModifications.add(nodeId);
124             }
125
126             if (relationship.isFlattened()) {
127                 if (relationship.isReadOnly()) {
128                     throw new CayenneRuntimeException(
129                             "Cannot unset the read-only flattened relationship "
130                                     + relationship.getName());
131                 }
132
133                 // Register this combination (so we can remove it later if an insert
134
// occurs before commit)
135
FlattenedArcKey key = new FlattenedArcKey(
136                         (ObjectId) nodeId,
137                         (ObjectId) targetNodeId,
138                         relationship);
139
140                 // If this combination has already been inserted, simply "uninsert" it
141
// also do not delete it twice
142
if (!flattenedInserts.remove(key)) {
143                     flattenedDeletes.add(key);
144                 }
145             }
146         }
147     }
148
149     public void nodeIdChanged(Object JavaDoc nodeId, Object JavaDoc newId) {
150         // noop
151
}
152
153     public void nodeCreated(Object JavaDoc nodeId) {
154         // noop
155
}
156
157     public void nodeRemoved(Object JavaDoc nodeId) {
158         // noop
159
}
160
161     public void nodePropertyChanged(
162             Object JavaDoc nodeId,
163             String JavaDoc property,
164             Object JavaDoc oldValue,
165             Object JavaDoc newValue) {
166         // noop
167
}
168 }
169
Popular Tags