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
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
package com.vci.server.omd.lifecycle.service;
 
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
 
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
import com.vci.common.log.ServerWithLog4j;
import com.vci.common.utility.ObjectUtility;
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.TransationHistoryRecord;
import com.vci.corba.omd.lcm.TransitionVO;
import com.vci.omd.constants.OmdConstants;
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
import com.vci.server.omd.lifecycle.delegate.LifeCycleServerDelegate;
import com.vci.server.omd.common.LifeCycleHelper;
import com.vci.server.omd.lifecycle.LifeCycleServerUtil;
import com.vci.server.omd.lifecycle.LifeCycleServiceImpl;
 
public class LifeCycleService {
    private static LifeCycleService instance;
 
    private LifeCycleService() {
 
    }
 
    public static LifeCycleService getInstance() {
        if (instance == null) {
            instance = new LifeCycleService();
        }
 
        return instance;
    }
 
    public boolean addLifeCyle(LifeCycle lc) throws Exception {
        boolean flag = false;
        String insertSql = "insert into pllifecycle values(?,?,?,?,?,?,?,?,?,xmltype(?))";
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(insertSql);
 
        String id = lc.id;
        if (StringUtils.isBlank(id)) {
            id = ObjectUtility.getNewObjectID36();
            lc.id = id;
        }
        pst.setString(1, id);
        pst.setString(2, lc.name);
        pst.setString(3, lc.tag);
        pst.setString(4, lc.description);
        long time = Calendar.getInstance().getTimeInMillis();
        Timestamp ts = new Timestamp(time);
        pst.setTimestamp(5, ts);
        pst.setString(6, lc.creator);
        pst.setTimestamp(7, ts);
        pst.setString(8, lc.modifier);
        pst.setTimestamp(9, ts);
        String xmlText = LifeCycleHelper.getInstance().getXmlText(lc);
        // CLOB content =
        // LifeCycleServerDelegate.getInstance().getXmlTypeContent(xmlText, connection);
        // pst.setObject(10, content);
        pst.setString(10, xmlText);
        pst.executeUpdate();
        ServerWithLog4j.logger.debug(insertSql);
        pst.close();
        flag = true;
        return flag;
    }
 
