P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
453
454
455
456
457
458
459
package foundation.io.define;
 
import foundation.dao.Filter;
import foundation.dao.bizlogic.ICodeProvider;
import foundation.data.entity.Entity;
import foundation.data.entity.EntitySet;
import foundation.data.object.DataObject;
import foundation.io.IOLoader;
import foundation.io.engine.LineValueTrigger;
import foundation.io.mapping.MappingDirection;
import foundation.io.mapping.MappingsRuntime;
import foundation.server.config.ServerStatus;
import foundation.util.MapList;
import foundation.util.Util;
 
public class DataIO {
    
    private String id;
    private String taskName;
    private String fromName;
    private String toName;
    private String toTempName;
    private String templateName;
    private String fileName;
    private boolean writeTitle;
    private IODirection direction;
    private String mappingId;
    private boolean distinctSelect;
    private boolean standardMove;
    private String[] filterFieldValues;
    private String[] keyFieldPairs;
    private String[] checkDocField;
    private String additionalFields;
    private AppendMode appendMode;
    private DeleteMode deleteMode;
    private int fromTitleRowNo;
    private int fromDataRowNo;
    private int toTitleRowNo;
    private int toDataRowNo;
    private String action;
    private String actionMethod;
    private boolean active;
    private IOSource source;
    private MapList<String, LineValueTrigger> valueTriggers;
    
    private DataObject fromDataObject;
    private DataObject toDataObject;
    private DataObject toTempDataObject;
    private MappingDirection mappingDirection;
    private IOSQLContext context;
    private IOWorkflow wrokflow;
 
    
    public DataIO() {
        distinctSelect = false;
        standardMove = true;
 
        fromTitleRowNo = 0;
        fromDataRowNo = 1;
        toTitleRowNo = 0;
        toDataRowNo = 1;
        
        mappingDirection = MappingDirection.Normal;
        appendMode = AppendMode.Append;
        deleteMode = DeleteMode.HardDelete;
 
        additionalFields = null;
        keyFieldPairs = null;
        filterFieldValues = null;
        
        active = true;
        context = new IOSQLContext(this);
    }            
                
    public void load(Entity entity) throws Exception {
        id = entity.getString("id");
        taskName = entity.getString("task_name");
        
        fromName = entity.getString("from_name");
        toName = entity.getString("to_name");
        toTempName = entity.getString("to_temp_name");
        fileName = templateName = entity.getString("template_name");
        writeTitle = entity.getBoolean("is_write_title", true);
        direction = IODirection.parse(entity.getString("direction_code"));
        
        mappingId = entity.getString("mapping_id");
        mappingDirection = MappingDirection.parse((entity.getString("mapping_direction")));
 
        distinctSelect = entity.getBoolean("is_distinct_select", false);
        standardMove = entity.getBoolean("is_standard_move", true);
 
        fromTitleRowNo = entity.getInteger("from_title_rowno", 0);
        fromDataRowNo = entity.getInteger("from_data_rowno", 1);
        toTitleRowNo = entity.getInteger("to_title_rowno", 0);
        toDataRowNo = entity.getInteger("to_data_rowno", 1);
        
        action = entity.getString("action_name");
        actionMethod = entity.getString("method_name");
         
        appendMode = AppendMode.parse(entity.getString("append_mode"), AppendMode.ClearAndAppend);
        deleteMode = DeleteMode.parse(entity.getString("delete_mode"), DeleteMode.HardDelete);
 
        additionalFields = entity.getString("additional_field");
 
        String keyField = entity.getString("key_field_pairs");
        if (!Util.isEmpty(keyField)) {
            keyFieldPairs = keyField.split(";");
        }
 
        String filterField = entity.getString("filter_field_values");
        if (!Util.isEmpty(filterField)) {
            filterFieldValues = filterField.split(";");
        }
        
        String docFilterFieldStr = entity.getString("check_doc_field");
        if (!Util.isEmpty(docFilterFieldStr)) {
            checkDocField = docFilterFieldStr.split(";");
        }
        
        
        active = entity.getBoolean("is_active", true);
        source = IOSource.ConfigDefine;
        
        valueTriggers = new MapList<String, LineValueTrigger>();
        loadValueTriggers(entity);
    }
 
    private void loadValueTriggers(Entity entity) throws Exception{
        DataObject dataObject = DataObject.getInstance("sys_io_trigger");
        Filter filter = new Filter("parent_id", id).add("is_active", "T");
        EntitySet dataIOTriggerSet = dataObject.getTableEntitySet(filter);
        
        for (Entity oneTrigger: dataIOTriggerSet) {
            LineValueTrigger trigger = new LineValueTrigger();
            trigger.load(oneTrigger);
            valueTriggers.add(trigger.getTriggerName(), trigger);
        }
 
    }
 
    public static String extractSheetName(String value) {
        if (Util.isEmpty(value)) {
            return "Sheet1";
        }
 
        int begin = value.indexOf("[");
 
        if (begin < 0) {
            return "Sheet1";
        }
 
        int end = value.indexOf("]", begin);
 
        if (end < 0) {
            return "Sheet1";
        }
        
        return value.substring(begin + 1, end - begin - 1);
    }
 
    public static String extractFileName(String value) {
        if (Util.isEmpty(value)) {
            return null;
        }
 
        int end = value.indexOf("[");
 
        if (end < 0) {
            return value;
        }
 
        return value.substring(0, end - 1);
    }
    
    public boolean existsFilterFieldValues() {
        if (filterFieldValues == null) {
            return false;
        }
 
        return filterFieldValues.length > 0;
    }
    
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
    
