package com.vci.server.bof.service; import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.sql.Clob; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; import java.sql.Types; import java.util.HashMap; import java.util.Map; import org.hibernate.SQLQuery; import com.vci.corba.common.VCIError; import com.vci.corba.omd.atm.AttribItem; import com.vci.corba.omd.lcm.LifeCycle; import com.vci.omd.constants.BusinessConstants; import com.vci.server.cache.OMCacheProvider; import com.vci.common.log.ServerWithLog4j; public class BOFactoryBaseService { public static String DATEFORMATTER = "yyyy-mm-dd hh24:mi:ss.ff"; public static String DATEFORMATTER_YMD = "yyyy-mm-dd"; /** * 获取业务类型的所有属性 * @param boName * @return * @throws Exception * @throws Throwable */ public AttribItem[] getBOAttributeItem(String boName) throws Exception, Throwable { ServerWithLog4j.logger.debug("getBOAttributeItem boName = " + boName); //OMDServicePrx omd = ServerServiceProvider.getOMDService(); //AttribItem[] items = BtmProvider.getInstance().getBtAbItems(boName); //String[] attrs = omd.getBTMService().getBtmApNameArray(boName); //LinkTypeProvider.getInstance().getLTAbItems(loName); //AttribItem[] items = omd.getAttributeService().getAttribItemsByNames(attrs); String[] attrs = OMCacheProvider.getBTAttributes(boName); AttribItem[] items = new AttribItem[attrs.length]; for (int i = 0; i < attrs.length; i++) { items[i] = OMCacheProvider.getAttribute(attrs[i]); } return items; } public Map getBOAttributeItemMap(String boName) throws Exception, Throwable { ServerWithLog4j.logger.debug("getBOAttributeItemMap boName = " + boName); AttribItem[] items = getBOAttributeItem(boName);//BtmProvider.getInstance().getBtAbItems(boName); Map itemMap = new HashMap(); for (int i = 0; i < items.length; i++) { itemMap.put(items[i].name.toLowerCase(), items[i]); } return itemMap; } public AttribItem[] getLOAttributeItem(String loName) throws Exception, Throwable { //OMDServicePrx omd = ServerServiceProvider.getOMDService(); //LinkType lt = omd.getLinkTypeService().getLinkType(loName); //String[] attrs = ltService.getLTNamesByAPName(loName); //LinkTypeProvider.getInstance().getLTAbItems(loName); //AttribItem[] items = omd.getAttributeService().getAttribItemsByNames(lt.attributes); AttribItem[] items = null; String[] attrs = OMCacheProvider.getLTAttributes(loName); if (attrs != null) { int count = attrs.length; items = new AttribItem[count]; for (int i = 0; i < count; i++) { items[i] = OMCacheProvider.getAttribute(attrs[i]); } } return items; } public Map getLOAttributeItemMap(String loName) throws Exception, Throwable { AttribItem[] items = getLOAttributeItem(loName);//LinkTypeProvider.getInstance().getLTAbItems(loName); Map itemMap = new HashMap(); for (int i = 0; i < items.length; i++) { itemMap.put(items[i].name.toLowerCase(), items[i]); } return itemMap; } public Map getBOAttributeItemDefaultValMap(String boName) throws Exception, Throwable { Map itemMap = new HashMap(); AttribItem[] items = getBOAttributeItem(boName); if (items != null) { for (int i = 0; i < items.length; i++) { itemMap.put(items[i].name.toLowerCase(), items[i].defValue); } } return itemMap; } public Map getBOAttributeItemDefaultValMap(String boName, Map attrNameMap) throws Exception, Throwable { Map itemMap = new HashMap(); AttribItem[] items = getBOAttributeItem(boName); if (items != null) { for (int i = 0; i < items.length; i++) { itemMap.put(items[i].name, items[i].defValue); attrNameMap.put(items[i].name.toLowerCase(), items[i]); } } return itemMap; } public Map getLOAttributeItemDefaultValMap(String loName) throws Exception, Throwable { Map itemMap = new HashMap(); AttribItem[] items = getLOAttributeItem(loName); if (items != null) { for (int i = 0; i < items.length; i++) { itemMap.put(items[i].name.toLowerCase(), items[i].defValue); } } return itemMap; } protected boolean isBOAttribute(String attrName, Map attrMap) { if (attrMap.containsKey(attrName.toLowerCase()) || attrMap.containsKey(attrName.toUpperCase()) || BusinessConstants.isBusinessConstants(attrName.toUpperCase())) { return true; } else { return false; } } /** * 根据类型名称获取对应的表名称 * * @param btmName * @return */ // public String getTableName(String btmName) { // return OmdTools.getBTTableName(btmName); // } /** * 获取去初始版本 * @param verRuleName * @return */ public String getVersionValue(int verRuleName) { String value = "0"; if (verRuleName == 0) { value = "1"; } else if (verRuleName == 1){ value = "a"; } else if (verRuleName == 2) { value = "0"; } return value; } /** * 根据生命周期名称获取第一个生命周期节点 * @param lifeCycleName * @return * @throws VCIError */ public String getLcFirstStatus(String lifeCycleName) throws VCIError { try { //LifeCyle[] lifeCycls = LifeCycleProvider.getInstance().getLifeCyles(); //LifeCycle lifeCycle = ServerServiceProvider.getOMDService().getLifeCycleService().getLifeCycle(lifeCycleName); LifeCycle lifeCycle = OMCacheProvider.getLifeCycle(lifeCycleName); if (lifeCycle.name.equals(lifeCycleName)) { return lifeCycle.startState; } } catch (Exception e) { e.printStackTrace(); throw new VCIError(); } catch (Throwable e) { e.printStackTrace(); throw new VCIError(); } return ""; } /** * 根据类型构建插入的sql语句 * * @param attrItem * ,格式 * @param value * ,值 * @return */ public String getSqlAccordingType(AttribItem attrItem, String value) { StringBuilder sqlBuffer = new StringBuilder(); if (value == null || value.trim().equals("")) { sqlBuffer.append("'").append(value).append("'"); return sqlBuffer.toString(); } if (attrItem.vtDataType.equals("VTInteger") || attrItem.vtDataType.equals("VTLong") || attrItem.vtDataType.equals("VTDouble")) { sqlBuffer.append(value); } else if (attrItem.vtDataType.equals("VTDate")) { sqlBuffer.append("to_date('").append(value).append("','") .append(DATEFORMATTER_YMD).append("')"); } else if (attrItem.vtDataType.equals("VTTime") || attrItem.vtDataType.equals("VTDateTime")) { sqlBuffer.append("to_timestamp('").append(value).append("','") .append(DATEFORMATTER).append("')"); } else { sqlBuffer.append("'").append(value).append("'"); } return sqlBuffer.toString(); } public void setQueryValueAccordingDataType(SQLQuery query, AttribItem attrItem, int index, String value) { if (value == null || value.trim().equals("")) { query.setString(index, value); return; } if (attrItem.vtDataType.equals("VTString")) { query.setString(index, value); } else if (attrItem.vtDataType.equals("VTInteger")) { query.setInteger(index, Integer.valueOf(value).intValue()); } else if (attrItem.vtDataType.equals("VTLong")) { query.setLong(index, Long.valueOf(value).longValue()); } else if (attrItem.vtDataType.equals("VTDouble")) { query.setDouble(index, Double.valueOf(value).doubleValue()); } else if (attrItem.vtDataType.equals("VTBoolean")) { query.setString(index, value); } else if (attrItem.vtDataType.equals("VTImage")) { query.setString(index, value); } else if (attrItem.vtDataType.equals("VTDate")) { if (value.isEmpty()) query.setParameter(index, null); else{ if(value.contains(":") && value.contains(" ")){ value = value.substring(0,value.indexOf(" ")); } query.setDate(index, Date.valueOf(value)); } //query.setDate(index, Date.valueOf(value)); } else if (attrItem.vtDataType.equals("VTTime")) { if (value.isEmpty()) query.setParameter(index, null); else query.setTimestamp(index, Timestamp.valueOf(value)); } else if (attrItem.vtDataType.equals("VTDateTime")) { if (value.isEmpty()) query.setParameter(index, null); else query.setTimestamp(index, Timestamp.valueOf(value)); } else if (attrItem.vtDataType.equals("VTNote")) { query.setString(index, value); } else if (attrItem.vtDataType.equals("VTFilePath")) { query.setString(index, value); } else if (attrItem.vtDataType.equals("VTClob")) { query.setString(index, value); } else { query.setString(index, value); } } public void setQueryValueAccordingDataType(PreparedStatement pst, AttribItem attrItem, int index, String value) throws SQLException { // 本段有严重的缺陷 // 对于VTInteger\VTLong\VTDobule类型时, // 如果value为空时,setString(index, value) 相当于为数值列设置了string类型的值, // 此时执行时会抛出异常 // 方案:删除此段,改取属性池定义的默认值, // 如果 定义的默认值为空时,则给0 // if (value == null || value.trim().equals("")) { // pst.setString(index, value); // return; // } if (value == null){ pst.setNull(index, getSqlType(attrItem)); return; } if (attrItem.vtDataType.equals("VTString")) { pst.setString(index, value); } else if (attrItem.vtDataType.equals("VTInteger")) { Integer val = isEmptyOrNull(value) ? (Integer)getDefVal(attrItem) : Integer.valueOf(value).intValue(); pst.setInt(index, val); } else if (attrItem.vtDataType.equals("VTLong")) { Long val = isEmptyOrNull(value) ? (Long)getDefVal(attrItem) : Long.valueOf(value).longValue(); pst.setLong(index, val); } else if (attrItem.vtDataType.equals("VTDouble")) { Double val = isEmptyOrNull(value) ? (Double)getDefVal(attrItem) : Double.valueOf(value).doubleValue(); pst.setDouble(index, val); } else if (attrItem.vtDataType.equals("VTBoolean")) { pst.setString(index, value); } else if (attrItem.vtDataType.equals("VTImage")) { pst.setString(index, value); } else if (attrItem.vtDataType.equals("VTDate")) { if (value.isEmpty()) pst.setNull(index, Types.DATE); else{ if(value.contains(":") && value.contains(" ")){ value = value.substring(0,value.indexOf(" ")); } pst.setDate(index, Date.valueOf(value)); } } else if (attrItem.vtDataType.equals("VTTime")) { if (value.isEmpty()) pst.setNull(index, Types.TIMESTAMP); else pst.setTimestamp(index, Timestamp.valueOf(value)); } else if (attrItem.vtDataType.equals("VTDateTime")) { if (value.isEmpty()) pst.setNull(index, Types.TIMESTAMP); else pst.setTimestamp(index, Timestamp.valueOf(value)); } else if (attrItem.vtDataType.equals("VTNote")) { pst.setString(index, value); } else if (attrItem.vtDataType.equals("VTFilePath")) { pst.setString(index, value); } else if (attrItem.vtDataType.equals("VTClob")) { pst.setString(index, value); } else { pst.setString(index, value); } } protected int getSqlType(AttribItem attrItem){ int sqlType = Types.VARCHAR; if (attrItem.vtDataType.equals("VTString")) { sqlType = Types.VARCHAR; } else if (attrItem.vtDataType.equals("VTInteger")) { sqlType = Types.NUMERIC; } else if (attrItem.vtDataType.equals("VTLong")) { sqlType = Types.NUMERIC; } else if (attrItem.vtDataType.equals("VTDouble")) { sqlType = Types.DOUBLE; } else if (attrItem.vtDataType.equals("VTBoolean")) { sqlType = Types.VARCHAR; } else if (attrItem.vtDataType.equals("VTImage")) { sqlType = Types.VARCHAR; } else if (attrItem.vtDataType.equals("VTDate")) { sqlType = Types.DATE; } else if (attrItem.vtDataType.equals("VTTime")) { sqlType = Types.TIMESTAMP; } else if (attrItem.vtDataType.equals("VTDateTime")) { sqlType = Types.TIMESTAMP; } else if (attrItem.vtDataType.equals("VTNote")) { sqlType = Types.VARCHAR; } else if (attrItem.vtDataType.equals("VTFilePath")) { sqlType = Types.VARCHAR; } else if (attrItem.vtDataType.equals("VTClob")) { sqlType = Types.VARCHAR; } else { sqlType = Types.VARCHAR; } return sqlType; } protected boolean isEmptyOrNull(String value){ return ((value == null) || "".equals(value.trim())); } private Object getDefVal(AttribItem attrItem){ Object res = null; if (attrItem.vtDataType.equals("VTInteger")) { res = isEmptyOrNull(attrItem.defValue) ? Integer.valueOf(0).intValue() : Integer.valueOf(attrItem.defValue).intValue(); } else if (attrItem.vtDataType.equals("VTLong")) { res = isEmptyOrNull(attrItem.defValue) ? Long.valueOf(0).longValue() : Long.valueOf(attrItem.defValue).longValue(); } else if (attrItem.vtDataType.equals("VTDouble")) { res = isEmptyOrNull(attrItem.defValue) ? Double.valueOf(0).doubleValue() : Double.valueOf(attrItem.defValue).doubleValue(); } return res; } /** * clob转String * @param value * @return */ private String clobToString(Clob value) { // TODO Auto-generated method stub String clobValue = ""; try { Reader is = value.getCharacterStream(); BufferedReader br = new BufferedReader(is); String s = br.readLine(); StringBuffer sb = new StringBuffer(); while(s!=null){ sb.append(s); s=br.readLine(); } clobValue = sb.toString(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return clobValue; } }