KickJava   Java API By Example, From Geeks To Geeks.

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


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.cayenne.CayenneRuntimeException;
23 import org.apache.cayenne.map.EntityResolver;
24
25 /**
26  * A convenience superclass of the queries that resolve into some other queries during the
27  * routing phase. Provides caching of a replacement query.
28  *
29  * @since 1.2
30  * @author Andrus Adamchik
31  */

32 public abstract class IndirectQuery implements Query {
33
34     protected String JavaDoc name;
35
36     protected transient Query replacementQuery;
37     protected transient EntityResolver lastResolver;
38
39     /**
40      * Returns the metadata obtained from the replacement query.
41      */

42     public QueryMetadata getMetaData(EntityResolver resolver) {
43         return getReplacementQuery(resolver).getMetaData(resolver);
44     }
45
46     public String JavaDoc getName() {
47         return name;
48     }
49
50     public void setName(String JavaDoc name) {
51         this.name = name;
52     }
53
54     /**
55      * Delegates routing to a replacement query.
56      */

57     public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery) {
58         getReplacementQuery(resolver).route(
59                 router,
60                 resolver,
61                 substitutedQuery != null ? substitutedQuery : this);
62     }
63
64     /**
65      * Creates a substitute query. An implementor is free to provide an arbitrary
66      * replacement query.
67      */

68     protected abstract Query createReplacementQuery(EntityResolver resolver);
69
70     /**
71      * Returns a replacement query, creating it on demand and caching it for reuse.
72      */

73     protected Query getReplacementQuery(EntityResolver resolver) {
74         if (replacementQuery == null || lastResolver != resolver) {
75             this.replacementQuery = createReplacementQuery(resolver);
76             this.lastResolver = resolver;
77         }
78
79         return replacementQuery;
80     }
81
82     /**
83      * Throws an exception as indirect query should not be executed directly.
84      */

85     public SQLAction createSQLAction(SQLActionVisitor visitor) {
86         throw new CayenneRuntimeException(this.getClass().getName()
87                 + " is an indirect query and doesn't support its own sql actions. "
88                 + "It should've been delegated to another "
89                 + "query during resolution or routing phase.");
90     }
91 }
92
Popular Tags