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
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
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.lcm.Bound;
import com.vci.corba.omd.lcm.LifeCycle;
import com.vci.corba.omd.lcm.LifeCycleService;
import com.vci.corba.omd.lcm.TransitionVO;
import com.vci.corba.omd.lcm.TransitionVOEvent;
import com.vci.server.cache.OMCacheProvider;
import com.vci.server.omd.lifecycle.LifeCycleServiceImpl;
 
public class LifeCycleHelper {
    private static LifeCycleService lifeCycleService = null;
    
    private static LifeCycleHelper 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 LifeCycleProvider();
     * 因此不能定义 私有的LifeCycleProvider();
     * @return
     */
    public static LifeCycleHelper getInstance(){
        if(instance == null){
            instance = new LifeCycleHelper();
        }
        return instance;
    }
    /***
     * 
     *  获取所有链接类型对象数组
     *  
     */
    public LifeCycle [] getLifeCyles(){
        return OMCacheProvider.getLifeCycles();
//        LifeCycle[] lifeCyles = null;
//        try {
//            lifeCyles = getService().getLifeCycles(null);
//        } catch (VCIError e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        return lifeCyles;
    }
    
    public LifeCycle getLifeCycleByName(String name){
        return OMCacheProvider.getLifeCycle(name);
//        LifeCycle[] lcs = null;
//        try {
//            lcs = getService().getLifeCycles(null);
//            
//            for (LifeCycle lc : lcs){
//                if (lc.name.equals(name))
//                    return lc; 
//            }
//        } catch (VCIError e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        return null;
    }
    
//    public LifeCycleService getService(){
//        try {
//            lifeCycleService = new LifeCycleServiceImpl();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return lifeCycleService;
//    }
    
    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, LifeCyle[] lcs){
//        Set<String> spNames = new HashSet<String>();
//        BufferedWriter bw = null;
//        FileOutputStream fos = null;
//        OutputStreamWriter osw =null;
//        try{
//            File file = new File(dir + "/lc.txt");
//            //将clob字段写到单独的文件中
//            new File(dir + "/lc").mkdir();
//            //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(LifeCyle lc : lcs){
//                for(Bound b : lc.bounds){
//                    spNames.add(b.name);
//                }
//                //若str小于缓冲区大小, 将写到缓冲区; 
//                //若str大于缓冲区大小, 将刷新缓冲区(将缓冲区内容写到底层流), 然后str直接写到底层流.
//                String text = getObjectText(lc);
//                bw.write(text);
//                bw.newLine(); 
//                BufferedWriter clobBW = null;
//                FileOutputStream fo = null;
//                OutputStreamWriter pw =null;
//                try{
//                    File clobFile = new File(dir + "/lc/" + lc.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(lc));
//                    clobBW.flush();
//                    //add by caill end
//                    /*clobW = new FileWriter(clobFile);
//                    clobBW = new BufferedWriter(clobW);
//                    clobBW.write( "<?xml version=\"1.0\" encoding=\"gb2312\" ?>");//gb2312
//                    clobBW.write(getXmlText(lc));
//                    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();
//            List<StatePool> sps = new ArrayList<StatePool>();
//            for(String name : spNames){
//                StatePool sp = StateHelper.getInstance().getService().getStatePool(name);
//                sps.add(sp);
//            }
//            boolean spFlag = StateHelper.getInstance().expData(dir, sps.toArray(new StatePool[0]));
//            if(true & spFlag){
//                System.out.println("**************生命周期导出成功************");
//            }else{
//                System.out.println("**************生命周期导出失败************");
//            }
//            
//            return true & spFlag;
//        }catch(IOException e){
//            e.printStackTrace();
//        } catch (VCIError 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();
//            }
//        }
//        return false;
//    }
//    
//    /**
//     * 将对象转化成字符串
//     * {oid:qqq, name:q}
//     * @param lc
//     * @return
//     */
//    public String getObjectText(LifeCyle lc) {
//        StringBuilder stb = new StringBuilder("{");
//        stb.append(OmdConstants.OID + ":" + lc.oid + ",");
//        stb.append(OmdConstants.NAME + ":" + lc.name + ",");
//        stb.append(OmdConstants.LABEL + ":" + lc.tag + ",");
//        stb.append(OmdConstants.DESCRIPTION + ":" + lc.description + ",");
//        stb.append(OmdConstants.TS + ":" + lc.ts + ",");
//        stb.append(OmdConstants.CREATOR + ":" + lc.creator + ",");
//        stb.append(OmdConstants.CREATETIME + ":" + lc.createTime + ",");
//        stb.append(OmdConstants.MODIFIER + ":" + lc.modifier + ",");
//        stb.append(OmdConstants.MODIFYTIME + ":" + lc.modifyTime);
//        stb.append("}");
//        return stb.toString();
//    }
    
    /**
     * 将LifeCyle转化成xmltext
     * @param bt
     * @return
     */
    public String getXmlText(LifeCycle lc){
        StringBuilder stb = new StringBuilder("<LifeCyle>");
        stb.append("<name>" + lc.name + "</name>");
        stb.append("<tag>" + lc.tag + "</tag>");
        stb.append("<description>" + lc.description + "</description>");
        stb.append("<startState>" + lc.startState + "</startState>");
        for(Bound b : lc.bounds){
            stb.append("<bound>");
            stb.append("<name>" + b.name + "</name>");
            stb.append("<cellx>" + b.cellx + "</cellx>");
            stb.append("<celly>" + b.celly + "</celly>");
            stb.append("<cellw>" + b.cellw + "</cellw>");
            stb.append("<cellh>" + b.cellh + "</cellh>");
            stb.append("<cellicon>" + b.cellicon + "</cellicon>");
            stb.append("</bound>");
        }
        
        for(TransitionVO t : lc.routes){
            stb.append("<transitionVO>");
            stb.append("<source>" + t.source + "</source>");
            stb.append("<destination>" + t.destination + "</destination>");
            stb.append("<connect>" + t.connect + "</connect>");
            for(TransitionVOEvent te : t.transitionVOEvents){
                stb.append("<transitionVOEvent>");
                stb.append("<id>" + te.id + "</id>");
                stb.append("<name>" + te.name + "</name>");
                stb.append("</transitionVOEvent>");
            }
            stb.append("</transitionVO>");
        }
        stb.append("</LifeCyle>");
        return stb.toString();
    }
//    
//    public List<LifeCyle> getLCFromFile(String dir) {
//        File file = new File(dir + "/lc.txt");
//        List<LifeCyle> list = new ArrayList<LifeCyle>();
//        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 + "/lc/" + oid + ".xml");
//                    if(!contentFile.exists()){
//                        System.out.println(dir + "/lc/" + oid + ".xml不存在");
//                        break;
//                    }
//                    LifeCyle lc = new LifeCyle();
//                    lc.oid = oid;
//                    lc.name = map.get("name");
//                    lc.tag = map.get("label");
//                    lc.description = map.get("description");
//                    lc.ts = map.get("ts");
//                    lc.creator = map.get("creator");
//                    lc.createTime = map.get("createTime");
//                    lc.modifier = map.get("modifier");
//                    lc.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);
//                    setLCValueFormDoc(lc, document.getRootElement());
//                    list.add(lc);
//                }else{
//                    System.out.println(dir + "lc.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.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 lc
     * @param element
     */
    public void setLCValueFormDoc(LifeCycle lc, Element element){
        String value = element.elementText("startState");
        lc.startState = (value == null ? "" : value);
        List<Element> bElements = element.elements("bound");
        List<Bound> bs = new ArrayList<Bound>();
        for(Element bElement : bElements){
            Bound b = new Bound();
            value = bElement.elementText("name");
            b.name = (value == null ? "" : value);
            value = bElement.elementText("cellx");
            b.cellx = (value == null ? "" : value);
            value = bElement.elementText("celly");
            b.celly = (value == null ? "" : value);
            value = bElement.elementText("cellw");
            b.cellw = (value == null ? "" : value);
            value = bElement.elementText("cellh");
            b.cellh = (value == null ? "" : value);
            value = bElement.elementText("cellicon");
            b.cellicon = (value == null ? "" : value);
            bs.add(b);
        }
        lc.bounds = bs.toArray(new Bound[0]);
        
        List<Element> tElements = element.elements("transitionVO");
        List<TransitionVO> ts = new ArrayList<TransitionVO>();
        for(Element tElement : tElements){
            TransitionVO t = new TransitionVO();
            value = tElement.elementText("source");
            t.source = (value == null ? "" : value);
            value = tElement.elementText("destination");
            t.destination = (value == null ? "" : value);
            value = tElement.elementText("connect");
            t.connect = (value == null ? "" : value);
            List<Element> teElements = tElement.elements("transitionVOEvent");
            List<TransitionVOEvent> tes = new ArrayList<TransitionVOEvent>();
            for(Element teElement : teElements){
                TransitionVOEvent te = new TransitionVOEvent();
                value = teElement.elementText("id");
                te.id = value;
                value = teElement.elementText("name");
                te.name = value;
                tes.add(te);
            }
            t.transitionVOEvents = tes.toArray(new TransitionVOEvent[0]);
            ts.add(t);
        }
        lc.routes = ts.toArray(new TransitionVO[0]);
    }
//    
//    public boolean impData(String dir, LifeCyle[] lcs) {
//        try {
//            List<StatePool> sps = StateHelper.getInstance().getSPFromFile(dir);
//            boolean spFlag = StateHelper.getInstance().impData(sps.toArray(new StatePool[0]));
//            if(!spFlag){
//                System.out.println("生命周期使用的状态池导入失败。导入中止。");
//                return false;
//            }
//            for(LifeCyle lc : lcs){
//                LifeCyle lc_ = getService().getLifeCycle(lc.name);
//                //已经存在
//                if(!lc_.oid.equals("")){
//                    //覆盖
//                    getService().deleteLifeCyle(lc_);
//                    getService().addLifeCyle(lc);
//                    System.out.println(lc.name + "在数据库中已经存在, 覆盖。");
//                }else{
//                    getService().addLifeCyle(lc);
//                }
//            }
//            return true;
//        } catch (VCIError e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
}