IT-KIMI_SHI\SINOIT.KIMI
2018-06-04 b12dfac6e495d6cf38df6b7e5222191f59762900
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
package frame.file;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.sun.org.apache.regexp.internal.recompile;
 
import frame.data.Entity;
import frame.data.meta.EntityMeta;
import frame.data.meta.Field;
import frame.util.Util;
 
public class IOMapping implements IFileload{
 
    private String id;
    private String name;
    private boolean open;
    private int typeCode;
    private List<IOMappingItem> itemList;
    private Map<String, IOMappingItem> fromItemMap;
    private Map<String, IOMappingItem> toItemMap;
 
    public IOMapping() {
        itemList = new ArrayList<IOMappingItem>();
        fromItemMap = new HashMap<String, IOMappingItem>();
        toItemMap = new HashMap<String, IOMappingItem>();
    }
 
    @Override
    public void initLoad(Entity entity) {
        // TODO Auto-generated method stub
        
    }
    public IOMappingRuntime createRuntime(FileIOContext context, String condition, EntityMeta fromMeta, EntityMeta toMeta, Direction direction) throws Exception {
        List<IOMappingItemRuntime> updateMappingRuntime = new ArrayList<IOMappingItemRuntime>();
        List<IOMappingItemRuntime> insertMappingRuntime = new ArrayList<IOMappingItemRuntime>();
        
        for (IOMappingItem mappingItem : itemList) {
            String fromField, toField;
            if (Direction.Normal == direction) {
                fromField = mappingItem.getFromField();
                toField = mappingItem.getToField();
            }
            else {
                fromField = mappingItem.getToField();
                toField = mappingItem.getFromField();
            }
            
            if (context != null) {
                fromField = context.setParametersTo(fromField);
                toField = context.setParametersTo(toField);
            }
            
            Field fieldByFrom = fromMeta.get(fromField);
            Field fieldByto = fromMeta.get(toField);
            
            if (fieldByFrom == null || fieldByto == null) {
                if (FileIoItem.TypeCode_DB.equalsIgnoreCase(condition)) {
                    if (mappingItem.isInsertIgnore() || mappingItem.isUpdateIgnore()) {
                        continue;
                    }
                    else {
                        throw new Exception("IO Mapping fields not in table:" + mappingItem.toString() + " " + fromMeta.getName() + ", " + toMeta.getName());
                    }
                }
                else {
                    continue;
                }
            }
            
            IOMappingItemRuntime mappingItemRuntime = new IOMappingItemRuntime(mappingItem, fromField, fieldByFrom, toField, fieldByto);
            
            if (!mappingItem.isInsertIgnore()) {
                insertMappingRuntime.add(mappingItemRuntime);
            }
            
            if (!mappingItem.isUpdateIgnore()) {
                updateMappingRuntime.add(mappingItemRuntime);
            }
        
        }
        
        if (open) {
            List<Field> fields = fromMeta.getFields();
            for (Field field : fields) {
                String fieldName = field.getName();
                if (!toMeta.contains(fieldName)) {
                    continue;
                }
                
                Field fromField = fromMeta.get(fieldName);
                Field toField = toMeta.get(fieldName);
                
                IOMappingItemRuntime itemRuntime = new IOMappingItemRuntime(null, field, fromField, field, toField);
                if (!fromItemMap.ContainsKey(field)) {
                    updateMappingRuntime.Add(itemRuntime);
                }
 
                if (!fromItemMap.ContainsKey(field)) {
                    insertMappingRuntime.Add(itemRuntime);
                } 
            }
        }
    }
    
    public int size() {
        return itemList.size();
    }
 
    public List<IOMappingItem> getList() {
        return itemList;
    }
 
    public void addItem(IOMappingItem item) {
        String fromField = item.getFromField();
        String toField = item.getToField();
 
        itemList.add(item);
 
        if (fromField != null) {
            fromItemMap.put(fromField, item);
        }
 
        if (toField != null) {
            toItemMap.put(toField, item);
        }
    }
    
    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) {
        IOMappingItem item = getIoMappingItem(field, isFromField);
        
        if (isFromField) {
            return item.getToField();
        }
        else {
            return item.getFromField();
        }
        
    }
    
    public void clear() {
        if (itemList != null) {
            itemList.clear();
        }
        
        if (fromItemMap != null) {
            fromItemMap.clear();
        }
    
        if (toItemMap != null) {
            toItemMap.clear();
        }
    }
 
    public IOMappingItem getItemByFromField(String fromField) {
        return getIoMappingItem(fromField, true);
    }
    
    public IOMappingItem getItemByToField(String toField) {
        return getIoMappingItem(toField, false);
    }
    
    private IOMappingItem getIoMappingItem(String field, boolean isFromField) {
        Map<String, IOMappingItem> itemMap = null;
        if (Util.isEmptyStr(field)) {
            return null;
        }
         
        field = field.toLowerCase();
        
        if (isFromField) {
            itemMap = fromItemMap;
        }
        else {
            itemMap = toItemMap;
        }
        
        if (!itemMap.containsKey(field)) {
            return null;
        }
 
        IOMappingItem item = itemMap.get(field);
        return item;
    }
 
    public String getId() {
        return id;
    }
 
    public String getName() {
        return name;
    }
 
    public boolean isOpen() {
        return open;
    }
 
    public int getTypeCode() {
        return typeCode;
    }
 
    public List<IOMappingItem> getItemList() {
        return itemList;
    }
 
 
}