Repository navigation
Expand file tree
/
Copy pathFindBy.java
More file actions
102 lines (90 loc) · 2.99 KB
/
Copy pathFindBy.java
File metadata and controls
102 lines (90 loc) · 2.99 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
package act.db.di;
import act.app.App;
import act.db.Dao;
import act.inject.param.ParamValueLoaderService;
import act.util.ActContext;
import org.osgl.inject.ValueLoader;
import org.osgl.util.*;
import java.util.Collection;
public class FindBy extends ValueLoader.Base {
private String bindName;
private Dao dao;
private StringValueResolver resolver;
private boolean findOne;
private boolean byId;
private String querySpec;
private Class<?> rawType;
@Override
protected void initialized() {
App app = App.instance();
rawType = spec.rawType();
findOne = !(Collection.class.isAssignableFrom(rawType));
dao = app.dbServiceManager().dao(findOne ? rawType : (Class) spec.typeParams().get(0));
byId = (Boolean) options.get("byId");
resolver = app.resolverManager().resolver(byId ? dao.idType() : (Class) options.get("fieldType"));
if (null == resolver) {
throw new IllegalArgumentException("Cannot find String value resolver for type: " + dao.idType());
}
bindName = ParamValueLoaderService.bindName(spec);
if (!byId) {
querySpec = S.string(options.get("value"));
if (S.blank(querySpec)) {
querySpec = bindName;
}
}
}
@Override
public Object get() {
ActContext ctx = ActContext.Base.currentContext();
E.illegalStateIf(null == ctx);
String value = resolve(bindName, ctx);
if (null == value) {
return null;
}
Object by = resolver.resolve(value);
if (null == by) {
return null;
}
Collection col = findOne ? null : (Collection) App.instance().getInstance(rawType);
if (byId) {
Object bean = dao.findById(by);
if (findOne) {
return bean;
} else {
col.add(bean);
return col;
}
} else {
if (findOne) {
return dao.findOneBy(Keyword.of(querySpec).javaVariable(), by);
} else {
col.addAll(C.list(dao.findBy(Keyword.of(querySpec).javaVariable(), by)));
return col;
}
}
}
private static String resolve(String bindName, ActContext ctx) {
String value = ctx.paramVal(bindName);
if (S.notBlank(value)) {
return value;
}
Keyword keyword = Keyword.of(bindName);
if (keyword.tokens().size() > 1) {
return resolve(keyword, ctx);
} else {
keyword = Keyword.of(bindName + " id");
return resolve(keyword, ctx);
}
}
private static String resolve(Keyword keyword, ActContext ctx) {
String value = ctx.paramVal(keyword.underscore());
if (S.notBlank(value)) {
return value;
}
value = ctx.paramVal(keyword.javaVariable());
if (S.notBlank(value)) {
return value;
}
return null;
}
}