root / middleware / src / com / gonnatrak / middleware / util / DAO.java @ 1

View | Annotate | Download

1
/**
2
 * Created by Andrey Khalzov
3
 * 12.07.2008 14:25:04
4
 */
5
package com.gonnatrak.middleware.util;
6
7
import com.gonnatrak.middleware.entity.UndeletableEntity;
8
import com.gonnatrak.middleware.exception.EntityNotFoundException;
9
import org.jetbrains.annotations.NotNull;
10
11
import javax.persistence.Entity;
12
import javax.persistence.EntityManager;
13
import javax.persistence.Query;
14
import java.util.Collections;
15
import java.util.List;
16
17
public class DAO {
18
    private static final String WHERE_NOT_DELETED = "(e.deleted is NULL or e.deleted = false)";
19
20
    private final EntityManager em;
21
22
    @NotNull
23
    public static String createWhereInQueryString(@NotNull List list) {
24
        if (list.isEmpty()) {
25
            throw new IllegalArgumentException("List of \"where in\" is empty");
26
        }
27
28
        final StringBuilder sb = new StringBuilder("(");
29
        for (Object object : list) {
30
            sb.append("?,");
31
        }
32
        sb.replace(sb.length() - 1, sb.length(), ")");
33
        return sb.toString();
34
    }
35
36
    public DAO(@NotNull EntityManager em) {
37
        this.em = em;
38
    }
39
40
    @NotNull
41
    public <T> T find(@NotNull Class<T> clazz, @NotNull Object o) throws EntityNotFoundException {
42
        final T t = _find(clazz, o);
43
        if (UndeletableEntity.class.isAssignableFrom(clazz)) {
44
            if (BooleanHelper.valueOf(((UndeletableEntity) t).isDeleted())) {
45
                throw new EntityNotFoundException();
46
            }
47
        }
48
        return t;
49
    }
50
51
    public <T> void persist(@NotNull T t) {
52
        em.persist(t);
53
    }
54
55
    public <T> void remove(@NotNull T t) {
56
        if (t instanceof UndeletableEntity) {
57
            ((UndeletableEntity) t).setDeleted(true);
58
            merge(t);
59
        } else {
60
            _remove(t);
61
        }
62
    }
63
64
    @NotNull
65
    public <T> T merge(@NotNull T t) {
66
        T mergedT = em.merge(t);
67
        if (mergedT == null) {
68
            throw new RuntimeException("EntityManager returned null on merge()");
69
        }
70
        return mergedT;
71
    }
72
73
    @SuppressWarnings("unchecked")
74
    @NotNull
75
    public <T> T findSingleResult(@NotNull Class<T> clazz, @NotNull String where, @NotNull Object... params)
76
            throws EntityNotFoundException
77
    {
78
        if (UndeletableEntity.class.isAssignableFrom(clazz)) {
79
            final Query query = createEntityQuery((Class<UndeletableEntity>) clazz, where, params);
80
            return getSingleResult(clazz, query);
81
        } else {
82
            return _findSingleResult(clazz, where, params);
83
        }
84
    }
85
86
    @NotNull
87
    public <T> List<T> find(@NotNull Class<T> clazz, int firstResult, int maxResults) {
88
        if (UndeletableEntity.class.isAssignableFrom(clazz)) {
89
            final Query query = _createQuery(clazz, "e", WHERE_NOT_DELETED);
90
            return getResultList(query, firstResult, maxResults);
91
        } else {
92
            return _find(clazz, firstResult, maxResults);
93
        }
94
    }
95
96
    @NotNull
97
    public <T extends UndeletableEntity> List<T> findOrdered(
98
            @NotNull Class<T> clazz,
99
            int firstResult,
100
            int maxResults,
101
            @NotNull String where,
102
            @NotNull String order,
103
            @NotNull Object... params
104
    ) {
105
        final Query query = createEntityOrderQuery(clazz, where, order, params);
106
        return getResultList(query, firstResult, maxResults);
107
    }
108
109
    @SuppressWarnings("unchecked")
110
    @NotNull
111
    public <T> List<T> find(
112
            @NotNull Class<T> clazz,
113
            int firstResult,
114
            int maxResults,
115
            @NotNull String where,
116
            @NotNull Object... params
117
    ) {
118
        if (UndeletableEntity.class.isAssignableFrom(clazz)) {
119
            final Query query = createEntityQuery((Class<UndeletableEntity>) clazz, where, params);
120
            return getResultList(query, firstResult, maxResults);
121
        } else {
122
            return _find(clazz, firstResult, maxResults, where, params);
123
        }
124
    }
125
126
    @SuppressWarnings("unchecked")
127
    @NotNull
128
    public <T extends UndeletableEntity> List<T> findOrdered(
129
            @NotNull Class<T> clazz,
130
            @NotNull String where,
131
            @NotNull String order,
132
            @NotNull Object... params
133
    ) {
134
        final Query query = createEntityOrderQuery(clazz, where, order, params);
135
        return (List<T>) query.getResultList();
136
    }
137
138
    @SuppressWarnings("unchecked")
139
    @NotNull
140
    public <T> List<T> find(@NotNull Class<T> clazz, @NotNull String where, @NotNull Object... params) {
141
        if (UndeletableEntity.class.isAssignableFrom(clazz)) {
142
            final Query query = createEntityQuery((Class< UndeletableEntity>) clazz, where, params);
143
            return (List<T>) query.getResultList();
144
        } else {
145
            return _find(clazz, where, params);
146
        }
147
    }
148
149
    @NotNull
150
    public <T extends UndeletableEntity> Query createEntityOrderQuery(
151
            @NotNull Class<T> clazz,
152
            @NotNull String where,
153
            @NotNull String order,
154
            @NotNull Object... params
155
    ) {
156
        return createOrderQuery(clazz, "e", where, order, params);
157
    }
158
159
    @NotNull
160
    public <T extends UndeletableEntity> Query createEntityQuery(
161
            @NotNull Class<T> clazz,
162
            @NotNull String where,
163
            @NotNull Object... params
164
    ) {
165
        return createQuery(clazz, "e", where, params);
166
    }
167
168
    @NotNull
169
    public <T extends UndeletableEntity> Query createOrderQuery(
170
            @NotNull Class<T> clazz,
171
            @NotNull String what,
172
            @NotNull String where,
173
            @NotNull String order,
174
            @NotNull Object... params
175
    ) {
176
        final String whereQuery = createWhereQuery(where);
177
        return _createQuery(
178
                clazz,
179
                what,
180
                whereQuery + " order by " + order,
181
                params
182
        );
183
    }
184
185
    @NotNull
186
    public <T extends UndeletableEntity> Query createQuery(
187
            @NotNull Class<T> clazz,
188
            @NotNull String what,
189
            @NotNull String where,
190
            @NotNull Object... params
191
    ) {
192
        final String whereQuery = createWhereQuery(where);
193
        return _createQuery(clazz, what, whereQuery, params);
194
    }
195
196
    private String createWhereQuery(String where) {
197
        String whereQuery = WHERE_NOT_DELETED;
198
        if (!StringHelper.isAlmostEmpty(where)) {
199
            whereQuery = whereQuery + " and (" + where + ")";
200
        }
201
        return whereQuery;
202
    }
203
204
    @NotNull
205
    private <T> T _find(@NotNull Class<T> clazz, @NotNull Object o) throws EntityNotFoundException {
206
        final T t = em.find(clazz, o);
207
        if (t == null) {
208
            throw new EntityNotFoundException();
209
        }
210
        return t;
211
    }
212
213
    private <T> void _remove(@NotNull T t) {
214
        em.remove(t);
215
    }
216
217
    @NotNull
218
    private <T> T _findSingleResult(@NotNull Class<T> clazz, @NotNull String where, @NotNull Object... params)
219
            throws EntityNotFoundException
220
    {
221
        final Query query = _createEntityQuery(clazz, where, params);
222
        return getSingleResult(clazz, query);
223
    }
224
225
    @NotNull
226
    private <T> List<T> _find(@NotNull Class<T> clazz, int firstResult, int maxResults) {
227
        final Query query = em.createQuery("select e from " +  getEntityName(clazz) + " e");
228
        return getResultList(query, firstResult, maxResults);
229
    }
230
231
    @NotNull
232
    private <T> List<T> _find(
233
            @NotNull Class<T> clazz,
234
            int firstResult,
235
            int maxResults,
236
            @NotNull String where,
237
            @NotNull Object... params
238
    ) {
239
        final Query query = _createEntityQuery(clazz, where, params);
240
        return getResultList(query, firstResult, maxResults);
241
    }
242
243
    @SuppressWarnings("unchecked")
244
    @NotNull
245
    private <T> List<T> _find(@NotNull Class<T> clazz, @NotNull String where, @NotNull Object... params) {
246
        final Query query = _createEntityQuery(clazz, where, params);
247
        return safeGetResultList(query);
248
    }
249
250
    @NotNull
251
    private <T> Query _createEntityQuery(@NotNull Class<T> clazz, @NotNull String where, @NotNull Object... params) {
252
        return _createQuery(clazz, "e", where, params);
253
    }
254
255
    @NotNull
256
    private <T> Query _createQuery(
257
            @NotNull Class<T> clazz,
258
            @NotNull String what,
259
            @NotNull String where,
260
            @NotNull Object... params
261
    ) {
262
        final Query query = em.createQuery(
263
                "select " + what + " from " + getEntityName(clazz) + " e where " + where
264
        );
265
        int j = 1;
266
        for (Object param : params) {
267
            if (param instanceof List) {
268
                for (Object listParam : (List) param) {
269
                    query.setParameter(j++, listParam);
270
                }
271
            } else {
272
                query.setParameter(j++, param);
273
            }
274
        }
275
        return query;
276
    }
277
278
    @SuppressWarnings("unchecked")
279
    @NotNull
280
    private <T> T getSingleResult(@NotNull Class<T> clazz, @NotNull Query query) throws EntityNotFoundException {
281
        List<T> list = (List<T>) query.getResultList();
282
        if (list == null || list.isEmpty() || list.get(0) == null) {
283
            throw new EntityNotFoundException();
284
        }
285
        return list.get(0);
286
    }
287
288
    @SuppressWarnings("unchecked")
289
    @NotNull
290
    private <T> List<T> getResultList(@NotNull Query query, int firstResult, int maxResults) {
291
        query.setFirstResult(firstResult);
292
        query.setMaxResults(maxResults);
293
        return safeGetResultList(query);
294
    }
295
296
    @SuppressWarnings("unchecked")
297
    @NotNull
298
    private <T> List<T> safeGetResultList(@NotNull Query query) {
299
        List<T> list = query.getResultList();
300
        if (list == null) {
301
            list = Collections.emptyList();
302
        }
303
        return list;
304
    }
305
306
    private <T> String getEntityName(@NotNull Class<T> clazz) {
307
        Entity annotation = clazz.getAnnotation(Entity.class);
308
        return StringHelper.isEmpty(annotation.name()) ? clazz.getSimpleName() : annotation.name();
309
    }
310
}