KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > DataDomainLegacyQueryAction


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.access;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.apache.cayenne.CayenneRuntimeException;
30 import org.apache.cayenne.map.DataMap;
31 import org.apache.cayenne.query.Query;
32 import org.apache.cayenne.query.QueryMetadata;
33 import org.apache.cayenne.query.QueryRouter;
34
35 /**
36  * DataDomain query action that relies on externally provided OperationObserver to process
37  * the results.
38  *
39  * @since 1.2
40  * @author Andrus Adamchik
41  */

42 // TODO: andrus, 7/19/2006 - why is this public? should probably be deprecated and/or
43
// removed.
44
public class DataDomainLegacyQueryAction implements QueryRouter, OperationObserver {
45
46     static final boolean DONE = true;
47
48     DataDomain domain;
49     OperationObserver callback;
50     Query query;
51     QueryMetadata metadata;
52
53     Map JavaDoc queriesByNode;
54     Map JavaDoc queriesByExecutedQueries;
55
56     DataDomainLegacyQueryAction(DataDomain domain, Query query, OperationObserver callback) {
57         this.domain = domain;
58         this.query = query;
59         this.metadata = query.getMetaData(domain.getEntityResolver());
60         this.callback = callback;
61     }
62
63     /*
64      * Gets response from the underlying DataNodes.
65      */

66     final void execute() {
67
68         // reset
69
queriesByNode = null;
70         queriesByExecutedQueries = null;
71
72         // categorize queries by node and by "executable" query...
73
query.route(this, domain.getEntityResolver(), null);
74
75         // run categorized queries
76
if (queriesByNode != null) {
77             Iterator JavaDoc nodeIt = queriesByNode.entrySet().iterator();
78             while (nodeIt.hasNext()) {
79                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) nodeIt.next();
80                 QueryEngine nextNode = (QueryEngine) entry.getKey();
81                 Collection JavaDoc nodeQueries = (Collection JavaDoc) entry.getValue();
82                 nextNode.performQueries(nodeQueries, this);
83             }
84         }
85     }
86
87     public void route(QueryEngine engine, Query query, Query substitutedQuery) {
88
89         List JavaDoc queries = null;
90         if (queriesByNode == null) {
91             queriesByNode = new HashMap JavaDoc();
92         }
93         else {
94             queries = (List JavaDoc) queriesByNode.get(engine);
95         }
96
97         if (queries == null) {
98             queries = new ArrayList JavaDoc(5);
99             queriesByNode.put(engine, queries);
100         }
101
102         queries.add(query);
103
104         // handle case when routing resuled in an "exectable" query different from the
105
// original query.
106
if (substitutedQuery != null && substitutedQuery != query) {
107
108             if (queriesByExecutedQueries == null) {
109                 queriesByExecutedQueries = new HashMap JavaDoc();
110             }
111
112             queriesByExecutedQueries.put(query, substitutedQuery);
113         }
114     }
115
116     public QueryEngine engineForDataMap(DataMap map) {
117         if (map == null) {
118             throw new NullPointerException JavaDoc("Null DataMap, can't determine DataNode.");
119         }
120
121         QueryEngine node = domain.lookupDataNode(map);
122
123         if (node == null) {
124             throw new CayenneRuntimeException("No DataNode exists for DataMap " + map);
125         }
126
127         return node;
128     }
129
130     public void nextCount(Query query, int resultCount) {
131         callback.nextCount(queryForExecutedQuery(query), resultCount);
132     }
133
134     public void nextBatchCount(Query query, int[] resultCount) {
135         callback.nextBatchCount(queryForExecutedQuery(query), resultCount);
136     }
137
138     public void nextDataRows(Query query, List JavaDoc dataRows) {
139         callback.nextDataRows(queryForExecutedQuery(query), dataRows);
140     }
141
142     public void nextDataRows(Query q, ResultIterator it) {
143         callback.nextDataRows(queryForExecutedQuery(q), it);
144     }
145
146     public void nextGeneratedDataRows(Query query, ResultIterator keysIterator) {
147         callback.nextGeneratedDataRows(queryForExecutedQuery(query), keysIterator);
148     }
149
150     public void nextQueryException(Query query, Exception JavaDoc ex) {
151         callback.nextQueryException(queryForExecutedQuery(query), ex);
152     }
153
154     public void nextGlobalException(Exception JavaDoc e) {
155         callback.nextGlobalException(e);
156     }
157
158     public boolean isIteratedResult() {
159         return callback.isIteratedResult();
160     }
161
162     Query queryForExecutedQuery(Query executedQuery) {
163         Query q = null;
164
165         if (queriesByExecutedQueries != null) {
166             q = (Query) queriesByExecutedQueries.get(executedQuery);
167         }
168
169         return q != null ? q : executedQuery;
170     }
171 }
172
Popular Tags