KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > BaseContext


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 package org.apache.cayenne;
20
21 import java.util.Collection JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.cayenne.graph.GraphManager;
25 import org.apache.cayenne.map.EntityResolver;
26 import org.apache.cayenne.query.ObjectIdQuery;
27 import org.apache.cayenne.query.Query;
28 import org.apache.cayenne.reflect.Property;
29
30 /**
31  * A common base superclass for Cayenne ObjectContext implementors.
32  *
33  * @since 3.0
34  * @author Andrus Adamchik
35  */

36 public abstract class BaseContext implements ObjectContext {
37
38     // if we are to pass CayenneContext around, channel should be left alone and
39
// reinjected later if needed
40
protected transient DataChannel channel;
41
42     public abstract void commitChanges();
43
44     public abstract void commitChangesToParent();
45
46     public abstract void deleteObject(Object JavaDoc object) throws DeleteDenyException;
47
48     public abstract Collection JavaDoc deletedObjects();
49
50     public DataChannel getChannel() {
51         return channel;
52     }
53
54     public abstract EntityResolver getEntityResolver();
55
56     public abstract GraphManager getGraphManager();
57
58     public abstract Persistent localObject(ObjectId id, Object JavaDoc prototype);
59
60     public abstract Collection JavaDoc modifiedObjects();
61
62     public abstract Persistent newObject(Class JavaDoc persistentClass);
63
64     public abstract void registerNewObject(Object JavaDoc object);
65
66     public abstract Collection JavaDoc newObjects();
67
68     public abstract QueryResponse performGenericQuery(Query query);
69
70     public abstract List JavaDoc performQuery(Query query);
71
72     /**
73      * @deprecated since 3.0 this method is replaced by
74      * {@link #prepareForAccess(Persistent, String, boolean)}.
75      */

76     public void prepareForAccess(Persistent object, String JavaDoc property) {
77         prepareForAccess(object, property, false);
78     }
79
80     public void prepareForAccess(Persistent object, String JavaDoc property, boolean lazyFaulting) {
81         if (object.getPersistenceState() == PersistenceState.HOLLOW) {
82
83             ObjectId oid = object.getObjectId();
84             List JavaDoc objects = performQuery(new ObjectIdQuery(oid, false, ObjectIdQuery.CACHE));
85
86             if (objects.size() == 0) {
87                 throw new FaultFailureException(
88                         "Error resolving fault, no matching row exists in the database for ObjectId: "
89                                 + oid);
90             }
91             else if (objects.size() > 1) {
92                 throw new FaultFailureException(
93                         "Error resolving fault, more than one row exists in the database for ObjectId: "
94                                 + oid);
95             }
96
97             // sanity check...
98
if (object.getPersistenceState() != PersistenceState.COMMITTED) {
99
100                 String JavaDoc state = PersistenceState.persistenceStateName(object
101                         .getPersistenceState());
102
103                 // TODO: andrus 4/13/2006, modified and deleted states are possible due to
104
// a race condition, should we handle them here?
105

106                 throw new FaultFailureException(
107                         "Error resolving fault for ObjectId: "
108                                 + oid
109                                 + " and state ("
110                                 + state
111                                 + "). Possible cause - matching row is missing from the database.");
112             }
113         }
114
115         // resolve relationship fault
116
if (lazyFaulting && property != null) {
117             Property propertyDescriptor = getEntityResolver().getClassDescriptor(
118                     object.getObjectId().getEntityName()).getProperty(property);
119
120             // this should trigger fault resolving
121
propertyDescriptor.readProperty(object);
122         }
123     }
124
125     public abstract void propertyChanged(
126             Persistent object,
127             String JavaDoc property,
128             Object JavaDoc oldValue,
129             Object JavaDoc newValue);
130
131     public abstract void rollbackChanges();
132
133     public abstract void rollbackChangesLocally();
134
135     public abstract Collection JavaDoc uncommittedObjects();
136 }
137
Popular Tags