package com.vci.server.bof.server; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.hibernate.HibernateException; import com.vci.corba.omd.btm.BtmItem; import com.vci.corba.omd.data.AttributeValue; import com.vci.corba.omd.data.BusinessObject; import com.vci.corba.omd.data.LinkObject; import com.vci.server.base.exception.ExceptionLocalHandler; import com.vci.server.base.persistence.dao.HibernateSessionFactory; import com.vci.server.base.utility.ServerServiceProvider; import com.vci.server.cache.OMCacheProvider; import com.vci.common.exception.VciExceptionTool; import com.vci.corba.common.VCIError; import com.vci.corba.common.data.VCIInvocationInfo; import com.vci.corba.common.data.UserEntityInfo; import com.vci.corba.framework.method.FrameworkServicePrx; import com.vci.common.log.LogType; /** * 定义工具方法 * 添加时请注意修饰 * @author lmh * */ public class ServiceFacadeUtil { //只用来继承,不能实例化 protected ServiceFacadeUtil(){}; private static UserEntityInfo userEntity = new UserEntityInfo(); // private static Map mapBtm = new HashMap(); /** * 日志记录 * @param user:操作人 * @param module:操作模块 * @param ip:操作人所在IP地址 * @param operation:操作类型 * @param type:对象类型 * @param objName:对象名称 * @param oid:对象OID * @throws VCIError */ protected void recordLog(String user, String module, String ip, String operation, String type, String objName, String oid) throws VCIError { userEntity.userName = user; if(module.equalsIgnoreCase(type)){ userEntity.modules = getBtmShowName(module); }else{ userEntity.modules = module; } userEntity.ip = ip; String con = "操作的数据是->" + getBtmShowName(type) + ":" + objName; //记录日志 ServerServiceProvider.getFrameService().savelogGeneralOperation("操作成功", con, userEntity, oid, type); //LogRecordUtil.writeLog(userEntity, operation, "操作成功", "操作的数据是->" + getBtmShowName(type) + ":" + objName, LogType.GeneralOperation, oid); } /** * 批量存储log * @param bos * @param ip * @param operation * @throws HibernateException * @throws SQLException * @throws VCIError */ protected void batchRecordLog(BusinessObject[] bos, String ip, String operation) throws HibernateException, SQLException, VCIError { batchRecordLog(bos,ip,operation,"操作成功"); } private void batchRecordLog(BusinessObject[] bos, String ip, String operation,String result) throws HibernateException, SQLException, VCIError{ if (bos == null || bos.length < 1) { return; } VCIInvocationInfo invInfo = HibernateSessionFactory.getVciSessionInfo(); if (StringUtils.isEmpty(invInfo.userName)) userEntity.userName = bos[0].creator; else userEntity.userName = invInfo.userName; userEntity.ip = ip; String type = bos[0].btName; userEntity.modules = getBtmShowName(type); FrameworkServicePrx fService= ServerServiceProvider.getFrameService(); // ArrayList logList = new ArrayList(); for (int i = 0; i < bos.length; i++) { BusinessObject bo = bos[i]; String info = StringUtils.isNotBlank(bo.name)?bo.name:(StringUtils.isNotBlank(bo.id)?bo.id:bo.oid); String btmShowName = getBtmShowName(bos[i].btName); try { fService.saveLogV2(result, "操作的数据为->"+ btmShowName + ":" + info, operation, (short)LogType.GeneralOperation.getIntVal(), bos[i].oid, userEntity); } catch (VCIError e) { // TODO Auto-generated catch block e.printStackTrace(); } // logList.add(LogRecordUtil.getLogSql(userEntity, operation, result, "操作的数据为->"+ btmShowName + ":" + info, LogType.GeneralOperation, bos[i].oid, role, btmShowName)); // if ((i + 1) % 200 == 0) { // LogRecordUtil.batchSaveLog(logList.toArray(new String[logList.size()])); // logList.clear(); // } } // if (bos.length % 200 != 0) { // LogRecordUtil.batchSaveLog(logList.toArray(new String[logList.size()])); // logList.clear(); // } } protected void batchFailRecordLog(BusinessObject[] bos, String ip, String operation) throws HibernateException, SQLException, VCIError{ batchRecordLog(bos,ip,operation,"操作失败"); } /** * 获取国际化的错误提示信息,将其设置到VCIError的key中 * @param VCIError * @return */ protected VCIError getLocalString(String key, Throwable e) { VCIError error = null; if (e == null) { error = new VCIError(key, new String[0]); } else if (e instanceof VCIError) { error = (VCIError) e; } else { error = new VCIError(key, new String[]{VciExceptionTool.getExceptionStr(e), VciExceptionTool.getExceptionDetail(e)}); } VCIError rsError = ExceptionLocalHandler.getInstance().getLocalString(error, "PLMBOFactory"); return rsError; } protected void dealBusinessObjectNullValue(LinkObject lo) { lo.oid = lo.oid == null ? "" : lo.oid; lo.creator = lo.creator == null ? "" : lo.creator; //lo.createTime = lo.createTime; lo.modifier = lo.modifier == null ? "" : lo.modifier; //lo.modifyTime = lo.modifyTime; lo.ltName = lo.ltName == null ? "" : lo.ltName; lo.fromOid = lo.fromOid == null ? "" : lo.fromOid; lo.fromRevOid = lo.fromRevOid == null ? "" : lo.fromRevOid; lo.fromNameOid = lo.fromNameOid == null ? "" : lo.fromNameOid; lo.fromBTName = lo.fromBTName == null ? "" : lo.fromBTName; lo.toOid = lo.toOid == null ? "" : lo.toOid; lo.toRevOid = lo.toRevOid == null ? "" : lo.toRevOid; lo.toNameOid = lo.toNameOid == null ? "" : lo.toNameOid; lo.toBTName = lo.toBTName == null ? "" : lo.toBTName; //lo.ts = lo.ts; lo.newAttrValList = new AttributeValue[0]; if (lo.hisAttrValList == null) { lo.hisAttrValList = new AttributeValue[0]; } for (int i = 0; i < lo.hisAttrValList.length; i++) { if (lo.hisAttrValList[i].attrVal == null) { lo.hisAttrValList[i].attrVal = ""; } } } protected synchronized String getBtmShowName(String btmName){ if(StringUtils.isBlank(btmName)){ return ""; } BtmItem bt = OMCacheProvider.getBizType(btmName); // if (!mapBtm.containsKey(btmName)) { // BtmItem bt; // try { // //bt = ServerServiceProvider.getOMDService().getBTMService().getBtmItemByName(btmName); // bt = OMCacheProvider.getBizType(btmName); // mapBtm.put(btmName, bt); // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // // BtmItem bt = mapBtm.get(btmName); if (bt == null) return ""; return bt.label; // String sql = " select label from plbtmtype where name = ?"; // SQLQuery query = HibernateSessionFactory.getSession().createSQLQuery(sql); // query.setString(0, btmName.trim()); // // List queryList = query.list(); // if(queryList.size()>0){ // Object value = queryList.get(0); // if(value instanceof Map){ // return ((Map)value).get("label").toString(); // }else{ // return value.toString(); // } // } // // return btmName; } }