Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/util/data/DataTransaction.java @ 457:51784d74a85b
Refactored data access. CDI is much cleaner now.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Mon, 16 Feb 2015 15:14:48 +0100 |
parents | |
children | d74b5b1190ae |
comparison
equal
deleted
inserted
replaced
456:dbeb56e913fd | 457:51784d74a85b |
---|---|
1 /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU GPL (v>=3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out | |
6 * the documentation coming with IMIS-Labordaten-Application for details. | |
7 */ | |
8 package de.intevation.lada.util.data; | |
9 | |
10 import javax.ejb.EJBTransactionRolledbackException; | |
11 import javax.ejb.Stateless; | |
12 import javax.ejb.TransactionAttribute; | |
13 import javax.ejb.TransactionAttributeType; | |
14 import javax.inject.Inject; | |
15 import javax.persistence.EntityExistsException; | |
16 import javax.persistence.EntityManager; | |
17 import javax.persistence.Query; | |
18 import javax.persistence.TransactionRequiredException; | |
19 | |
20 /** | |
21 * Abstract class implementing low level data operations. | |
22 * | |
23 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> | |
24 */ | |
25 @Stateless | |
26 public class DataTransaction | |
27 { | |
28 @Inject | |
29 protected EntityManagerProducer emp; | |
30 | |
31 protected String jndiPath; | |
32 | |
33 /** | |
34 * Create object in the database. | |
35 * This operation can not be undone. | |
36 * | |
37 * @param object The object to create | |
38 * | |
39 * @throws EntityExistsException | |
40 * @throws IllegalArgumentException | |
41 * @throws EJBTransactionRolledbackException | |
42 * @throws TransactionRequiredException | |
43 */ | |
44 @TransactionAttribute(TransactionAttributeType.REQUIRED) | |
45 public void persistInDatabase(Object object, String dataSource) | |
46 throws EntityExistsException, | |
47 IllegalArgumentException, | |
48 EJBTransactionRolledbackException, | |
49 TransactionRequiredException | |
50 { | |
51 emp.entityManager(dataSource).persist(object); | |
52 } | |
53 | |
54 /** | |
55 * Create object in the database. | |
56 * This operation can not be undone. | |
57 * | |
58 * @param object The object to create | |
59 * | |
60 * @throws EntityExistsException | |
61 * @throws IllegalArgumentException | |
62 * @throws EJBTransactionRolledbackException | |
63 * @throws TransactionRequiredException | |
64 */ | |
65 @TransactionAttribute(TransactionAttributeType.REQUIRED) | |
66 public void updateInDatabase(Object object, String dataSource) | |
67 throws EntityExistsException, | |
68 IllegalArgumentException, | |
69 EJBTransactionRolledbackException, | |
70 TransactionRequiredException | |
71 { | |
72 emp.entityManager(dataSource).merge(object); | |
73 } | |
74 | |
75 /** | |
76 * Remove an object from the datebase. | |
77 * This operation can not be undone. | |
78 * | |
79 * @param object The object to remove | |
80 * | |
81 * @throws IllegalArgumentException | |
82 * @throws TransactionRequiredException | |
83 */ | |
84 @TransactionAttribute(TransactionAttributeType.REQUIRED) | |
85 public void removeFromDatabase(Object object, String dataSource) | |
86 throws IllegalArgumentException, | |
87 TransactionRequiredException | |
88 { | |
89 EntityManager em = emp.entityManager(dataSource); | |
90 em.remove( | |
91 em.contains(object) ? | |
92 object : em.merge(object)); | |
93 } | |
94 | |
95 public Query queryFromString(String sql, String dataSource) { | |
96 EntityManager em = emp.entityManager(dataSource); | |
97 return em.createNativeQuery(sql); | |
98 } | |
99 | |
100 public EntityManager entityManager(String dataSource) { | |
101 return emp.entityManager(dataSource); | |
102 } | |
103 } |