KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > query > QueryChain


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.query;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.Collection JavaDoc;
60 import java.util.Iterator JavaDoc;
61
62 import org.apache.log4j.Level;
63 import org.objectstyle.cayenne.CayenneRuntimeException;
64 import org.objectstyle.cayenne.map.EntityResolver;
65
66 /**
67  * A Query decorator for a collection of other queries.
68  *
69  * @since 1.2
70  * @author Andrus Adamchik
71  */

72 public class QueryChain implements Query {
73
74     protected String JavaDoc name;
75     protected Level loggingLevel;
76     protected Collection JavaDoc chain;
77
78     /**
79      * Creates an empty QueryChain.
80      */

81     public QueryChain() {
82     }
83
84     /**
85      * Creates a new QueryChain with a collection of Queries.
86      */

87     public QueryChain(Collection JavaDoc queries) {
88         if (queries != null && !queries.isEmpty()) {
89             this.chain = new ArrayList JavaDoc(queries);
90         }
91     }
92
93     /**
94      * Adds a query to the chain.
95      */

96     public void addQuery(Query query) {
97         if (chain == null) {
98             chain = new ArrayList JavaDoc();
99         }
100
101         chain.add(query);
102     }
103
104     /**
105      * Removes a query from the chain, returning true if the query was indeed present in
106      * the chain and was removed.
107      */

108     public boolean removeQuery(Query query) {
109         return (chain != null) ? chain.remove(query) : false;
110     }
111
112     public boolean isEmpty() {
113         return chain == null || chain.isEmpty();
114     }
115
116     public String JavaDoc getName() {
117         return name;
118     }
119
120     public void setName(String JavaDoc name) {
121         this.name = name;
122     }
123
124     public Level getLoggingLevel() {
125         return loggingLevel;
126     }
127
128     /**
129      * Logging level of the QueryChanin can be set, but will be ignored. Keeping for
130      * compatibility with Query interface.
131      */

132     public void setLoggingLevel(Level loggingLevel) {
133         this.loggingLevel = loggingLevel;
134     }
135
136     /**
137      * Throws an exception - chain has no root of its own and each query in a chain is
138      * routed individually.
139      */

140     public Object JavaDoc getRoot() {
141         throw new CayenneRuntimeException(
142                 "Chain doesn't support its own root. Root should be deprecated soon anyway");
143     }
144
145     /**
146      * Throws an exception - chain has no root of its own and each query in a chain is
147      * routed individually.
148      */

149     public void setRoot(Object JavaDoc root) {
150         throw new CayenneRuntimeException(
151                 "Chain doesn't support its own root. Root should be deprecated soon anyway. An attempt to set it to "
152                         + root);
153     }
154
155     /**
156      * Resolves queries in the chain, creating another QueryChain that contains resolved
157      * queries.
158      */

159     public Query resolve(EntityResolver resolver) {
160         if (isEmpty()) {
161             return this;
162         }
163
164         Collection JavaDoc resolvedChain = new ArrayList JavaDoc(chain.size());
165
166         Iterator JavaDoc it = chain.iterator();
167         while (it.hasNext()) {
168             Query resolved = ((Query) it.next()).resolve(resolver);
169             if (resolved != null) {
170                 resolvedChain.add(resolved);
171             }
172         }
173
174         return new QueryChain(resolvedChain);
175     }
176
177     /**
178      * Delegates routing to each individual query in the chain. If there is no queries,
179      * this method does nothing.
180      */

181     public void route(QueryRouter router, EntityResolver resolver) {
182         if (chain != null && !chain.isEmpty()) {
183             Iterator JavaDoc it = chain.iterator();
184             while (it.hasNext()) {
185                 Query q = (Query) it.next();
186                 q.route(router, resolver);
187             }
188         }
189     }
190
191     /**
192      * Throws an exception as execution should've been delegated to the queries contained
193      * in the chain.
194      */

195     public SQLAction createSQLAction(SQLActionVisitor visitor) {
196         throw new CayenneRuntimeException("Chain doesn't support its own execution "
197                 + "and should've been split into separate queries during routing phase.");
198     }
199 }
200
Popular Tags