18
18
import io .nop .record .codec .impl .DefaultFieldCodecContext ;
19
19
import io .nop .record .model .IRecordFieldsMeta ;
20
20
import io .nop .record .model .RecordFieldMeta ;
21
+ import io .nop .record .model .RecordFieldSwitch ;
21
22
import io .nop .record .model .RecordFileMeta ;
22
23
import io .nop .record .model .RecordObjectMeta ;
23
24
import io .nop .record .model .RecordPaginationMeta ;
25
+ import io .nop .record .model .RecordTypeMeta ;
24
26
import org .slf4j .Logger ;
25
27
import org .slf4j .LoggerFactory ;
26
28
27
29
import java .io .IOException ;
30
+ import java .nio .charset .Charset ;
28
31
import java .util .Collection ;
29
32
import java .util .Map ;
30
33
34
+ import static io .nop .record .RecordErrors .ARG_CASE_VALUE ;
35
+ import static io .nop .record .RecordErrors .ARG_FIELD_NAME ;
36
+ import static io .nop .record .RecordErrors .ARG_TYPE_NAME ;
37
+ import static io .nop .record .RecordErrors .ERR_RECORD_NO_MATCH_FOR_CASE_VALUE ;
38
+ import static io .nop .record .RecordErrors .ERR_RECORD_NO_SWITCH_ON_FIELD ;
39
+
31
40
public abstract class AbstractModelBasedRecordOutput <T > implements IRecordOutput <T > {
32
41
static final Logger LOG = LoggerFactory .getLogger (AbstractModelBasedRecordOutput .class );
33
42
@@ -135,7 +144,7 @@ public void writeObject(RecordObjectMeta recordMeta, Object record, String name)
135
144
if (!runIfExpr (recordMeta .getIfExpr (), record , name ))
136
145
return ;
137
146
138
- writeTemplateOrFields (recordMeta , record );
147
+ writeTemplateOrFields (recordMeta , null , record );
139
148
140
149
if (recordMeta .getAfterWrite () != null )
141
150
recordMeta .getAfterWrite ().call1 (null , record , context .getEvalScope ());
@@ -158,10 +167,10 @@ protected void writeField(RecordFieldMeta field, Object record) throws IOExcepti
158
167
if (record instanceof Collection ) {
159
168
Collection <?> c = (Collection <?>) record ;
160
169
for (Object o : c ) {
161
- writeVirtualField (field , o );
170
+ writeSwitch (field , o );
162
171
}
163
172
} else {
164
- writeVirtualField (field , record );
173
+ writeSwitch (field , record );
165
174
}
166
175
}
167
176
@@ -177,6 +186,46 @@ boolean runIfExpr(IEvalFunction expr, Object record, String name) {
177
186
return true ;
178
187
}
179
188
189
+ protected void writeSwitch (RecordFieldMeta field , Object record ) throws IOException {
190
+ if (field .getSwitch () != null ) {
191
+ RecordFieldSwitch switchMeta = field .getSwitch ();
192
+ String onField = switchMeta .getOnField ();
193
+ String onValue = null ;
194
+ if (onField != null ) {
195
+ onValue = ConvertHelper .toString (getPropByName (record , onField ));
196
+ } else if (switchMeta .getOn () != null ) {
197
+ onValue = ConvertHelper .toString (switchMeta .getOn ().call1 (null , record , context .getEvalScope ()));
198
+ }
199
+
200
+ if (onValue == null )
201
+ throw new NopException (ERR_RECORD_NO_SWITCH_ON_FIELD )
202
+ .source (field )
203
+ .param (ARG_FIELD_NAME , field .getName ());
204
+
205
+ String caseType = switchMeta .getTypeByCaseValue (onValue );
206
+ if (caseType == null )
207
+ throw new NopException (ERR_RECORD_NO_MATCH_FOR_CASE_VALUE )
208
+ .source (field )
209
+ .param (ARG_FIELD_NAME , field .getName ())
210
+ .param (ARG_CASE_VALUE , onValue );
211
+
212
+ RecordTypeMeta typeMeta = fileMeta .getType (caseType );
213
+ if (typeMeta == null )
214
+ throw new NopException (ERR_RECORD_NO_MATCH_FOR_CASE_VALUE )
215
+ .source (field )
216
+ .param (ARG_FIELD_NAME , field .getName ())
217
+ .param (ARG_CASE_VALUE , onValue )
218
+ .param (ARG_TYPE_NAME , caseType );
219
+
220
+ Object value = getProp (field , record );
221
+ writeTemplateOrFields (typeMeta , field .getCharsetObj (), value );
222
+
223
+ return ;
224
+ }
225
+
226
+ writeVirtualField (field , record );
227
+ }
228
+
180
229
protected void writeVirtualField (RecordFieldMeta field , Object record ) throws IOException {
181
230
if (field .isVirtual ()) {
182
231
if (field .getFields () != null ) {
@@ -186,15 +235,15 @@ protected void writeVirtualField(RecordFieldMeta field, Object record) throws IO
186
235
}
187
236
} else if (field .getFields () != null ) {
188
237
Object value = getProp (field , record );
189
- writeTemplateOrFields (field , value );
238
+ writeTemplateOrFields (field , field . getCharsetObj (), value );
190
239
} else {
191
240
writeField0 (field , record );
192
241
}
193
242
if (field .getAfterWrite () != null )
194
243
field .getAfterWrite ().call1 (null , record , context .getEvalScope ());
195
244
}
196
245
197
- protected void writeTemplateOrFields (IRecordFieldsMeta fields , Object record ) throws IOException {
246
+ protected void writeTemplateOrFields (IRecordFieldsMeta fields , Charset charset , Object record ) throws IOException {
198
247
SimpleTextTemplate template = fields .getNormalizedTemplate ();
199
248
if (template != null ) {
200
249
for (Object part : template .getParts ()) {
@@ -203,7 +252,7 @@ protected void writeTemplateOrFields(IRecordFieldsMeta fields, Object record) th
203
252
RecordFieldMeta field = fields .requireField (name );
204
253
writeField (field , record );
205
254
} else {
206
- writeString (part .toString ());
255
+ writeString (part .toString (), charset );
207
256
}
208
257
}
209
258
} else {
@@ -215,7 +264,7 @@ protected void writeTemplateOrFields(IRecordFieldsMeta fields, Object record) th
215
264
216
265
abstract protected void writeOffset (int offset ) throws IOException ;
217
266
218
- abstract protected void writeString (String str ) throws IOException ;
267
+ abstract protected void writeString (String str , Charset charset ) throws IOException ;
219
268
220
269
abstract protected void writeField0 (RecordFieldMeta field , Object record ) throws IOException ;
221
270
@@ -227,6 +276,10 @@ protected Object getProp(RecordFieldMeta field, Object record) {
227
276
return null ;
228
277
229
278
String propName = field .getPropOrFieldName ();
279
+ return getPropByName (record , propName );
280
+ }
281
+
282
+ protected Object getPropByName (Object record , String propName ) {
230
283
if (record instanceof IVariableScope )
231
284
return ((IVariableScope ) record ).getValueByPropPath (propName );
232
285
0 commit comments