wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
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
package com.vci.client.omd.provider;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
import com.vci.omd.constants.OmdConstants;
import com.vci.client.common.providers.ServiceProvider;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.etm.EnumChild;
import com.vci.corba.omd.etm.EnumItem;
import com.vci.corba.omd.etm.EnumServicePrx;
 
public class EnumProvider {
 
 
    private EnumProvider(){
        
    }
    private static class InstanceHolder{
        private static EnumProvider instance = new EnumProvider();
    }
    public static EnumProvider getInstance(){
        return InstanceHolder.instance;
    }
    public EnumServicePrx getService(){
        try {
            return ServiceProvider.getOMDService().getEnumService();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 获取所有枚举
     * @return
     */
    public EnumItem[] getAllEnums(){
        try {
            return getService().getEmItems("", 1, 1);
        } catch (VCIError e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 获取指定的枚举
     * @param name
     * @return
     */
    public EnumItem getEnumByName(String name){
        try {
            return getService().getEmItemByName(name);
        } catch (VCIError e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 根据枚举名和 枚举项名获取枚举项值
     * @param enumName
     * @param enumChildName
     * @return
     */
    public String getEnumItemValue(String enumName, String enumChildName) {
        EnumItem enumItem = getInstance().getEnumByName(enumName);
        for(EnumChild ec : enumItem.children){
            if(ec.name.equalsIgnoreCase(enumChildName)){
                return ec.value;
            }
        }
        return null;
    }
    
    public boolean expData(String dir, EnumItem[] objs) {
        //TODO调用ENUM的导出
        BufferedWriter bw = null;
        FileOutputStream fos = null;
        OutputStreamWriter osw =null;
        try{
            File file = new File(dir + "/enum.txt");
            //将clob字段写到单独的文件中
            new File(dir + "/enum").mkdir();
            //add by caill start 2015.12.16     导出时将txt文件设置为“utf-8”格式
            fos = new FileOutputStream(file);
            osw = new OutputStreamWriter(fos, "utf-8");
            bw = new BufferedWriter(osw);
            //add by caill end
            /*w = new FileWriter(file);
            bw = new BufferedWriter(w);*/
            for(EnumItem o : objs){
                //若str小于缓冲区大小, 将写到缓冲区; 
                //若str大于缓冲区大小, 将刷新缓冲区(将缓冲区内容写到底层流), 然后str直接写到底层流.
                String text = getObjectText(o);
                bw.write(text);
                bw.newLine();
                BufferedWriter clobBW = null;
                FileOutputStream fo = null;
                OutputStreamWriter pw =null;
                try{
                    File clobFile = new File(dir + "/enum/" + o.oid + ".xml");
                    //add by caill start 2015.12.15 将xml内容和xml文件格式统一
                    fo = new FileOutputStream(clobFile);
                    pw = new OutputStreamWriter(fo, "utf-8");
                    clobBW = new BufferedWriter(pw);
                    clobBW.write( "<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                    clobBW.write(getXmlText(o));
                    clobBW.flush();
                    //pw.close();
                    //add by caill end
                    /*File clobFile = new File(dir + "/enum/" + o.oid + ".xml");
                     clobW = new FileWriter(clobFile);
                    clobBW = new BufferedWriter(clobW);
                    clobBW.write( "<?xml version=\"1.0\" encoding=\"gb2312\" ?>");//gb2312
                    clobBW.write(getXmlText(o));
                    clobBW.flush();*/
                }catch(IOException e2){
                    e2.printStackTrace();
                }finally{
                    
                    try{
                        if(clobBW != null){
                            //clobW.close();
                            fo.close();        //add by caill 2015.12.16 将fo关闭
                            pw.close();        //add by caill 2015.12.16 将pw关闭
                            clobBW.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            bw.flush();
            System.out.println("**************枚举导出成功************");
            return true;
        }catch(IOException e){
            e.printStackTrace();
            System.out.println("**************枚举导出失败************");
            return false;
        }finally{
            try {
                if(bw != null){
                    //w.close();
                    fos.close();        //add by caill 2015.12.16 将fos关闭
                    osw.close();        //add by caill 2015.12.16 将osw关闭
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    /**
     * 将对象转化成字符串
     * {oid:qqq, name:q}
     * @param o
     * @return
     */
    public String getObjectText(EnumItem o) {
        StringBuilder stb = new StringBuilder("{");
        stb.append(OmdConstants.OID + ":" + o.oid + ",");
        stb.append(OmdConstants.NAME + ":" + o.name + ",");
        stb.append(OmdConstants.LABEL + ":" + o.label + ",");
        stb.append(OmdConstants.TS + ":" + o.ts + ",");
        stb.append(OmdConstants.CREATOR + ":" + o.creator + ",");
        stb.append(OmdConstants.CREATETIME + ":" + o.createTime + ",");
        stb.append(OmdConstants.MODIFIER + ":" + o.modifier + ",");
        stb.append(OmdConstants.MODIFYTIME + ":" + o.modifyTime);
        stb.append("}");
        return stb.toString();
    }
    
    /**
     * 将emItem转化成xmltext
     * @param emItem
     * @return
     */
    public String getXmlText(EnumItem emItem) {
        StringBuilder stb = new StringBuilder("<enum>");
        stb.append("<name>" + emItem.name + "</name>");
        stb.append("<label>" + emItem.label + "</label>");
        stb.append("<type>" + emItem.type + "</type>");
        stb.append("<length>" + emItem.length + "</length>");
        for(EnumChild ec : emItem.children){
            stb.append("<child>");
            stb.append("<name>" + ec.name + "</name>");
            stb.append("<value>" + ec.value + "</value>");
            stb.append("<description>" + ec.description + "</description>");
            stb.append("</child>");
        }
        stb.append("</enum>");
        return stb.toString();
    }
    
    /**
     * 从导入文件中解析出EnumItem
     * @param dir
     * @return
     */
    public List<EnumItem> getEnumsFromFile(String dir){
        File file = new File(dir + "/enum.txt");
        List<EnumItem> list = new ArrayList<EnumItem>();
        BufferedReader br = null;
        FileInputStream fo = null;
        InputStreamReader isw =null;
        try {
            /*r = new FileReader(file);
            br = new BufferedReader(r);*/
            //add by caill start 2015.12.16     导入时将txt文件内容设置为“utf-8”格式
            fo = new FileInputStream(file);
            isw = new InputStreamReader(fo, "utf-8");
            br = new BufferedReader(isw);
            //add by caill end
            String str = null;
            while((str = br.readLine()) != null){
                Map<String, String> map = getMapFormText(str);
                String oid = map.get("oid");
                if(oid != null && !oid.equals("")){
                    File contentFile = new File(dir + "/enum/" + oid + ".xml");
                    if(!contentFile.exists()){
                        System.out.println(dir + "/enum/" + oid + ".xml不存在");
                        break;
                    }
                    EnumItem em = new EnumItem();
                    em.oid = oid;
                    em.name = map.get("name");
                    em.label = map.get("label");
                    //em.label = map.get("ts");
                    em.ts = map.get("ts");
                    em.creator = map.get("creator");
                    em.createTime = Long.valueOf(map.get("createTime"));
                    em.modifier = map.get("modifier");
                    em.modifyTime = Long.valueOf(map.get("modifyTime"));
                    SAXReader sa = new SAXReader();
                    //add by caill 2015.12.16    导入时将saxReader对象设置为“utf-8”格式
                    sa.setEncoding("utf-8");
                
                    Document document = sa.read(contentFile);
                    setEnumValueFormDoc(em, document.getRootElement());
                    list.add(em);
                }else{
                    System.out.println(dir + "enum.txt中存在oid为空的错误记录");
                    break;
                }
            }
            return list;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally{
            if(br != null){
                try {
                    //r.close();
                    fo.close();        //add by caill 2015.12.16    将fo关闭
                    isw.close();    //add by caill 2015.12.16    将isw关闭
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    
    /**
     * 将文件中一条记录解析成key-value
     * @param str
     * @return
     */
    public Map<String, String> getMapFormText(String str) {
        str = str.trim();
        if(!str.startsWith("{") || !str.endsWith("}")){
            return null;
        }
        Map<String, String> map = new HashMap<String, String>();
        str = str.substring(1, str.length() - 1);
        String[] kvs = str.split(",");
        for(String kv : kvs){
            String[] kv_ = kv.split(":");
            if(kv_.length == 1){
                map.put(kv_[0].trim(), "");    
            }else{
                map.put(kv_[0].trim(), kv_[1].trim());
            }
        }
        return map;
    }
    
    /**
     * 设置EnumItem存在
     * @param em
     * @param element
     */
    public void setEnumValueFormDoc(EnumItem em, Element element){
        String value = element.elementText("type");
        em.type = (value == null ? "" : value);
        value = element.elementText("length");
        em.length = (value == null ? 0 : Integer.valueOf(value));
        List<Element> children = element.elements("child");
        List<EnumChild> ecList = new ArrayList<EnumChild>();
        for(Element child : children){
            EnumChild ec = new EnumChild();
            value = child.elementText("name");
            ec.name = (value == null ? "" : value);
            value = child.elementText("value");
            ec.value = (value == null ? "" : value);
            value = child.elementText("description");
            ec.description = (value == null ? "" : value);
            ecList.add(ec);
        }
        em.children = ecList.toArray(new EnumChild[0]);
    }
    
    /**
     * 导入数据
     * @param array
     * @return
     */
    public boolean impData(EnumItem[] ems) {
        try {
            for(EnumItem em : ems){
                EnumItem em_ = getService().getEmItemByName(em.name);
                //已经存在
                if(!em_.oid.equals("")){
                    //类型一致则覆盖
                    if(em.type.equals(em_.type)){
                        getService().deleteEmItem(em_);
                        getService().addEmItem(em);
                        System.out.println(em.name + "在数据库中已经存在,且类型与文件中的一致。 覆盖。");
                    //类型不一致则中止导入
                    }else{
                        System.out.println(em.name + "在数据库中已经存在,且类型与文件中的不一致。 中止导入。");
                        return false;
                    }
                }else{
                    getService().addEmItem(em);
                }
            }
            return true;
        } catch (VCIError e) {
            e.printStackTrace();
        }
        return false;
    }
}