package com.vci.server.omd.attribpool; import java.io.IOException; import java.sql.Clob; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; //import org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor; import com.vci.omd.constants.AttributeConstants; import com.vci.omd.constants.OmdConstants; import com.vci.server.base.persistence.dao.HibernateSessionFactory; import com.vci.server.base.utility.AttributeHelper; import com.vci.server.omd.enumtype.service.EnumService; import com.vci.corba.omd.atm.AttribItem; import com.vci.corba.omd.etm.EnumItem; public class APServiceImplHelper { private static APServiceImplHelper helper = null; private APServiceImplHelper(){ } public static APServiceImplHelper getInstance(){ if(helper == null){ helper = new APServiceImplHelper(); } return helper; } /** * 根据属性名返回属性项 * @throws Throwable */ public AttribItem getAttribItemByName(String abName) throws Throwable{ try{ //String sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plattribute t where t.name =?"; String sql = ""; switch (HibernateSessionFactory.getDbType()) { case DM8: sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, content from plattribute t where t.name =?"; break; case ORACL: default: sql = "select oid, name, label, description, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plattribute t where t.name =?"; break; } Connection connection = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = connection.prepareStatement(sql); pst.setString(1, abName); ResultSet rs = pst.executeQuery(); AttribItem att = null; while(rs.next()){ att = getAttribute(rs); } rs.close(); pst.close(); if(att == null){ att = new AttribItem(); } return att; }catch(Throwable e){ throw e; } } /** * 根据属性名数组获取属性名对应的枚举数组 * 当属性名数组为空时, 返回全部枚举 * @param abNames * @return * @throws Throwable */ public EnumItem[] getEnumsByAbNames(String[] abNames) throws Throwable{ try{ if(abNames == null || abNames.length == 0){ return EnumService.getInstance().getEmItems("", 1, 1); }else { List emList = new ArrayList(); for(int i = 0; i < abNames.length; i++){ EnumItem em = getEMByAbName(abNames[i]); if(em == null){ continue; } emList.add(em); } return emList.toArray(new EnumItem[0]); } }catch(Throwable e){ throw e; } } /** * 根据属性名获取对应的枚举 * @param abName * @return * @throws Throwable */ public EnumItem getEMByAbName(String abName) throws Throwable{ try{ if(abName == null || abName.equals("")){ return null; } AttribItem abItem = getAttribItemByName(abName); String other = abItem.other; String enumName = AttributeHelper.getOtherValueByType(other, AttributeConstants.ENUMNAME); return EnumService.getInstance().getEmItemByName(enumName); }catch(Throwable e){ throw e; } } /** * 获取other中指定项目的值 * * @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; // } /** * 将att转化成xmltext * @param bt * @return */ public String getXmlText(AttribItem att) { return AttributeHelper.getXmlText(att); } /** * 将xmlText内容写入到临时的CLOB对象中 * @param xmlText * @param connection * @return * @throws SQLException * @throws IOException */ // public CLOB getXmlTypeContent(String xmlText, Connection connection) throws SQLException, IOException { // C3P0NativeJdbcExtractor c3p0NativeJdbcExtractor = new C3P0NativeJdbcExtractor(); // Connection nativeConnection = c3p0NativeJdbcExtractor.getNativeConnection(connection); // CLOB clob = CLOB.createTemporary(nativeConnection, false, CLOB.DURATION_SESSION); // clob.open(CLOB.MODE_READWRITE); // Writer clobWriter = clob.setCharacterStream(1000); // clobWriter.write(xmlText); // clobWriter.flush(); // clobWriter.close(); // clob.close(); // return clob; // } /** * 将查询的clob对象转换成xml的element, 便于解析 * @param clob * @return * @throws SQLException * @throws IOException * @throws DocumentException */ public Element getAttributeDetails(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; } /** * 将一条数据库中的记录转化成AttribItem * @param rs * @return * @throws SQLException */ public AttribItem getAttribute(ResultSet rs) throws SQLException, IOException, DocumentException{ AttribItem att = new AttribItem(); String value = rs.getString("oid"); att.oid = (value == null ? "" : value); value = rs.getString("name"); att.name = (value == null ? "" : value); value = rs.getString("label"); att.label = (value == null ? "" : value); value = rs.getString("description"); att.description = (value == null ? "" : value); att.ts = OmdConstants.tsDF.format(rs.getTimestamp("ts")); value = rs.getString("creator"); att.creator = (value == null ? "" : value); att.createTime = rs.getTimestamp("createTime").getTime(); value = rs.getString("modifier"); att.modifier = (value == null ? "" : value); att.modifyTime = rs.getTimestamp("modifyTime").getTime(); //CLOB clob = (CLOB) rs.getClob("content"); Clob clob = rs.getClob("content"); Element attDetails = getAttributeDetails(clob); // ApProvider.getInstance().setAttValueFormDoc(att, attDetails); setAttValueFormDoc(att, attDetails); return att; } private void setAttValueFormDoc(AttribItem att, Element element){ String value = element.elementText("vtDataType"); att.vtDataType = (value == null ? "" : value); value = element.elementText("defValue"); att.defValue = (value == null ? "" : value); value = element.elementText("rage"); att.rage = (value == null ? "" : value); value = element.elementText("other"); att.other = (value == null ? "" : value); } }