-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChildrenMappingModelFieldBuilder.java
213 lines (157 loc) · 4.46 KB
/
ChildrenMappingModelFieldBuilder.java
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
package wbs.framework.entity.build;
import static wbs.utils.collection.MapUtils.mapItemForKeyRequired;
import static wbs.utils.etc.Misc.doesNotContain;
import static wbs.utils.etc.NullUtils.ifNull;
import static wbs.utils.etc.TypeUtils.classForNameRequired;
import static wbs.utils.etc.TypeUtils.classNameFormat;
import static wbs.utils.string.StringUtils.hyphenToCamel;
import static wbs.utils.string.StringUtils.hyphenToCamelCapitalise;
import static wbs.utils.string.StringUtils.hyphenToSpaces;
import static wbs.utils.string.StringUtils.naivePluralise;
import static wbs.utils.string.StringUtils.stringFormat;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import lombok.NonNull;
import org.apache.commons.lang3.reflect.TypeUtils;
import wbs.framework.builder.Builder;
import wbs.framework.builder.BuilderComponent;
import wbs.framework.builder.annotations.BuildMethod;
import wbs.framework.builder.annotations.BuilderParent;
import wbs.framework.builder.annotations.BuilderSource;
import wbs.framework.builder.annotations.BuilderTarget;
import wbs.framework.component.annotations.ClassSingletonDependency;
import wbs.framework.component.annotations.PrototypeComponent;
import wbs.framework.component.annotations.SingletonDependency;
import wbs.framework.component.scaffold.PluginManager;
import wbs.framework.component.scaffold.PluginRecordModelSpec;
import wbs.framework.component.scaffold.PluginSpec;
import wbs.framework.entity.meta.collections.ChildrenMappingSpec;
import wbs.framework.entity.model.ModelField;
import wbs.framework.entity.model.ModelFieldType;
import wbs.framework.logging.LogContext;
import wbs.framework.logging.OwnedTaskLogger;
import wbs.framework.logging.TaskLogger;
import wbs.utils.etc.ClassName;
@PrototypeComponent ("childrenMappingModelFieldBuilder")
@ModelBuilder
public
class ChildrenMappingModelFieldBuilder
implements BuilderComponent {
// singleton dependencies
@ClassSingletonDependency
LogContext logContext;
@SingletonDependency
PluginManager pluginManager;
// builder
@BuilderParent
ModelFieldBuilderContext context;
@BuilderSource
ChildrenMappingSpec spec;
@BuilderTarget
ModelFieldBuilderTarget target;
// build
@Override
@BuildMethod
public
void build (
@NonNull TaskLogger parentTaskLogger,
@NonNull Builder <TaskLogger> builder) {
try (
OwnedTaskLogger taskLogger =
logContext.nestTaskLogger (
parentTaskLogger,
"build");
) {
String fieldName =
ifNull (
spec.name (),
naivePluralise (
spec.typeName ()));
PluginRecordModelSpec fieldTypePluginModel =
mapItemForKeyRequired (
pluginManager.pluginRecordModelsByName (),
spec.typeName ());
PluginSpec fieldTypePlugin =
fieldTypePluginModel.plugin ();
ClassName fullFieldTypeName =
classNameFormat (
"%s.model.%sRec",
fieldTypePlugin.packageName (),
hyphenToCamelCapitalise (
spec.typeName ()));
Class <?> fieldTypeClass =
classForNameRequired (
fullFieldTypeName);
if (
doesNotContain (
mapTypeClasses.keySet (),
spec.mapType ())
) {
throw new RuntimeException (
stringFormat (
"Invalid map type %s ",
spec.mapType (),
"for model child field %s.%s",
context.modelMeta.name (),
fieldName));
}
Class <?> mapTypeClass =
mapTypeClasses.get (
spec.mapType ());
// create model field
ModelField modelField =
new ModelField ()
.model (
target.model ())
.parentField (
context.parentModelField ())
.name (
hyphenToCamel (
fieldName))
.label (
hyphenToSpaces (
fieldName))
.type (
ModelFieldType.collection)
.parent (
false)
.identity (
false)
.valueType (
Map.class)
.parameterizedType (
TypeUtils.parameterize (
Map.class,
mapTypeClass,
fieldTypeClass))
.collectionKeyType (
mapTypeClass)
.collectionValueType (
fieldTypeClass)
.whereSql (
spec.whereSql ())
.orderSql (
spec.orderSql ())
.joinColumnName (
spec.joinColumnName ())
.mappingKeyColumnName (
spec.mapColumnName ());
// store field
target.fields ().add (
modelField);
target.fieldsByName ().put (
modelField.name (),
modelField);
}
}
public final static
Map <String, Class <?>> mapTypeClasses =
ImmutableMap.<String, Class <?>> builder ()
.put (
"string",
String.class)
.put (
"integer",
Long.class)
.build ();
}