Repository navigation
Expand file tree
/
Copy pathAutoBinder.java
More file actions
133 lines (123 loc) · 4.65 KB
/
Copy pathAutoBinder.java
File metadata and controls
133 lines (123 loc) · 4.65 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
package act.data;
import act.app.ActionContext;
import act.app.App;
import act.app.data.StringValueResolverManager;
import org.apache.commons.lang3.math.NumberUtils;
import org.osgl.$;
import org.osgl.cache.CacheService;
import org.osgl.logging.LogManager;
import org.osgl.logging.Logger;
import org.osgl.mvc.result.BadRequest;
import org.osgl.mvc.util.ParamValueProvider;
import org.osgl.util.C;
import org.osgl.util.S;
import org.osgl.util.StringValueResolver;
import java.util.Collection;
import java.util.List;
import java.util.Set;
public class AutoBinder {
private static Logger logger = LogManager.get(AutoBinder.class);
private CacheService cacheService;
private StringValueResolverManager resolverManager;
public AutoBinder(App app) {
this.cacheService = app.cache();
this.resolverManager = app.resolverManager();
}
public Object resolveSimpleTypeContainer(Collection container, String bindName, ParamValueProvider paramValueProvider, Class<?> componentType) {
ActionContext ctx = $.cast(paramValueProvider);
$.T2<Set<String>, Integer> t2 = prepare(bindName, paramValueProvider);
if (null == t2) {
// ActFramework pass zero length array or container
// to action method even if the parameter not found
return container;
}
Set<String> relevantParams = t2._1;
int prefixLen = t2._2;
for (String s : relevantParams) {
String[] va = ctx.paramVals(s);
if (null == va) {
continue;
}
for (String v: va) {
if (S.blank(v)) {
continue;
}
String propertyPath = s.substring(prefixLen);
if (propertyPath.contains("]")) {
propertyPath = propertyPath.replace('[', '.').replace("]", "");
}
Object o = resolverManager.resolve(v, componentType);
if (S.blank(propertyPath)) {
container.add(o);
} else if (NumberUtils.isDigits(propertyPath)) {
if (container instanceof List) {
List list = (List) container;
int index = Integer.parseInt(propertyPath);
while (list.size() < index + 1) {
list.add(null);
}
((List) container).set(Integer.parseInt(propertyPath), o);
} else {
container.add(o);
}
} else {
return null;
}
}
}
return container;
}
public Object resolve(Object entity, String bindName, ParamValueProvider paramValueProvider) {
ActionContext ctx = $.cast(paramValueProvider);
$.T2<Set<String>, Integer> t2 = prepare(bindName, paramValueProvider);
if (null == t2) {
return null;
}
Set<String> relevantParams = t2._1;
int prefixLen = t2._2;
for (String s : relevantParams) {
String v = ctx.paramVal(s);
if (S.blank(v)) {
continue;
}
String propertyPath = s.substring(prefixLen);
try {
$.setProperty(cacheService, entity, v, propertyPath);
} catch (RuntimeException e) {
Throwable t = e.getCause();
if (t instanceof NoSuchFieldException) {
logger.warn("Cannot assign to unknown property %s on class %s", propertyPath, entity.getClass());
// ignore now
// TODO: cache this information so that next time we won't trigger it again
} else {
throw e;
}
}
}
return entity;
}
private $.T2<Set<String>, Integer> prepare(String bindName, ParamValueProvider paramValueProvider) {
ActionContext ctx = $.cast(paramValueProvider);
Set<String> params = ctx.allParams().keySet();
Set<String> relevantParams = C.newSet();
String prefix = bindName;
for (String s : params) {
String p2;
if (s.contains(".")) {
p2 = prefix + ".";
} else if (s.contains("[")) {
p2 = prefix + "[";
} else {
continue;
}
if (s.startsWith(p2)) {
relevantParams.add(s);
}
}
if (relevantParams.isEmpty()) {
return null;
}
int prefixLen = prefix.length() + 1;
return $.T2(relevantParams, prefixLen);
}
}