1 56 57 package org.objectstyle.cayenne.access; 58 59 import java.util.ArrayList ; 60 import java.util.Collection ; 61 import java.util.Collections ; 62 import java.util.Iterator ; 63 import java.util.List ; 64 65 import org.apache.commons.collections.map.LinkedMap; 66 import org.objectstyle.cayenne.CayenneRuntimeException; 67 import org.objectstyle.cayenne.QueryResponse; 68 import org.objectstyle.cayenne.access.util.DefaultOperationObserver; 69 import org.objectstyle.cayenne.query.Query; 70 import org.objectstyle.cayenne.util.Util; 71 72 79 public class QueryResult extends DefaultOperationObserver implements QueryResponse { 80 81 protected LinkedMap queries = new LinkedMap(); 87 88 89 public void clear() { 90 queries.clear(); 91 } 92 93 96 public Iterator getQueries() { 97 return queries.asList().iterator(); 98 } 99 100 105 public Collection getResults() { 106 if (queries.isEmpty()) { 107 return Collections.EMPTY_LIST; 108 } 109 110 Collection results = new ArrayList (5); 111 112 Iterator it = queries.values().iterator(); 113 while (it.hasNext()) { 114 Collection queryResult = (Collection ) it.next(); 115 results.addAll(queryResult); 116 } 117 118 return results; 119 } 120 121 126 public List getResults(Query query) { 127 List list = (List ) queries.get(query); 128 return (list != null) ? list : Collections.EMPTY_LIST; 129 } 130 131 136 public int getFirstUpdateCount(Query query) { 137 List allResults = getResults(query); 138 int size = allResults.size(); 139 if (size > 0) { 140 Iterator it = allResults.iterator(); 141 while (it.hasNext()) { 142 Object obj = it.next(); 143 if (obj instanceof Number ) { 144 return ((Number ) obj).intValue(); 145 } 146 } 147 } 148 return -1; 149 } 150 151 155 public List getFirstRows(Query query) { 156 List allResults = getResults(query); 157 int size = allResults.size(); 158 if (size == 0) { 159 return Collections.EMPTY_LIST; 160 } else { 161 Iterator it = allResults.iterator(); 162 while (it.hasNext()) { 163 Object obj = it.next(); 164 if (obj instanceof List ) { 165 return (List ) obj; 166 } 167 } 168 } 169 170 return Collections.EMPTY_LIST; 171 } 172 173 178 public List getRows(Query query) { 179 List allResults = getResults(query); 180 int size = allResults.size(); 181 if (size == 0) { 182 return Collections.EMPTY_LIST; 183 } 184 185 List list = new ArrayList (size); 186 Iterator it = allResults.iterator(); 187 while (it.hasNext()) { 188 Object obj = it.next(); 189 if (obj instanceof List ) { 190 list.add(obj); 191 } 192 } 193 194 return list; 195 } 196 197 202 public List getUpdates(Query query) { 203 List allResults = getResults(query); 204 int size = allResults.size(); 205 if (size == 0) { 206 return Collections.EMPTY_LIST; 207 } 208 209 List list = new ArrayList (size); 210 Iterator it = allResults.iterator(); 211 while (it.hasNext()) { 212 Object obj = it.next(); 213 if (obj instanceof Number ) { 214 list.add(obj); 215 } 216 } 217 218 return list; 219 } 220 221 225 public void nextQueryException(Query query, Exception ex) { 226 super.nextQueryException(query, ex); 227 throw new CayenneRuntimeException( 228 "Query exception.", 229 Util.unwindException(ex)); 230 } 231 232 236 public void nextGlobalException(Exception ex) { 237 super.nextGlobalException(ex); 238 throw new CayenneRuntimeException( 239 "Global exception.", 240 Util.unwindException(ex)); 241 } 242 243 246 public boolean isIteratedResult() { 247 return false; 248 } 249 250 public void nextBatchCount(Query query, int[] resultCount) { 251 for (int i = 0; i < resultCount.length; i++) { 252 nextCount(query, resultCount[i]); 253 } 254 } 255 256 public void nextCount(Query query, int resultCount) { 257 super.nextCount(query, resultCount); 258 259 List list = (List ) queries.get(query); 260 if (list == null) { 261 list = new ArrayList (5); 262 queries.put(query, list); 263 } 264 265 list.add(new Integer (resultCount)); 266 } 267 268 public void nextDataRows(Query query, List dataRows) { 269 super.nextDataRows(query, dataRows); 270 271 List list = (List ) queries.get(query); 272 if (list == null) { 273 list = new ArrayList (5); 274 queries.put(query, list); 275 } 276 277 list.add(dataRows); 278 } 279 280 public void nextDataRows(Query q, ResultIterator it) { 281 throw new CayenneRuntimeException( 282 "Iterated results are not supported by " 283 + this.getClass().getName()); 284 } 285 } 286 | Popular Tags |