MenuAction.java

package com.mycim.webapp.actions.setting.security.menu.set;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.logging.Logger;
import com.mycim.framework.logging.LoggerFactory;
import com.mycim.framework.utils.beans.BeanUtils;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.framework.utils.lang.ObjectUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.Relation;
import com.mycim.valueobject.consts.LinkTypeList;
import com.mycim.valueobject.security.Menu;
import com.mycim.valueobject.security.MenuTreeNode;
import com.mycim.valueobject.sys.Label;
import com.mycim.valueobject.sys.LabelItem;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.SystemSetupAction;
import com.mycim.webapp.forms.security.MenuInfoForm;
import com.mycim.webapp.forms.security.MenuSetForm;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 菜单设置
 *
 * @author Pinyan Song
 * @version 0.0.1
 * @since 2019-8-19 14:33
 **/
public class MenuAction extends SystemSetupAction {

    private static final Logger log = LoggerFactory.getLogger(MenuAction.class);

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm theform, HttpServletRequest request,
                              HttpServletResponse response) {

        return mapping.getInputForward();
    }

    /**
     * @param theform
     * @return
     */
    public MenuInfoForm getMenu(MenuInfoForm theform) {
        long menuRrn = theform.getMenuRrn();
        Menu menu = new Menu();
        if (menuRrn > 0) {
            menu.setInstanceRrn(menuRrn);
            menu = securityService.getMenu(menu);
            if (menu != null) {
                BeanUtils.copyProperties(menu, theform);
            }
            theform.setTransId(Constants.MODIFY_KEY);
            String contentText = sysService
                    .getMenuLabelText(I18nUtils.getCurrentLanguage(), menu.getLabelRrn(), menu.getInstanceRrn());
            theform.setInstanceDesc(contentText);
            theform.setIcon(StringUtils.trim(menu.getIcon()));
        } else {
            menu = new Menu("", getNamedSpace(ObjectList.MENU_KEY, LocalContext.getFacilityRrn()), ObjectList.MENU_KEY);
            BeanUtils.copyProperties(menu, theform);
            theform.setTransId(Constants.CREATE_KEY);
        }
        return theform;
    }

    public void saveOrUpdateMenu(MenuInfoForm theForm) throws Exception {
        String menuId = StringUtils.trimToUpperCase(theForm.getInstanceId());

        Assert.isFalse(StringUtils.isBlank(menuId),
                       Errors.create().key(MessageIdList.MENU_MISSING_ID).content("菜单号不存在!").build());

        // add by liran 限制父菜单类型不能为PROGRAM类型
        String parentObjectType = StringUtils.trimToUpperCase(theForm.getParentObjectType());
        Assert.state(!StringUtils.equals(ObjectList.MENU_PROGRAM_KEY, parentObjectType),
                     Errors.create().key(MessageIdList.OBJECTTYPE_CANNOT_BE_PROGRAM)
                           .content("选择的父菜单类型不能为Program!").args().build());

        // menu 处理
        Menu menu = new Menu(menuId, getNamedSpace(ObjectList.MENU_KEY, LocalContext.getFacilityRrn()),
                              ObjectList.MENU_KEY);
        menu.setInstanceRrn(theForm.getMenuRrn());
        PropertyUtils.copyProperties(menu, theForm);
        HashMap<String, Object> menuInfoParams = new HashMap<String, Object>();
        menuInfoParams.put("menuId", menuId);
        menuInfoParams.put("parentMenuRrn", theForm.getParentMenuRrn());
        menuInfoParams.put("labelId", theForm.getLabelId());
        menuInfoParams.put("instanceId", theForm.getInstanceId());
        menuInfoParams.put("instanceDesc", theForm.getInstanceDesc());
        // add by liran 为了满足事务的一致性,在service层,包装了老代码的业务逻辑
        securityService.saveOrUpdateMenuRelation(menu, menuInfoParams);
    }

    public void deleteMenu(Map<String, Object> params) {
        String isDelete = MapUtils.getString(params, "isDelete");
        String menuId = MapUtils.getString(params, "menuId");
        Long parentMenuRrn = MapUtils.getLong(params, "parentMenuRrn");
        Assert.isFalse(StringUtils.isBlank(menuId),
                       Errors.create().key(MessageIdList.MENU_MISSING_ID).content("菜单号不存在!").build());
        Assert.isFalse(parentMenuRrn == null,
                       Errors.create().key(MessageIdList.MENU_MISSING_PARENT_MENU_RRN).content("父菜单rrn为空!").build());
        Menu menu = new Menu(menuId, getNamedSpace(ObjectList.MENU_KEY, LocalContext.getFacilityRrn()),
                             ObjectList.MENU_KEY);

        long menuRrn = baseService.getNamedObjectRrn(menu);

        Relation relation = new Relation();
        if (StringUtils.trim(isDelete).equals(Constants.DELETE_KEY)) {
            relation.setTransId(Constants.DELETE_KEY);
        }
        relation.setToRrn(menuRrn);
        relation.setFromRrn(parentMenuRrn);
        relation.setTransPerformedby(LocalContext.getUserId());
        relation.setLinkType(LinkTypeList.MENU_SUBMENU_KEY);

        boolean isExisted = securityService.isExistedRelation(menuRrn);
        if (isExisted) {
            if (StringUtils.trim(isDelete).equals(Constants.DELETE_KEY)) {
                menu.setTransId(Constants.DELETE_KEY);
                menu.setTransPerformedby(LocalContext.getUserId());
            }
            process(menu);
        }
        processDetails(relation);

    }

    public Map qrySecurityMenuList(MenuSetForm theform) {
        String menuId = theform.getMenuId();
        String isLeaf = theform.getIsLeaf();
        List<Map> menuTree;
        long menuRootRrn = this.getInstanceRrn("ROOTMENU", LocalContext.getFacilityRrn(), ObjectList.MENU_KEY);
        if (StringUtils.isNotBlank(menuId)) {
            menuTree = securityService
                    .getSecurityMenuListById(menuRootRrn, I18nUtils.getCurrentLanguage(), menuId, isLeaf);
        } else {
            menuTree = securityService.getSecurityMenuList(menuRootRrn, I18nUtils.getCurrentLanguage());
        }
        Map<String, Object> json = new HashMap<>(2);
        json.put("results", menuTree);
        json.put("rows", menuTree.size());
        return json;
    }

    public List<Map<String, Object>> qrySecurityMenuTree() {
        return securityService.qrySecuritySysParaSetTree(LocalContext.getFacilityRrn(), I18nUtils.getCurrentLanguage());
    }

    private void deleteMenuRelation(long menuRrn, MenuInfoForm theForm) {
        List<Relation> results = baseService.getRelationsUseToRrn(menuRrn, LinkTypeList.MENU_SUBMENU_KEY);

        for (Relation r : results) {
            if (!r.getFromRrn().equals(theForm.getParentMenuRrn())) {
                r.setTransId(Constants.DELETE_KEY);
                processDetails(r);
            }
        }
    }

    private void processLabelItem(MenuInfoForm theForm, Label label) {
        LabelItem labelItem = new LabelItem();
        labelItem.setText(theForm.getInstanceDesc());
        labelItem.setLanguage(I18nUtils.getCurrentLanguage().toString());
        processDetails(label.getInstanceRrn(), labelItem);
    }

    private Label processLabel(MenuInfoForm theForm) {
        String labelName = StringUtils.trimToUpperCase(theForm.getLabelId());
        String autoLabelId = null;

        if (StringUtils.isNotBlank(labelName)) {
            autoLabelId = labelName;
        } else {
            if (StringUtils.isNotBlank(theForm.getInstanceId())) {
                autoLabelId =
                        "LBL_" + StringUtils.trimToUpperCase(StringUtils.substringAfter(theForm.getInstanceId(), "_"));
            }
        }

        // 标签
        Label label = new Label(autoLabelId, getNamedSpace(ObjectList.LABEL_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.LABEL_KEY);
        long labelRrn = baseService.getNamedObjectRrn(label);
        label.setTransPerformedby(LocalContext.getUserId());
        if (labelRrn <= 0) {
            label.setInstanceId(autoLabelId);
            label.setInstanceDesc(theForm.getInstanceDesc());
            label.setObjectType("NONE");
            label.setTransId(Constants.CREATE_KEY);
        } else {
            label.setInstanceRrn(labelRrn);
            label.setTransId(Constants.MODIFY_KEY);
        }
        process(label);

        return label;
    }

    private void processDetails(long labelRrn, LabelItem labelItem) {
        sysService.updateLabelItem(labelRrn, labelItem);
    }

    private void processDetails(Relation relation) {
        String action = relation.getTransId();

        if (action.equals(Constants.CREATE_KEY)) {
            if (baseService.getRelation(relation) != null) {
                return;
            }
            Assert.isFalse(this.isNodeInTree(relation.getToRrn(), relation.getFromRrn()),
                           Errors.create().key(MessageIdList.MENU_FORBID_ADD).content("无法添加此菜单!").build());

            securityService.addSubMenuToMenu(relation);
        } else if (action.equals(Constants.DELETE_KEY)) {
            baseService.deleteRelation(relation);
        }
    }

    private boolean isNodeInTree(Long rootRrn, Long menuRrn) {
        return securityService.isNodeInTree(rootRrn, menuRrn);
    }

    private long getUserMenuRrn(long useRrn) {
        long usemenurrn = 0;
        usemenurrn = securityService.getUserMenuRrn(useRrn);
        return usemenurrn;
    }

}