BORResourceSaveAction.java

package com.mycim.webapp.actions.bor;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
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.time.DateUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.inv.LotInventoryDO;
import com.mycim.valueobject.prp.BOR;
import com.mycim.valueobject.prp.BORResource;
import com.mycim.valueobject.prp.BORVersion;
import com.mycim.valueobject.prp.Item;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.BorVersionInfoForm;
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.List;


public class BORResourceSaveAction extends PrpSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        return mapping.findForward("init");
    }


    public ActionForward saveOrUpdata(ActionMapping mapping, BorVersionInfoForm theform, HttpServletRequest request,
                                      HttpServletResponse response) {
        BOR bor = prpService.getBOR(LocalContext.getFacilityRrn(), theform.getInstanceId());
        BORVersion borVersion = new BORVersion(bor.getInstanceRrn(), Integer.valueOf(theform.getVersionId()));
        borVersion = prpService.getBORVersion(borVersion);


        Assert.isFalse(StringUtils.isEmpty(theform.getLotNumber()),
                       Errors.create().content("Material batch id cannot be empty!").build());

        Assert.isFalse(StringUtils.isEmpty(theform.getLossType()),
                       Errors.create().content("The consumption material type " + "cannot be empty!").build());

        Assert.isFalse(StringUtils.isEmpty(theform.getBasisCode()),
                       Errors.create().content("Material consumption mode cannot " + "be empty!").build());

        BORResource borResource = new BORResource();
        borResource.copyObjectVersion(borVersion);
        borResource.setLotNumber(StringUtils.trimToUpperCase(theform.getLotNumber()));
        borResource.setResourceId(theform.getResourceId());
        borResource.setWarehouseId(theform.getWarehouseId());
        borResource.setLossPercentOrQty(theform.getLossPercentOrQty());
        borResource.setLossType(theform.getLossType());
        borResource.setBasisCode(theform.getBasisCode());
        borResource.setExpirationDate(DateUtils.stringToTimestamp(theform.getExpirationDateStr()));
        List<BORResource> existBorResources = prpService.getBorResources(borVersion);

        String resourceIdTemp = borResource.getResourceId();
        String warehouseIdTemp = borResource.getWarehouseId();
        long resourceRrnTemp = 0;
        long warehouseRrnTemp = 0;
        if ("ITEM".equalsIgnoreCase(bor.getObjectSubtype())) {
            Item item = prpService.getItem(
                    new Item(resourceIdTemp, getNamedSpace(ObjectList.ITEM_KEY, LocalContext.getFacilityRrn()),
                             ObjectList.ITEM_KEY));

            resourceRrnTemp = item.getInstanceRrn();
            warehouseRrnTemp = getInstanceRrn(warehouseIdTemp,
                                              getNamedSpace(ObjectList.OPERATION_KEY, LocalContext.getFacilityRrn()),
                                              ObjectList.OPERATION_KEY);
            borResource.setResourceType("MATERIAL");
        } else {
            resourceRrnTemp = getInstanceRrn(resourceIdTemp,
                                             getNamedSpace(ObjectList.ENTITY_KEY, LocalContext.getFacilityRrn()),
                                             ObjectList.ENTITYGROUP_KEY);
            borResource.setResourceType("RESOURCE");
        }

        Assert.isFalse(resourceRrnTemp <= 0 || warehouseRrnTemp <= 0,
                       Errors.create().content("Check material or wareHouse " + "please!").build());

        borResource.setWarehouseRrn(warehouseRrnTemp);
        borResource.setResourceRrn(resourceRrnTemp);
        LotInventoryDO lotInventoryDO = warehouseService
                .getLotInventory(borResource.getLotNumber(), borResource.getResourceRrn(),
                                 borResource.getWarehouseRrn());
        Assert.notNull(lotInventoryDO,
                       Errors.create().content("Material batch id does not exist! Please check and add " + "again!")
                             .build());
        if (StringUtils.isEqual(theform.getTransId(), "create")) {
            Assert.isFalse(
                    existBorResources.stream().filter(r -> StringUtils.equals(r.getLotNumber(), theform.getLotNumber()))
                                     .count() > 0L, Errors.create().content("Material lot exist!").build());
            borResource.setTransId("create");
        } else if (StringUtils.isEqual(theform.getTransId(), "modify")) {
            borResource.setTransId("modify");
            borResource.setSequenceNumber(theform.getSequenceNumber());
        } else if (StringUtils.isEqual(theform.getTransId(), "delete")) {
            borResource.setTransId("delete");
            borResource.setSequenceNumber(theform.getSequenceNumber());
        }
        borResource.setSubContractorSuppliedFlag(null);
        process(borResource);
        PropertyUtils.copyProperties(theform, bor);
        borVersion.setResources(prpService.getBorResources(borVersion));
        request.setAttribute(SessionNames.BORVERSION_KEY, borVersion);
        theform.setVersionEditEnable(String.valueOf(checkVersionEditEnable(borVersion)));
        theform.setVersionStatus(borVersion.getVersionStatus());
        theform.setTransId("modify");
        return mapping.findForward("modify");

    }

    public ActionForward cancle(ActionMapping mapping, BorVersionInfoForm theform, HttpServletRequest request,
                                HttpServletResponse response) {
        BOR bor = prpService.getBOR(LocalContext.getFacilityRrn(), theform.getInstanceId());
        BORVersion borVersion = new BORVersion(bor.getInstanceRrn(), Integer.valueOf(theform.getVersionId()));
        borVersion = prpService.getBORVersion(borVersion);
        List<BORResource> borResources = prpService.getBorResources(borVersion);
        PropertyUtils.copyProperties(theform, bor);
        borVersion.setResources(borResources);
        request.setAttribute(SessionNames.BORVERSION_KEY, borVersion);
        theform.setVersionEditEnable(String.valueOf(checkVersionEditEnable(borVersion)));
        theform.setVersionStatus(borVersion.getVersionStatus());
        theform.setTransId("modify");
        return mapping.findForward("modify");
    }

}