KickJava   Java API By Example, From Geeks To Geeks.

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


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.map.ObjRelationship;
30 import org.apache.cayenne.reflect.ArcProperty;
31 import org.apache.cayenne.reflect.ClassDescriptor;
32
33 /**
34  * A query that selects objects related to a given object via a mapped relationship.
35  * RelationshipQuery is used by Cayenne internally to resolve relationships, and is rarely
36  * executed by the application directly, although this of course is possible too.
37  *
38  * @since 1.2
39  * @author Andrus Adamchik
40  */

41 public class RelationshipQuery extends IndirectQuery {
42
43     protected ObjectId objectId;
44     protected String JavaDoc relationshipName;
45     protected boolean refreshing;
46
47     protected transient EntityResolver metadataResolver;
48     protected transient QueryMetadata metadata;
49     protected transient ArcProperty arc;
50
51     // exists for deserialization with Hessian
52
private RelationshipQuery() {
53
54     }
55
56     /**
57      * Creates a RelationshipQuery. Same as
58      * <em>new RelationshipQuery(objectID, relationshipName, true)</em>.
59      *
60      * @param objectID ObjectId of a root object of the relationship.
61      * @param relationshipName The name of the relationship.
62      */

63     public RelationshipQuery(ObjectId objectID, String JavaDoc relationshipName) {
64         this(objectID, relationshipName, true);
65     }
66
67     /**
68      * Creates a RelationshipQuery.
69      *
70      * @param objectID ObjectId of a root object of the relationship.
71      * @param relationshipName The name of the relationship.
72      * @param refreshing whether objects should be refreshed
73      */

74     public RelationshipQuery(ObjectId objectID, String JavaDoc relationshipName,
75             boolean refreshing) {
76         if (objectID == null) {
77             throw new CayenneRuntimeException("Null objectID");
78         }
79
80         this.objectId = objectID;
81         this.relationshipName = relationshipName;
82         this.refreshing = refreshing;
83     }
84
85     /**
86      * Returns query metadata object.
87      */

88     // return metadata without creating replacement, as this is not always possible to
89
// create replacement (one-way relationships, etc.)
90
public QueryMetadata getMetaData(final EntityResolver resolver) {
91         updateMetadata(resolver);
92         return metadata;
93     }
94
95     public ObjectId getObjectId() {
96         return objectId;
97     }
98
99     public boolean isRefreshing() {
100         return refreshing;
101     }
102
103     public String JavaDoc getRelationshipName() {
104         return relationshipName;
105     }
106
107     protected Query createReplacementQuery(EntityResolver resolver) {
108
109         if (objectId.isTemporary() && !objectId.isReplacementIdAttached()) {
110             throw new CayenneRuntimeException("Can't build a query for relationship '"
111                     + relationshipName
112                     + "' for temporary id: "
113                     + objectId);
114         }
115
116         ObjRelationship relationship = getRelationship(resolver);
117
118         // build executable select...
119
Expression qualifier = ExpressionFactory.matchDbExp(relationship
120                 .getReverseDbRelationshipPath(), objectId);
121
122         SelectQuery query = new SelectQuery(
123                 (ObjEntity) relationship.getTargetEntity(),
124                 qualifier);
125         query.setRefreshingObjects(refreshing);
126         return query;
127     }
128
129     /**
130      * Returns a non-null relationship object for this query.
131      */

132     public ObjRelationship getRelationship(EntityResolver resolver) {
133         updateMetadata(resolver);
134         return arc.getRelationship();
135     }
136
137     void updateMetadata(EntityResolver resolver) {
138         // caching metadata as it may be accessed multiple times (at a DC and DD level)
139
if (metadataResolver != resolver) {
140
141             if (objectId == null) {
142                 throw new CayenneRuntimeException(
143                         "Can't resolve query - objectID is null.");
144             }
145
146             ClassDescriptor descriptor = resolver.getClassDescriptor(objectId
147                     .getEntityName());
148             this.arc = (ArcProperty) descriptor.getProperty(relationshipName);
149
150             if (arc == null) {
151                 throw new CayenneRuntimeException("No relationship named "
152                         + relationshipName
153                         + " found in entity "
154                         + objectId.getEntityName()
155                         + "; object id: "
156                         + objectId);
157             }
158
159             this.metadata = new DefaultQueryMetadata() {
160
161                 public boolean isRefreshingObjects() {
162                     return refreshing;
163                 }
164
165                 public ObjEntity getObjEntity() {
166                     return arc.getTargetDescriptor().getEntity();
167                 }
168
169                 public ClassDescriptor getClassDescriptor() {
170                     return arc.getTargetDescriptor();
171                 }
172             };
173
174             this.metadataResolver = resolver;
175         }
176     }
177
178     /**
179      * Overrides toString() outputting a short string with query class and relationship
180      * name.
181      */

182     public String JavaDoc toString() {
183         return StringUtils.substringAfterLast(getClass().getName(), ".")
184                 + ":"
185                 + getRelationshipName();
186     }
187 }
188
Popular Tags