ludc
2024-09-14 36c2449aec5b51e5ed4e5c6841154b746060e09a
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
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.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
import com.vci.corba.omd.atm.AttPoolServicePrx;
import com.vci.corba.omd.atm.AttribItem;
import com.vci.corba.omd.etm.EnumItem;
import com.vci.client.common.ClientLog4j;
import com.vci.client.common.providers.ServiceProvider;
import com.vci.corba.common.VCIError;
import com.vci.omd.constants.OmdConstants;
import com.vci.omd.dataType.VTDataType;
import com.vci.omd.objects.OtherInfo;
 
public class ApProvider {
    private static ApProvider apProvider = null;
    
    private ApProvider(){
        
    }
    
    public static ApProvider getInstance(){
        if(apProvider == null){
            apProvider = new ApProvider();
        }
        return apProvider;
    }
    
    private static AttPoolServicePrx getService(){
        try {
            return ServiceProvider.getOMDService().getAttributeService();
        } catch (Exception e) {
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        }
        return null;
    }
    /**
     * 根据多个属性名获取多个属性
     * @param abNames
     * @return
     */
    public static AttribItem[] getAttribItemsByNames(String[] abNames){
        AttribItem[] attribItems = null;
        try {
            attribItems = getService().getAttribItemsByNames(abNames);
        } catch (VCIError e) {
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        }
        if(attribItems == null){
            attribItems = new AttribItem[0];
        }
        return attribItems;
    }
    
    /**
     * 获取指定的属性
     * @param abName
     * @return
     */
    public static AttribItem getAbItemByName(String abName){
        AttribItem abItem = null;
        try {
            abItem = getService().getAttribItemByName(abName);
        } catch (VCIError e) {
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        }
        return abItem;
    }
    
    /**
     * 获取全部属性
     * @return
     */
    public static AttribItem[] getAllAbItems(){
        AttribItem[] abArray = null;
        try {
            abArray = getService().getAttribItems("", 1, 1);
        } catch (VCIError e) {
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        }
        return abArray;
    }
    /**
     * 获取属性的数据类型
     * @param abName
     * @return
     */
    public static String getAbItemDataType(String abName){
        String dataType = null;
        String abUpperName = abName.toUpperCase();
        //系统属性
        if(abUpperName.equals("OID") || abUpperName.equals("REVISIONOID") || abUpperName.equals("NAMEOID") || abUpperName.equals("BTMNAME")
                || abUpperName.equals("CREATOR") || abUpperName.equals("LASTMODIFIER") || abUpperName.equals("REVISIONRULE")
                || abUpperName.equals("VERSIONRULE") || abUpperName.equals("REVISIONVALUE") || abUpperName.equals("VERSIONVALUE")
                || abUpperName.equals("LCTID") || abUpperName.equals("LCSTATUS")
                || abUpperName.equals("ID") || abUpperName.equals("NAME") || abUpperName.equals("DESCRIPTION") 
                || abUpperName.equals("OWNER") || abUpperName.equals("CHECKINBY") 
                || abUpperName.equals("CHECKOUTBY") || abUpperName.equals("COPYFROMVERSION")
                || abUpperName.equals("ISLASTR") || abUpperName.equals("ISFIRSTR")
                || abUpperName.equals("ISLASTV") || abUpperName.equals("ISFIRSTV")
                || abUpperName.equals("F_OID") || abUpperName.equals("F_REVISIONOID") 
                || abUpperName.equals("F_NAMEOID") || abUpperName.equals("F_BTWNAME")
                || abUpperName.equals("T_OID") || abUpperName.equals("T_REVISIONOID") 
                || abUpperName.equals("T_NAMEOID") || abUpperName.equals("T_BTWNAME")){
            dataType = "VTString";
        }else if(abUpperName.equals("REVISIONSEQ") || abUpperName.equals("VERSIONSEQ")){
            dataType = "VTInteger";
        }else if(abUpperName.equals("CREATETIME") || abUpperName.equals("LASTMODIFYTIME")
                || abUpperName.equals("TS") || abUpperName.equals("CHECKINTIME") || abUpperName.equals("CHECKOUTTIME")){
            dataType = "VTDateTime";
        //属性池中属性
        }else{        
            try {
                dataType = getService().getAttribItemDataType(abName);
            } catch (VCIError e) {
                //e.printStackTrace();
                ClientLog4j.logger.error(e);
            }
        }
        return dataType;
    }
    
