对象组提交

This commit is contained in:
wangxin
2019-06-13 20:57:29 +08:00
parent 446c802ece
commit 7501eb4317
31 changed files with 4197 additions and 338 deletions

View File

@@ -0,0 +1,441 @@
package com.nis.web.controller.configuration;
import com.nis.domain.FunctionServiceDict;
import com.nis.domain.Page;
import com.nis.domain.basics.PolicyGroupInfo;
import com.nis.domain.configuration.BaseCfg;
import com.nis.domain.configuration.CachePolicyUserRegion;
import com.nis.domain.configuration.CfgIndexInfo;
import com.nis.domain.configuration.ObjGroupCfg;
import com.nis.exceptions.CallExternalProceduresException;
import com.nis.exceptions.MaatConvertException;
import com.nis.util.Constants;
import com.nis.util.DictUtils;
import com.nis.util.LogUtils;
import com.nis.util.StringUtil;
import com.nis.web.controller.BaseController;
import com.nis.web.security.UserUtils;
import com.nis.web.service.configuration.ObjectGroupService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
@Controller
@RequestMapping("${adminPath}/objgroup")
public class ObjectGroupController extends BaseController {
@Autowired
private ObjectGroupService objectGroupService;
@RequestMapping(value = { "/list" })
public String list(Model model, @ModelAttribute("cfg") CfgIndexInfo cfg, HttpServletRequest request,
HttpServletResponse response){
Page<CfgIndexInfo> searchPage=new Page<CfgIndexInfo>(request,response,"a");
Page<CfgIndexInfo> page = objectGroupService.getPolicyListList(searchPage, cfg);
model.addAttribute("page", page);
initPageCondition(model,cfg);
return "/cfg/objgroup/list";
}
@RequestMapping(value = { "/form" })
@RequiresPermissions(value = { "objgroup:config" })
public String form(Model model, String ids, CfgIndexInfo entity) {
List<PolicyGroupInfo> ipGroups=new ArrayList<>();
List<PolicyGroupInfo> urlGroups=new ArrayList<>();
List<PolicyGroupInfo> subscribeIdGroups=new ArrayList<>();
List<PolicyGroupInfo> domainGroups=new ArrayList<>();
//查询所有可选择没有被其他配置引用并且组下有域配置的组ud_flag=1
ipGroups=policyGroupInfoService.findPolicyGroupInfosByTypeforUD(Constants.IP_OBJ_GROUP_TYPE,1);
urlGroups=policyGroupInfoService.findPolicyGroupInfosByTypeforUD(Constants.URL_OBJ_GROUP_TYPE,1);
subscribeIdGroups=policyGroupInfoService.findPolicyGroupInfosByTypeforUD(Constants.SUBID_OBJ_GROUP_TYPE,1);
domainGroups=policyGroupInfoService.findPolicyGroupInfosByTypeforUD(Constants.DOMAIN_OBJ_GROUP_TYPE,1);
if (StringUtils.isNotBlank(ids)) {
entity = objectGroupService.getObjectGroupCfg(Long.parseLong(ids), null);
initUpdateFormCondition(model, entity);
//查询配置引用到的分组,加到对应的组下(维持选中选项)
if(MapUtils.isNotEmpty(entity.getUserRegion())){
for(Map.Entry<String,Object> e:entity.getUserRegion().entrySet()){
if(e.getKey().equals("ipGroup")&&StringUtils.isNotBlank(e.getValue().toString())){
List<PolicyGroupInfo> _ipGrpups=policyGroupInfoService.findPolicyByServiceGroupInfoList(e.getValue().toString());
if(CollectionUtils.isNotEmpty(_ipGrpups)){
ipGroups.addAll(_ipGrpups);
}
}else if(e.getKey().equals("urlGroup")&&StringUtils.isNotBlank(e.getValue().toString())){
List<PolicyGroupInfo> _urlGrpups=policyGroupInfoService.findPolicyByServiceGroupInfoList(e.getValue().toString());
if(CollectionUtils.isNotEmpty(_urlGrpups)){
urlGroups.addAll(_urlGrpups);
}
}else if(e.getKey().equals("subscribeIdGroup")&&StringUtils.isNotBlank(e.getValue().toString())){
List<PolicyGroupInfo> _subscribeIdGroups=policyGroupInfoService.findPolicyByServiceGroupInfoList(e.getValue().toString());
if(CollectionUtils.isNotEmpty(_subscribeIdGroups)){
subscribeIdGroups.addAll(_subscribeIdGroups);
}
}else if(e.getKey().equals("domainGroup")&&StringUtils.isNotBlank(e.getValue().toString())){
List<PolicyGroupInfo> _domainGroups=policyGroupInfoService.findPolicyByServiceGroupInfoList(e.getValue().toString());
if(CollectionUtils.isNotEmpty(_domainGroups)){
domainGroups.addAll(_domainGroups);
}
}
}
}
} else {
initFormCondition(model, entity);
}
model.addAttribute("ipGroups", ipGroups);
model.addAttribute("urlGroups", urlGroups);
model.addAttribute("subscribeIdGroups", subscribeIdGroups);
model.addAttribute("domainGroups", domainGroups);
model.addAttribute("_cfg", entity);
return "/cfg/objgroup/form";
}
@RequestMapping(value = {"saveOrUpdate"})
@RequiresPermissions(value={"objgroup:config"})
public String saveOrUpdate(Model model, HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("cfg")CfgIndexInfo cfg, RedirectAttributes redirectAttributes){
try{
String ipGroup=formatMultSelect(request.getParameterValues("ipGroup")),
subscribeIdGroup=formatMultSelect(request.getParameterValues("subscribeIdGroup")),
domainGroup=formatMultSelect(request.getParameterValues("domainGroup")),
urlGroup=formatMultSelect(request.getParameterValues("urlGroup"));
Map<String,Object> map = new HashMap();
if(StringUtils.isNotBlank(ipGroup)){
map.put("ipGroup", ipGroup.toString());
}
if(StringUtils.isNotBlank(subscribeIdGroup)){
map.put("subscribeIdGroup", subscribeIdGroup.toString());
}
if(StringUtils.isNotBlank(domainGroup)){
map.put("domainGroup", domainGroup.toString());
}
if(StringUtils.isNotBlank(urlGroup)){
map.put("urlGroup", urlGroup.toString());
}
cfg.setUserRegion(map);
objectGroupService.saveOrUpdate(cfg);
//配置仅保存
if(StringUtil.isEmpty(cfg.getIsValid()) || cfg.getIsValid()!=1) {
addMessage(redirectAttributes, "success", "save_success");
}else {
//配置直接生效
addMessage(redirectAttributes, "success", "audit_success");
}
} catch (MaatConvertException e) {
logger.error("对象组配置下发失败:",e);
addMessage(redirectAttributes, "error", "request_service_failed");
LogUtils.saveLog(request, null, e, null);
} catch (CallExternalProceduresException e) {
logger.error("调用外部程序出错:",e);
addMessage(redirectAttributes, "error", "call_external_procedures_failed");
LogUtils.saveLog(request, null, e, null);
} catch (Exception e) {
logger.error("对象组配置保存失败:",e);
addMessage(redirectAttributes, "error", "save_failed");
LogUtils.saveLog(request, null, e, null);
}
return "redirect:" + adminPath +"/objgroup/list?functionId="+cfg.getFunctionId();
}
@RequestMapping(value = {"updateValid"})
@RequiresPermissions(value={"objgroup:config"})
public String delete(Integer isAudit,Integer isValid,String ids,Integer functionId, RedirectAttributes redirectAttributes,
HttpServletRequest request, HttpServletResponse response, @ModelAttribute("cfg")CfgIndexInfo cfg){
try {
if (!StringUtil.isEmpty(ids)) {
objectGroupService.updatePolicyValid(isValid,ids,functionId);
}else {
// 批量删除
Page<CfgIndexInfo> searchPage = new Page<CfgIndexInfo>(request, response, "a");
deleteAll(searchPage, functionId, cfg);
}
addMessage(redirectAttributes, "success", "delete_success");
} catch (Exception e) {
logger.error("配置删除失败:", e);
if (e instanceof MaatConvertException) {
addMessage(redirectAttributes, "error", "request_service_failed");
LogUtils.saveLog(request, null, e, null);
} else {
addMessage(redirectAttributes, "error", "delete_failed");
LogUtils.saveLog(request, null, e, null);
}
}
return "redirect:" + adminPath +"/objgroup/list?functionId="+functionId;
}
/**
* 界面批量删除当前检索条件下的配置
* @param page
* @param functionId
* @param entity
* @throws Exception
*/
public void deleteAll(Page page,Integer functionId, Object entity)throws Exception {
long start=System.currentTimeMillis();
page.setOrderBy("");
page.setPageSize(Constants.MAAT_JSON_SEND_SIZE);
page.setPageNo(1);
page.setLastPage(false);
CfgIndexInfo searchCfg = new CfgIndexInfo();
// 传递检索条件
if(entity != null && (entity instanceof CfgIndexInfo)) {
BeanUtils.copyProperties(entity, searchCfg);
searchCfg.setFunctionId(functionId);
}
BaseCfg batchCfg = new BaseCfg();
batchCfg.setIsValid(-1);
batchCfg.setIsAudit(0);
batchCfg.setEditTime(new Date());
batchCfg.setEditorId(UserUtils.getUser().getId());
//cfg_index_info表中存有servicdId
boolean hasData = true;
while(hasData){
page.setPageNo(1);
page.setLastPage(false);
List<CfgIndexInfo> list = getDataList(page,searchCfg); //获取主配置表数据
if(CollectionUtils.isNotEmpty(list)){
StringBuffer serviceGroupIds=new StringBuffer();
List<BaseCfg> baseCfgList=new ArrayList<>();
for (CfgIndexInfo cfg : list) {
if(cfg.getUserRegion()!=null){
for (Object val:cfg.getUserRegion().values()) {
if(StringUtils.isNotBlank(val.toString())){
if(val.toString().startsWith(",")){
serviceGroupIds.append(val.toString().substring(1));
}else{
serviceGroupIds.append(val.toString());
}
}
}
}
BaseCfg baseCfg=new BaseCfg();
BeanUtils.copyProperties(cfg, baseCfg);
baseCfgList.add(baseCfg);
}
if(serviceGroupIds.lastIndexOf(",")==(serviceGroupIds.toString().length()-1)){
serviceGroupIds.deleteCharAt(serviceGroupIds.toString().length()-1);
}
hasData = objectGroupService.batchDeleteMaatData(page, batchCfg, baseCfgList, hasData,serviceGroupIds.toString());
}else{
hasData = false;
}
}
long end=System.currentTimeMillis();
logger.warn("配置批量删除耗时:"+(end-start));
}
public List getDataList(Page searchPage
,CfgIndexInfo searchCfg
){
BaseCfg baseCfg=new BaseCfg<>();
if(!StringUtil.isEmpty(searchCfg)) {
BeanUtils.copyProperties(searchCfg, baseCfg);
}
Page pageResult=new Page();
pageResult=objectGroupService.getPolicyListList(searchPage,searchCfg);
return pageResult.getList();
}
@RequestMapping(value = {"audit"})
@RequiresPermissions(value={"objgroup:confirm"})
public String audit(Model model,@ModelAttribute("cfg") CfgIndexInfo cfg,
Integer isValid,
Integer isAudit,
String ids,
Integer functionId,
RedirectAttributes redirectAttributes,
HttpServletResponse response,
HttpServletRequest request) {
if(!StringUtil.isEmpty(ids)){
CfgIndexInfo entity = new CfgIndexInfo();
String[] idArray = ids.split(",");
for(String id :idArray){
entity = objectGroupService.getObjectGroupCfg(Long.parseLong(id),null);
entity.setIsAudit(isAudit);
entity.setIsValid(isValid);
entity.setAuditorId(UserUtils.getUser().getId());
entity.setAuditTime(new Date());
entity.setFunctionId(functionId);
try {
objectGroupService.auditPolicy(entity,isAudit,Constants.INSERT_ACTION);
addMessage(redirectAttributes,"success", "audit_success");
} catch ( Exception e) {
logger.error("策略对象组下发失败:"+e);
if(e instanceof MaatConvertException) {
addMessage(redirectAttributes,"error","request_service_failed");
LogUtils.saveLog(request, null, e, null);
}else {
addMessage(redirectAttributes,"error","audit_failed");
LogUtils.saveLog(request, null, e, null);
}
}
}
}else {
Page<CfgIndexInfo> searchPage=new Page<CfgIndexInfo>(request,response,"r");
Page<CfgIndexInfo> auditPage=new Page<CfgIndexInfo>(request,response,"r");
try {
BeanUtils.copyProperties(searchPage, auditPage);
auditAll(auditPage,isValid , cfg);
addMessage(redirectAttributes,"success", "audit_success");
} catch (Exception e) {
logger.error("策略对象组下发失败:",e);
if(e instanceof MaatConvertException) {
addMessage(redirectAttributes,"error", "request_service_failed");
LogUtils.saveLog(request, null, e, null);
}else {
addMessage(redirectAttributes,"error", "audit_failed");
LogUtils.saveLog(request, null, e, null);
}
}
return list(model, cfg, request, response);
}
return "redirect:" + adminPath +"/objgroup/list?functionId="+functionId;
}
private String formatMultSelect(Object requestParameter){
if(StringUtil.isEmpty(requestParameter)){
return null;
}else if(requestParameter.getClass().isArray()){
String[] array=(String[])requestParameter;
StringBuffer buf=new StringBuffer(",");
for(String param:array){
buf.append(param).append(",");
}
return buf.toString().length()==1?null:buf.toString();
}else{
return ","+requestParameter.toString()+",";
}
}
@RequestMapping(value = { "/ajaxGetNames" })
@ResponseBody
public Map<String,Map<String,String>> ajaxGetNames(Model model, @RequestParam(required=true,value="groupIds")String group) {
String _group=group.replaceAll("\\|",",");
Map<String,Map<String,String>> datas=new HashMap<>();
datas.put("ipGroup",new HashMap<String,String>());
datas.put("urlGroup",new HashMap<String,String>());
datas.put("subscribeIdGroup",new HashMap<String,String>());
datas.put("domainGroup",new HashMap<String,String>());
if(StringUtils.isNotBlank(group)){
List<PolicyGroupInfo> infos=policyGroupInfoService.findPolicyByServiceGroupInfoList(_group);
for(PolicyGroupInfo info:infos){
for(String g:group.split("\\|")){
if(objectGroupService.indexOfContains(g,",",info.getServiceGroupId().toString())){
Map<String,String> map=null;
if(info.getGroupType().equals(Constants.IP_OBJ_GROUP_TYPE)){
map=datas.get("ipGroup");
}else if(info.getGroupType().equals(Constants.URL_OBJ_GROUP_TYPE)){
map=datas.get("urlGroup");
}else if(info.getGroupType().equals(Constants.SUBID_OBJ_GROUP_TYPE)){
map=datas.get("subscribeIdGroup");
}else if(info.getGroupType().equals(Constants.DOMAIN_OBJ_GROUP_TYPE)){
map=datas.get("domainGroup");
}
map.put(g,map.containsKey(g)?map.get(g)+info.getGroupName()+",":info.getGroupName()+",");
}
}
}
}
//删除最后的逗号
for(Map<String,String> m:datas.values()){
for(Map.Entry<String,String> e:m.entrySet()){
if(e.getValue().endsWith(",")){
m.put(e.getKey(),e.getValue().substring(0,e.getValue().length()-1));
}
}
}
return datas;
}
//http配置导出
@RequestMapping(value = "exportObjGroup")
public void exportdomain(Model model, HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("cfg") ObjGroupCfg entity, String ids, RedirectAttributes redirectAttributes){
try {
//export data info
List<String> titleList=new ArrayList<String>();
Map<String, Class<?>> classMap=new HashMap<String, Class<?>>();
Map<String, List> dataMap=new HashMap<String, List>();
Map<String, String> noExportMap = new HashMap<String, String>();
List<ObjGroupCfg> lists = new ArrayList<ObjGroupCfg>();
Properties msgProp = getMsgProp();
// 导出选中记录
if (!StringUtil.isEmpty(ids)) {
lists = objectGroupService.getObjGroupList(ids,msgProp,entity.getFunctionId());
} else {
Page<ObjGroupCfg> pageInfo=new Page<ObjGroupCfg>(request, response,"a");
pageInfo.setPageNo(1);
pageInfo.setPageSize(Constants.MAX_EXPORT_SIZE);
Page<ObjGroupCfg> page = objectGroupService.getObjGroup(pageInfo, entity,msgProp);
lists = page.getList();
}
for(ObjGroupCfg c:lists){
if(StringUtils.isNotBlank(c.getIpGroup())&&c.getIpGroup().startsWith(",")){
c.setIpGroup(c.getIpGroup().substring(1));
}
if(StringUtils.isNotBlank(c.getUrlGroup())&&c.getUrlGroup().startsWith(",")){
c.setUrlGroup(c.getUrlGroup().substring(1));
}
if(StringUtils.isNotBlank(c.getSubscribeIdGroup())&&c.getSubscribeIdGroup().startsWith(",")){
c.setSubscribeIdGroup(c.getSubscribeIdGroup().substring(1));
}
if(StringUtils.isNotBlank(c.getDomainGroup())&&c.getDomainGroup().startsWith(",")){
c.setDomainGroup(c.getDomainGroup().substring(1));
}
}
titleList.add(entity.getMenuNameCode());
classMap.put(entity.getMenuNameCode(), ObjGroupCfg.class);
String cfgIndexInfoNoExport=",block_type,do_log,policy_name,group_name,userregion1,userregion2,userregion3,userregion4,userregion5,&action:block_type-";
// 时间过滤
if (entity.getSearch_create_time_start() == null) {
cfgIndexInfoNoExport = ",config_time" + cfgIndexInfoNoExport;
}
if (entity.getSearch_edit_time_start() == null) {
cfgIndexInfoNoExport = ",edit_time" + cfgIndexInfoNoExport;
}
if (entity.getSearch_audit_time_start() == null) {
cfgIndexInfoNoExport = ",audit_time" + cfgIndexInfoNoExport;
}
if (!StringUtil.isEmpty(entity.gethColumns())) {
cfgIndexInfoNoExport = "," + entity.gethColumns() + "," + cfgIndexInfoNoExport;
}
noExportMap.put(entity.getMenuNameCode(),cfgIndexInfoNoExport);
dataMap.put(entity.getMenuNameCode(), lists);
String timeRange = initTimeMap(entity);
noExportMap.put("timeRange", timeRange);
if ("csv".equals(entity.getExType())) {
this._exportCsv(model, request, response, redirectAttributes, entity.getMenuNameCode(), titleList,
classMap, dataMap, noExportMap);
} else {
this._export(model, request, response, redirectAttributes, entity.getMenuNameCode(), titleList,
classMap, dataMap, noExportMap);
}
} catch (Exception e) {
logger.error("Obj group export failed",e);
addMessage(redirectAttributes,"error", "export_failed");
LogUtils.saveLog(request, null, e, null);
}
}
}