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 lcs = new ArrayList(); 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 allLifyCycleFileName = LifeCycleServerUtil.getInstance().getAllLifeEventConf(); String[] str = new String[allLifyCycleFileName.size()]; Set> entrySet = allLifyCycleFileName.entrySet(); Iterator> it = entrySet.iterator(); int sign = 0; while (it.hasNext()) { Entry 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 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 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; } }