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
package foundation.io.mapping;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import foundation.data.entity.DictionaryTranslator;
import foundation.data.entity.Entity;
import foundation.data.meta.field.Field;
import foundation.data.meta.field.FieldsRuntime;
import foundation.io.define.IODirection;
import foundation.io.define.IOSource;
import foundation.io.object.Header;
import foundation.io.object.Headers;
import foundation.io.object.Titles;
import foundation.util.Util;
 
public class IOMappings {
    
    private IOSource source;
    private String id;
    private String name;
    private boolean open;
    private List<FieldMapping> mappingList;
    private Map<String, FieldMapping> fromFieldMap;
    private Map<String, FieldMapping> toFieldMap;
    private Map<String, DictionaryTranslator> dictionaryList;
 
    
    public IOMappings(IOSource source) {
        this.source = source;
        
        mappingList = new ArrayList<FieldMapping>();
        fromFieldMap = new HashMap<String, FieldMapping>();
        toFieldMap = new HashMap<String, FieldMapping>();
        dictionaryList = new HashMap<String, DictionaryTranslator>();
    }
 
    public void load(Entity entity) throws Exception {
        id = entity.getString("id");
        name = entity.getString("name");
        open = entity.getBoolean("is_open", true);
    }
    
    public MappingsRuntime createRuntime(FieldsRuntime fromMeta, FieldsRuntime toMeta, MappingDirection direction) throws Exception {
        return createRuntime(fromMeta, toMeta, direction, false);
    }
    
    public MappingsRuntime createRuntime(FieldsRuntime fromMeta, FieldsRuntime toMeta, MappingDirection direction, boolean open) throws Exception {
        MappingsRuntime mappingSetRuntime = new MappingsRuntime();
        
        for (FieldMapping fieldMapping : mappingList) {
            String fromName, toName;
            if (MappingDirection.Normal == direction) {
                fromName = fieldMapping.getFromName();
                toName = fieldMapping.getToName();
            }
            else {
                fromName = fieldMapping.getToName();
                toName = fieldMapping.getFromName();
            }
            
            Field formField = fromMeta.get(fromName);
            Field toField = toMeta.get(toName);
            
            if (formField == null || toField == null) {
                continue;
            }
            
            int fromIndex = fromMeta.getIndex(fromName);
            int toIndex = toMeta.getIndex(toName);
            
            FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(fieldMapping, formField, toField, fromIndex, toIndex);
            mappingSetRuntime.addItem(mappingRuntime);
        }
        
        if (open) {
            List<Field> fields = fromMeta.getFields();
            for (Field field : fields) {
                String fieldName = field.getName();
                
                if (mappingSetRuntime.containsFromField(fieldName)) {
                    continue;
                }
                if (!toMeta.contains(fieldName)) {
                    continue;
                }
                
                Field fromField = fromMeta.get(fieldName);
                Field toField = toMeta.get(fieldName);
                
                int fromIndex = fromMeta.getIndex(fieldName);
                int toIndex = toMeta.getIndex(fieldName);
                
                FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(null, fromField, toField, fromIndex, toIndex);
                mappingSetRuntime.addItem(mappingRuntime);
            }
        }
        
        return mappingSetRuntime;
    }
    
    public MappingsRuntime createRuntime(Titles fromTitles, FieldsRuntime toMeta, MappingDirection direction) throws Exception {
        MappingsRuntime mappingsRuntime = new MappingsRuntime();
         
        for (FieldMapping fieldMapping : mappingList) {
            String fromName, toName;
            if (MappingDirection.Normal == direction) {
                fromName = fieldMapping.getFromName();
                toName = fieldMapping.getToName();
            }
            else {
                fromName = fieldMapping.getToName();
                toName = fieldMapping.getFromName();
            }
            
            Integer fromIndex = fromTitles.getIndex(fromName);
            Field toField = toMeta.get(toName);
            
            if (fromIndex == null || toField == null) {
                continue;
            }
            
            int toIndex = toMeta.getIndex(toName);
            
            FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(fieldMapping, fromName, toField, fromIndex, toIndex);
            mappingsRuntime.addItem(mappingRuntime);
        }
        
        return mappingsRuntime;
    }
    
