1 /***
2 * @(#)JDOServiceImpl.java
3 *
4 * JFoxSOAF, Service-Oriented Application Framework
5 *
6 * Copyright(c) JFoxSOAF Team
7 *
8 * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.gnu.org/copyleft/lesser.html
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * For more information, please visit:
21 * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 * http://www.huihoo.org/jfox/jfoxsoaf
23 */
24
25 package org.huihoo.jfox.soaf.services.persistence;
26
27 import java.util.Collection;
28 import java.util.Map;
29
30 import javax.jdo.JDOException;
31 import javax.jdo.PersistenceManager;
32 import javax.jdo.PersistenceManagerFactory;
33 import javax.jdo.Query;
34 import javax.jdo.Transaction;
35
36 import org.huihoo.jfox.soaf.exception.DAOException;
37
38 /***
39 * <p>
40 * JDO persistence service wrapper implementation
41 * </p>
42 *
43 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
44 * @version $Revision: 1.1 $ $Date: 2006/02/15 08:45:45 $
45 * @version Revision: 1.0
46 */
47
48 public class JdoServiceImpl implements JdoService {
49
50 private PersistenceManagerFactory pmFactory;
51
52 private Transaction transaction;
53
54 private PersistenceManager pm;
55
56 /***
57 * Default Constructor.
58 */
59 public JdoServiceImpl() {
60 }
61
62 /***
63 * Set SessionFactory
64 * @param sessionFactory
65 */
66 public void setPersistenceManagerFactory(PersistenceManagerFactory pmFactory) {
67 this.pmFactory = pmFactory;
68 }
69
70 /***
71 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getPersistenceManager()
72 */
73 private PersistenceManager getPersistenceManager() throws JDOException {
74 if (pm == null) {
75 pm = pmFactory.getPersistenceManager();
76 }
77 return pm;
78 }
79
80 /***
81 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#openTransaction()
82 */
83 public void beginTransaction() throws JDOException {
84 if (pm == null && transaction == null) {
85 pm = getPersistenceManager();
86 transaction = pm.currentTransaction();
87 transaction.begin();
88 }
89 }
90
91 /***
92 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#commit()
93 */
94 public void commit() throws JDOException {
95 if (pm != null && !pm.isClosed() && transaction != null
96 && transaction.isActive()) {
97 transaction.commit();
98 }
99 }
100
101 /***
102 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#rollback()
103 */
104 public void rollback() throws JDOException {
105 if (pm != null && !pm.isClosed() && transaction != null
106 && transaction.isActive()) {
107 transaction.rollback();
108 }
109 }
110
111 /***
112 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#close()
113 */
114 public void close() throws JDOException {
115 if (pm != null && !pm.isClosed()) {
116 pm.close();
117 pm = null;
118 }
119 }
120
121 /***
122 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Object)
123 */
124 public Object getObjectById(Object oid) throws JDOException, DAOException {
125 Object obj;
126 PersistenceManager pm;
127 try {
128 pm = getPersistenceManager();
129 beginTransaction();
130 obj = pm.getObjectById(oid);
131 commit();
132 } catch (Exception e) {
133 rollback();
134 throw new DAOException(e);
135 } finally {
136 pm = null;
137 close();
138 }
139 return obj;
140 }
141
142 /***
143 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Class, Object)
144 */
145 public Object getObjectById(Class clazz, Object oid) throws JDOException,
146 DAOException {
147 Object obj;
148 PersistenceManager pm;
149 try {
150 pm = getPersistenceManager();
151 beginTransaction();
152 obj = pm.getObjectById(clazz, oid);
153 commit();
154 } catch (Exception e) {
155 rollback();
156 throw new DAOException(e);
157 } finally {
158 pm = null;
159 close();
160 }
161 return obj;
162 }
163
164 /***
165 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Object, boolean)
166 */
167 public Object getObjectById(Object oid, boolean validate)
168 throws JDOException, DAOException {
169 Object obj;
170 PersistenceManager pm;
171 try {
172 pm = getPersistenceManager();
173 beginTransaction();
174 obj = pm.getObjectById(oid, validate);
175 commit();
176 } catch (Exception e) {
177 rollback();
178 throw new DAOException(e);
179 } finally {
180 pm = null;
181 close();
182 }
183 return obj;
184 }
185
186 /***
187 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Object)
188 */
189 public Object getObjectId(Object pc) throws JDOException, DAOException {
190 Object obj;
191 PersistenceManager pm;
192 try {
193 pm = getPersistenceManager();
194 beginTransaction();
195 obj = pm.getObjectById(pc);
196 commit();
197 } catch (Exception e) {
198 rollback();
199 throw new DAOException(e);
200 } finally {
201 pm = null;
202 close();
203 }
204 return obj;
205 }
206
207 /***
208 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Class)
209 */
210 public Class getObjectIdClass(Class clazz) throws JDOException,
211 DAOException {
212 Class claz;
213 PersistenceManager pm;
214 try {
215 pm = getPersistenceManager();
216 beginTransaction();
217 claz = pm.getObjectIdClass(clazz);
218 commit();
219 } catch (Exception e) {
220 rollback();
221 throw new DAOException(e);
222 } finally {
223 pm = null;
224 close();
225 }
226 return claz;
227 }
228
229 /***
230 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectsById(Collection)
231 */
232 public Collection getObjectsById(Collection oids) throws JDOException,
233 DAOException {
234 Collection coll;
235 PersistenceManager pm;
236 try {
237 pm = getPersistenceManager();
238 beginTransaction();
239 coll = pm.getObjectsById(oids);
240 commit();
241 } catch (Exception e) {
242 rollback();
243 throw new DAOException(e);
244 } finally {
245 pm = null;
246 close();
247 }
248 return coll;
249 }
250
251 /***
252 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Object, boolean)
253 */
254 public Collection getObjectsById(Collection oids, boolean validate)
255 throws JDOException, DAOException {
256 Collection coll;
257 PersistenceManager pm;
258 try {
259 pm = getPersistenceManager();
260 beginTransaction();
261 coll = pm.getObjectsById(oids, validate);
262 commit();
263 } catch (Exception e) {
264 rollback();
265 throw new DAOException(e);
266 } finally {
267 pm = null;
268 close();
269 }
270 return coll;
271 }
272
273 /***
274 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectsById(Object[])
275 */
276 public Object[] getObjectsById(Object[] oids) throws JDOException,
277 DAOException {
278 Object[] objects;
279 PersistenceManager pm;
280 try {
281 pm = getPersistenceManager();
282 beginTransaction();
283 objects = pm.getObjectsById(oids);
284 commit();
285 } catch (Exception e) {
286 rollback();
287 throw new DAOException(e);
288 } finally {
289 pm = null;
290 close();
291 }
292 return objects;
293 }
294
295 /***
296 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectsById(Object[], boolean)
297 */
298 public Object[] getObjectsById(Object[] oids, boolean validate)
299 throws JDOException, DAOException {
300 Object[] objects;
301 PersistenceManager pm;
302 try {
303 pm = getPersistenceManager();
304 beginTransaction();
305 objects = pm.getObjectsById(oids, validate);
306 commit();
307 } catch (Exception e) {
308 rollback();
309 throw new DAOException(e);
310 } finally {
311 pm = null;
312 close();
313 }
314 return objects;
315 }
316
317 /***
318 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Class)
319 */
320 public Collection query(Class clazz) throws JDOException, DAOException {
321 Collection coll;
322 PersistenceManager pm;
323 Query query;
324 try {
325 pm = getPersistenceManager();
326 beginTransaction();
327 query = pm.newQuery(clazz);
328 coll = (Collection) query.execute();
329 commit();
330 } catch (Exception e) {
331 rollback();
332 throw new DAOException(e);
333 } finally {
334 pm = null;
335 close();
336 }
337 return coll;
338 }
339
340 /***
341 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Class, String)
342 */
343 public Collection query(Class clazz, String filter) throws JDOException,
344 DAOException {
345 return query(clazz, filter, null);
346 }
347
348 /***
349 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Class, String)
350 */
351 public Collection query(Class clazz, String filter, String ordering)
352 throws JDOException, DAOException {
353 Collection coll;
354 PersistenceManager pm;
355 Query query;
356 try {
357 pm = getPersistenceManager();
358 beginTransaction();
359 query = pm.newQuery(clazz, filter);
360 if (ordering != null) {
361 query.setOrdering(ordering);
362 }
363 coll = (Collection) query.execute();
364 commit();
365 } catch (Exception e) {
366 rollback();
367 throw new DAOException(e);
368 } finally {
369 pm = null;
370 close();
371 }
372 return coll;
373 }
374
375 /***
376 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#queryWithMap(Class, String, String, Map)
377 */
378 public Collection queryWithMap(Class clazz, String filter,
379 String parameters, Map values) throws JDOException, DAOException {
380 return queryWithMap(clazz, filter, null, parameters, values);
381 }
382
383 /***
384 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#queryWithMap(Class, String, String, String, Map)
385 */
386 public Collection queryWithMap(Class clazz, String filter, String ordering,
387 String parameters, Map values) throws JDOException, DAOException {
388 Collection coll;
389 PersistenceManager pm;
390 Query query;
391 try {
392 pm = getPersistenceManager();
393 beginTransaction();
394 query = pm.newQuery(clazz, filter);
395 if (ordering != null) {
396 query.setOrdering(ordering);
397 }
398 query.declareParameters(parameters);
399 coll = (Collection) query.executeWithMap(values);
400 commit();
401 } catch (Exception e) {
402 rollback();
403 throw new DAOException(e);
404 } finally {
405 pm = null;
406 close();
407 }
408 return coll;
409 }
410
411 /***
412 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#queryWithMap(Class, String, String, Object[])
413 */
414 public Collection queryWithMap(Class clazz, String filter,
415 String parameters, Object[] values) throws JDOException,
416 DAOException {
417 return queryWithMap(clazz, filter, null, parameters, values);
418 }
419
420 /***
421 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#queryWithMap(Class, String, String, String, Object[])
422 */
423 public Collection queryWithMap(Class clazz, String filter, String ordering,
424 String parameters, Object[] values) throws JDOException,
425 DAOException {
426 Collection coll;
427 PersistenceManager pm;
428 Query query;
429 try {
430 pm = getPersistenceManager();
431 beginTransaction();
432 query = pm.newQuery(clazz, filter);
433 if (ordering != null) {
434 query.setOrdering(ordering);
435 }
436 query.declareParameters(parameters);
437 coll = (Collection) query.executeWithArray(values);
438 commit();
439 } catch (Exception e) {
440 rollback();
441 throw new DAOException(e);
442 } finally {
443 pm = null;
444 close();
445 }
446 return coll;
447 }
448
449 /***
450 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Class, Collection)
451 */
452 public Collection query(Class clazz, Collection coll) throws JDOException,
453 DAOException {
454 Collection cln;
455 PersistenceManager pm;
456 Query query;
457 try {
458 pm = getPersistenceManager();
459 beginTransaction();
460 query = pm.newQuery(clazz, coll);
461 cln = (Collection) query.execute();
462 commit();
463 } catch (Exception e) {
464 rollback();
465 throw new DAOException(e);
466 } finally {
467 pm = null;
468 close();
469 }
470 return cln;
471
472 }
473
474 /***
475 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Class, Collection, String)
476 */
477 public Collection query(Class clazz, Collection coll, String filter)
478 throws JDOException, DAOException {
479 Collection cln;
480 PersistenceManager pm;
481 Query query;
482 try {
483 pm = getPersistenceManager();
484 beginTransaction();
485 query = pm.newQuery(clazz, coll, filter);
486 cln = (Collection) query.execute();
487 commit();
488 } catch (Exception e) {
489 rollback();
490 throw new DAOException(e);
491 } finally {
492 pm = null;
493 close();
494 }
495 return cln;
496 }
497
498 /***
499 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Object)
500 */
501 public Collection query(Object object) throws JDOException, DAOException {
502 Collection cln;
503 PersistenceManager pm;
504 Query query;
505 try {
506 pm = getPersistenceManager();
507 beginTransaction();
508 query = pm.newQuery(object);
509 cln = (Collection) query.execute();
510 commit();
511 } catch (Exception e) {
512 rollback();
513 throw new DAOException(e);
514 } finally {
515 pm = null;
516 close();
517 }
518 return cln;
519 }
520
521 /***
522 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(String)
523 */
524 public Collection query(String queryStr) throws JDOException, DAOException {
525 Collection cln;
526 PersistenceManager pm;
527 Query query;
528 try {
529 pm = getPersistenceManager();
530 beginTransaction();
531 query = pm.newQuery(queryStr);
532 cln = (Collection) query.execute();
533 commit();
534 } catch (Exception e) {
535 rollback();
536 throw new DAOException(e);
537 } finally {
538 pm = null;
539 close();
540 }
541 return cln;
542 }
543
544 /***
545 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#evict(Object)
546 */
547 public void evict(Object object) throws JDOException, DAOException {
548 PersistenceManager pm;
549 try {
550 pm = getPersistenceManager();
551 pm.evict(object);
552 } catch (Exception e) {
553 throw new DAOException(e);
554 } finally {
555 pm = null;
556 close();
557 }
558 }
559
560 /***
561 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#evictAll()
562 */
563 public void evictAll() throws JDOException, DAOException {
564 PersistenceManager pm;
565 try {
566 pm = getPersistenceManager();
567 pm.evictAll();
568 } catch (Exception e) {
569 throw new DAOException(e);
570 } finally {
571 pm = null;
572 close();
573 }
574 }
575
576 /***
577 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#evictAll(Collection)
578 */
579 public void evictAll(Collection coll) throws JDOException, DAOException {
580 PersistenceManager pm;
581 try {
582 pm = getPersistenceManager();
583 pm.evictAll(coll);
584 } catch (Exception e) {
585 throw new DAOException(e);
586 } finally {
587 pm = null;
588 close();
589 }
590 }
591
592 /***
593 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#evictAll(Object[])
594 */
595 public void evictAll(Object[] objects) throws JDOException, DAOException {
596 PersistenceManager pm;
597 try {
598 pm = getPersistenceManager();
599 pm.evictAll(objects);
600 } catch (Exception e) {
601 rollback();
602 throw new DAOException(e);
603 } finally {
604 pm = null;
605 close();
606 }
607 }
608
609 /***
610 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#flush()
611 */
612 public void flush() throws JDOException, DAOException {
613 PersistenceManager pm;
614 try {
615 pm = getPersistenceManager();
616 pm.flush();
617 } catch (Exception e) {
618 rollback();
619 throw new DAOException(e);
620 } finally {
621 pm = null;
622 close();
623 }
624 }
625
626 /***
627 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#makePersistent(Object)
628 */
629 public void makePersistent(Object object) throws JDOException, DAOException {
630 PersistenceManager pm;
631 try {
632 pm = getPersistenceManager();
633 pm.makePersistent(object);
634 } catch (Exception e) {
635 rollback();
636 throw new DAOException(e);
637 } finally {
638 pm = null;
639 close();
640 }
641 }
642
643 /***
644 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#makePersistentAll(Collection)
645 */
646 public void makePersistentAll(Collection coll) throws JDOException,
647 DAOException {
648 PersistenceManager pm;
649 try {
650 pm = getPersistenceManager();
651 pm.makePersistentAll(coll);
652 } catch (Exception e) {
653 rollback();
654 throw new DAOException(e);
655 } finally {
656 pm = null;
657 close();
658 }
659 }
660
661 /***
662 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#makePersistentAll(Object[])
663 */
664 public void makePersistentAll(Object[] objects) throws JDOException,
665 DAOException {
666 PersistenceManager pm;
667 try {
668 pm = getPersistenceManager();
669 pm.makePersistentAll(objects);
670 } catch (Exception e) {
671 rollback();
672 throw new DAOException(e);
673 } finally {
674 pm = null;
675 close();
676 }
677 }
678
679 /***
680 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#deletePersistent(Object)
681 */
682 public void deletePersistent(Object object) throws JDOException,
683 DAOException {
684 PersistenceManager pm;
685 try {
686 pm = getPersistenceManager();
687 pm.deletePersistent(object);
688 } catch (Exception e) {
689 rollback();
690 throw new DAOException(e);
691 } finally {
692 pm = null;
693 close();
694 }
695 }
696
697 /***
698 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#deletePersistentAll(Collection)
699 */
700 public void deletePersistentAll(Collection coll) throws JDOException,
701 DAOException {
702 PersistenceManager pm;
703 try {
704 pm = getPersistenceManager();
705 pm.deletePersistent(coll);
706 } catch (Exception e) {
707 rollback();
708 throw new DAOException(e);
709 } finally {
710 pm = null;
711 close();
712 }
713 }
714
715 /***
716 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#deletePersistentAll(Object[])
717 */
718 public void deletePersistentAll(Object[] objects) {
719 PersistenceManager pm;
720 try {
721 pm = getPersistenceManager();
722 pm.deletePersistent(objects);
723 } catch (Exception e) {
724 rollback();
725 throw new DAOException(e);
726 } finally {
727 pm = null;
728 close();
729 }
730 }
731 }