RouteSaveAction.java

package com.mycim.webapp.actions.route;

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.utils.beans.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.ObjectVersion;
import com.mycim.valueobject.prp.ProcessPlanning;
import com.mycim.valueobject.prp.Route;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.VersionForm;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Route Save
 *
 * @author Johnson.Wang
 * @version 6.0.0
 * @date 2019/8/29
 **/
public class RouteSaveAction extends PrpSetupAction {

    private static final String DASH = "--";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        VersionForm versionForm = (VersionForm) form;

        String routeId = StringUtils.isEmpty(versionForm.getInstanceId()) ? "" : versionForm.getInstanceId().trim()
                                                                                            .toUpperCase();
        Assert.isFalse(routeId.contains(DASH),
                       Errors.create().key(MessageIdList.ROUTE_CONTAIN_DASH).content("Can't contain --").build());
        Route route = new Route(routeId, getNamedSpace(ObjectList.WFL_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.WFL_KEY);

        route = (Route) getInstance(route);
        Assert.isFalse(StringUtils.isNotEmpty(route.getSystemUsed()),
                       Errors.create().key(MessageIdList.ROUTE_DUMMY).content("SubPlan: {} is DUMMY SubPlan!")
                             .args(routeId).build());

        PropertyUtils.copyProperties(versionForm, route);

        versionForm.setTransId(Constants.CREATE_KEY);

        if (route.getInstanceRrn() > 0) {
            Assert.isFalse(
                    route.getObjectType() == null || !route.getObjectType().equalsIgnoreCase(ObjectList.ROUTE_KEY),
                    Errors.create().key(MessageIdList.ROUTE_VALIDATE_TYPE).content("It is not subPlan!").build());

            Assert.isFalse(route.getObjectSubtype() == null ||
                                   !route.getObjectSubtype().equalsIgnoreCase(ObjectList.PROCEDURE_KEY),
                           Errors.create().key(MessageIdList.ROUTE_VALIDATE_TYPE).content("It is not subPlan!")
                                 .build());
            versionForm.setTransId(Constants.MODIFY_KEY);
            //处理是否是从基本信息界面发起的请求,而不是其他 tab
            if (!WebUtils.getParameterBoolean("homePage", request)) {
                versionForm.setNpwType(route.getAttribute3());
            }
            //判断NPWproduct 和 普通 product 是否对应菜单
            if ((StringUtils.isBlank(route.getAttribute3()) &&
                    StringUtils.equalsIgnoreCase(versionForm.getNpwType(), Constants.NPW_TYPE)) ||
                    (StringUtils.isBlank(versionForm.getNpwType()) &&
                            StringUtils.equalsIgnoreCase(route.getAttribute3(), Constants.NPW_TYPE))) {
                versionForm.setInstanceId("");
                request.setAttribute("errorMsgAlert", I18nUtils.getMessage("route.cant_edit_this_menu",
                                                                           "Route:{} not allowed to edit in " + "this" +
                                                                                   " menu! ", route.getInstanceId()));
                return init(mapping, form, request, response);
            }
        }

        List<ObjectVersion> versions = baseService.getObjectVersions(route);

        versionForm.setVersionEditEnable(String.valueOf(checkVersionEditEnable(versions)));
        versionForm.setObjectDeleteEnable(checkObjectDeleteEnable(versions));

        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward create(ActionMapping mapping, VersionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        String routeId = StringUtils.isEmpty(form.getInstanceId()) ? "" : form.getInstanceId().trim().toUpperCase();
        Assert.isFalse(routeId.contains(DASH),
                       Errors.create().key(MessageIdList.ROUTE_CONTAIN_DASH).content("Can't contain --").build());

        Assert.isFalse(StringUtils.isEmpty(form.getInstanceId()),
                       Errors.create().key(MessageIdList.ROUTE_EMPTY_ID).content("Route Id empty").build());

        Assert.isFalse(NumberUtils.isCreatable(routeId) && routeId.matches("^[0-9]*$"),
                       Errors.create().key(MessageIdList.ROUTE_PURE_NUMBER).content("路径号不能纯数字").build());

        Route route = new Route(routeId, getNamedSpace(ObjectList.WFL_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.WFL_KEY);

        Assert.isFalse(getInstanceRrn(routeId, LocalContext.getFacilityRrn(), ObjectList.WFL_KEY) > 0,
                       Errors.create().key(MessageIdList.ROUTE_INVALID_SAME).content("路径已存在").build());

        form.setInstanceId(routeId);
        PropertyUtils.copyProperties(route, form);
        route.setAttribute3(form.getNpwType());
        route.setObjectType(ObjectList.ROUTE_KEY);
        route.setObjectSubtype(ObjectList.PROCEDURE_KEY);

        if (form.getReworkFlag() != null) {
            route.setReworkFlag(form.getReworkFlag().trim());
        }
        if (form.getDisableFlag() != null) {
            route.setDisableFlag(form.getDisableFlag().trim());
        }

        route.setTransPerformedby(LocalContext.getUserId());
        route.setTransId(Constants.CREATE_KEY);

        prpService.insertRoute(route);

        WebUtils.setSuccessMsg(request);
        return init(mapping, form, request, response);
    }

    public ActionForward update(ActionMapping mapping, VersionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        Route route = new Route(form.getInstanceId(), getNamedSpace(ObjectList.WFL_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.WFL_KEY);

        route = (Route) getInstance(route);
        PropertyUtils.copyProperties(route, form);
        if (form.getReworkFlag() != null) {
            route.setReworkFlag(form.getReworkFlag().trim());
        } else {
            route.setReworkFlag(null);
        }
        if (form.getDisableFlag() != null) {
            route.setDisableFlag(form.getDisableFlag().trim());
        } else {
            route.setDisableFlag(null);
        }

        route.setTransPerformedby(LocalContext.getUserId());
        route.setTransId(Constants.MODIFY_KEY);

        prpService.updateRoute(route);

        WebUtils.setSuccessMsg(request);
        return init(mapping, form, request, response);
    }

    public ActionForward copy(ActionMapping mapping, VersionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        // the new Id is stored in the url COPY key
        Route route = new Route(request.getParameter(Constants.COPY_KEY),
                                getNamedSpace(ObjectList.WFL_KEY, LocalContext.getFacilityRrn()), ObjectList.WFL_KEY);
        Assert.isFalse(StringUtils.isEmpty(form.getInstanceId()) || StringUtils.isBlank(form.getInstanceId()),
                       Errors.create().key(MessageIdList.ROUTE_ENTER_ROUTE_ID)
                             .content("Please enter the sub plan id first!").build());

        doCopy(form, route);

        // for logic equal tag, copy is the same action as create
        form.setTransId(Constants.CREATE_KEY);

        return (mapping.findForward(Constants.MODIFY_KEY));
    }

    public ActionForward delete(ActionMapping mapping, VersionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        Route route = new Route(form.getInstanceId(), getNamedSpace(ObjectList.WFL_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.WFL_KEY);

        prpService.deleteRoute(baseService.getNamedObjectRrn(route));

        form.setInstanceId(null);
        WebUtils.setSuccessMsg(request);
        return init(mapping, form, request, response);
    }

    public ActionForward useInfo(ActionMapping mapping, VersionForm versionInfo,
                                 HttpServletRequest request) throws Exception {
        Route route = new Route(versionInfo.getInstanceId(),
                                getNamedSpace(ObjectList.WFL_KEY, LocalContext.getFacilityRrn()), ObjectList.WFL_KEY);

        route = (Route) getInstance(route);

        PropertyUtils.copyProperties(versionInfo, route);

        List<ProcessPlanning> usedByProcess = prpService.getUsedByProcess(route.getInstanceRrn());
        if (CollectionUtils.isNotEmpty(usedByProcess)) {
            List<Map> wflLists = new ArrayList<>();
            for (ProcessPlanning product : usedByProcess) {
                Map<String, Object> wfl = new HashMap<>(4);
                wfl.put("processId", product.getInstanceId());
                wfl.put("processDesc", product.getInstanceDesc());
                wfl.put("processRrn", product.getInstanceRrn());
                wfl.put("processVersion", product.getCurrentVersion());
                wflLists.add(wfl);
            }
            request.setAttribute("processLists", wflLists);
        }
        return mapping.findForward("routewheretouse");
    }

}