KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > query > ObjectIdQuery


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.query;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.cayenne.CayenneRuntimeException;
24 import org.apache.cayenne.ObjectId;
25 import org.apache.cayenne.exp.Expression;
26 import org.apache.cayenne.exp.ExpressionFactory;
27 import org.apache.cayenne.map.EntityResolver;
28 import org.apache.cayenne.map.ObjEntity;
29 import org.apache.cayenne.reflect.ClassDescriptor;
30 import org.apache.cayenne.util.Util;
31
32 /**
33  * A query that matches zero or one object or data row corresponding to the ObjectId. Used
34  * internally by Cayenne to lookup objects by id. Notice that cache policies of
35  * ObjectIdQuery are different from generic {@link QueryMetadata} cache policies.
36  * ObjectIdQuery is special - it is the only query that can be done against Cayenne main
37  * cache, thus cache handling is singnificantly different from all other of the queries.
38  *
39  * @since 1.2
40  * @author Andrus Adamchik
41  */

42 public class ObjectIdQuery extends IndirectQuery {
43
44     // TODO: Andrus, 2/18/2006 - reconcile this with QueryMetadata cache policies
45
public static final int CACHE = 1;
46     public static final int CACHE_REFRESH = 2;
47     public static final int CACHE_NOREFRESH = 3;
48
49     protected ObjectId objectId;
50     protected int cachePolicy;
51     protected boolean fetchingDataRows;
52
53     protected transient EntityResolver metadataResolver;
54     protected transient QueryMetadata metadata;
55
56     // needed for hessian serialization
57
private ObjectIdQuery() {
58         this.cachePolicy = CACHE_REFRESH;
59     }
60
61     /**
62      * Creates a refreshing SingleObjectQuery.
63      */

64     public ObjectIdQuery(ObjectId objectID) {
65         this(objectID, false, CACHE_REFRESH);
66     }
67
68     /**
69      * Creates a new ObjectIdQuery.
70      */

71     public ObjectIdQuery(ObjectId objectId, boolean fetchingDataRows, int cachePolicy) {
72         if (objectId == null) {
73             throw new NullPointerException JavaDoc("Null objectID");
74         }
75
76         this.objectId = objectId;
77         this.cachePolicy = cachePolicy;
78         this.fetchingDataRows = fetchingDataRows;
79     }
80
81     /**
82      * Returns query metadata object.
83      */

84     // return metadata without creating replacement, as it is not always possible to
85
// create replacement (e.g. temp ObjectId).
86
public QueryMetadata getMetaData(final EntityResolver resolver) {
87         // caching metadata as it may be accessed multiple times (at a DC and DD level)
88
if (metadata == null || metadataResolver != resolver) {
89             this.metadata = new DefaultQueryMetadata() {
90
91                 public ClassDescriptor getClassDescriptor() {
92                     return resolver.getClassDescriptor(objectId.getEntityName());
93                 }
94                 
95                 public ObjEntity getObjEntity() {
96                     return getClassDescriptor().getEntity();
97                 }
98
99                 public boolean isFetchingDataRows() {
100                     return fetchingDataRows;
101                 }
102             };
103
104             this.metadataResolver = resolver;
105         }
106
107         return metadata;
108     }
109
110     public ObjectId getObjectId() {
111         return objectId;
112     }
113
114     protected Query createReplacementQuery(EntityResolver resolver) {
115         if (objectId == null) {
116             throw new CayenneRuntimeException("Can't resolve query - objectId is null.");
117         }
118
119         if (objectId.isTemporary() && !objectId.isReplacementIdAttached()) {
120             throw new CayenneRuntimeException("Can't build a query for temporary id: "
121                     + objectId);
122         }
123
124         SelectQuery query = new SelectQuery(objectId.getEntityName(), ExpressionFactory
125                 .matchAllDbExp(objectId.getIdSnapshot(), Expression.EQUAL_TO));
126
127         // if we got to the point of fetch, always force refresh....
128
query.setRefreshingObjects(true);
129         query.setFetchingDataRows(fetchingDataRows);
130         return query;
131     }
132
133     public int getCachePolicy() {
134         return cachePolicy;
135     }
136
137     public boolean isFetchMandatory() {
138         return cachePolicy == CACHE_REFRESH;
139     }
140
141     public boolean isFetchAllowed() {
142         return cachePolicy != CACHE_NOREFRESH;
143     }
144
145     public boolean isFetchingDataRows() {
146         return fetchingDataRows;
147     }
148
149     /**
150      * Overrides toString() outputting a short string with query class and ObjectId.
151      */

152     public String JavaDoc toString() {
153         return StringUtils.substringAfterLast(getClass().getName(), ".") + ":" + objectId;
154     }
155
156     /**
157      * An object is considered equal to this query if it is also a SingleObjectQuery with
158      * an equal ObjectId.
159      */

160     public boolean equals(Object JavaDoc object) {
161         if (this == object) {
162             return true;
163         }
164
165         if (!(object instanceof ObjectIdQuery)) {
166             return false;
167         }
168
169         ObjectIdQuery query = (ObjectIdQuery) object;
170
171         return Util.nullSafeEquals(objectId, query.getObjectId());
172     }
173
174     /**
175      * Implements a standard hashCode contract considering custom 'equals' implementation.
176      */

177     public int hashCode() {
178         return (objectId != null) ? objectId.hashCode() : 11;
179     }
180 }
181
Popular Tags