|
1 | 1 | package info.unterrainer.commons.httpserver.daos;
|
2 | 2 |
|
3 |
| -import java.util.List; |
4 |
| -import java.util.function.Function; |
5 |
| - |
6 |
| -import javax.persistence.EntityManager; |
7 | 3 | import javax.persistence.EntityManagerFactory;
|
8 |
| -import javax.persistence.LockModeType; |
9 |
| -import javax.persistence.TypedQuery; |
10 | 4 |
|
11 |
| -import info.unterrainer.commons.rdbutils.Transactions; |
12 | 5 | import info.unterrainer.commons.rdbutils.entities.BasicAsyncJpa;
|
13 |
| -import info.unterrainer.commons.rdbutils.enums.AsyncState; |
14 | 6 |
|
15 |
| -public class JpqlAsyncDao<P extends BasicAsyncJpa> extends JpqlDao<P> implements BasicAsyncDao<P, EntityManager> { |
| 7 | +public class JpqlAsyncDao<P extends BasicAsyncJpa> extends JpqlDao<P> { |
16 | 8 |
|
17 | 9 | public JpqlAsyncDao(final EntityManagerFactory emf, final Class<P> type) {
|
18 | 10 | super(emf, type);
|
19 | 11 | }
|
20 | 12 |
|
21 | 13 | @Override
|
22 |
| - public P getLastWith(final AsyncState... states) { |
23 |
| - return Transactions.withNewTransactionReturning(emf, em -> getLastWith(em, states)); |
24 |
| - } |
25 |
| - |
26 |
| - @Override |
27 |
| - public P getLastWith(final EntityManager em, final AsyncState... states) { |
28 |
| - return internalGet(em, false, false, null, states); |
29 |
| - } |
30 |
| - |
31 |
| - @Override |
32 |
| - public List<P> getLastNWith(final Long count, final AsyncState... states) { |
33 |
| - return Transactions.withNewTransactionReturning(emf, em -> getLastNWith(em, count, states)); |
34 |
| - } |
35 |
| - |
36 |
| - @Override |
37 |
| - public List<P> getLastNWith(final EntityManager em, final Long count, final AsyncState... states) { |
38 |
| - return internalGetList(em, count, false, false, null, states); |
39 |
| - } |
40 |
| - |
41 |
| - @Override |
42 |
| - public P getNextWith(final AsyncState... states) { |
43 |
| - return Transactions.withNewTransactionReturning(emf, em -> getNextWith(em, states)); |
44 |
| - } |
45 |
| - |
46 |
| - @Override |
47 |
| - public P getNextWith(final EntityManager em, final AsyncState... states) { |
48 |
| - return internalGet(em, false, true, null, states); |
49 |
| - } |
50 |
| - |
51 |
| - @Override |
52 |
| - public List<P> getNextNWith(final Long count, final AsyncState... states) { |
53 |
| - return Transactions.withNewTransactionReturning(emf, em -> getNextNWith(em, count, states)); |
54 |
| - } |
55 |
| - |
56 |
| - @Override |
57 |
| - public List<P> getNextNWith(final EntityManager em, final Long count, final AsyncState... states) { |
58 |
| - return internalGetList(em, count, false, true, null, states); |
59 |
| - } |
60 |
| - |
61 |
| - @Override |
62 |
| - public P lockedGetNextWith(final AsyncState stateToSetTo, final AsyncState... states) { |
63 |
| - return Transactions.withNewTransactionReturning(emf, em -> lockedGetNextWith(em, stateToSetTo, states)); |
64 |
| - } |
65 |
| - |
66 |
| - @Override |
67 |
| - public P lockedGetNextWith(final EntityManager em, final AsyncState stateToSetTo, final AsyncState... states) { |
68 |
| - return internalGet(em, true, true, stateToSetTo, states); |
69 |
| - } |
70 |
| - |
71 |
| - @Override |
72 |
| - public List<P> lockedGetNextNWith(final Long count, final AsyncState stateToSetTo, final AsyncState... states) { |
73 |
| - return Transactions.withNewTransactionReturning(emf, em -> lockedGetNextNWith(em, count, stateToSetTo, states)); |
74 |
| - } |
75 |
| - |
76 |
| - @Override |
77 |
| - public List<P> lockedGetNextNWith(final EntityManager em, final Long count, final AsyncState stateToSetTo, |
78 |
| - final AsyncState... states) { |
79 |
| - return internalGetList(em, count, true, true, stateToSetTo, states); |
80 |
| - } |
81 |
| - |
82 |
| - public P internalGet(final EntityManager em, final boolean lockPessimistic, final boolean ascending, |
83 |
| - final AsyncState stateToSetTo, final AsyncState... states) { |
84 |
| - List<P> list = internalGetList(em, 1, lockPessimistic, ascending, stateToSetTo, states); |
85 |
| - if (list.size() == 0) |
86 |
| - return null; |
87 |
| - return list.get(0); |
88 |
| - } |
89 |
| - |
90 |
| - public List<P> internalGetList(final EntityManager em, final long size, final boolean lockPessimistic, |
91 |
| - final boolean ascending, final AsyncState stateToSetTo, final AsyncState... states) { |
92 |
| - int s = Integer.MAX_VALUE; |
93 |
| - if (size < s) |
94 |
| - s = (int) size; |
95 |
| - |
96 |
| - StringBuilder sb = new StringBuilder(); |
97 |
| - |
98 |
| - sb.append("SELECT o FROM %s AS o "); |
99 |
| - boolean isFirst = true; |
100 |
| - for (int i = 0; i < states.length; i++) { |
101 |
| - if (isFirst) { |
102 |
| - sb.append("WHERE "); |
103 |
| - isFirst = false; |
104 |
| - } else |
105 |
| - sb.append("OR "); |
106 |
| - sb.append("state = :state"); |
107 |
| - sb.append(i); |
108 |
| - sb.append(" "); |
109 |
| - } |
110 |
| - sb.append("ORDER BY o.id " + (ascending ? "ASC" : "DESC")); |
111 |
| - |
112 |
| - TypedQuery<P> q = em.createQuery(String.format(sb.toString(), type.getSimpleName()), type).setMaxResults(s); |
113 |
| - if (lockPessimistic) |
114 |
| - q.setLockMode(LockModeType.PESSIMISTIC_WRITE); |
115 |
| - |
116 |
| - for (int i = 0; i < states.length; i++) |
117 |
| - q.setParameter("state" + i, states[i]); |
118 |
| - |
119 |
| - List<P> r = q.getResultList(); |
120 |
| - |
121 |
| - if (stateToSetTo != null) |
122 |
| - for (P jpa : r) { |
123 |
| - jpa.setState(stateToSetTo); |
124 |
| - em.merge(jpa); |
125 |
| - } |
126 |
| - |
127 |
| - return r; |
128 |
| - } |
129 |
| - |
130 |
| - @Override |
131 |
| - public P setStateTo(final AsyncState stateToSetTo, final Long id) { |
132 |
| - return setStateTo(stateToSetTo, id, null); |
133 |
| - } |
134 |
| - |
135 |
| - @Override |
136 |
| - public P setStateTo(final EntityManager em, final AsyncState stateToSetTo, final Long id) { |
137 |
| - return setStateTo(em, stateToSetTo, id, null); |
138 |
| - } |
139 |
| - |
140 |
| - @Override |
141 |
| - public P setStateTo(final AsyncState stateToSetTo, final Long id, final Function<P, P> additionalTransformations) { |
142 |
| - return Transactions.withNewTransactionReturning(emf, |
143 |
| - em -> setStateTo(em, stateToSetTo, id, additionalTransformations)); |
144 |
| - } |
145 |
| - |
146 |
| - @Override |
147 |
| - public P setStateTo(final EntityManager em, final AsyncState stateToSetTo, final Long id, |
148 |
| - final Function<P, P> additionalTransformations) { |
149 |
| - P jpa = getById(em, id); |
150 |
| - jpa.setState(stateToSetTo); |
151 |
| - if (additionalTransformations != null) |
152 |
| - jpa = additionalTransformations.apply(jpa); |
153 |
| - update(em, jpa); |
154 |
| - return jpa; |
| 14 | + public QueryAsyncBuilder<P, P> query() { |
| 15 | + return new QueryAsyncBuilder<>(emf, this, type); |
155 | 16 | }
|
156 | 17 | }
|
0 commit comments