package com.vci.server.framework.delegate; import java.sql.Timestamp; import java.util.List; import org.apache.commons.lang3.StringUtils; import com.vci.server.base.utility.LogHelper; import com.vci.server.base.utility.LogRecordUtil; import com.vci.server.framework.systemConfig.security.MachSecurity; import com.vci.server.framework.systemConfig.security.MachSecurityService; import com.vci.common.utility.ObjectUtility; import com.vci.corba.common.VCIError; import com.vci.corba.framework.data.MachSecurityInfo; import com.vci.corba.log.data.LogType; import com.vci.corba.common.data.UserEntityInfo; import com.vci.server.base.delegate.BaseDelegate; public class MachSecurityDelegate extends BaseDelegate { private MachSecurityService service = null; public MachSecurityDelegate(UserEntityInfo userInfo) { super(userInfo); service = new MachSecurityService(userEntity); } public MachSecurityDelegate() { service = new MachSecurityService(); } @Override public void setUserEntityInfo(UserEntityInfo userInfo) { super.setUserEntityInfo(userInfo); service.setUserEntity(userEntity); } public MachSecurityInfo[] getAllMachSecurity() throws VCIError { List list = null; try { list = service.getMachSecurityList(); } catch (Exception e) { throw new VCIError("120311", new String[0]); } int size = list.size(); MachSecurityInfo[] infos = new MachSecurityInfo[size]; for (int i = 0; i < size; i++) { infos[i] = changeEntityToInfo(list.get(i)); } return infos; } public int getMachSecurityTolal() throws VCIError { int total = 0; try { total = service.getMachSecurityTolal(); } catch (Exception e) { throw new VCIError("120312", new String[0]); } return total; } public int getMachSecurityTolalByCondition(String name, String ipAddress, int security) throws VCIError { int total = 0; try { total = service.getMachSecurityTolal(name, ipAddress, security); } catch (Exception e) { throw new VCIError("120312", new String[0]); } return total; } public MachSecurityInfo[] fetchMachSecurityByPage(int pageNo, int pageSize) throws VCIError { List list = null; try { list = service.fetchMachSecurityByPage(pageNo, pageSize); } catch (Exception e) { throw new VCIError("120313", new String[0]); } int size = list.size(); MachSecurityInfo[] infos = new MachSecurityInfo[size]; for (int i = 0; i < size; i++) { infos[i] = changeEntityToInfo(list.get(i)); } return infos; } public MachSecurityInfo[] fetchMachSecurityByConditionPage(String name, String ipAddress, int security, int pageNo, int pageSize) throws VCIError { List list = null; try { list = service.fetchMachSecurityByConditionPage(name, ipAddress, security, pageNo, pageSize); } catch (Exception e) { throw new VCIError("120314", new String[0]); } int size = list.size(); MachSecurityInfo[] infos = new MachSecurityInfo[size]; for (int i = 0; i < size; i++) { infos[i] = changeEntityToInfo(list.get(i)); } return infos; } public String saveMachSecurity(MachSecurityInfo info, UserEntityInfo userInfo) throws VCIError { this.setUserEntityInfo(userInfo); MachSecurity security = changeInfoToEntity(info); //System.out.println("======saveMachSecurity:user=" + userEntity.getUserName() + "==============="); security.setCreator(userEntity.getUserName()); security.setModifier(userEntity.getUserName()); Timestamp currentTime = new Timestamp(System.currentTimeMillis()); security.setCreateTime(currentTime); security.setModifyTime(currentTime); String id = security.getId(); if (StringUtils.isBlank(id)) { id = ObjectUtility.getNewObjectID36(); security.setId(id); } try { service.saveMachSecurity(security); String log = String.format("添加机器密级:%s [%s]", security.getName(), LogHelper.toNewLogString(security)); LogRecordUtil.writeLog(userInfo, "添加", "成功", log, LogType.General, security.getId()); } catch (Exception e) { throw new VCIError("120315", new String[0]); } return id; } public boolean batchSaveMachSecurity(MachSecurityInfo[] infos, UserEntityInfo userInfo) throws VCIError { this.setUserEntityInfo(userInfo); MachSecurity[] securities = new MachSecurity[infos.length]; String[] ids = new String[infos.length]; String id = null; for (int i = 0; i < infos.length; i++) { securities[i] = changeInfoToEntity(infos[i]); id = securities[i].getId(); if (StringUtils.isBlank(id)) { id = ObjectUtility.getNewObjectID36(); securities[i].setId(id); } ids[i] = id; } try { MachSecurityService machService = new MachSecurityService(userEntity); machService.saveMachSecurities(securities); String log = String.format("添加机器密级对象:%s", String.join(";", ids)); LogRecordUtil.writeLog(userInfo, "添加", "成功", log, LogType.General, id); } catch (Exception e) { throw new VCIError("120316", new String[0]); } return false; } public boolean updateMachSecurity(MachSecurityInfo info, UserEntityInfo userInfo) throws VCIError { this.setUserEntityInfo(userInfo); boolean rs = true; MachSecurity ent = changeInfoToEntity(info); try { MachSecurityService msService = new MachSecurityService(); MachSecurity old = msService.selectMachSecurity(ent.getId()); rs = msService.updateMachSecurity(ent); String log = ""; log = String.format("更改机器密级信息:%s; %s", ent.getName(), LogHelper.toUpdataLogString(old, ent)); if (rs) LogRecordUtil.writeLog(userInfo, "更新", "成功", log, LogType.General, ent.getId()); else LogRecordUtil.writeLog(userInfo, "更新", "失败", log, LogType.General, ent.getId()); return rs; } catch (Exception e) { throw new VCIError("120117", new String[0]); } } public boolean batchUpdateMachSecurity(MachSecurityInfo[] infos, UserEntityInfo userInfo) throws VCIError { this.setUserEntityInfo(userInfo); MachSecurity[] securities = new MachSecurity[infos.length]; String[] ids = new String[infos.length]; for (int i = 0; i < infos.length; i++) { securities[i] = changeInfoToEntity(infos[i]); ids[i] = securities[i].getId(); } String log = String.format("更新机器密级对象:%s", String.join(";", ids)); try { MachSecurityService machService = new MachSecurityService(userEntity); machService.updateMachSecurities(securities); LogRecordUtil.writeLog(userInfo, "更新", "成功", log, LogType.General, ids[0]); return true; } catch (Exception e) { LogRecordUtil.writeLog(userInfo, "更新", "失败", log, LogType.General, ids[0]); throw new VCIError("120318", new String[0]); } } public boolean deletMachSecurity(String[] ids, UserEntityInfo userEnt) throws VCIError { this.setUserEntityInfo(userEnt); return service.deleteMachSecurityByMQL(ids); } public int getMachSecurity(String machAddress, int type, UserEntityInfo userInfo) throws VCIError { this.setUserEntityInfo(userInfo); MachSecurity ms = service.selectMachSecurityByIP(machAddress); if (ms != null) { long sgrade = ms.getSecretGrade(); return (int)sgrade; } return 0; } public MachSecurityInfo getMachSecurityInfo(String machAddress, int type, UserEntityInfo userInfo) throws VCIError { this.setUserEntityInfo(userInfo); MachSecurityInfo info = new MachSecurityInfo(); MachSecurity ms = service.selectMachSecurityByIP(machAddress); if (ms != null) { info = changeEntityToInfo(ms); } else { info = new MachSecurityInfo(); } return info; } private MachSecurityInfo changeEntityToInfo(MachSecurity ent) { MachSecurityInfo info = new MachSecurityInfo(); info.id = ent.getId(); info.name = ent.getName() == null ? "" : ent.getName(); info.ipAddress = ent.getIpAddress() == null ? "" : ent.getIpAddress(); info.macAddress = ent.getMacAddress() == null ? "" : ent.getMacAddress(); info.secretGrade = ent.getSecretGrade(); info.createTime = ent.getCreateTime() == null ? 0 : ent.getCreateTime().getTime(); info.creator = ent.getCreator() == null ? "" : ent.getCreator(); info.modifyTime = ent.getModifyTime() == null ? 0 : ent.getModifyTime().getTime(); info.modifier = ent.getModifier() == null ? "" : ent.getModifier(); info.desc = ent.getDescription() == null ? "" : ent.getDescription(); return info; } private MachSecurity changeInfoToEntity(MachSecurityInfo info) { MachSecurity ent = new MachSecurity(); ent.setId(info.id); ent.setName(info.name); ent.setIpAddress(info.ipAddress == "" ? null : info.ipAddress); ent.setMacAddress(info.macAddress == "" ? null : info.macAddress); ent.setSecretGrade(info.secretGrade); ent.setDescription(info.desc == "" ? null : info.desc); ent.setName(info.name == "" ? null : info.name); ent.setCreateTime(info.createTime == 0 ? new java.sql.Timestamp(System.currentTimeMillis()) : new java.sql.Timestamp(info.createTime)); ent.setCreator(info.creator == "" ? null : info.creator); ent.setModifyTime(info.modifyTime == 0 ? new java.sql.Timestamp(System.currentTimeMillis()) : new java.sql.Timestamp(info.createTime)); ent.setModifier(info.modifier == "" ? null : info.modifier); return ent; } }