KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.apache.cayenne.CayenneRuntimeException;
28 import org.apache.cayenne.map.EntityResolver;
29
30 /**
31  * A Query decorator for a collection of other queries. Note that QueryChain will always
32  * return DataRows (that is if it returns data), as it has no way of knowing how to
33  * convert the results to objects.
34  *
35  * @since 1.2
36  * @author Andrus Adamchik
37  */

38 public class QueryChain implements Query {
39
40     protected Collection JavaDoc chain;
41     protected String JavaDoc name;
42
43     /**
44      * Creates an empty QueryChain.
45      */

46     public QueryChain() {
47     }
48
49     /**
50      * Creates a new QueryChain out of an array of queries.
51      */

52     public QueryChain(Query[] queries) {
53         if (queries != null && queries.length > 0) {
54             this.chain = new ArrayList JavaDoc(Arrays.asList(queries));
55         }
56     }
57
58     /**
59      * Creates a new QueryChain with a collection of Queries.
60      */

61     public QueryChain(Collection JavaDoc queries) {
62         if (queries != null && !queries.isEmpty()) {
63             this.chain = new ArrayList JavaDoc(queries);
64         }
65     }
66
67     /**
68      * Adds a query to the chain.
69      */

70     public void addQuery(Query query) {
71         if (chain == null) {
72             chain = new ArrayList JavaDoc();
73         }
74
75         chain.add(query);
76     }
77
78     /**
79      * Removes a query from the chain, returning true if the query was indeed present in
80      * the chain and was removed.
81      */

82     public boolean removeQuery(Query query) {
83         return (chain != null) ? chain.remove(query) : false;
84     }
85
86     public boolean isEmpty() {
87         return chain == null || chain.isEmpty();
88     }
89
90     /**
91      * Delegates routing to each individual query in the chain. If there is no queries,
92      * this method does nothing.
93      */

94     public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery) {
95         if (chain != null && !chain.isEmpty()) {
96             Iterator JavaDoc it = chain.iterator();
97             while (it.hasNext()) {
98                 Query q = (Query) it.next();
99                 q.route(router, resolver, substitutedQuery);
100             }
101         }
102     }
103
104     /**
105      * Throws an exception as execution should've been delegated to the queries contained
106      * in the chain.
107      */

108     public SQLAction createSQLAction(SQLActionVisitor visitor) {
109         throw new CayenneRuntimeException("Chain doesn't support its own execution "
110                 + "and should've been split into separate queries during routing phase.");
111     }
112
113     public String JavaDoc getName() {
114         return name;
115     }
116
117     public void setName(String JavaDoc name) {
118         this.name = name;
119     }
120
121     /**
122      * Returns default metadata.
123      */

124     public QueryMetadata getMetaData(EntityResolver resolver) {
125         QueryMetadataWrapper wrapper = new QueryMetadataWrapper(
126                 DefaultQueryMetadata.defaultMetadata);
127         wrapper.override(QueryMetadata.FETCHING_DATA_ROWS_PROPERTY, Boolean.TRUE);
128         return wrapper;
129     }
130 }
131
Popular Tags