|
2 | 2 |
|
3 | 3 | import java.time.LocalDateTime;
|
4 | 4 | import java.util.List;
|
5 |
| -import java.util.Map; |
6 |
| -import java.util.Map.Entry; |
7 |
| -import java.util.Set; |
8 | 5 |
|
9 | 6 | import javax.persistence.EntityManager;
|
10 | 7 | import javax.persistence.EntityManagerFactory;
|
11 |
| -import javax.persistence.LockModeType; |
12 |
| -import javax.persistence.NoResultException; |
13 |
| -import javax.persistence.Query; |
14 | 8 | import javax.persistence.TypedQuery;
|
15 | 9 |
|
16 | 10 | import info.unterrainer.commons.httpserver.jsons.ListJson;
|
17 | 11 | import info.unterrainer.commons.jreutils.DateUtils;
|
18 | 12 | import info.unterrainer.commons.rdbutils.Transactions;
|
19 | 13 | import info.unterrainer.commons.rdbutils.entities.BasicJpa;
|
20 |
| -import info.unterrainer.commons.rdbutils.enums.AsyncState; |
21 |
| -import lombok.RequiredArgsConstructor; |
22 |
| -import lombok.extern.slf4j.Slf4j; |
| 14 | +import lombok.Getter; |
23 | 15 |
|
24 |
| -@RequiredArgsConstructor |
25 |
| -@Slf4j |
26 |
| -public class BasicJpqlDao<P extends BasicJpa> implements BasicDao<P, EntityManager> { |
| 16 | +public class BasicJpqlDao<P extends BasicJpa> implements CoreDaoProvider<P, EntityManager> { |
27 | 17 |
|
28 | 18 | protected final EntityManagerFactory emf;
|
29 | 19 | protected final Class<P> type;
|
| 20 | + @Getter |
| 21 | + protected JpqlCoreDao<P> coreDao; |
| 22 | + |
| 23 | + public BasicJpqlDao(final EntityManagerFactory emf, final Class<P> type) { |
| 24 | + super(); |
| 25 | + this.emf = emf; |
| 26 | + this.type = type; |
| 27 | + coreDao = new JpqlCoreDao<>(emf, type); |
| 28 | + } |
30 | 29 |
|
31 | 30 | P _getById(final Long id) {
|
32 |
| - return Transactions.withNewTransactionReturning(emf, em -> _getById(em, id)); |
| 31 | + return Transactions.withNewTransactionReturning(emf, em -> coreDao.getById(em, id)); |
33 | 32 | }
|
34 | 33 |
|
35 | 34 | ListJson<P> getList(final EntityManager em, final long offset, final long size, final String selectClause,
|
36 | 35 | final String joinClause, final String whereClause, final ParamMap params, final String orderByClause) {
|
37 | 36 | ListJson<P> r = new ListJson<>();
|
38 |
| - r.setEntries( |
39 |
| - getList(em, |
40 |
| - getQuery(em, selectClause, joinClause, whereClause, |
41 |
| - params == null ? null : params.getParameters(), type, orderByClause, false, null), |
42 |
| - offset, size)); |
43 |
| - r.setCount((Long) getCountQuery(em, selectClause, joinClause, whereClause, |
44 |
| - params == null ? null : params.getParameters(), null).getSingleResult()); |
| 37 | + r.setEntries(getList(em, |
| 38 | + coreDao.getQuery(em, selectClause, joinClause, whereClause, |
| 39 | + params == null ? null : params.getParameters(), type, orderByClause, false, null), |
| 40 | + offset, size)); |
| 41 | + r.setCount( |
| 42 | + (Long) coreDao |
| 43 | + .getCountQuery(em, selectClause, joinClause, whereClause, |
| 44 | + params == null ? null : params.getParameters(), null) |
| 45 | + .getSingleResult()); |
45 | 46 | return r;
|
46 | 47 | }
|
47 | 48 |
|
48 | 49 | P create(final P entity) {
|
49 |
| - return Transactions.withNewTransactionReturning(emf, em -> create(em, entity)); |
50 |
| - } |
51 |
| - |
52 |
| - P create(final EntityManager em, final P entity) { |
53 |
| - LocalDateTime time = DateUtils.nowUtc(); |
54 |
| - entity.setCreatedOn(time); |
55 |
| - entity.setEditedOn(time); |
56 |
| - em.persist(entity); |
57 |
| - return entity; |
| 50 | + return Transactions.withNewTransactionReturning(emf, em -> coreDao.create(em, entity)); |
58 | 51 | }
|
59 | 52 |
|
60 | 53 | P _update(final P entity) {
|
61 |
| - return Transactions.withNewTransactionReturning(emf, em -> _update(em, entity)); |
| 54 | + return Transactions.withNewTransactionReturning(emf, em -> update(em, entity)); |
62 | 55 | }
|
63 | 56 |
|
64 |
| - P _update(final EntityManager em, final P entity) { |
| 57 | + P update(final EntityManager em, final P entity) { |
65 | 58 | LocalDateTime time = DateUtils.nowUtc();
|
66 | 59 | entity.setEditedOn(time);
|
67 | 60 | return em.merge(entity);
|
@@ -93,201 +86,20 @@ UpsertResult<P> _upsert(final EntityManager em, final TypedQuery<P> query, final
|
93 | 86 | boolean wasUpdated = false;
|
94 | 87 | P e = getFirst(em, query);
|
95 | 88 | if (e == null) {
|
96 |
| - e = create(em, entity); |
| 89 | + e = coreDao.create(em, entity); |
97 | 90 | wasInserted = true;
|
98 | 91 | } else {
|
99 | 92 | entity.setId(e.getId());
|
100 | 93 | entity.setCreatedOn(e.getCreatedOn());
|
101 |
| - e = _update(em, entity); |
| 94 | + e = update(em, entity); |
102 | 95 | wasUpdated = true;
|
103 | 96 | }
|
104 | 97 | return UpsertResult.<P>builder().wasInserted(wasInserted).wasUpdated(wasUpdated).jpa(e).build();
|
105 | 98 | }
|
106 | 99 |
|
107 | 100 | void _delete(final Long id) {
|
108 | 101 | Transactions.withNewTransaction(emf, em -> {
|
109 |
| - _delete(em, id); |
| 102 | + coreDao.delete(em, id); |
110 | 103 | });
|
111 | 104 | }
|
112 |
| - |
113 |
| - void _delete(final EntityManager em, final Long id) { |
114 |
| - em.createQuery(String.format("DELETE FROM %s AS o WHERE o.id = :id", type.getSimpleName())) |
115 |
| - .setParameter("id", id) |
116 |
| - .executeUpdate(); |
117 |
| - } |
118 |
| - |
119 |
| - P _getById(final EntityManager em, final Long id) { |
120 |
| - try { |
121 |
| - return getQuery(em, "o", null, "o.id = :id", Map.of("id", id), type, null, false, null).getSingleResult(); |
122 |
| - } catch (NoResultException e) { |
123 |
| - return null; |
124 |
| - } |
125 |
| - } |
126 |
| - |
127 |
| - private boolean isSet(final String str) { |
128 |
| - return str != null && !str.isBlank(); |
129 |
| - } |
130 |
| - |
131 |
| - private boolean isSet(final Set<?> set) { |
132 |
| - return set != null && !set.isEmpty(); |
133 |
| - } |
134 |
| - |
135 |
| - private String buildWhereClause(final String whereClause, final Set<AsyncState> asyncStates) { |
136 |
| - String r = ""; |
137 |
| - if (!isSet(whereClause) && !isSet(asyncStates)) |
138 |
| - return r; |
139 |
| - |
140 |
| - r = " WHERE "; |
141 |
| - if (isSet(whereClause) && !isSet(asyncStates)) |
142 |
| - r += whereClause; |
143 |
| - |
144 |
| - if (!isSet(whereClause) && isSet(asyncStates)) |
145 |
| - r += addAsyncStatesToWhereClause(asyncStates); |
146 |
| - |
147 |
| - if (isSet(whereClause) && isSet(asyncStates)) |
148 |
| - r += "( " + whereClause + " ) AND ( " + addAsyncStatesToWhereClause(asyncStates) + " )"; |
149 |
| - |
150 |
| - return r; |
151 |
| - } |
152 |
| - |
153 |
| - private String addAsyncStatesToWhereClause(final Set<AsyncState> asyncStates) { |
154 |
| - StringBuilder sb = new StringBuilder(); |
155 |
| - boolean isFirst = true; |
156 |
| - |
157 |
| - for (int i = 0; i < asyncStates.size(); i++) { |
158 |
| - if (isFirst) |
159 |
| - isFirst = false; |
160 |
| - else |
161 |
| - sb.append(" OR "); |
162 |
| - sb.append("state = :state"); |
163 |
| - sb.append(i); |
164 |
| - } |
165 |
| - |
166 |
| - return sb.toString(); |
167 |
| - } |
168 |
| - |
169 |
| - private <T> TypedQuery<T> addAsyncStatesParamsToQuery(final Set<AsyncState> asyncStates, |
170 |
| - final TypedQuery<T> query) { |
171 |
| - |
172 |
| - if (!isSet(asyncStates)) |
173 |
| - return query; |
174 |
| - |
175 |
| - int count = 0; |
176 |
| - for (AsyncState state : asyncStates) |
177 |
| - query.setParameter("state" + count++, state); |
178 |
| - |
179 |
| - return query; |
180 |
| - } |
181 |
| - |
182 |
| - private Query addAsyncStatesParamsToQuery(final Set<AsyncState> asyncStates, final Query query) { |
183 |
| - |
184 |
| - if (!isSet(asyncStates)) |
185 |
| - return query; |
186 |
| - |
187 |
| - int count = 0; |
188 |
| - for (AsyncState state : asyncStates) |
189 |
| - query.setParameter("state" + count++, state); |
190 |
| - |
191 |
| - return query; |
192 |
| - } |
193 |
| - |
194 |
| - <T> TypedQuery<T> getQuery(final EntityManager em, final String selectClause, final String joinClause, |
195 |
| - final String whereClause, final Map<String, Object> params, final Class<T> type, final String orderBy, |
196 |
| - final boolean lockPessimistic, final Set<AsyncState> asyncStates) { |
197 |
| - String query = "SELECT "; |
198 |
| - if (selectClause == null || selectClause.isBlank()) |
199 |
| - query += "o"; |
200 |
| - else |
201 |
| - query += selectClause; |
202 |
| - query += " FROM %s AS o"; |
203 |
| - |
204 |
| - if (joinClause != null && !joinClause.isBlank()) |
205 |
| - query += " " + joinClause; |
206 |
| - |
207 |
| - query += buildWhereClause(whereClause, asyncStates); |
208 |
| - |
209 |
| - if (orderBy == null) |
210 |
| - query += " ORDER BY o.id ASC"; |
211 |
| - else if (!orderBy.isBlank()) |
212 |
| - query += " ORDER BY " + orderBy; |
213 |
| - |
214 |
| - query = String.format(query, this.type.getSimpleName()); |
215 |
| - |
216 |
| - String msg = query; |
217 |
| - if (params != null) |
218 |
| - for (Entry<String, Object> p : params.entrySet()) |
219 |
| - msg += "\\n " + p.getKey() + ": " + p.getValue(); |
220 |
| - log.debug(msg); |
221 |
| - |
222 |
| - @SuppressWarnings("unchecked") |
223 |
| - Class<T> t = (Class<T>) this.type; |
224 |
| - if (type != null) |
225 |
| - t = type; |
226 |
| - |
227 |
| - TypedQuery<T> q = em.createQuery(query, t); |
228 |
| - if (lockPessimistic) |
229 |
| - q.setLockMode(LockModeType.PESSIMISTIC_WRITE); |
230 |
| - q = addAsyncStatesParamsToQuery(asyncStates, q); |
231 |
| - if (params != null) |
232 |
| - for (Entry<String, Object> e : params.entrySet()) |
233 |
| - q.setParameter(e.getKey(), e.getValue()); |
234 |
| - return q; |
235 |
| - } |
236 |
| - |
237 |
| - <T> TypedQuery<T> getDeleteQuery(final EntityManager em, final String joinClause, final String whereClause, |
238 |
| - final Map<String, Object> params) { |
239 |
| - String query = "DELETE FROM %s AS o"; |
240 |
| - |
241 |
| - if (joinClause != null && !joinClause.isBlank()) |
242 |
| - query += " " + joinClause; |
243 |
| - |
244 |
| - query += buildWhereClause(whereClause, null); |
245 |
| - |
246 |
| - query = String.format(query, this.type.getSimpleName()); |
247 |
| - |
248 |
| - String msg = query; |
249 |
| - if (params != null) |
250 |
| - for (Entry<String, Object> p : params.entrySet()) |
251 |
| - msg += "\\n " + p.getKey() + ": " + p.getValue(); |
252 |
| - log.debug(msg); |
253 |
| - |
254 |
| - @SuppressWarnings("unchecked") |
255 |
| - Class<T> t = (Class<T>) this.type; |
256 |
| - |
257 |
| - TypedQuery<T> q = em.createQuery(query, t); |
258 |
| - if (params != null) |
259 |
| - for (Entry<String, Object> e : params.entrySet()) |
260 |
| - q.setParameter(e.getKey(), e.getValue()); |
261 |
| - return q; |
262 |
| - } |
263 |
| - |
264 |
| - private boolean isAllowed(final info.unterrainer.commons.httpserver.daos.ListQueryBuilder query, |
265 |
| - final EntityManager em) { |
266 |
| - String tenantReferenceField = "testId"; |
267 |
| - String tenantIdField = "tenantId"; |
268 |
| - Long tenantId = 55L; |
269 |
| - BasicJpa tenantJpa = null; |
270 |
| - getQuery(em, "o", |
271 |
| - "RIGHT JOIN " + tenantJpa.getClass().getSimpleName() + " tenantTable on o.id = tenantTable." |
272 |
| - + tenantReferenceField, |
273 |
| - "tenantTable." + tenantReferenceField + " IS NULL OR tenantTable." + tenantIdField + " = :tenantId", |
274 |
| - null, type, null, false, null); |
275 |
| - return true; |
276 |
| - } |
277 |
| - |
278 |
| - Query getCountQuery(final EntityManager em, final String selectClause, final String joinClause, |
279 |
| - final String whereClause, final Map<String, Object> params, final Set<AsyncState> asyncStates) { |
280 |
| - String query = "SELECT COUNT(" + selectClause + ") FROM %s AS o"; |
281 |
| - if (joinClause != null && !joinClause.isBlank()) |
282 |
| - query += " " + joinClause; |
283 |
| - query += buildWhereClause(whereClause, asyncStates); |
284 |
| - |
285 |
| - Query q = em.createQuery(String.format(query, this.type.getSimpleName())); |
286 |
| - |
287 |
| - q = addAsyncStatesParamsToQuery(asyncStates, q); |
288 |
| - if (params != null) |
289 |
| - for (Entry<String, Object> e : params.entrySet()) |
290 |
| - q.setParameter(e.getKey(), e.getValue()); |
291 |
| - return q; |
292 |
| - } |
293 | 105 | }
|
0 commit comments