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 package org.apache.openjpa.kernel;
20
21 import java.util.Collection;
22 import java.util.Map;
23
24 import org.apache.commons.collections.map.LinkedMap;
25 import org.apache.openjpa.kernel.exps.AggregateListener;
26 import org.apache.openjpa.kernel.exps.FilterListener;
27 import org.apache.openjpa.meta.ClassMetaData;
28 import org.apache.openjpa.util.RuntimeExceptionTranslator;
29
30 /**
31 * Delegating query that can also perform exception translation
32 * for use in facades.
33 *
34 * @since 0.4.0
35 * @author Abe White
36 * @nojavadoc
37 */
38 public class DelegatingQuery
39 implements Query {
40 ///////////////////////////////////////////////////////////////
41 // NOTE: when adding a public API method, be sure to add it to
42 // JDO and JPA facades!
43 ///////////////////////////////////////////////////////////////
44
45 private final Query _query;
46 private final DelegatingQuery _del;
47 private final RuntimeExceptionTranslator _trans;
48
49 /**
50 * Constructor; supply delegate.
51 */
52 public DelegatingQuery(Query query) {
53 this(query, null);
54 }
55
56 /**
57 * Constructor; supply delegate and exception translator.
58 */
59 public DelegatingQuery(Query query, RuntimeExceptionTranslator trans) {
60 _query = query;
61 if (query instanceof DelegatingQuery)
62 _del = (DelegatingQuery) query;
63 else
64 _del = null;
65 _trans = trans;
66 }
67
68 /**
69 * Return the direct delegate.
70 */
71 public Query getDelegate() {
72 return _query;
73 }
74
75 /**
76 * Return the native delegate.
77 */
78 public Query getInnermostDelegate() {
79 return (_del == null) ? _query : _del.getInnermostDelegate();
80 }
81
82 public int hashCode() {
83 return getInnermostDelegate().hashCode();
84 }
85
86 public boolean equals(Object other) {
87 if (other == this)
88 return true;
89 if (other instanceof DelegatingQuery)
90 other = ((DelegatingQuery) other).getInnermostDelegate();
91 return getInnermostDelegate().equals(other);
92 }
93
94 /**
95 * Translate the OpenJPA exception.
96 */
97 protected RuntimeException translate(RuntimeException re) {
98 return (_trans == null) ? re : _trans.translate(re);
99 }
100
101 public Broker getBroker() {
102 try {
103 return _query.getBroker();
104 } catch (RuntimeException re) {
105 throw translate(re);
106 }
107 }
108
109 public Query getQuery() {
110 return this;
111 }
112
113 public StoreContext getStoreContext() {
114 try {
115 return _query.getStoreContext();
116 } catch (RuntimeException re) {
117 throw translate(re);
118 }
119 }
120
121 public int getOperation() {
122 try {
123 return _query.getOperation();
124 } catch (RuntimeException re) {
125 throw translate(re);
126 }
127 }
128
129 public String getLanguage() {
130 try {
131 return _query.getLanguage();
132 } catch (RuntimeException re) {
133 throw translate(re);
134 }
135 }
136
137 public FetchConfiguration getFetchConfiguration() {
138 try {
139 return _query.getFetchConfiguration();
140 } catch (RuntimeException re) {
141 throw translate(re);
142 }
143 }
144
145 public String getQueryString() {
146 try {
147 return _query.getQueryString();
148 } catch (RuntimeException re) {
149 throw translate(re);
150 }
151 }
152
153 public boolean getIgnoreChanges() {
154 try {
155 return _query.getIgnoreChanges();
156 } catch (RuntimeException re) {
157 throw translate(re);
158 }
159 }
160
161 public Object getCompilation() {
162 try {
163 return _query.getCompilation();
164 } catch (RuntimeException re) {
165 throw translate(re);
166 }
167 }
168
169 public String getAlias() {
170 try {
171 return _query.getAlias();
172 } catch (RuntimeException re) {
173 throw translate(re);
174 }
175 }
176
177 public String[] getProjectionAliases() {
178 try {
179 return _query.getProjectionAliases();
180 } catch (RuntimeException re) {
181 throw translate(re);
182 }
183 }
184
185 public Class[] getProjectionTypes() {
186 try {
187 return _query.getProjectionTypes();
188 } catch (RuntimeException re) {
189 throw translate(re);
190 }
191 }
192
193 public boolean isAggregate() {
194 try {
195 return _query.isAggregate();
196 } catch (RuntimeException re) {
197 throw translate(re);
198 }
199 }
200
201 public boolean hasGrouping() {
202 try {
203 return _query.hasGrouping();
204 } catch (RuntimeException re) {
205 throw translate(re);
206 }
207 }
208
209 public ClassMetaData[] getAccessPathMetaDatas() {
210 try {
211 return _query.getAccessPathMetaDatas();
212 } catch (RuntimeException re) {
213 throw translate(re);
214 }
215 }
216
217 public FilterListener getFilterListener(String tag) {
218 try {
219 return _query.getFilterListener(tag);
220 } catch (RuntimeException re) {
221 throw translate(re);
222 }
223 }
224
225 public AggregateListener getAggregateListener(String tag) {
226 try {
227 return _query.getAggregateListener(tag);
228 } catch (RuntimeException re) {
229 throw translate(re);
230 }
231 }
232
233 public Collection getFilterListeners() {
234 try {
235 return _query.getFilterListeners();
236 } catch (RuntimeException re) {
237 throw translate(re);
238 }
239 }
240
241 public Collection getAggregateListeners() {
242 try {
243 return _query.getAggregateListeners();
244 } catch (RuntimeException re) {
245 throw translate(re);
246 }
247 }
248
249 public Collection getCandidateCollection() {
250 try {
251 return _query.getCandidateCollection();
252 } catch (RuntimeException re) {
253 throw translate(re);
254 }
255 }
256
257 public Class getCandidateType() {
258 try {
259 return _query.getCandidateType();
260 } catch (RuntimeException re) {
261 throw translate(re);
262 }
263 }
264
265 public boolean hasSubclasses() {
266 try {
267 return _query.hasSubclasses();
268 } catch (RuntimeException re) {
269 throw translate(re);
270 }
271 }
272
273 public void setCandidateType(Class cls, boolean subs) {
274 try {
275 _query.setCandidateType(cls, subs);
276 } catch (RuntimeException re) {
277 throw translate(re);
278 }
279 }
280
281 public boolean isReadOnly() {
282 try {
283 return _query.isReadOnly();
284 } catch (RuntimeException re) {
285 throw translate(re);
286 }
287 }
288
289 public void setReadOnly(boolean readOnly) {
290 try {
291 _query.setReadOnly(readOnly);
292 } catch (RuntimeException re) {
293 throw translate(re);
294 }
295 }
296
297 public Class getResultMappingScope() {
298 try {
299 return _query.getResultMappingScope();
300 } catch (RuntimeException re) {
301 throw translate(re);
302 }
303 }
304
305 public String getResultMappingName() {
306 try {
307 return _query.getResultMappingName();
308 } catch (RuntimeException re) {
309 throw translate(re);
310 }
311 }
312
313 public void setResultMapping(Class scope, String name) {
314 try {
315 _query.setResultMapping(scope, name);
316 } catch (RuntimeException re) {
317 throw translate(re);
318 }
319 }
320
321 public boolean isUnique() {
322 try {
323 return _query.isUnique();
324 } catch (RuntimeException re) {
325 throw translate(re);
326 }
327 }
328
329 public void setUnique(boolean unique) {
330 try {
331 _query.setUnique(unique);
332 } catch (RuntimeException re) {
333 throw translate(re);
334 }
335 }
336
337 public Class getResultType() {
338 try {
339 return _query.getResultType();
340 } catch (RuntimeException re) {
341 throw translate(re);
342 }
343 }
344
345 public void setResultType(Class cls) {
346 try {
347 _query.setResultType(cls);
348 } catch (RuntimeException re) {
349 throw translate(re);
350 }
351 }
352
353 public long getStartRange() {
354 try {
355 return _query.getStartRange();
356 } catch (RuntimeException re) {
357 throw translate(re);
358 }
359 }
360
361 public long getEndRange() {
362 try {
363 return _query.getEndRange();
364 } catch (RuntimeException re) {
365 throw translate(re);
366 }
367 }
368
369 public void setRange(long start, long end) {
370 try {
371 _query.setRange(start, end);
372 } catch (RuntimeException re) {
373 throw translate(re);
374 }
375 }
376
377 public String getParameterDeclaration() {
378 try {
379 return _query.getParameterDeclaration();
380 } catch (RuntimeException re) {
381 throw translate(re);
382 }
383 }
384
385 public LinkedMap getParameterTypes() {
386 try {
387 return _query.getParameterTypes();
388 } catch (RuntimeException re) {
389 throw translate(re);
390 }
391 }
392
393 public Map getUpdates() {
394 try {
395 return _query.getUpdates();
396 } catch (RuntimeException re) {
397 throw translate(re);
398 }
399 }
400
401 public void declareParameters(String params) {
402 try {
403 _query.declareParameters(params);
404 } catch (RuntimeException re) {
405 throw translate(re);
406 }
407 }
408
409 public Number deleteInMemory(StoreQuery q, StoreQuery.Executor ex,
410 Object[] params) {
411 try {
412 return _query.deleteInMemory(q, ex, params);
413 } catch (RuntimeException re) {
414 throw translate(re);
415 }
416 }
417
418 public Number updateInMemory(StoreQuery q, StoreQuery.Executor ex,
419 Object[] params) {
420 try {
421 return _query.updateInMemory(q, ex, params);
422 } catch (RuntimeException re) {
423 throw translate(re);
424 }
425 }
426
427 public Class classForName(String name, String[] imports) {
428 try {
429 return _query.classForName(name, imports);
430 } catch (RuntimeException re) {
431 throw translate(re);
432 }
433 }
434
435 public void lock() {
436 try {
437 _query.lock();
438 } catch (RuntimeException re) {
439 throw translate(re);
440 }
441 }
442
443 public void unlock() {
444 try {
445 _query.unlock();
446 } catch (RuntimeException re) {
447 throw translate(re);
448 }
449 }
450
451 public void addFilterListener(FilterListener listener) {
452 try {
453 _query.addFilterListener(listener);
454 } catch (RuntimeException re) {
455 throw translate(re);
456 }
457 }
458
459 public void removeFilterListener(FilterListener listener) {
460 try {
461 _query.removeFilterListener(listener);
462 } catch (RuntimeException re) {
463 throw translate(re);
464 }
465 }
466
467 public void addAggregateListener(AggregateListener listener) {
468 try {
469 _query.addAggregateListener(listener);
470 } catch (RuntimeException re) {
471 throw translate(re);
472 }
473 }
474
475 public void removeAggregateListener(AggregateListener listener) {
476 try {
477 _query.removeAggregateListener(listener);
478 } catch (RuntimeException re) {
479 throw translate(re);
480 }
481 }
482
483 public Extent getCandidateExtent() {
484 try {
485 return _query.getCandidateExtent();
486 } catch (RuntimeException re) {
487 throw translate(re);
488 }
489 }
490
491 public void setCandidateExtent(Extent extent) {
492 try {
493 _query.setCandidateExtent(extent);
494 } catch (RuntimeException re) {
495 throw translate(re);
496 }
497 }
498
499 public void setCandidateCollection(Collection coll) {
500 try {
501 _query.setCandidateCollection(coll);
502 } catch (RuntimeException re) {
503 throw translate(re);
504 }
505 }
506
507 public void compile() {
508 try {
509 _query.compile();
510 } catch (RuntimeException re) {
511 throw translate(re);
512 }
513 }
514
515 public Object execute() {
516 try {
517 return _query.execute();
518 } catch (RuntimeException re) {
519 throw translate(re);
520 }
521 }
522
523 public Object execute(Map params) {
524 try {
525 return _query.execute(params);
526 } catch (RuntimeException re) {
527 throw translate(re);
528 }
529 }
530
531 public Object execute(Object[] params) {
532 try {
533 return _query.execute(params);
534 } catch (RuntimeException re) {
535 throw translate(re);
536 }
537 }
538
539 public long deleteAll() {
540 try {
541 return _query.deleteAll();
542 } catch (RuntimeException re) {
543 throw translate(re);
544 }
545 }
546
547 public long deleteAll(Object[] parameters) {
548 try {
549 return _query.deleteAll(parameters);
550 } catch (RuntimeException re) {
551 throw translate(re);
552 }
553 }
554
555 public long deleteAll(Map parameterMap) {
556 try {
557 return _query.deleteAll(parameterMap);
558 } catch (RuntimeException re) {
559 throw translate(re);
560 }
561 }
562
563 public long updateAll() {
564 try {
565 return _query.updateAll();
566 } catch (RuntimeException re) {
567 throw translate(re);
568 }
569 }
570
571 public long updateAll(Object[] parameters) {
572 try {
573 return _query.updateAll(parameters);
574 } catch (RuntimeException re) {
575 throw translate(re);
576 }
577 }
578
579 public long updateAll(Map parameterMap) {
580 try {
581 return _query.updateAll(parameterMap);
582 } catch (RuntimeException re) {
583 throw translate(re);
584 }
585 }
586
587 public void closeAll() {
588 try {
589 _query.closeAll();
590 } catch (RuntimeException re) {
591 throw translate(re);
592 }
593 }
594
595 public void closeResources() {
596 try {
597 _query.closeResources();
598 } catch (RuntimeException re) {
599 throw translate(re);
600 }
601 }
602
603 public String[] getDataStoreActions(Map params) {
604 try {
605 return _query.getDataStoreActions(params);
606 } catch (RuntimeException re) {
607 throw translate(re);
608 }
609 }
610
611 public boolean setQuery(Object query) {
612 try {
613 return _query.setQuery(query);
614 } catch (RuntimeException re) {
615 throw translate(re);
616 }
617 }
618
619 public void setIgnoreChanges(boolean ignore) {
620 try {
621 _query.setIgnoreChanges(ignore);
622 } catch (RuntimeException re) {
623 throw translate(re);
624 }
625 }
626
627 public void assertOpen() {
628 try {
629 _query.assertOpen();
630 } catch (RuntimeException re) {
631 throw translate(re);
632 }
633 }
634
635 public void assertNotReadOnly() {
636 try {
637 _query.assertNotReadOnly();
638 } catch (RuntimeException re) {
639 throw translate(re);
640 }
641 }
642
643 public void assertNotSerialized() {
644 try {
645 _query.assertNotSerialized();
646 } catch (RuntimeException re) {
647 throw translate(re);
648 }
649 }
650 }
651