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
package foundation.data.mapping;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import foundation.data.entity.Entity;
import foundation.data.meta.field.Field;
import foundation.data.meta.field.FieldsRuntime;
import foundation.util.ContentBuilder;
import foundation.util.Util;
 
public class Mappings implements Iterable<FieldMapping> {
    
    private String id;
    private String name;
    private boolean open;
    private List<FieldMapping> mappingList;
    private Map<String, FieldMapping> fromFieldMap;
    private Map<String, FieldMapping> toFieldMap;
 
    
    public Mappings() {
        mappingList = new ArrayList<FieldMapping>();
        fromFieldMap = new HashMap<String, FieldMapping>();
        toFieldMap = new HashMap<String, FieldMapping>();
    }
 
    public Mappings(String id) {
        this.id = id;
        mappingList = new ArrayList<FieldMapping>();
        fromFieldMap = new HashMap<String, FieldMapping>();
        toFieldMap = new HashMap<String, FieldMapping>();
    }
    
    public static Mappings getInstance(FieldsRuntime one, FieldsRuntime another) {
        Mappings result = new Mappings();
        String fieldName = null; FieldMapping mapping = null; int idx = 0;
        
        for (Field field: one) {
            fieldName = field.getName();
            
            if (!another.contains(fieldName)) {
                continue;
            }
            
            mapping = new FieldMapping();
            mapping.setFromName(fieldName);
            mapping.setToName(fieldName);
            mapping.setIndexNo(idx++);
            
            result.loadOne(mapping);
        }
        
        return result;
    }
    
    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 {
        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 int size() {
        return mappingList.size();
    }
 
    public List<FieldMapping> getList() {
        return mappingList;
    }
 
    public void loadOne(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();
        }
    }
 
    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 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;
    }
 
    @Override
    public Iterator<FieldMapping> iterator() {
        return mappingList.iterator();
    }
 
    public String getFromFieldsString() {
        ContentBuilder builder = new ContentBuilder(", ");
        
        for (FieldMapping mapping: mappingList) {
            builder.append(mapping.getFromName());
        }
        
        return builder.toString();
    }
    
    public String getToFieldsString() {
        ContentBuilder builder = new ContentBuilder(", ");
        
        for (FieldMapping mapping: mappingList) {
            builder.append(mapping.getToName());
        }
        
        return builder.toString();
    }
 
}