package com.vci.server.omd.common; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.dom4j.Element; import com.vci.corba.omd.atm.AttribItem; import com.vci.corba.omd.ltm.LinkType; import com.vci.corba.omd.ltm.LinkTypeService; import com.vci.corba.common.VCIError; import com.vci.server.base.utility.OmdHelper; import com.vci.server.cache.OMCacheProvider; import com.vci.server.omd.ddlTool.DDLHelper; import com.vci.server.omd.linktype.LinkTypeServiceImpl; /** * 获取属性的数据类型, 属性: 系统属性, 属性池属性 * */ public class LinkTypeHelper { private static LinkTypeHelper linkTypeHelper = null; /** * 链接类型table名的前缀 */ private static LinkTypeService linkTypeService = null; /** * oracle中table名的最大长度 */ // private static final int TABLEMAXLENTGH = 30; // 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"); // private String[] sysAttibutes = {"OID","Creator","CreateTime","LastModifier","LASTMODIFYTIME","F_OID","F_REVISIONOID","F_NAMEOID","F_BtwName", // "T_OID","T_REVISIONOID","T_NAMEOID","T_BtwName","TS"}; private final String otherFieldLt = "\n\tOID VARCHAR2(36) not null," + "\n\tCreator VARCHAR2(36),\n\tCreateTime TIMESTAMP,\n\tLastModifier VARCHAR2(36)," + "\n\tLastModifyTime TIMESTAMP,\n\tF_OID VARCHAR2(36) not null,\n\tF_REVISIONOID VARCHAR2(36)," + "\n\tF_NAMEOID VARCHAR2(36),\n\tF_BtwName VARCHAR2(36),\n\tT_OID VARCHAR2(36) not null,\n\tT_REVISIONOID VARCHAR2(36)," + "\n\tT_NAMEOID VARCHAR2(36),\n\tT_BtwName VARCHAR2(36),\n\tTS TIMESTAMP,\n\t"; // private AttribItem[] _sysAttrItems = null; // 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 LinkTypeProvider(); * 因此不能定义 私有的LinkTypeProvider(); * @return */ public static LinkTypeHelper getInstance(){ if(linkTypeHelper == null){ linkTypeHelper = new LinkTypeHelper(); } return linkTypeHelper; } /** * * @return */ public String getLTData(){ String content = ""; try { content = getService().getLTData(null); } catch (Exception e) { e.printStackTrace(); } return content; } /*** * * 获取所有链接类型对象数组 * */ public LinkType [] getLinkTypes(){ return OMCacheProvider.getLinkTypes(); // LinkType[] linkTypes = null; // try { // linkTypes = getService().getLinkTypes(null); // } catch (VCIError e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // return linkTypes; } /*** * * 根据链接类型名称获取链接类型对象 */ public LinkType getLinkTypeByName(String name){ return OMCacheProvider.getLinkType(name); // LinkType[] linkTypes = null; // try { // linkTypes = getService().getLinkTypes(null); // for(LinkType lt:linkTypes){ // if(lt.name.equals(name)){ // return lt; // } // } // } catch (VCIError e) { // e.printStackTrace(); // } // return null; } public LinkType[] getLinkTypeByBtmName(String btmName, String direction) { LinkType[] linkTypes = null; try { linkTypes = getService().getLinkTypeByBtmName(btmName, direction, null); } catch (VCIError e) { e.printStackTrace(); } return linkTypes; } public LinkTypeService getService(){ try { linkTypeService = new LinkTypeServiceImpl(); } catch (Exception e) { e.printStackTrace(); } return linkTypeService; } /** * 返回创建链接类型的sql * @param linkType * @return */ public String getCreateLtSql(LinkType linkType){ String tableName = OmdHelper.getLTTableName(linkType.name); String sql = "create table " + tableName + "(" + otherFieldLt; String[] abInfo = linkType.attributes; for(int i = 0; i < abInfo.length; i++){ com.vci.corba.omd.atm.AttribItem abItem = null; abItem = OMCacheProvider.getAttribute(abInfo[i]); String abSql = DDLHelper.getAbSql(abItem); sql += abSql; } sql = sql.substring(0, sql.lastIndexOf(",")); sql += "\n)"; return sql; } /** * 获取增加主键sql * @param typeName * @return */ public String getAddPKSql(String typeName){ return "alter table " + OmdHelper.getLTTableName(typeName) + " add constraint PKLT_" + typeName + " primary key (OID)\n"; } // public String getAbSql(AttribItem abItem){ // String sql = ""; // if(abItem == null){ // return sql; // } // String abName = abItem.name; // String vtType = abItem.vtDataType; // String other = abItem.other; // String defValue = abItem.defValue; // // if(vtType.equals("VTString")){ // int length = 50; // String lengthStr = getOtherValueByType(other, "length"); // if(lengthStr != null && !lengthStr.equals("")){ // length = Integer.valueOf(lengthStr); // } // sql += abName.toUpperCase() + " VARCHAR2(" + length + ")"; // if(!defValue.equals("")){ // sql += " default '" + defValue + "'"; // } // sql += ",\n\t"; // }else if(vtType.equals("VTInteger")){ // sql += abName.toUpperCase() + " NUMBER"; // if(!defValue.equals("")){ // sql += " default " + defValue; // } // sql += ",\n\t"; // }else if(vtType.equals("VTDouble")){ // int length = 20; // String lengthStr = getOtherValueByType(other, "length"); // if(lengthStr != null && !lengthStr.equals("")){ // length = Integer.valueOf(lengthStr); // } // // int accuracy = 2; // String accuracyStr = getOtherValueByType(other, "accuracy"); // if(accuracyStr != null && !accuracyStr.equals("")){ // accuracy = Integer.valueOf(accuracyStr); // } // sql += abName.toUpperCase() + " NUMBER(" + length + ", " + accuracy +")"; // if(!defValue.equals("")){ // sql += " default " + defValue; // } // sql += ",\n\t"; // }else if(vtType.equals("VTBoolean")){ // sql += abName.toUpperCase() + " VARCHAR2(8)"; // if(!defValue.equals("")){ // sql += " default '" + defValue + "'"; // } // sql += ",\n\t"; // }else if(vtType.equals("VTImage")){ // sql += abName.toUpperCase() + " VARCHAR2(255)"; // sql += ",\n\t"; // }else if(vtType.equals("VTDate")){ // sql += abName.toUpperCase() + " DATE"; // sql += ",\n\t"; // }else if(vtType.equals("VTTime")){ // sql += abName.toUpperCase() + " TIMESTAMP"; // sql += ",\n\t"; // }else if(vtType.equals("VTDateTime")){ // sql += abName.toUpperCase() + " TIMESTAMP"; // sql += ",\n\t"; // }else if(vtType.equals("VTNote")){ // sql += abName.toUpperCase() + " VARCHAR2(255)"; // sql += ",\n\t"; // }else if(vtType.equals("VTFilePath")){ // sql += abName.toUpperCase() + " VARCHAR2(255)"; // sql += ",\n\t"; // }else if(vtType.equals("VTClob")){ // sql += abName.toUpperCase() + " CLOB"; // sql += ",\n\t"; // } // // return sql; // } /** * 获取属性other中type的值 * @param other * @param type * @return */ // public String getOtherValueByType(String other, String type){ // String[] otherArray = other.split(";"); // for(int i = 0; i < otherArray.length; i++){ // String otherValue = otherArray[i]; // if(otherValue.contains(type)){ // return otherValue.substring(otherValue.indexOf("=") + 2, otherValue.length()); // } // } // return null; // // } // /** * Array-->List * @param array * @return */ // public List parseArrayToList(String[] array){ // List list = new ArrayList(); // for(int i = 0; i < array.length; i++){ // list.add(array[i]); // } // return list; // } /** * 指定的链接类型是否使用了指定的业务类型 * @param link * @param btmName * @return */ public boolean containsBtm(LinkType link, String btmName){ String[] btms = link.btmItemsFrom; for(int i = 0; i < btms.length; i++){ if(btmName.equalsIgnoreCase(btms[i])){ return true; } } btms = link.btmItemsTo; for(int i = 0; i < btms.length; i++){ if(btmName.equalsIgnoreCase(btms[i])){ return true; } } return false; } /** * * 获取链接类型的全部属性(包括系统属性)数组 * @return */ public List getAllAbNames(LinkType link){ List abList = new ArrayList(); for(String attr : OmdHelper.getLTSysAbItems()){ abList.add(attr); } String[] abNames = getAbNames(link); for(int i = 0; i < abNames.length; i++){ abList.add(abNames[i]); } return abList; } /** * 获取链接类型的属性(不包括系统属性)数组 * @param btmName * @return */ public String[] getAbNames(LinkType link){ return link.attributes; } /** * 获取链接类型对应的属性 * @param linkTypeName * @return */ public AttribItem[] getLTAbItems(String linkTypeName) { return OMCacheProvider.getAttribItemsByLinkType(linkTypeName); // LinkType linkType = getLinkTypeByName(linkTypeName); // if (linkType.attributes == null || linkType.attributes.length == 0) { // return new AttribItem[0]; // } // AttribItem[] abItems = AttributeHelper.getAttribItemsByNames(linkType.attributes); // // return abItems; } // // public boolean expData(String dir, LinkType[] lts){ // Set attNameSet = new HashSet(); // Set btNameSet = new HashSet(); // BufferedWriter bw = null; // FileOutputStream fos = null; // OutputStreamWriter osw =null; // try{ // File file = new File(dir + "/lt.txt"); // //将clob字段写到单独的文件中 // new File(dir + "/lt").mkdir(); // //add by caill start 2015.12.24 导出时将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(LinkType lt : lts){ // String[] apNames = lt.attributes; // if(apNames.length > 0){ // attNameSet.addAll(Arrays.asList(apNames)); // } // for(String btName : lt.btmItemsFrom){ // btNameSet.add(btName); // } // for(String btName : lt.btmItemsTo){ // btNameSet.add(btName); // } // //若str小于缓冲区大小, 将写到缓冲区; // //若str大于缓冲区大小, 将刷新缓冲区(将缓冲区内容写到底层流), 然后str直接写到底层流. // String text = getObjectText(lt); // bw.write(text); // bw.newLine(); // BufferedWriter clobBW = null; // FileOutputStream fo = null; // OutputStreamWriter pw =null; // try{ // File clobFile = new File(dir + "/lt/" + lt.oid + ".xml"); // //add by caill start 2015.12.24 将xml内容和xml文件格式统一 // fo = new FileOutputStream(clobFile); // pw = new OutputStreamWriter(fo, "utf-8"); // clobBW = new BufferedWriter(pw); // clobBW.write( ""); // clobBW.write(getXmlText(lt)); // clobBW.flush(); // //add by caill end // /*clobW = new FileWriter(clobFile); // clobBW = new BufferedWriter(clobW); // clobBW.write( "");//gb2312 // clobBW.write(getXmlText(lt)); // clobBW.flush();*/ // }catch(IOException e2){ // e2.printStackTrace(); // }finally{ // try{ // if(clobBW != null){ // fo.close(); //add by caill 2015.12.24 将fo关闭 // pw.close(); //add by caill 2015.12.24 将pw关闭 // clobBW.close(); // } // } catch (IOException e) { // e.printStackTrace(); // } // } // } // bw.flush(); // //导出属性 // AttribItem[] abs = AttributeHelper.getAttribItemsByNames(attNameSet.toArray(new String[0])); // boolean apFlag = AttributeHelper.expData(dir, abs); // // //导出业务类型 // List bts = new ArrayList(); // for(String btName : btNameSet){ // BtmItem bt = BtmHelper.getBtmItemByName(btName); // bts.add(bt); // } // boolean btFlag = BtmHelper.expData(dir, bts.toArray(new BtmItem[0])); // if(true & apFlag & btFlag){ // System.out.println("**************链接类型导出成功************"); // }else{ // System.out.println("**************链接类型导出失败************"); // } // return true & apFlag & btFlag; // }catch(IOException e){ // e.printStackTrace(); // }finally{ // try { // if(bw != null){ // fos.close(); //add by caill 2015.12.24 将fos关闭 // osw.close(); //add by caill 2015.12.24 将osw关闭 // bw.close(); // } // } catch (IOException e) { // e.printStackTrace(); // } // } // return false; // } // // /** // * 将对象转化成字符串 // * {oid:qqq, name:q} // * @param lt // * @return // */ // public String getObjectText(LinkType lt) { // StringBuilder stb = new StringBuilder("{"); // stb.append(OmdTools.OID + ":" + lt.oid + ","); // stb.append(OmdTools.NAME + ":" + lt.name + ","); // stb.append(OmdTools.LABEL + ":" + lt.tag + ","); // stb.append(OmdTools.DESCRIPTION + ":" + lt.description + ","); // stb.append(OmdTools.TS + ":" + lt.ts + ","); // stb.append(OmdTools.CREATOR + ":" + lt.creator + ","); // stb.append(OmdTools.CREATETIME + ":" + lt.createTime + ","); // stb.append(OmdTools.MODIFIER + ":" + lt.modifier + ","); // stb.append(OmdTools.MODIFYTIME + ":" + lt.modifyTime); // stb.append("}"); // return stb.toString(); // } // /** * 将LinkType lt转化成xmltext * @param bt * @return */ public String getXmlText(LinkType lt){ StringBuilder stb = new StringBuilder(""); stb.append("" + lt.name + ""); stb.append("" + lt.tag + ""); stb.append("" + lt.description + ""); for(String btmFrom : lt.btmItemsFrom){ stb.append("" + btmFrom + ""); } for(String btmTo : lt.btmItemsTo){ stb.append("" + btmTo + ""); } stb.append("" + lt.primitivesFrom + ""); stb.append("" + lt.primitivesTo + ""); stb.append("" + lt.relationFrom + ""); stb.append("" + lt.relationTo + ""); stb.append("" + lt.relation + ""); stb.append("" + lt.implClass + ""); stb.append("" + lt.shape + ""); for(String attibute : lt.attributes){ stb.append("" + attibute + ""); } stb.append(""); return stb.toString(); } // // public List getLTFromFile(String dir) { // File file = new File(dir + "/lt.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.24 导入时将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 + "/lt/" + oid + ".xml"); // if(!contentFile.exists()){ // System.out.println(dir + "/lt/" + oid + ".xml不存在"); // break; // } // LinkType lt = new LinkType(); // lt.oid = oid; // lt.name = map.get("name"); // lt.tag = map.get("label"); // lt.description = map.get("description"); // lt.ts = map.get("ts"); // lt.creator = map.get("creator"); // lt.createTime = map.get("createTime"); // lt.modifier = map.get("modifier"); // lt.modifyTime = map.get("modifyTime"); // SAXReader sa = new SAXReader(); // //add by caill 2015.12.24 导入时将saxReader对象设置为“utf-8”格式 // sa.setEncoding("utf-8"); // Document document = sa.read(contentFile); // setLTValueFormDoc(lt, document.getRootElement()); // list.add(lt); // }else{ // System.out.println(dir + "bt.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.24 将fo关闭 // isw.close(); //add by caill 2015.12.24 将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 att * @param element */ public void setLTValueFormDoc(LinkType lt, Element element){ List fromElements = element.elements("btmFrom"); List froms = new ArrayList(); for(Element fromElement : fromElements){ froms.add(fromElement.getText()); } lt.btmItemsFrom = froms.toArray(new String[0]); List toElements = element.elements("btmTo"); List tos = new ArrayList(); for(Element toElement : toElements){ tos.add(toElement.getText()); } lt.btmItemsTo = tos.toArray(new String[0]); String value = element.elementText("primitivesFrom"); lt.primitivesFrom = (value == null ? "" : value); value = element.elementText("primitivesTo"); lt.primitivesTo = (value == null ? "" : value); value = element.elementText("relationFrom"); lt.relationFrom = (value == null ? "" : value); value = element.elementText("relationTo"); lt.relationTo = (value == null ? "" : value); value = element.elementText("relation"); lt.relation = (value == null ? "" : value); value = element.elementText("implClass"); lt.implClass = (value == null ? "" : value); value = element.elementText("shape"); lt.shape = (value == null ? "" : value); List attElements = element.elements("attibute"); List atts = new ArrayList(); for(Element attElement : attElements){ atts.add(attElement.getText()); } lt.attributes = atts.toArray(new String[0]); } // // public boolean impData(String dir, LinkType[] lts) { // try { // List atts = AttributeHelper.getAttFromFile(dir); // boolean attFlag = AttributeHelper.impData(dir, atts.toArray(new AttribItem[0])); // if(!attFlag){ // System.out.println("链接类型使用的属性导入失败。导入中止。"); // return false; // } // // List bts = BtmHelper.getBTFromFile(dir); // boolean btFlag = BtmHelper.impData(dir, bts.toArray(new BtmItem[0])); // if(!btFlag){ // System.out.println("链接类型使用的业务类型导入失败。导入中止。"); // return false; // } // // for(LinkType lt : lts){ // LinkType lt_ = getService().getLinkType(lt.name); // //已经存在 // if(!lt_.oid.equals("")){ // getService().deleteLinkTypeNoCache(lt_); // getService().addLinkTypeNoCache(lt); // System.out.println(lt.name + "在数据库中已经存在, 覆盖。"); // }else{ // getService().addLinkTypeNoCache(lt); // } // } // return true; // } catch (VCIError e) { // e.printStackTrace(); // } // return false; // } /**将linktype导出为.sql文件 * add by caill 2015.12.22 * * */ public boolean expLinkSqlData(String dir, LinkType[] linkTypes) throws VCIError { new File(dir).mkdir(); File file = new File(dir + "/platformLink.sql"); FileWriter w = null;; BufferedWriter bw = null; try { w = new FileWriter(file); bw = new BufferedWriter(w); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(int i = 0; i < linkTypes.length; i++){ String sql = getLinkTableSqlByName(linkTypes[i]); try { bw.write(sql); bw.write("\r\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { bw.flush(); bw.close(); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } /** * 生成链接类型导出为.sql文件的sql语句 add by caill 2015.12.22 * @param linkType * @return */ public String getLinkTableSqlByName(LinkType linkType){ String tableName = OmdHelper.getLTTableName(linkType.name); String sql = "create table " + tableName +"(" + otherFieldLt; String[] abInfo = linkType.attributes; for(int i = 0; i < abInfo.length; i++){ com.vci.corba.omd.atm.AttribItem abItem = null; abItem = OMCacheProvider.getAttribute(abInfo[i]); String abSql = DDLHelper.getAbSql(abItem); sql += abSql; } sql = sql.substring(0, sql.lastIndexOf(",")); sql += "\n);"; sql += "\r\ncomment on table "+tableName+" is '"+ linkType.tag+"';"; //添加表注释 for(int i = 0; i < abInfo.length; i++){ com.vci.corba.omd.atm.AttribItem abItem = null; abItem = OMCacheProvider.getAttribute(abInfo[i]); //添加列注释 sql += "\r\ncomment on column "+tableName+"."+abItem.name+" is '"+ abItem.label +"';"; } return sql; } }