    public boolean modifyLifeCyle(LifeCycle lc) throws Exception {
        boolean flag = false;
        String sql = "update pllifecycle t set t.name=?, t.label=?, t.description=?, t.ts=?, t.creator=?, t.createtime=?, t.modifier=?, t.modifytime=?, t.content=xmltype(?) where t.oid=? and t.ts = ?";
        Connection conn = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1, lc.name);
        pst.setString(2, lc.tag);
        pst.setString(3, lc.description);
        long time = Calendar.getInstance().getTimeInMillis();
        Timestamp ts = new Timestamp(time);
        pst.setTimestamp(4, ts);
        pst.setString(5, lc.creator);
        pst.setTimestamp(6, new Timestamp(lc.createTime));
        pst.setString(7, lc.modifier);
        pst.setTimestamp(8, ts);
        String xmlText = LifeCycleHelper.getInstance().getXmlText(lc);
        // CLOB content =
        // LifeCycleServerDelegate.getInstance().getXmlTypeContent(xmlText, conn);
        // pst.setObject(9, content);
        pst.setString(9, xmlText);
        pst.setString(10, lc.oid);
        pst.setTimestamp(11, Timestamp.valueOf(lc.ts));
        pst.executeUpdate();
        ServerWithLog4j.logger.debug(sql);
        pst.close();
        flag = true;
        return flag;
    }
 
    public boolean deleteLifeCyle(LifeCycle lc) throws Exception {
        boolean flag = false;
        String sql = "delete from pllifecycle where oid = ? and ts = ?";
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sql);
        pst.setString(1, lc.oid);
        String ts = lc.ts;
        pst.setTimestamp(2, Timestamp.valueOf(ts));
        pst.executeUpdate();
        ServerWithLog4j.logger.debug(sql);
        pst.close();
        flag = true;
        return flag;
    }
 
    /**
     * 删除生命周期
     * 
     * @throws Exception
     */
 
    public boolean deleteLifeCyles(LifeCycle[] lcs) throws Exception {
        boolean flag = false;
        for (LifeCycle lc : lcs) {
            deleteLifeCyle(lc);
        }
        flag = true;
        return flag;
    }
 
    public LifeCycle[] getLifeCyles() throws Exception {
        String sql = "";
 
        switch (HibernateSessionFactory.getDbType()) {
        case DM8:
            sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, content from pllifecycle t";
            break;
        case ORACL:
        default:
            sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from pllifecycle t";
            break;
        }
 
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        ServerWithLog4j.logger.debug(sql);
        List<LifeCycle> lcs = new ArrayList<LifeCycle>();
        while (rs.next()) {
            LifeCycle lc = resultSetToLifeCycle(rs);
            lcs.add(lc);
        }
        rs.close();
        pst.close();
        return lcs.toArray(new LifeCycle[0]);
    }
 
    /*
     * public String getLifeCycleEventPath() throws VCIError { return
     * LifeCycleServerDelegate.getInstance().getLifeCycleEventPath(); }
     */
    public String getLifeCycleEventPath() throws VCIError {
        String classPath = LifeCycleServiceImpl.class.getResource("").getPath();
        String subPath;
        String filePath;
        if (classPath.contains("file:")) {
            subPath = classPath.substring(classPath.indexOf("file:") + 6,
                    classPath.indexOf("plm-lifeCycle-server.jar"));
            filePath = subPath + "properties/lce.properties";
        } else {
            subPath = classPath.substring(1, classPath.indexOf("LifeCycle"));
            filePath = subPath + "LifeCycle/properties/lce.properties";
        }
        return handlerOS(filePath);
    }
 
    /**
     * 处理不同OS的路径格式不同问题
     * 
     * @param filePath
     * @return
     */
    public String handlerOS(String filePath) {
        String osName = System.getProperty("os.name");
        // unix, linux
        if (!osName.startsWith("Win")) {
            filePath = "/" + filePath.replace("\\", "/");
        }
        return filePath;
    }
 
    /*
     * public String getLifeCycleEventViewPath() throws VCIError { return
     * LifeCycleServerDelegate.getInstance().getLifeCycleEventViewPath(); }
     */
    public String getLifeCycleEventViewPath() throws VCIError {
        String classPath = LifeCycleServiceImpl.class.getResource("").getPath();
        String subPath;
        String filePath;
        if (classPath.contains("file:")) {
            subPath = classPath.substring(classPath.indexOf("file:") + 6,
                    classPath.indexOf("plm-lifeCycle-server.jar"));
            filePath = subPath + "properties/lce_view.properties";
        } else {
            subPath = classPath.substring(1, classPath.indexOf("LifeCycle"));
            filePath = subPath + "LifeCycle/properties/lce_view.properties";
        }
        return handlerOS(filePath);
    }
 
    /*
     * public String getLifeCycleEventViewSavePath() throws VCIError { return
     * LifeCycleServerDelegate.getInstance().getLifeCycleEventViewSavePath(); }
     */
    public String getLifeCycleEventViewSavePath() throws VCIError {
        String classPath = LifeCycleServiceImpl.class.getResource("").getPath();
        String subPath;
        String filePath;
        if (classPath.contains("file:")) {
            subPath = classPath.substring(classPath.indexOf("file:") + 6,
                    classPath.indexOf("plm-lifeCycle-server.jar"));
            filePath = subPath + "properties/lce_view_save.properties";
        } else {
            subPath = classPath.substring(1, classPath.indexOf("LifeCycle"));
            filePath = subPath + "LifeCycle/properties/lce_view_save.properties";
        }
        return handlerOS(filePath);
    }
 
    /*
     * public String get_lifecycle_event_path() throws VCIError { try{ return
     * LifeCycleServerDelegate.getInstance().get_lifecycle_event_path();
     * }catch(Throwable e){ e.printStackTrace(); throw
     * getLocalVciError("P0010LIFECYCLE-00009", e); } }
     */
    /**
     * 根据jar包中文件路径获取
     * 
     * @return
     * @throws VCIError
     */
    public String get_lifecycle_event_path() throws VCIError {
        String classPath = LifeCycleServerDelegate.class.getResource("").getPath();
        String subPath;
        String filePath = "";
        // String curProjectPath = System.getProperty("user.dir");
        // ProjectEnum pe = getFileNameByPrjPath(curProjectPath);
 
        if (classPath.contains("file:")) {
            subPath = classPath.substring(classPath.indexOf("file:") + 6,
                    classPath.indexOf("plm-lifeCycle-server.jar"));
            filePath = subPath + "properties/lce_events.properties";
        } else {
            subPath = classPath.substring(1, classPath.indexOf("LifeCycle"));
            filePath = subPath + "LifeCycle/properties/lce_events.properties";
            // if (pe != null) {
            // subPath = classPath.substring(1, classPath.indexOf(pe.prjName));
            // filePath = subPath + pe.prjName + "/src/properties/" +
            // pe.fileName;
            // }
        }
        return handlerOS(filePath);
    }
 
    // 跃迁记录
    public boolean recordTransitionHistory(TransationHistoryRecord transationHistoryRecord) throws Throwable {
        try {
            return false;
        } catch (Throwable e) {
            throw e;
        }
    }
 
    /**
     * 获取事件key
     * 
     * @throws Throwable
     */
 
    public String[] getLCEventKeys() throws Throwable {
        // add by caill start 2016.3.10 获取LifeCycleServerUtil.java中的所有key.
        Map<String, String> allLifyCycleFileName = LifeCycleServerUtil.getInstance().getAllLifeEventConf();
        String[] str = new String[allLifyCycleFileName.size()];
        Set<Entry<String, String>> entrySet = allLifyCycleFileName.entrySet();
        Iterator<Entry<String, String>> it = entrySet.iterator();
        int sign = 0;
        while (it.hasNext()) {
            Entry<String, String> element = it.next();
            String key = element.getKey().trim();
            str[sign] = key;
            ServerWithLog4j.logger.debug(element.getKey());
            sign++;
        }
        // add by caill end
 
        return str;
    }
 
    /**
     * 获取事件value
     * 
     * @throws Throwable
     */
 
    public String getLCEventValueByKey(String key) throws Throwable {
        try {
            String value = "";
            // add by caill start 2016.3.10根据propertity文件中的key获取value
            Map<String, String> allLifyCycleFileName = LifeCycleServerUtil.getInstance().getAllLifeEventConf();
            value = allLifyCycleFileName.get(key);
            // add by caill end
            return value;
        } catch (Throwable e) {
            throw e;
        }
    }
 
    public LifeCycle getLifeCycle(String name) throws Exception {
        String sql = "";
        switch (HibernateSessionFactory.getDbType()) {
        case DM8:
            sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, content from pllifecycle t where t.name =?";
            break;
        case ORACL:
        default:
            sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from pllifecycle t where t.name =?";
            break;
        }
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sql);
        pst.setString(1, name);
        ResultSet rs = pst.executeQuery();
        ServerWithLog4j.logger.debug(sql);
        LifeCycle lc = null;
        while (rs.next()) {
            lc = resultSetToLifeCycle(rs);
        }
        rs.close();
        pst.close();
        if (lc == null) {
            lc = new LifeCycle();
            lc.bounds = new Bound[0];
            lc.routes = new TransitionVO[0];
        }
        return lc;
    }
 
    public boolean xml2DB(String userName) throws VCIError {
//        List<LifeCyle> news = Xml2DBDelegate.getInstance().getNews(userName);
//        if (news == null) {
//            return true;
//        }
//        for (LifeCyle o : news) {
//            try {
//                addLifeCyle(o);
//            } catch (Throwable e) {
//                e.printStackTrace();
//                ServerWithLog4j.logger.warn(o.name + "迁移失败, 生命周期的迁移中止!");
//                return false;
//            }
//        }
        return true;
    }
 
    // 根据id查询状态
    public LifeCycle getLifeCycleByOid(String oid) throws Exception {
        String sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from pllifecycle t where t.oid =?";
        Connection connection = HibernateSessionFactory.getSessionConnection();
        PreparedStatement pst = connection.prepareStatement(sql);
        pst.setString(1, oid);
        ResultSet rs = pst.executeQuery();
        ServerWithLog4j.logger.debug(sql);
        LifeCycle lc = null;
        while (rs.next()) {
            lc = resultSetToLifeCycle(rs);
        }
        rs.close();
        pst.close();
        /*
         * if(lc == null){ lc = new LifeCyle(); lc.bounds = new Bound[0]; lc.routes =
         * new TransitionVO[0]; }
         */
        return lc;
    }
    
    /**
     * 将一条数据库中的记录转化成btmitem
     * @param rs
     * @return
     * @throws SQLException
     */
    private LifeCycle resultSetToLifeCycle(ResultSet rs) throws SQLException, DocumentException{
        LifeCycle lc = new LifeCycle();
        String value = rs.getString("oid");
        lc.oid = (value == null ? "" : value);
        value = rs.getString("name");
        lc.name = (value == null ? "" : value);
        value = rs.getString("label");
        lc.tag = (value == null ? "" : value);
        value = rs.getString("description");
        lc.description = (value == null ? "" : value);
        lc.ts = OmdConstants.tsDF.format(rs.getTimestamp("ts"));
        value = rs.getString("creator");
        lc.creator = (value == null ? "" : value);
        lc.createTime = rs.getTimestamp("createTime").getTime();
        value = rs.getString("modifier");
        lc.modifier = (value == null ? "" : value);
        lc.modifyTime = rs.getTimestamp("modifyTime").getTime();
        //CLOB clob = (CLOB) rs.getClob("content");
        Clob clob = rs.getClob("content");
        Element lcDetails = getLCDetails(clob);
        LifeCycleHelper.getInstance().setLCValueFormDoc(lc, lcDetails);
        return lc;
    }
    
    /**
     * 将查询的clob对象转换成xml的element, 便于解析
     * @param clob
     * @return
     * @throws SQLException
     * @throws IOException
     * @throws DocumentException
     */
    //public Element getLCDetails(CLOB clob) throws DocumentException, SQLException{
    private Element getLCDetails(Clob clob) throws DocumentException, SQLException{
        SAXReader saxReader = new SAXReader();
        //Document document = saxReader.read(clob.characterStreamValue());
        Document document = saxReader.read(clob.getCharacterStream());
        Element root = document.getRootElement();
        return root;
    }
}