    /**
     * 提供(对外)属性池的数据文件数据
     * @return
     */
    public static String getAPData(){
        String content = "";
        try {
            content = getService().getAPData();
        } catch (VCIError e) {
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        }
        return content;
    }
 
    /**
     * 获取other中指定项目的值
     * @return
     */
    public static String getOtherValueByType(String other, String type){
        String[] otherArray = other.split(";");
        for(int i = 0; i < otherArray.length; i++){
            String otherValue = otherArray[i];
            if(otherValue.contains(type)){
                return otherValue.substring(otherValue.indexOf("=") + 2, otherValue.length());
            }
        }
        return null;
    }
    
    /**
     * 检查属性类型与属性值是否匹配
     * @param vtType
     * @param value
     * @return
     */
    public static boolean checkDataType(String vtType, String value){
        
        if(vtType.equals(VTDataType.VTSTRING)){
            try{
                String.valueOf(value);
            }catch(Exception e){
                return false;
            }
        }else if(vtType.equals(VTDataType.VTINTEGER)){
            try{
                Integer.valueOf(value);
            }catch(Exception e){
                return false;
            }
        }else if(vtType.equals(VTDataType.VTLONG)){
            try{
                Long.valueOf(value);
            }catch(Exception e){
                return false;
            }
        }else if(vtType.equals(VTDataType.VTDOUBLE)){
            try{
                Double.valueOf(value);
            }catch(Exception e){
                return false;
            }
        }
    
        return true;
    }
    
