package com.vci.server.omd.enumtype.service; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.dom4j.DocumentException; import org.dom4j.Element; import com.vci.common.log.ServerWithLog4j; import com.vci.common.utility.ObjectUtility; import com.vci.corba.common.VCIError; import com.vci.corba.omd.etm.EnumChild; import com.vci.corba.omd.etm.EnumItem; import com.vci.server.base.persistence.dao.HibernateSessionFactory; import com.vci.server.cache.ConfigCacheProvider; import com.vci.server.mw.ServerContextVariable; import com.vci.server.omd.enumtype.EnumServiceImplHelper; public class EnumService { private static final String ENUM_SECURITYENUM = "securityenum"; private static final String ENUM_USERSECURITYENUM = "usersecurityenum"; private static final String ENUM_IPSECURITYENUM = "ipsecurityenum"; private static EnumService instance; public static EnumService getInstance() { if (instance == null) { instance = new EnumService(); instance.checkSysEnum(); } return instance; } /** * 检查系统枚举是否存在,不存在则创建 * @return */ private void checkSysEnum() { //System.out.println("=========checkSysEnum:Begin=========="); try{ // 缺少用户密级枚举 if (!checkRowIsExists(ENUM_USERSECURITYENUM)){ AddUserSecurityEnum(); } } catch (Throwable e) { e.printStackTrace(); } // try{ // // 缺少密级枚举 // if (!checkRowIsExists(ENUM_SECURITYENUM)){ // AddSecurityEnum(); // } // } // catch (Throwable e) { // e.printStackTrace(); // } try{ // 缺少密级枚举 if (!checkRowIsExists(ENUM_IPSECURITYENUM)){ AddIpSecurityEnum(); } } catch (Throwable e) { e.printStackTrace(); } //System.out.println("=========checkSysEnum:End=========="); return; } /** * 增加枚举 * @throws SQLException * @throws IOException */ public boolean addEmItem(EnumItem emItem) throws VCIError, SQLException, IOException { boolean flag = false; String insertSql = "insert into plenum values(?,?,?,?,?,?,?,?,xmltype(?))"; Connection connection = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = connection.prepareStatement(insertSql); pst.setString(1, ObjectUtility.getNewObjectID36()); pst.setString(2, emItem.name); pst.setString(3, emItem.label); long time = Calendar.getInstance().getTimeInMillis(); Timestamp ts = new Timestamp(time); pst.setTimestamp(4, ts); pst.setString(5, emItem.creator); pst.setTimestamp(6, ts); pst.setString(7, emItem.modifier); pst.setTimestamp(8, ts); String xmlText = EnumServiceImplHelper.getInstance().getXmlText(emItem); //CLOB content = EnumServiceImplHelper.getInstance().getXmlTypeContent(xmlText, connection); //pst.setObject(9, content); pst.setString(9, xmlText); pst.executeUpdate(); pst.close(); flag = true; return flag; } /** * 修改枚举 * @throws IOException * @throws SQLException */ public boolean modifyEmItem(EnumItem emItem) throws VCIError, SQLException, IOException { boolean flag = false; String sql = "update plenum t set t.name=?, t.label=?, t.ts=?, t.creator=?, t.createtime=?, t.modifier=?, t.modifytime=?, t.content=xmltype(?) where t.oid=? and t.ts = ?"; Connection conn = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, emItem.name); pst.setString(2, emItem.label); long time = Calendar.getInstance().getTimeInMillis(); Timestamp ts = new Timestamp(time); pst.setTimestamp(3, ts); pst.setString(4, emItem.creator); pst.setTimestamp(5, new Timestamp(emItem.createTime)); pst.setString(6, emItem.modifier); pst.setTimestamp(7, ts); String xmlText = EnumServiceImplHelper.getInstance().getXmlText(emItem); //CLOB content = EnumServiceImplHelper.getInstance().getXmlTypeContent(xmlText, conn); //pst.setObject(8, content); pst.setString(8, xmlText); pst.setString(9, emItem.oid); pst.setTimestamp(10, Timestamp.valueOf(emItem.ts)); pst.executeUpdate(); pst.close(); flag = true; return flag; } /** * 删除枚举 * @throws SQLException */ public boolean deleteEmItems(EnumItem[] emItems) throws VCIError, SQLException { boolean flag = false; for(EnumItem em : emItems){ deleteEmItem(em); } flag = true; return flag; } public boolean deleteEmItem(EnumItem em)throws VCIError, SQLException { boolean flag = false; String sql = "delete from plenum where oid = ? and ts = ?"; Connection connection = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = connection.prepareStatement(sql); pst.setString(1, em.oid); String ts = em.ts; pst.setTimestamp(2, Timestamp.valueOf(ts)); pst.executeUpdate(); pst.close(); flag = true; return flag; } public boolean deleteEmItemNoCache(EnumItem em)throws VCIError, SQLException { boolean flag = false; String sql = "delete from plenum where oid = ? and ts = ?"; Connection connection = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = connection.prepareStatement(sql); pst.setString(1, em.oid); String ts = em.ts; pst.setTimestamp(2, Timestamp.valueOf(ts)); pst.executeUpdate(); pst.close(); ServerWithLog4j.logger.debug(sql); flag = true; return flag; } /** * 查询枚举 * @throws DocumentException * @throws SQLException */ public EnumItem[] getEmItems(String filter, int start, int rows) throws VCIError, SQLException, DocumentException { //String sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plenum t "; String sql = ""; switch (HibernateSessionFactory.getDbType()) { case DM8: sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, content from plenum t "; break; case ORACL: default: sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plenum t "; break; } if(StringUtils.isNotBlank(filter)){ sql = sql + " where t.name like '" + filter + "%'"; //add by caill 在filter后面添加“.name” } sql += " ORDER BY upper(name)"; ServerWithLog4j.logger.debug(sql); Connection connection = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = connection.prepareStatement(sql); ResultSet rs = pst.executeQuery(); List ems = new ArrayList(); while(rs.next()){ EnumItem em = EnumServiceImplHelper.getInstance().getEnum(rs); em = this.FilterSecretGrade(em); ems.add(em); } rs.close(); pst.close(); return ems.toArray(new EnumItem[0]); } /** * 检查枚举名是否存在 * @throws SQLException */ public boolean checkRowIsExists(String name) throws VCIError, SQLException { String sql = "select count(name) count from plenum t where t.name = ?"; Connection connection = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = connection.prepareStatement(sql); pst.setString(1, name); ResultSet rs = pst.executeQuery(); while(rs.next()){ int count = rs.getInt("count"); if(count > 0){ return true; }else{ return false; } } rs.close(); pst.close(); return false; } /** * 根据枚举类型查询枚举 * @throws DocumentException * @throws SQLException */ public EnumItem[] getEmItemsByType(String type) throws VCIError, SQLException, DocumentException { //String sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plenum t where extractvalue(content, '/enum/type') = ? order by name"; String sql = ""; switch (HibernateSessionFactory.getDbType()) { case DM8: sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, content from plenum t " + "where extractvalue(content, '/enum/type') = ? order by name"; break; case ORACL: default: sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plenum t " + "where extractvalue(content, '/enum/type') = ? order by name"; break; } Connection connection = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = connection.prepareStatement(sql); pst.setString(1, type); ResultSet rs = pst.executeQuery(); List ems = new ArrayList(); while(rs.next()){ EnumItem em = EnumServiceImplHelper.getInstance().getEnum(rs); em = FilterSecretGrade(em); ems.add(em); } rs.close(); pst.close(); return ems.toArray(new EnumItem[0]); } public EnumItem getCloneEnumItem(EnumItem ei){ EnumItem res = new EnumItem(); res.oid = ei.oid; res.ts = ei.ts; res.creator = ei.creator; res.createTime = ei.createTime; res.modifier = ei.modifier; res.modifyTime = ei.modifyTime; res.name = ei.name; res.label = ei.label; res.type = ei.type; res.length = ei.length; EnumChild[] childs = new EnumChild[ei.children.length]; for (int i = 0; i < childs.length; i++) { childs[i] = ei.children[i]; } res.children = childs; return res; } /* * 密级的枚举名称是固定的,统一为Enumsecretgrade,仅仅针对这个进行处理 * * */ public EnumItem FilterSecretGrade(EnumItem ei) throws VCIError, SQLException { EnumItem em = getCloneEnumItem(ei); if (HibernateSessionFactory.getVciSessionInfo() != null) { int userType = getUserType(HibernateSessionFactory.getVciSessionInfo().userID); String curUserName = HibernateSessionFactory.getVciSessionInfo().userName; if(curUserName != null && curUserName.equals("developer") || userType <= 1) { return em; } } else { return em; } //特殊处理"Enumsecretgrade"密级枚举取值 if ( ( !em.name.isEmpty() ) && ( em.name.trim().equals("Enumsecretgrade") ) && ( em.children != null ) && ( em.children.length >0) ) { //特殊处理业务对象密级枚举值:“业务对象密级”<=“机器密级”<=“人员密级”,by zhonggy 2015-07-23 try { int compareSecret = 0; boolean ipSecuritySwitch = ConfigCacheProvider.isIpSecurity();//ServerContextVariable.getIpSecretSwitch();//机器密级是否有效 boolean userSecuritySwith = ConfigCacheProvider.isUserSecurity();//ServerContextVariable.getUserSecretSwith();//用户密级是否有效 //AppConfigDetailInfo userSecuritySwith=new AppConfigDetailDelegate(new UserEntityInfo()).getAppConfigDetailByKey("userSecuritySwith");//用户密级是否有效 //AppConfigDetailInfo ipSecuritySwitch=new AppConfigDetailDelegate(new UserEntityInfo()).getAppConfigDetailByKey("ipSecuritySwitch");//机器密级是否有效 if(ipSecuritySwitch){ String machineSecret= ServerContextVariable.getMachineSecret();//机器密级 if(machineSecret == null || machineSecret.equals("")){ machineSecret = "10"; } if(userSecuritySwith){ String userSecret = ServerContextVariable.getUserSecret();//用户密级 if (userSecret == null || userSecret.trim().equals("")) { userSecret = "10"; } int int1 = Integer.parseInt(userSecret); int int2 = Integer.parseInt(machineSecret); if (int1>=int2) { compareSecret = int2; }else { compareSecret = int1; } }else{ compareSecret=Integer.parseInt(machineSecret); } }else{ if(userSecuritySwith){ String userSecret = ServerContextVariable.getUserSecret();//用户密级 if (userSecret == null || userSecret.trim().equals("")) { userSecret = "10"; } compareSecret=Integer.parseInt(userSecret); }else{ return em; } } EnumChild[] children = em.children; List revisedEnumChild = new ArrayList(); for (EnumChild enumItem : children) { Integer enumIntValue = Integer.parseInt(enumItem.value); if (compareSecret >= enumIntValue) { revisedEnumChild.add(enumItem); } } em.children = revisedEnumChild.toArray(new EnumChild[]{}); } catch (Exception e) { e.printStackTrace(); } } return em; } /** * 根据枚举名字查询枚举 * @throws DocumentException * @throws SQLException */ public EnumItem getEmItemByName(String name) throws VCIError, SQLException, DocumentException { //String sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plenum t where name = ?"; String sql = ""; switch (HibernateSessionFactory.getDbType()) { case DM8: sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, content from plenum t where name = ?"; break; case ORACL: default: sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content from plenum t where name = ?"; break; } Connection connection = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = connection.prepareStatement(sql); pst.setString(1, name); ResultSet rs = pst.executeQuery(); EnumItem em = null; while(rs.next()){ em = EnumServiceImplHelper.getInstance().getEnum(rs); } if(em == null){ em = new EnumItem(); em.length = 0; em.children = new EnumChild[0]; } rs.close(); rs = null; pst.close(); pst = null; return FilterSecretGrade(em); } public boolean xml2DB(String userName) throws VCIError { // List news = null; // try { // news = Xml2DBDelegate.getInstance().getNews(userName); // } catch (Throwable e1) { // e1.printStackTrace(); // } // if(news == null){ // return true; // } // for(EnumItem o : news){ // try{ // addEmItemNoCache(o); // }catch(Throwable e){ // e.printStackTrace(); // return false; // } // } return true; } public boolean addEmItemNoCache(EnumItem emItem) throws VCIError, SQLException, IOException { boolean flag = false; String insertSql = "insert into plenum values(?,?,?,?,?,?,?,?,xmltype(?))"; Connection connection = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = connection.prepareStatement(insertSql); pst.setString(1, ObjectUtility.getNewObjectID36()); pst.setString(2, emItem.name); pst.setString(3, emItem.label); long time = Calendar.getInstance().getTimeInMillis(); Timestamp ts = new Timestamp(time); pst.setTimestamp(4, ts); pst.setString(5, emItem.creator); pst.setTimestamp(6, ts); pst.setString(7, emItem.modifier); pst.setTimestamp(8, ts); String xmlText = EnumServiceImplHelper.getInstance().getXmlText(emItem); //CLOB content = EnumServiceImplHelper.getInstance().getXmlTypeContent(xmlText, connection); //pst.setObject(9, content); pst.setString(9, xmlText); pst.executeUpdate(); pst.close(); flag = true; return flag; } /** * 设置EnumItem存在 * @param em * @param element */ public void setEnumValueFormDoc(EnumItem em, Element element){ String value = element.elementText("type"); em.type = (value == null ? "" : value); value = element.elementText("length"); em.length = (value == null ? 0 : Integer.valueOf(value)); List children = element.elements("child"); List ecList = new ArrayList(); for(Element child : children){ EnumChild ec = new EnumChild(); value = child.elementText("name"); ec.name = (value == null ? "" : value); value = child.elementText("value"); ec.value = (value == null ? "" : value); value = child.elementText("description"); ec.description = (value == null ? "" : value); ecList.add(ec); } em.children = ecList.toArray(new EnumChild[0]); } //private static final String PROP_SECURITY = "secret"; //private static final String PROP_ENABLE = "enable"; //private static final String PROP_IPSECURITY = "ipsecret"; /** * @Title :判断机器密级是否有效 * @Description : * @return */ // public boolean isIPSecretValid(){ // boolean isOpen = false; // try { // String queryTemp = "ipstopmanagerQueryFinalValue"; // QTServiceImpl qtServiceImpl = new QTServiceImpl(); // QTWrapper qt = qtServiceImpl.getQT(queryTemp); // QueryTemplate qtByQTText1 = Tool.getQTByQTText(qt.qtName, qt.qtText); // qtByQTText1.setRightFlag(false); // BusinessObject[] findBTMObjects = qtServiceImpl.findBTMObjects(qtByQTText1.getId(), Tool.getQTTextByQT(qtByQTText1)); // if(findBTMObjects != null && findBTMObjects.length == 1){ // String enableValue = BTMUtils.getBtmArrValue(findBTMObjects[0], EnumServiceConstant.PROP_ENABLE); // if(enableValue != null && enableValue.equalsIgnoreCase("true")){ // isOpen = true; // } // } // } catch (Throwable e) { // e.printStackTrace(); // } // return isOpen; // } public EnumItem getEnumItemByOid(String oid) throws VCIError, SQLException, IOException, DocumentException { // String sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content " + // "from plenum t where t.oid =?"; String sql = ""; switch (HibernateSessionFactory.getDbType()) { case DM8: sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, content " + "from plenum t where t.oid =?"; break; case ORACL: default: sql = "select oid, name, label, ts, creator, createtime, modifier, modifytime, t.content.getclobval() content " + "from plenum t where t.oid =?"; break; } Connection connection = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = connection.prepareStatement(sql); pst.setString(1, oid); ResultSet rs = pst.executeQuery(); EnumItem em = null; while(rs.next()){ em = EnumServiceImplHelper.getInstance().getEnum(rs); } rs.close(); pst.close(); if(em == null){ em = new EnumItem(); } return em; } private int getUserType(String userId) throws VCIError, SQLException { String sql = "select plusertype from pluser t where t.pluid=?"; Connection connection = HibernateSessionFactory.getSessionConnection(); PreparedStatement pst = connection.prepareStatement(sql); pst.setString(1, userId); ResultSet rs = pst.executeQuery(); int userType = 2; if (rs.next()) { userType = rs.getInt("plusertype"); } rs.close(); pst.close(); return userType; } /** * 创建密级枚举对象 * @return */ private EnumItem AddSecurityEnum() { EnumItem em = new EnumItem(); em.name = ENUM_SECURITYENUM; em.label = "密级"; em.type = "Integer"; em.length = 10; EnumChild[] items = new EnumChild[7]; em.children = items; items[0] = new EnumChild("公开", "1", "公开"); items[1] = new EnumChild("内部", "2", "内部"); items[2] = new EnumChild("秘密", "3", "秘密"); items[3] = new EnumChild("机密", "4", "机密"); items[4] = new EnumChild("绝密", "5", "绝密"); try{ if (addEmItem(em)) return em; } catch (Throwable e) { e.printStackTrace(); } // TODO Auto-generated method stub return null; } /** * 创建IP密级枚举对象 * @return */ private EnumItem AddIpSecurityEnum() { EnumItem em = new EnumItem(); em.name = ENUM_IPSECURITYENUM; em.label = "IP密级枚举"; em.type = "Integer"; em.length = 10; EnumChild[] items = new EnumChild[3]; em.children = items; items[0] = new EnumChild("公开", "10", "公开"); items[1] = new EnumChild("秘密", "20", "秘密"); items[2] = new EnumChild("机密", "30", "机密"); try{ if (addEmItem(em)) return em; } catch (Throwable e) { e.printStackTrace(); } return null; } /** * 创建用户密级枚举对象 * @return */ private EnumItem AddUserSecurityEnum() { EnumItem em = new EnumItem(); em.name = ENUM_USERSECURITYENUM; em.label = "用户密级枚举"; em.type = "Integer"; em.length = 10; EnumChild[] items = new EnumChild[3]; em.children = items; items[0] = new EnumChild("公开", "10", "公开"); items[1] = new EnumChild("秘密", "20", "秘密"); items[2] = new EnumChild("机密", "30", "机密"); try{ if (addEmItem(em)) return em; } catch (Throwable e) { e.printStackTrace(); } return null; } /** * 将emItem转化成xmltext * @param emItem * @return */ public String getXmlText(EnumItem emItem) { StringBuilder stb = new StringBuilder(""); stb.append("" + emItem.name + ""); stb.append(""); stb.append("" + emItem.type + ""); stb.append("" + emItem.length + ""); for(EnumChild ec : emItem.children){ stb.append(""); stb.append("" + ec.name + ""); stb.append("" + ec.value + ""); stb.append("" + ec.description + ""); stb.append(""); } stb.append(""); return stb.toString(); } }