KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
23
24 import org.apache.cayenne.CayenneRuntimeException;
25 import org.apache.cayenne.ObjectContext;
26 import org.apache.cayenne.ObjectId;
27 import org.apache.cayenne.Persistent;
28 import org.apache.cayenne.QueryResponse;
29 import org.apache.cayenne.graph.GraphChangeHandler;
30 import org.apache.cayenne.map.ObjEntity;
31 import org.apache.cayenne.query.ObjectIdQuery;
32 import org.apache.cayenne.query.Query;
33 import org.apache.cayenne.reflect.AttributeProperty;
34 import org.apache.cayenne.reflect.ClassDescriptor;
35 import org.apache.cayenne.reflect.Property;
36 import org.apache.cayenne.reflect.PropertyVisitor;
37 import org.apache.cayenne.reflect.ToManyProperty;
38 import org.apache.cayenne.reflect.ToOneProperty;
39
40 /**
41  * A GraphChangeHandler that loads child ObjectContext diffs into a parent DataContext.
42  * Graph node ids are expected to be ObjectIds.
43  *
44  * @since 1.2
45  * @author Andrus Adamchik
46  */

47 class ChildDiffLoader implements GraphChangeHandler {
48
49     ObjectContext context;
50
51     ChildDiffLoader(ObjectContext context) {
52         this.context = context;
53     }
54
55     public void nodeIdChanged(Object JavaDoc nodeId, Object JavaDoc newId) {
56         throw new CayenneRuntimeException("Not supported");
57     }
58
59     public void nodeCreated(Object JavaDoc nodeId) {
60         ObjectId id = (ObjectId) nodeId;
61         if (id.getEntityName() == null) {
62             throw new NullPointerException JavaDoc("Null entity name in id " + id);
63         }
64
65         ObjEntity entity = context.getEntityResolver().getObjEntity(id.getEntityName());
66         if (entity == null) {
67             throw new IllegalArgumentException JavaDoc("Entity not mapped with Cayenne: " + id);
68         }
69
70         Persistent dataObject = null;
71         try {
72             dataObject = (Persistent) entity.getJavaClass().newInstance();
73         }
74         catch (Exception JavaDoc ex) {
75             throw new CayenneRuntimeException("Error instantiating object.", ex);
76         }
77
78         dataObject.setObjectId(id);
79         context.registerNewObject(dataObject);
80     }
81
82     public void nodeRemoved(Object JavaDoc nodeId) {
83         context.deleteObject(findObject(nodeId));
84     }
85
86     public void nodePropertyChanged(
87             Object JavaDoc nodeId,
88             String JavaDoc property,
89             Object JavaDoc oldValue,
90             Object JavaDoc newValue) {
91
92         // this change is for simple property, so no need to convert targets to server
93
// objects...
94
Persistent object = findObject(nodeId);
95         ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(
96                 ((ObjectId) nodeId).getEntityName());
97
98         try {
99             descriptor.getProperty(property).writeProperty(object, null, newValue);
100         }
101         catch (Exception JavaDoc e) {
102             throw new CayenneRuntimeException("Error setting property: " + property, e);
103         }
104     }
105
106     public void arcCreated(Object JavaDoc nodeId, Object JavaDoc targetNodeId, Object JavaDoc arcId) {
107
108         final Persistent source = findObject(nodeId);
109         final Persistent target = findObject(targetNodeId);
110
111         ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(
112                 ((ObjectId) nodeId).getEntityName());
113         Property property = descriptor.getProperty(arcId.toString());
114
115         property.visit(new PropertyVisitor() {
116
117             public boolean visitAttribute(AttributeProperty property) {
118                 return false;
119             }
120
121             public boolean visitToMany(ToManyProperty property) {
122                 property.addTarget(source, target, false);
123                 return false;
124             }
125
126             public boolean visitToOne(ToOneProperty property) {
127                 property.setTarget(source, target, false);
128                 return false;
129             }
130         });
131     }
132
133     public void arcDeleted(Object JavaDoc nodeId, final Object JavaDoc targetNodeId, Object JavaDoc arcId) {
134         final Persistent source = findObject(nodeId);
135
136         ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(
137                 ((ObjectId) nodeId).getEntityName());
138         Property property = descriptor.getProperty(arcId.toString());
139         property.visit(new PropertyVisitor() {
140
141             public boolean visitAttribute(AttributeProperty property) {
142                 return false;
143             }
144
145             public boolean visitToMany(ToManyProperty property) {
146                 Persistent target = findObject(targetNodeId);
147                 property.removeTarget(source, target, false);
148                 return false;
149             }
150
151             public boolean visitToOne(ToOneProperty property) {
152                 property.setTarget(source, null, false);
153                 return false;
154             }
155         });
156     }
157
158     Persistent findObject(Object JavaDoc nodeId) {
159         // first do a lookup in ObjectStore; if even a hollow object is found, return it;
160
// if not - fetch.
161

162         Persistent object = (Persistent) context.getGraphManager().getNode(nodeId);
163         if (object != null) {
164             return object;
165         }
166
167         // skip context cache lookup, go directly to its channel
168
Query query = new ObjectIdQuery((ObjectId) nodeId);
169         QueryResponse response = context.getChannel().onQuery(context, query);
170         List JavaDoc objects = response.firstList();
171
172         if (objects.size() == 0) {
173             throw new CayenneRuntimeException("No object for ID exists: " + nodeId);
174         }
175         else if (objects.size() > 1) {
176             throw new CayenneRuntimeException(
177                     "Expected zero or one object, instead query matched: "
178                             + objects.size());
179         }
180
181         return (Persistent) objects.get(0);
182     }
183 }
184
Popular Tags