    public String getTaskName() {
        return taskName;
    }
 
    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }
 
    public String getFromName() {
        return fromName;
    }
 
    public void setFromName(String fromName) {
        this.fromName = fromName;
    }
 
    public String getToName() {
        return toName;
    }
 
    public boolean existsToName() {
        return !Util.isEmpty(toName);
    }
 
    public void setToName(String toName) {
        this.toName = toName;
    }
 
    public String getToTempName() {
        return toTempName;
    }
    
    public void setToTempName(String toTempName) {
        this.toTempName = toTempName;
    }
 
    public MapList<String, LineValueTrigger> getValueTriggers() {
        return valueTriggers;
    }
    
    public IODirection getDirection() {
        return direction;
    }
    
    public void setDirection(IODirection direction) {
        this.direction = direction;
    }
 
    public boolean isWriteTitle() {
        return writeTitle;
    }
 
    public void setWriteTitle(boolean value) {
        writeTitle = value;
    }
    
    public String getTemplateName() {
        return templateName;
    }
 
    public void setTemplateName(String value) {
        templateName = value;
    }
 
    public String getFileName() {
        if (!Util.isEmpty(fileName)) {
            return fileName;
        }
        
        if (IODirection.TableToSheet == direction) {
            fileName = getFromDataObject().getTitle();
            fileName = fileName + ".xlsx";
        }
        
        return fileName;
    }
 
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
 
    public String getMappingId() {
        return mappingId;
    }
 
    public boolean isDistinctSelect() {
        return distinctSelect;
    }
 
    public boolean isStandardMove() {
        return standardMove;
    }
 
    public String[] getFilterFieldValues() {
        return filterFieldValues;
    }
 
    public String[] getKeyFieldPairs() {
        return keyFieldPairs;
    }
 
    public String[] getCheckDocField() {
        return checkDocField;
    }
    
    public AppendMode getAppendMode() {
        return appendMode;
    }
 
    public void setAppendMode(AppendMode appendMode) {
        this.appendMode = appendMode;
    }
 
    public DeleteMode getDeleteMode() {
        return deleteMode;
    }
 
    public MappingDirection getMappingDirection() {
        return mappingDirection;
    }
 
    public int getFromTitleRowNo() {
        return fromTitleRowNo;
    }
 
    public int getFromDataRowNo() {
        return fromDataRowNo;
    }
 
    public int getToTitleRowNo() {
        return toTitleRowNo;
    }
 
    public int getToDataRowNo() {
        return toDataRowNo;
    }
 
    public String getAdditionalFields() {
        return additionalFields;
    }
 
    public boolean isActive() {
        return active;
    }
 
    public IOSQLContext getContext() {
        return context;
    }
 
    public DataObject getToDataObject() {
        if (toDataObject == null) {
            toDataObject = DataObject.getInstance(toName);
        }
        
        return toDataObject;
    }
    
    public DataObject getToTempDataObject() {
        if (toTempDataObject == null) {
            toTempDataObject = DataObject.getInstance(toTempName);
        }
        
        return toTempDataObject;
    }
    
    public DataObject getFromDataObject() {
        if (fromDataObject == null) {
            fromDataObject = DataObject.getInstance(fromName);
        }
        
        return fromDataObject;
    }
 
    public IOWorkflow getWorkflows() throws Exception {
        if (wrokflow != null && !ServerStatus.isCheckMetaUpdateOnTime()) {
            return wrokflow;
        }
        
        synchronized (this) {
            if (wrokflow != null && !ServerStatus.isCheckMetaUpdateOnTime()) {
                return wrokflow;
            }
            
            wrokflow = IOLoader.loadIOActions(this, wrokflow);
        }
        
        return wrokflow;
    }
 
    public IOSource getSource() {
        return source;
    }
 
    public void setSource(IOSource source) {
        this.source = source;
    }
 
    public String getAction() {
        return action;
    }
 
    public void setMappingId(String mappingId) {
        this.mappingId = mappingId;
    }
 
    public String getActionMethod() {
        return actionMethod;
    }
 
    public void setAction(String action) {
        this.action = action;
    }
 
    public void setActionMethod(String actionMethod) {
        this.actionMethod = actionMethod;
    }
 
    public ICodeProvider getCodeRule(MappingsRuntime mappingsRuntime) throws Exception {
        //1. 如果不是导入操作,不需要自动编码
        if (IODirection.SheetToTable != direction) {
            return null;
        }
        
        //2. 遍历 mapping,获取第一个编码规则(目前只支持一个编码)
        DataObject toObject = getToDataObject();
        
        if (toObject == null) {
            return null;
        }
        
        ICodeProvider codeProvider = toObject.getCodeProvider();
        
        if (codeProvider == null) {
            return null;
        }
        
        return codeProvider;
    }
 
    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        
        if (IODirection.SheetToTable == direction) {
            String sheetName = Util.isEmpty(fromName) ? "Sheet1" : fromName;
                    
            result.append("Sheet[").append(sheetName).append("]");
            result.append("-->");
            result.append("表[").append(toTempName).append("]");
        }
        else if (IODirection.TableToSheet == direction) {
            String sheetName = Util.isEmpty(toName) ? "Sheet1" : toName;
            
            result.append("表[").append(fromName).append("]");
            result.append("-->");
            result.append("Sheet[").append(sheetName).append("]");            
        }
        else if (IODirection.TableToTable == direction) {
            result.append("表[").append(fromName).append("]");
            result.append("-->");
            result.append("表[").append(toName).append("]");                
        }
        
        result.append(" 规则:").append(source.toChinese());
        
        return result.toString();
    }
    
}