IT-KIMI_SHI\SINOIT.KIMI
2018-06-01 64c40fb427bff513f575f11e4c1e7bd9a1bfe3e3
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
package frame.file;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import frame.file.office.IOMappingItemRuntime;
import frame.persist.NamedSQL;
import frame.util.Util;
 
public class FileIOContext implements IFileContext {
    
    private List<String> paramNames;
    private Map<String, String> paramMap;
    private FileIOItem ioItem;
    
    public FileIOContext(FileIOItem fileIoItem) {
        paramMap = new HashMap<String, String>();
        ioItem = fileIoItem;
        initParamNames();
    }
    
    public void setParametersTo(NamedSQL namedSQL) {
        for (String name : paramNames) {
            if (namedSQL.containsData(name)) {
                String value = getParameter(name);
 
                if (value == null) {
                    continue;
                }
 
                namedSQL.setParam(name, value);
            }
        }
    }
    
    public String getParameter(String name) {
        if (Util.isEmptyStr(name)) {
            return name;
        }
 
        name = name.toLowerCase();
 
        if ("@totable".equalsIgnoreCase(name)) {
            return ioItem.getToName();
        }
        else if ("@fromtable".equalsIgnoreCase(name)) {
            return ioItem.getFromName();
        }
        else if ("@distinct".equalsIgnoreCase(name)) {
            return ioItem.isDistinctSelect() ? "distinct" : "";
        }
        else if ("@updatefieldpairs".equalsIgnoreCase(name)) {
            return getUpdateFieldPairs();
        }
        else if ("@updatefromfieldnames".equalsIgnoreCase(name)) {
            return getUpdateFromFieldNames();
        }
        else if ("@inserttofieldnames".equalsIgnoreCase(name)) {
             return getInsertToFieldNames();
        }
        else if ("@insertfromfieldnames".equalsIgnoreCase(name)) {
            return getInsertFromFieldNames();
        }
        else if ("@filterfieldvalues".equalsIgnoreCase(name)) {
            return getFilterFieldValues();
        }
        else if ("@keyfieldpairs".equalsIgnoreCase(name)) {
            return getKeyFieldPairs();
        }
 
        if (paramMap.containsKey(name)) {
            return paramMap.get(name);
        }
 
        return null;
    }
    
    
    private String getUpdateFieldPairs() {
        StringBuilder result = new StringBuilder(", ");
 
        List<IOMappingItemRuntime> runtimeList = ioItem.getUpdateMappingRuntime();
        String toTable = ioItem.getToName();
        String fromTable = ioItem.getFromName();
 
        for (IOMappingItemRuntime itemRuntime : runtimeList) {
            result.append(toTable + "." + itemRuntime.getToField() + " = " + fromTable + "." + itemRuntime.getFromField());
        }
 
        return result.toString();
    }
 
    
    public void addParameters(Map<String, String> paramsMap) {
        if (paramsMap == null) {
            return;
        }
        Set<String> keySet = paramsMap.keySet();
        for (String key : keySet) {
            key = key.toLowerCase();
            if (!"@".equals(key.charAt(0))) {
                key = "@" + key;
            }
            paramsMap.put(key, paramsMap.get(key));
            paramNames.add(key);
        }
    }
    
    private String getUpdateFromFieldNames() {
        return getMappingRuntimeList2String("update", true);
    }
    //TODO   INSERT  UPDATE ??
    private String getInsertFromFieldNames() {
        return getMappingRuntimeList2String("insert", true);
    }
 
    private String getInsertToFieldNames() {
        return getMappingRuntimeList2String("insert", false);
    }
    
    private String getMappingRuntimeList2String (String type, boolean isfromField) {
        StringBuilder builder = new StringBuilder(", ");
        List<IOMappingItemRuntime> dataList = null;
         
        if ("insert".equalsIgnoreCase(type)) {
            dataList = ioItem.getInsertMappingRuntime();
        }
        else if ("update".equalsIgnoreCase(type)) {
            dataList = ioItem.getUpdateMappingRuntime();
        }
        
        for (IOMappingItemRuntime ioMappingItemRuntime : dataList) {
            String field = null;
            if (isfromField) {
                field = ioMappingItemRuntime.getFromField();
            }
            else {
                field = ioMappingItemRuntime.getToField();
            }
            builder.append(field);
        }
        
        return builder.toString();
    }
    
    
    private String getFilterFieldValues() {
        String andPair = getAndPair("filter");
        
        return andPair;
    }
 
    private String getKeyFieldPairs() {
        String andPair = getAndPair("key");
        
        return andPair;
    }
 
    private String getAndPair(String type) {
        String[] fields = null;
        if ("key".equalsIgnoreCase(type)) {
            fields = ioItem.getKeyFieldPairs();
            if (fields == null || fields.length == 0) {
                return ioItem.getFromName() + ".id = " + ioItem.getToName() + ".id";
            }
        }
        else if ("filter".equalsIgnoreCase(type)) {
            String[] filterFieldValues = ioItem.getFilterFieldValues();
 
            if (filterFieldValues == null || filterFieldValues.length == 0) {
                return "1=1";
            }
        }
        
         // if define
        StringBuilder result = new StringBuilder(" and ");
 
        for (String fieldPair : fields) {
            String keyFieldPair = setParametersTo(fieldPair);
            result.append(keyFieldPair);
        }
 
        return result.toString();
 
    }
    
    public String setParametersTo(String segment) {
        if (segment == null) {
            return segment;
        }
 
        segment = segment.toLowerCase();
 
        for (String name : paramNames) {
            if (segment.indexOf(name) >= 0) {
                String value = getParameter(name);
 
                if (value == null) {
                    continue;
                }
 
                segment = segment.replace(name, value);
            }
        }
 
        return segment;
    }
    
    
    private void initParamNames() {
        paramNames = new ArrayList<String>();
        paramNames.add("@taskid");
        paramNames.add("@typecode");
        paramNames.add("@totable");
        paramNames.add("@fromtable");
        paramNames.add("@distinct");
        paramNames.add("@keyfieldpairs");
        paramNames.add("@updatefieldpairs");
        paramNames.add("@filterFieldValues");
        paramNames.add("@fieldnames");
        paramNames.add("@inserttofieldnames");
        paramNames.add("@insertfromfieldnames");
        paramNames.add("@updatefromfieldnames");
        
    }
}