1 19 20 package org.apache.cayenne.access; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 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 42 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 queriesByNode; 54 Map 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 66 final void execute() { 67 68 queriesByNode = null; 70 queriesByExecutedQueries = null; 71 72 query.route(this, domain.getEntityResolver(), null); 74 75 if (queriesByNode != null) { 77 Iterator nodeIt = queriesByNode.entrySet().iterator(); 78 while (nodeIt.hasNext()) { 79 Map.Entry entry = (Map.Entry ) nodeIt.next(); 80 QueryEngine nextNode = (QueryEngine) entry.getKey(); 81 Collection nodeQueries = (Collection ) entry.getValue(); 82 nextNode.performQueries(nodeQueries, this); 83 } 84 } 85 } 86 87 public void route(QueryEngine engine, Query query, Query substitutedQuery) { 88 89 List queries = null; 90 if (queriesByNode == null) { 91 queriesByNode = new HashMap (); 92 } 93 else { 94 queries = (List ) queriesByNode.get(engine); 95 } 96 97 if (queries == null) { 98 queries = new ArrayList (5); 99 queriesByNode.put(engine, queries); 100 } 101 102 queries.add(query); 103 104 if (substitutedQuery != null && substitutedQuery != query) { 107 108 if (queriesByExecutedQueries == null) { 109 queriesByExecutedQueries = new HashMap (); 110 } 111 112 queriesByExecutedQueries.put(query, substitutedQuery); 113 } 114 } 115 116 public QueryEngine engineForDataMap(DataMap map) { 117 if (map == null) { 118 throw new NullPointerException ("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 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 ex) { 151 callback.nextQueryException(queryForExecutedQuery(query), ex); 152 } 153 154 public void nextGlobalException(Exception 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 |