EquipmentPartListAction.java

package com.mycim.webapp.actions.equipment;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.jdbc.Page;
import com.mycim.framework.utils.lang.BooleanUtils;
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.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.EquipmentPartRelation;
import com.mycim.valueobject.bas.Relation;
import com.mycim.valueobject.consts.LinkTypeList;
import com.mycim.valueobject.ems.Entity;
import com.mycim.valueobject.inv.MaterialDO;
import com.mycim.valueobject.prp.Item;
import com.mycim.valueobject.prp.Recipe;
import com.mycim.valueobject.prp.RecipeStatus;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.forms.EntityInfoForm;
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: yibing.liu
 * @Date: 2021/9/1 14:48
 */
public class EquipmentPartListAction extends EquipmentSaveAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        EntityInfoForm theForm = getForm(form);
        Entity theEntity = getEntityInfo(theForm);
        Assert.isFalse(theEntity == null || theEntity.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.EQUIPMENT_MISSING_ID).build());
        if (StringUtils.isNotBlank(WebUtils.getParameter("equipmentReadOnlyFlag", request))) {
            request.setAttribute("equipmentReadOnlyFlag", 1);
        }

        if (StringUtils.isNotEmpty(theEntity.getChamberType())) {
            Assert.isFalse((theEntity.getParentEntityRrn() != null && theEntity.getParentEntityRrn() != 0),
                           Errors.create().key(MessageIdList.CHAMBER_CANNOT_SET).content("Sub chamber equipment cannot set available part!").build());
        }

        List<EquipmentPartRelation> partList = emsService.getFullRelationForPart(theEntity.getInstanceRrn(), LinkTypeList.ENTITY_TO_PART);

        request.setAttribute("partList", partList);
        theForm.setTransId("modify");
        return mapping.getInputForward();
    }




    public ActionForward addPartBind(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                             HttpServletResponse response) throws Exception {
        EntityInfoForm theForm = (EntityInfoForm) form;
        Entity theEntity = getEntityInfo(theForm);

        String partId = StringUtils.trimToUpperCase(theForm.getPartId());
        Assert.isFalse(theEntity == null || theEntity.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.EQUIPMENT_MISSING_ID).build());
        Assert.isFalse(StringUtils.isBlank(partId),
                       Errors.create().key(MessageIdList.PART_ID_NOT_EMPTY).build());

        MaterialDO part = asmService.getMaterial(LocalContext.getFacilityRrn(), partId);
        Assert.isFalse(part == null || part.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.PART_ID_NOT_EXIST_2).args(partId).build());

        List<EquipmentPartRelation> partList = emsService.getFullRelationForPart(theEntity.getInstanceRrn(), LinkTypeList.ENTITY_TO_PART);
        if (CollectionUtils.isNotEmpty(partList)) {
            for (Relation r : partList) {
                Assert.isFalse(r.getToRrn() == part.getInstanceRrn(),
                               Errors.create().key(MessageIdList.PART_ID_HAS_EXIST).args(partId).build());
            }
        }

        Relation relation = new Relation();
        relation.setFromRrn(theEntity.getInstanceRrn());
        relation.setLinkType(LinkTypeList.ENTITY_TO_PART);
        relation.setAttributedata1(RecipeStatus.ON_KEY);
        relation.setAttributedata2(String.valueOf(LocalContext.getUserRrn()));
        relation.setAttributedata3(DateUtils.formatDate(new Date()));
        relation.setToRrn(part.getInstanceRrn());
        relation.setTransPerformedby(LocalContext.getUserId());
        relation.setTransId(Constants.ADD_KEY);

        emsService.insertRelation4EntityToPart(relation);

        theForm.setPartId("");
        WebUtils.setSuccessMsg(request);
        return init(mapping, theForm, request, response);
    }

    public ActionForward deletePartBind(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {
        EntityInfoForm theForm = getForm(form);
        Entity theEntity = getEntityInfo(theForm);
        Assert.isFalse(theEntity == null || theEntity.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.EQUIPMENT_MISSING_ID).build());
        Long partRrn = new Long(request.getParameter(Constants.ITEM_KEY));

        Relation relation = emsService.getRelationForPart(theEntity.getInstanceRrn(), partRrn, LinkTypeList.ENTITY_TO_PART);
        if (relation != null) {
            relation.setFromRrn(theEntity.getInstanceRrn());
            relation.setToRrn(partRrn);
            relation.setLinkType(LinkTypeList.ENTITY_TO_PART);
            relation.setLastUpdatedUser(LocalContext.getUserRrn());
            relation.setTransPerformedby(LocalContext.getUserId());
            relation.setTransId(Constants.DELETE_KEY);

            emsService.deleteRelation4EntityToPart(relation);
        }
        WebUtils.setSuccessMsg(request);
        return init(mapping, theForm, request, response);
    }

    public ActionForward partsHistory(ActionMapping mapping, ActionForm form, HttpServletRequest request) {
        EntityInfoForm theForm = getForm(form);
        if (StringUtils.isNotBlank(request.getParameter("flag"))) {
            request.setAttribute("flag", "flag");
        }
        return mapping.findForward(Constants.HISTORY_KEY);
    }

    public Map<String, Object> query(Map<String, Object> params) {
        String entityId = StringUtils.trimToUpperCase(MapUtils.getString(params, "instanceId"));
        Entity theEntity = emsService.getEntity(
                new Entity(entityId, getNamedSpace(ObjectList.ENTITY_KEY, LocalContext.getFacilityRrn()), ObjectList.ENTITY_KEY));

        int pageNo = MapUtils.getInteger(params, "currentPage");
        if (pageNo <= 0) {
            pageNo = 1;
        }

        int pageSize = 10;

        Page page = new Page(pageNo, pageSize);
        page = emsService.getRelationHistoryByEntity2Part(theEntity.getInstanceRrn(), page);

        Map<String, Object> result = new HashMap<>();

        result.put("pageSize", page.getNextPage());
        result.put("pageNum", page.getPageNo());
        result.put("maxPage", page.getTotalPages());
        result.put("startRowNum", page.getStartRow());

        result.put("isFirstPage", page.isFirstPage());
        result.put("hasPreviousPage", page.isHasPrePage());
        result.put("isLastPage", page.getTotalPages() == 0 || page.isLastPage());
        result.put("hasNextPage", page.isHasNextPage());

        result.put("list", page.getResults());


        return result;
    }

}