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 parseArrayToList(String[] array){ List list = new ArrayList(array.length); for(String a : array){ list.add(a); } return list; } // // public boolean expData(String dir, LifeCyle[] lcs){ // Set spNames = new HashSet(); // 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( ""); // clobBW.write(getXmlText(lc)); // clobBW.flush(); // //add by caill end // /*clobW = new FileWriter(clobFile); // clobBW = new BufferedWriter(clobW); // clobBW.write( "");//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 sps = new ArrayList(); // 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(""); stb.append("" + lc.name + ""); stb.append("" + lc.tag + ""); stb.append("" + lc.description + ""); stb.append("" + lc.startState + ""); for(Bound b : lc.bounds){ stb.append(""); stb.append("" + b.name + ""); stb.append("" + b.cellx + ""); stb.append("" + b.celly + ""); stb.append("" + b.cellw + ""); stb.append("" + b.cellh + ""); stb.append("" + b.cellicon + ""); stb.append(""); } for(TransitionVO t : lc.routes){ stb.append(""); stb.append("" + t.source + ""); stb.append("" + t.destination + ""); stb.append("" + t.connect + ""); for(TransitionVOEvent te : t.transitionVOEvents){ stb.append(""); stb.append("" + te.id + ""); stb.append("" + te.name + ""); stb.append(""); } stb.append(""); } stb.append(""); return stb.toString(); } // // public List getLCFromFile(String dir) { // File file = new File(dir + "/lc.txt"); // List list = new ArrayList(); // 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 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 getMapFormText(String str) { // str = str.trim(); // if(!str.startsWith("{") || !str.endsWith("}")){ // return null; // } // Map map = new HashMap(); // 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 bElements = element.elements("bound"); List bs = new ArrayList(); 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 tElements = element.elements("transitionVO"); List ts = new ArrayList(); 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 teElements = tElement.elements("transitionVOEvent"); List tes = new ArrayList(); 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 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; // } }