-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEbeanDao.java
More file actions
452 lines (409 loc) · 14.2 KB
/
EbeanDao.java
File metadata and controls
452 lines (409 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package act.db.ebean;
import act.ActComponent;
import act.app.ActionContext;
import act.app.App;
import act.app.DbServiceManager;
import act.db.DB;
import act.db.DaoBase;
import act.db.DbService;
import act.db.Model;
import act.util.General;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.QueryIterator;
import com.avaje.ebean.SqlUpdate;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import org.osgl.$;
import org.osgl.logging.L;
import org.osgl.logging.Logger;
import org.osgl.util.C;
import org.osgl.util.E;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.persistence.Id;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@General
@ActComponent
public class EbeanDao<ID_TYPE, MODEL_TYPE> extends DaoBase<ID_TYPE, MODEL_TYPE, EbeanQuery<MODEL_TYPE>> {
private static final Logger logger = L.get(EbeanDao.class);
private Class<MODEL_TYPE> modelType;
private volatile EbeanServer ebean;
private String tableName;
private Field idField = null;
private List<QueryIterator> queryIterators = C.newList();
private App app;
EbeanDao(Class<MODEL_TYPE> modelType, EbeanService service) {
E.NPE(modelType, service.ebean());
this.modelType = modelType;
for (Field f: modelType.getDeclaredFields()) {
Id idAnno = f.getAnnotation(Id.class);
if (null != idAnno) {
idField = f;
f.setAccessible(true);
break;
}
}
this.ebean = service.ebean();
this.tableName = ((SpiEbeanServer) ebean).getBeanDescriptor(modelType).getBaseTable();
this.app = service.app();
}
EbeanDao() {
}
protected EbeanDao(Class<MODEL_TYPE> modelType) {
this.modelType = modelType;
this.app = App.instance();
}
@Inject
public void setApp(App app) {
this.app = app;
}
public void ebean(EbeanServer ebean) {
this.ebean = $.notNull(ebean);
}
public void modelType(Class<?> type) {
this.modelType = $.cast(type);
}
@Inject
public void setActionContext(@Nullable ActionContext actionContext) {
if (null != actionContext) {
actionContext.addDestroyable(this);
}
}
@Override
protected void releaseResources() {
if (null != queryIterators) {
for (QueryIterator i : queryIterators) {
try {
i.close();
} catch (Exception e) {
logger.warn(e, "error closing query iterators");
}
}
queryIterators.clear();
queryIterators = null;
}
}
private EbeanService getService(String dbId, DbServiceManager mgr) {
DbService svc = mgr.dbService(dbId);
E.invalidConfigurationIf(null == svc, "Cannot find db service by id: %s", dbId);
E.invalidConfigurationIf(!(svc instanceof EbeanService), "The db service[%s|%s] is not ebean service", dbId, svc.getClass());
return $.cast(svc);
}
protected EbeanServer ebean() {
if (null != ebean) {
return ebean;
}
synchronized (this) {
if (null == ebean) {
DB db = modelType.getAnnotation(DB.class);
String dbId = null == db ? DbServiceManager.DEFAULT : db.value();
EbeanService dbService = getService(dbId, app.dbServiceManager());
E.NPE(dbService);
ebean = dbService.ebean();
}
}
return ebean;
}
void registerQueryIterator(QueryIterator i) {
queryIterators.add(i);
}
@Override
public MODEL_TYPE findById(ID_TYPE id) {
return ebean().find(modelType, id);
}
@Override
public Iterable<MODEL_TYPE> findBy(String fields, Object... values) throws IllegalArgumentException {
EbeanQuery<MODEL_TYPE> q = q(fields, values);
return q.fetch();
}
@Override
public Iterable<MODEL_TYPE> findByIdList(Collection<ID_TYPE> idList) {
EbeanQuery<MODEL_TYPE> q = q();
q.where().idIn(C.list(idList));
return q.fetch();
}
@Override
public MODEL_TYPE findOneBy(String fields, Object... values) throws IllegalArgumentException {
EbeanQuery<MODEL_TYPE> q = q(fields, values);
return q.first();
}
@Override
public Iterable<MODEL_TYPE> findAll() {
return q().fetch();
}
@Override
public List<MODEL_TYPE> findAllAsList() {
return q().findList();
}
@Override
public MODEL_TYPE reload(MODEL_TYPE entity) {
ebean().refresh(entity);
return entity;
}
@Override
public ID_TYPE getId(MODEL_TYPE entity) {
if (entity instanceof Model) {
return (ID_TYPE) ((Model) entity)._id();
} else if (null != idField) {
try {
return (ID_TYPE) idField.get(entity);
} catch (IllegalAccessException e) {
throw E.unexpected(e);
}
} else {
return null;
}
}
@Override
public long count() {
return q().findRowCount();
}
@Override
public long countBy(String fields, Object... values) throws IllegalArgumentException {
EbeanQuery<MODEL_TYPE> q = q(fields, values);
return q.count();
}
@Override
public void save(MODEL_TYPE entity) {
ebean().save(entity);
}
@Override
public void save(Iterable<MODEL_TYPE> iterable) {
ebean().saveAll(C.list(iterable));
}
@Override
public void save(MODEL_TYPE entity, String fields, Object... values) throws IllegalArgumentException {
ebean.update(entity);
}
@Override
public void delete(MODEL_TYPE entity) {
ebean().delete(entity);
}
@Override
public void delete(EbeanQuery<MODEL_TYPE> query) {
ebean().delete(query);
}
@Override
public void deleteById(ID_TYPE id) {
ebean().delete(modelType(), id);
}
@Override
public void deleteBy(String fields, Object... values) throws IllegalArgumentException {
EbeanQuery<MODEL_TYPE> q = q(fields, values);
ebean().delete(q);
}
@Override
public void drop() {
String sql = "DELETE from " + tableName;
SqlUpdate sqlUpdate = ebean().createSqlUpdate(sql);
ebean().execute(sqlUpdate);
}
@Override
public EbeanQuery<MODEL_TYPE> q() {
return new EbeanQuery<MODEL_TYPE>(this, modelType);
}
public Class modelType() {
return modelType;
}
private enum R2 {
betweenProperties() {
@Override
void applyTo(ExpressionList<?> where, String field1, String field2, Object val) {
where.betweenProperties(field1, field2, val);
}
},
bp() {
@Override
void applyTo(ExpressionList<?> where, String field1, String field2, Object val) {
where.betweenProperties(field1, field2, val);
}
};
abstract void applyTo(ExpressionList<?> where, String field1, String field2, Object val);
}
public static final String ID = "_id";
private enum R1 {
eq() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
if (ID.equals(field)) {
where.idEq(val);
return;
}
if (null == val) {
where.isNull(field);
} else {
where.eq(field, val);
}
}
}, ne() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.ne(field, val);
}
}, ieq() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.ieq(field, val.toString());
}
}, between() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
if (val instanceof $.T2) {
$.T2 t2 = $.cast(val);
where.between(field, t2._1, t2._2);
} else if (val.getClass().isArray()) {
int len = Array.getLength(val);
if (len != 2) {
throw E.unexpected("<between> value array length is not correct, expected: 2; found: %s", len);
}
where.between(field, Array.get(val, 0), Array.get(val, 1));
} else if (val instanceof Collection) {
int len = ((Collection) val).size();
if (len != 2) {
throw E.unexpected("<between> value collection size is not correct, expected: 2; found: %s", len);
}
Iterator<?> itr = ((Collection) val).iterator();
where.between(field, itr.next(), itr.next());
} else {
throw E.unexpected("<between> value type not recognized: %s", val.getClass());
}
}
}, gt() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.gt(field, val);
}
}, ge() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.ge(field, val);
}
}, lt() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.lt(field, val);
}
}, le() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.le(field, val);
}
}, isNull() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
// val is not relevant here
where.isNull(field);
}
}, isNotNull() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
// val is not relevant here
where.isNotNull(field);
}
}, like() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.like(field, val.toString());
}
}, ilike() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.ilike(field, val.toString());
}
}, startsWith() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.startsWith(field, val.toString());
}
}, istartsWith() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.istartsWith(field, val.toString());
}
}, endsWith() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.endsWith(field, val.toString());
}
}, contains() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.contains(field, val.toString());
}
}, icontains() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
where.icontains(field, val.toString());
}
}, in() {
@Override
void applyTo(ExpressionList<?> where, String field, Object val) {
E.NPE(field, val);
if (val instanceof Query) {
where.in(field, (Query)val);
} else if (val instanceof Collection) {
if (ID.equals(field)) {
where.idIn(C.list((Collection)val));
} else {
where.in(field, (Collection) val);
}
} else if (val.getClass().isArray()) {
int len = Array.getLength(val);
if (len == 0) {
if (ID.equals(field)) {
where.idIn(C.list());
} else {
where.in(field, new Object[0]);
}
} else {
Object[] array = new Object[len];
for (int i = 0; i < len; ++i) {
array[i] = Array.get(val, i);
}
if (ID.equals(field)) {
where.idIn(C.listOf(array));
} else {
where.in(field, array);
}
}
} else {
throw E.unexpected("Unknown <in> value type: %s", val.getClass());
}
}
}
;
abstract void applyTo(ExpressionList<?> where, String field, Object val);
}
private void buildWhere(ExpressionList<MODEL_TYPE> where, String key, Object val) {
String[] sa = key.split("\\s+");
switch (sa.length) {
case 1:
where.eq(sa[0], val);
break;
case 2:
R1.valueOf(sa[1]).applyTo(where, sa[0], val);
break;
case 3:
R2.valueOf(sa[2]).applyTo(where, sa[0], sa[1], val);
default:
throw E.unexpected("Unknown where expression: %s", key);
}
}
@Override
public EbeanQuery<MODEL_TYPE> q(String keys, Object... values) {
int len = values.length;
E.illegalArgumentIf(len == 0, "no values supplied");
String[] sa = keys.split("[,;:]+");
E.illegalArgumentIf(sa.length != len, "The number of values does not match the number of fields");
EbeanQuery<MODEL_TYPE> q = q();
ExpressionList<MODEL_TYPE> el = q.where();
for (int i = 0; i < len; ++i) {
buildWhere(el, sa[i], values[i]);
}
return q;
}
}