    public MappingsRuntime createRuntime(FieldsRuntime fromMeta, Headers toHeaders, MappingDirection direction, IODirection ioDirection) throws Exception {
        MappingsRuntime mappingSetRuntime = new MappingsRuntime();
        
        //1. if to titles is empty
        if (toHeaders.isEmpty()) {
            for (FieldMapping fieldMapping : mappingList) {
                String fromName, toName;
                if (MappingDirection.Normal == direction) {
                    fromName = fieldMapping.getFromName();
                    toName = fieldMapping.getToName();
                }
                else {
                    fromName = fieldMapping.getToName();
                    toName = fieldMapping.getFromName();
                }
                
                Field fromField = fromMeta.get(fromName);
                
                if (fromField == null) {
                    continue;
                }
                
                int fromIndex = fromMeta.getIndex(fromName);
                
                FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(fieldMapping, fromField, toName, fromIndex, fromIndex);
                mappingSetRuntime.addItem(mappingRuntime);
                
                toHeaders.addOne(toName);
            }
             
            if (IODirection.ErrorsToSheet == ioDirection) {
                String fromName = "error_message";
                String toName = "导入反馈";
                Field fromField = fromMeta.get(fromName);
                int fromIndex = fromMeta.getIndex(fromName);
                FieldMapping fieldMapping = new FieldMapping();
                FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(fieldMapping, fromField, toName, fromIndex, fromIndex);
                mappingSetRuntime.addItem(mappingRuntime);
                
                toHeaders.addOne(toName);
            }
            
            return mappingSetRuntime;
        }
        
        //2. if to headers is not empty
        for (Header header: toHeaders) {
            Field fromField = fromMeta.get(header.getFieldName());
            
            if (fromField == null) {
                continue;
            }
 
            FieldMappingRuntime mappingRuntime = new FieldMappingRuntime(null, fromField, header.getTitle(), fromField.getIndexNo(), header.getIndex());
            mappingSetRuntime.addItem(mappingRuntime);
            
        }
        
        return mappingSetRuntime;
    }
    
    public int size() {
        return mappingList.size();
    }
 
    public List<FieldMapping> getList() {
        return mappingList;
    }
 
    public void addFieldMapping(FieldMapping mapping) {
        String fromField = mapping.getFromName();
        String toField = mapping.getToName();
 
        mappingList.add(mapping);
 
        if (fromField != null) {
            fromFieldMap.put(fromField, mapping);
        }
 
        if (toField != null) {
            toFieldMap.put(toField, mapping);
        }
    }
    
    public String getFromField(String toField) {
        return convertField(toField, true);
    }
    
    public String getToField(String fromField) {
       return convertField(fromField, false);
    }
    
    private String convertField(String field, boolean isFromField) {
        FieldMapping item = getIoMappingItem(field, isFromField);
        
        if (isFromField) {
            return item.getToName();
        }
        else {
            return item.getFromName();
        }
    }
    
    public void clear() {
        if (mappingList != null) {
            mappingList.clear();
        }
        
        if (fromFieldMap != null) {
            fromFieldMap.clear();
        }
    
        if (toFieldMap != null) {
            toFieldMap.clear();
        }
        
        if (dictionaryList != null) {
            dictionaryList.clear();
        }
    }
 
    public FieldMapping getItemByFromField(String fromField) {
        return getIoMappingItem(fromField, true);
    }
    
    public FieldMapping getItemByToField(String toField) {
        return getIoMappingItem(toField, false);
    }
    
    private FieldMapping getIoMappingItem(String field, boolean isFromField) {
        Map<String, FieldMapping> itemMap = null;
        if (Util.isEmpty(field)) {
            return null;
        }
         
        field = field.toLowerCase();
        
        if (isFromField) {
            itemMap = fromFieldMap;
        }
        else {
            itemMap = toFieldMap;
        }
        
        if (!itemMap.containsKey(field)) {
            return null;
        }
 
        FieldMapping item = itemMap.get(field);
        return item;
    }
 
    public boolean containsToField(String toField) {
        if (Util.isEmpty(toField)) {
            return false;
        }
        
        toField = toField.toLowerCase();
        return toFieldMap.containsKey(toField);
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public boolean isOpen() {
        return open;
    }
 
    public void setOpen(boolean open) {
        this.open = open;
    }
 
    public List<FieldMapping> getItemList() {
        return mappingList;
    }
 
    public IOSource getSource() {
        return source;
    }
}