    public static boolean expData(String dir, AttribItem[] objs) {
        Set<String> enumNames = new HashSet<String>();
        List<EnumItem> enums = new ArrayList<EnumItem>();
        BufferedWriter bw = null;
        FileOutputStream fos = null;
        OutputStreamWriter osw =null;
        try{
            File file = new File(dir + "/att.txt");
            //将clob字段写到单独的文件中
            if(!file.exists()){
                new File(dir + "/att").mkdir();
            }
            //add by caill start 2015.12.23     导出时将txt文件设置为“utf-8”格式
            fos = new FileOutputStream(file,true);
            osw = new OutputStreamWriter(fos, "utf-8");
            bw = new BufferedWriter(osw);
            //add by caill end
            //w = new FileWriter(file, true);
            //bw = new BufferedWriter(w);
            for(AttribItem o : objs){
                OtherInfo otherInfo = OtherInfo.getOtherInfoByText(o.other);
                String enumName = otherInfo.getEnumName();
                if(enumName != null && !enumName.equals("")){
                    enumNames.add(enumName);
                }
                //若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 + "/att/" + o.oid + ".xml");
                    //add by caill start 2015.12.23 将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\" ?>");//gb2312
                    clobBW.write(getXmlText(o));
                    clobBW.flush();
                    //add by caill end    
                }catch(IOException e2){
                    e2.printStackTrace();
                }finally{
                    try{
                        if(clobBW != null){
                            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();
                        ClientLog4j.logger.error(e);
                    }
                }
            }
            bw.flush();
            for(String name : enumNames){
                EnumItem emItem = EnumProvider.getInstance().getService().getEmItemByName(name);
                enums.add(emItem);
            }
            boolean flag = EnumProvider.getInstance().expData(dir, enums.toArray(new EnumItem[0]));
            ClientLog4j.logger.info("**************属性池导出成功************");
            return true & flag;
        }catch(IOException e){
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        } catch (VCIError e) {
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        }finally{
            try {
                if(bw != null){
                    fos.close();    //add by caill 2015.12.23  将fos关闭
                    osw.close();    //add by caill 2015.12.23  将osw关闭
                    bw.close();
                }
            } catch (IOException e) {
                //e.printStackTrace();
                ClientLog4j.logger.error(e);
            }
        }
        ClientLog4j.logger.warn("**************属性池导出失败************");
        return false;
    }
    
    
    /**
     * 将对象转化成字符串
     * {oid:qqq, name:q}
     * @param o
     * @return
     */
    public static String getObjectText(AttribItem 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.DESCRIPTION + ":" + o.description + ",");
        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();
    }
    
    /**
     * 将att转化成xmltext
     * @param bt
     * @return
     */
    public static String getXmlText(AttribItem att) {
        StringBuilder stb = new StringBuilder("<attribute>");
        stb.append("<name>" + att.name + "</name>");
        stb.append("<label>" + att.label + "</label>");
        stb.append("<description>" + att.description + "</description>");
        stb.append("<vtDataType>" + att.vtDataType + "</vtDataType>");
        stb.append("<defValue>" + att.defValue + "</defValue>");
        stb.append("<rage>" + att.rage + "</rage>");
        stb.append("<other>" + att.other + "</other>");
        stb.append("</attribute>");
        return stb.toString();
    }
 
    public static List<AttribItem> getAttFromFile(String dir) {
        File file = new File(dir + "/att.txt");
        List<AttribItem> list = new ArrayList<AttribItem>();
        BufferedReader br = null;
        FileInputStream fo = null;
        InputStreamReader isw =null;
        try {
            /*r = new FileReader(file);
            br = new BufferedReader(r);*/
            //add by caill start 2015.12.23     导入时将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 + "/att/" + oid + ".xml");
                    if(!contentFile.exists()){
                        ClientLog4j.logger.info(dir + "/att/" + oid + ".xml不存在");
                        break;
                    }
                    AttribItem att = new AttribItem();
                    att.oid = oid;
                    att.name = map.get("name");
                    att.label = map.get("label");
                    att.description = map.get("description");
                    att.ts = map.get("ts");
                    att.creator = map.get("creator");
                    att.createTime = Long.valueOf(map.get("createTime"));
                    att.modifier = map.get("modifier");
                    att.modifyTime = Long.valueOf(map.get("modifyTime"));
                    SAXReader sa = new SAXReader();
                    //add by caill 2015.12.23    导入时将saxReader对象设置为“utf-8”格式
                    sa.setEncoding("utf-8");
                    Document document = sa.read(contentFile);
                    setAttValueFormDoc(att, document.getRootElement());
                    list.add(att);
                }else{
                    ClientLog4j.logger.info(dir + "att.txt中存在oid为空的错误记录");
                    break;
                }
            }
            return list;
        } catch (FileNotFoundException e) {
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        } catch (IOException e) {
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        } catch (DocumentException e) {
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        } finally{
            if(br != null){
                try {
                    fo.close();        //add by caill 2015.12.23    将fo关闭
                    isw.close();    //add by caill 2015.12.23    将isw关闭
                    br.close();
                } catch (IOException e) {
                    //e.printStackTrace();
                    ClientLog4j.logger.error(e);
                }
            }
        }
        return null;
    }
    /**
     * 将文件中一条记录解析成key-value
     * @param str
     * @return
     */
    public static 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 att
     * @param element
     */
    private static void setAttValueFormDoc(AttribItem att, Element element){
        String value = element.elementText("vtDataType");
        att.vtDataType = (value == null ? "" : value);
        value = element.elementText("defValue");
        att.defValue = (value == null ? "" : value);
        value = element.elementText("rage");
        att.rage = (value == null ? "" : value);
        value = element.elementText("other");
        att.other = (value == null ? "" : value);
    }
    
    public static boolean impData(String dir, AttribItem[] atts) {
        try {
            List<EnumItem> enums = EnumProvider.getInstance().getEnumsFromFile(dir);
            boolean enumFlag = EnumProvider.getInstance().impData(enums.toArray(new EnumItem[0]));
            if(!enumFlag){
                ClientLog4j.logger.info("属性池使用的枚举导入失败。导入中止。");
                return false;
            }
            for(AttribItem att : atts){
                AttribItem att_ = getService().getAttribItemByName(att.name);
                //已经存在
                if(!att_.oid.equals("")){
                    //类型一致则覆盖
                    if(att.vtDataType.equals(att_.vtDataType)){
                        getService().deleteAbItem(att_);
                        getService().addAttribItem(att);
                        ClientLog4j.logger.info(att.name + "在数据库中已经存在,且类型与文件中的一致。 覆盖。");
                    //类型不一致则中止导入
                    }else{
                        ClientLog4j.logger.info(att.name + "在数据库中已经存在,且类型与文件中的不一致。 中止导入。");
                        return false;
                    }
                }else{
                    getService().addAttribItem(att);
                }
            }
            return true;
        } catch (VCIError e) {
            //e.printStackTrace();
            ClientLog4j.logger.error(e);
        }
        return false;
    }
}