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 parseArrayToList(String[] array){ List list = new ArrayList(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( ""); // clobBW.write(getXmlText(sp)); // clobBW.flush(); // //pw.close(); // //add by caill end // /*clobW = new FileWriter(clobFile); // clobBW = new BufferedWriter(clobW); // clobBW.write( "");//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(""); stb.append("" + statePool.name + ""); stb.append("" + statePool.tag + ""); stb.append("" + statePool.imagePath + ""); stb.append("" + statePool.description + ""); stb.append(""); return stb.toString(); } // // public List getSPFromFile(String dir) { // File file = new File(dir + "/sp.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 + "/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 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 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; // } }