ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
package com.vci.server.omd.common;
 
 
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Element;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.stm.StatePool;
import com.vci.corba.omd.stm.StatePoolService;
import com.vci.server.cache.OMCacheProvider;
import com.vci.server.omd.statepool.StatePoolServiceImpl;
 
public class StateHelper {
 
    private static StatePoolService statePoolService = null;
    
    private static StateHelper instance = null;
//    public static DateFormat tsDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//    public static DateFormat timeDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//    public static final String OID = "oid";
//    public static final String NAME = "name";
//    public static final String LABEL = "label";
//    public static final String DESCRIPTION = "description";
//    public static final String TS = "ts";
//    public static final String CREATOR = "creator";
//    public static final String CREATETIME = "createTime";
//    public static final String MODIFIER = "modifier";
//    public static final String MODIFYTIME = "modifyTime";
    /**
     * 缓存已经调用new StatepoolProvider();
     * 因此不能定义 私有的StatepoolProvider();
     * @return
     */
    public static StateHelper getInstance(){
        if(instance == null){
            instance = new StateHelper();
        }
        return instance;
    }
    
    /***
     * 
     *  获取所有链接类型对象数组
     *  
     */
    public StatePool [] getStatePools(){
        return OMCacheProvider.getLifeStates();
//        StatePool[] statePools = null;
//        try {
//            statePools = getService().getStatePools(null);
//        } catch (VCIError e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        return statePools;
    }
    
//    public StatePoolService getService(){
//        try {
//            statePoolService = new StatePoolServiceImpl();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return statePoolService;
//    }
 
    public List<String> parseArrayToList(String[] array){
        List<String> list = new ArrayList<String>(array.length);
        for(String a : array){
            list.add(a);
        }
        return list;
    }
//    
//    public boolean expData(String dir, StatePool[] sps){
//        BufferedWriter bw = null;
//        FileOutputStream fos = null;
//        OutputStreamWriter osw =null;
//        try{
//            new File(dir).mkdir();
//            new File(dir + "/sp").mkdir();
//            File file = new File(dir + "/sp.txt");
//            //将clob字段写到单独的文件中
//            //add by caill start 2015.12.23     导出时将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(StatePool sp : sps){
//                //若str小于缓冲区大小, 将写到缓冲区; 
//                //若str大于缓冲区大小, 将刷新缓冲区(将缓冲区内容写到底层流), 然后str直接写到底层流.
//                String text = getObjectText(sp);
//                bw.write(text);
//                bw.newLine();
//                BufferedWriter clobBW = null;
//                FileOutputStream fo = null;
//                OutputStreamWriter pw =null;
//                try{                    
//                    File clobFile = new File(new File(dir).getPath() + "/sp/" + sp.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\" ?>");
//                    clobBW.write(getXmlText(sp));
//                    clobBW.flush();
//                    //pw.close();
//                    //add by caill end
//                    /*clobW = new FileWriter(clobFile);
//                    clobBW = new BufferedWriter(clobW);
//                    clobBW.write( "<?xml version=\"1.0\" encoding=\"gb2312\" ?>");//gb2312
//                    clobBW.write(getXmlText(sp));
//                    clobBW.flush();*/
//                }catch(IOException e2){
//                    e2.printStackTrace();
//                }finally{
//                    try{
//                        if(clobBW != null){
//                            fo.close();        //add by caill 2015.12.23 将fo关闭
//                            pw.close();        //add by caill 2015.12.23 将pw关闭
//                            clobBW.close();
//                        }
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//            bw.flush();
//            System.out.println("**************状态池导出成功************");
//            return true;
//        }catch(IOException e){
//            e.printStackTrace();
//        }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();
//            }
//        }
//        System.out.println("**************状态池导出失败************");
//        return false;
//    }
//    
//    /**
//     * 将对象转化成字符串
//     * {oid:qqq, name:q}
//     * @param sp
//     * @return
//     */
//    public String getObjectText(StatePool sp) {
//        StringBuilder stb = new StringBuilder("{");
//        stb.append(OmdConstants.OID + ":" + sp.oid + ",");
//        stb.append(OmdConstants.NAME + ":" + sp.name + ",");
//        stb.append(OmdConstants.LABEL + ":" + sp.tag + ",");
//        stb.append(OmdConstants.DESCRIPTION + ":" + sp.description + ",");
//        stb.append(OmdConstants.TS + ":" + sp.ts + ",");
//        stb.append(OmdConstants.CREATOR + ":" + sp.creator + ",");
//        stb.append(OmdConstants.CREATETIME + ":" + sp.createTime + ",");
//        stb.append(OmdConstants.MODIFIER + ":" + sp.modifier + ",");
//        stb.append(OmdConstants.MODIFYTIME + ":" + sp.modifyTime);
//        stb.append("}");
//        return stb.toString();
//    }
//    
    /**
     * 将statePool转化成xmltext
     * @param bt
     * @return
     */
    public String getXmlText(StatePool statePool) {
        StringBuilder stb = new StringBuilder("<statePool>");
        stb.append("<name>" + statePool.name + "</name>");
        stb.append("<tag>" + statePool.tag + "</tag>");
        stb.append("<imagePath>" + statePool.imagePath + "</imagePath>");
        stb.append("<description>" + statePool.description + "</description>");
        stb.append("</statePool>");
        return stb.toString();
    }
//
//    public List<StatePool> getSPFromFile(String dir) {
//        File file = new File(dir + "/sp.txt");
//        List<StatePool> list = new ArrayList<StatePool>();
//        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 + "/sp/" + oid + ".xml");
//                    if(!contentFile.exists()){
//                        System.out.println(dir + "/sp/" + oid + ".xml不存在");
//                        break;
//                    }
//                    StatePool sp = new StatePool();
//                    sp.oid = oid;
//                    sp.name = map.get("name");
//                    sp.tag = map.get("label");
//                    sp.description = map.get("description");
//                    sp.ts = map.get("ts");
//                    sp.creator = map.get("creator");
//                    sp.createTime = map.get("createTime");
//                    sp.modifier = map.get("modifier");
//                    sp.modifyTime = 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);
//                    setSPValueFormDoc(sp, document.getRootElement());
//                    list.add(sp);
//                }else{
//                    System.out.println(dir + "sp.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 {
//                    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();
//                }
//            }
//        }
//        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 sp
     * @param element
     */
    public void setSPValueFormDoc(StatePool sp, Element element){
        String value = element.elementText("imagePath");
        sp.imagePath = (value == null ? "" : value);
    }
//    
//    public boolean impData(StatePool[] sps) {
//        try {
//            for(StatePool sp : sps){
//                StatePool sp_ = getService().getStatePool(sp.name);
//                //已经存在
//                if(!sp_.oid.equals("")){
//                    //覆盖
//                    getService().deleteStatePool(sp_);
//                    getService().addStatePool(sp);
//                    System.out.println(sp.name + "在数据库中已经存在, 覆盖。");
//                }else{
//                    getService().addStatePool(sp);
//                }
//            }
//            return true;
//        } catch (VCIError e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
}