KickJava   Java API By Example, From Geeks To Geeks.

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


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.builder.ToStringBuilder;
23 import org.apache.cayenne.CayenneRuntimeException;
24 import org.apache.cayenne.map.DataMap;
25 import org.apache.cayenne.map.DbEntity;
26 import org.apache.cayenne.map.EntityResolver;
27 import org.apache.cayenne.map.ObjEntity;
28 import org.apache.cayenne.map.Procedure;
29
30 /**
31  * A common superclass of Cayenne queries.
32  *
33  * @author Andrus Adamchik
34  */

35 public abstract class AbstractQuery implements Query {
36
37     /**
38      * The root object this query. May be an entity name, Java class, ObjEntity or
39      * DbEntity, depending on the specific query and how it was constructed.
40      */

41     protected Object JavaDoc root;
42     protected String JavaDoc name;
43
44     /**
45      * Returns a symbolic name of the query.
46      *
47      * @since 1.1
48      */

49     public String JavaDoc getName() {
50         return name;
51     }
52
53     /**
54      * Sets a symbolic name of the query.
55      *
56      * @since 1.1
57      */

58     public void setName(String JavaDoc name) {
59         this.name = name;
60     }
61
62     /**
63      * Returns default select parameters.
64      *
65      * @since 1.2
66      */

67     public QueryMetadata getMetaData(EntityResolver resolver) {
68         BaseQueryMetadata md = new BaseQueryMetadata();
69         md.resolve(getRoot(), resolver, getName());
70         return md;
71     }
72
73     /**
74      * Returns the root of this query.
75      */

76     public Object JavaDoc getRoot() {
77         return root;
78     }
79
80     /**
81      * Sets the root of the query
82      *
83      * @param value The new root
84      * @throws IllegalArgumentException if value is not a String, ObjEntity, DbEntity,
85      * Procedure, DataMap, Class or null.
86      */

87     public void setRoot(Object JavaDoc value) {
88         if (value == null) {
89             this.root = null;
90         }
91
92         // sanity check
93
if (!((value instanceof String JavaDoc)
94                 || (value instanceof ObjEntity)
95                 || (value instanceof DbEntity)
96                 || (value instanceof Class JavaDoc)
97                 || (value instanceof Procedure) || (value instanceof DataMap))) {
98
99             String JavaDoc rootClass = (value != null) ? value.getClass().getName() : "null";
100
101             throw new IllegalArgumentException JavaDoc(
102                     getClass().getName()
103                             + ": \"setRoot(..)\" takes a DataMap, String, ObjEntity, DbEntity, Procedure, "
104                             + "or Class. It was passed a "
105                             + rootClass);
106         }
107
108         this.root = value;
109     }
110
111     public String JavaDoc toString() {
112         return new ToStringBuilder(this)
113                 .append("root", root)
114                 .append("name", getName())
115                 .toString();
116     }
117
118     /**
119      * @since 1.2
120      */

121     public abstract SQLAction createSQLAction(SQLActionVisitor visitor);
122
123     /**
124      * Implements default routing mechanism relying on the EntityResolver to find DataMap
125      * based on the query root. This mechanism should be sufficient for most queries that
126      * "know" their root.
127      *
128      * @since 1.2
129      */

130     public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery) {
131         DataMap map = getMetaData(resolver).getDataMap();
132
133         if (map == null) {
134             throw new CayenneRuntimeException("No DataMap found, can't route query "
135                     + this);
136         }
137
138         router.route(router.engineForDataMap(map), this, substitutedQuery);
139     }
140 }
141
Popular Tags