ViewUnitBondedHistoryAction.java

package com.mycim.webapp.actions.lot.lotbondinginfo;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.Unit;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.unit.UnitInfoForm;
import org.apache.commons.beanutils.PropertyUtils;
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.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author yanbing.chen
 * @date 2019/12/23
 * @since 1.8
 **/
public class ViewUnitBondedHistoryAction extends WipSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, new UnitInfoForm());
        request.setAttribute(SessionNames.COLLECTION_KEY, new ArrayList());
        request.removeAttribute(mapping.getAttribute());
        return mapping.findForward("view");
    }

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

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

    public ActionForward viewUnitBondedHistory(ActionMapping mapping, UnitInfoForm theform, HttpServletRequest request,
                                               HttpServletResponse response) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        String unitId = theform.getUnitId().toUpperCase().trim();

        String lotId = WebUtils.getParameter("lotId", request).toUpperCase().trim();

        Assert.isTrue(validateInput(lotId) || validateInput(unitId),
                      Errors.create().content("Please enter at least one wafer " + "Id or lot Id.").build());

        Unit unit = wipQueryService.getUnit(facilityRrn, unitId);
        Lot lot = lotQueryService.getLot(lotId, facilityRrn);

        /*
         * modified for sitri on 2017-8-16 as the query is able to execute by lot if the unitId input is
         * not found;
         */
        boolean queryByLot = unitId == null || unitId.equals("");
        Assert.isFalse(queryByLot && (lot == null || lot.getLotRrn() <= 0),
                       Errors.create().content("Lot {} {}").args(facilityRrn, lotId).build());

        Assert.isFalse(!queryByLot && (unit == null || unit.getUnitRrn() <= 0),
                       Errors.create().content("Lot {} {}").args(facilityRrn, unitId).build());

        Lot lotByUnit = queryByLot ? lot : lotQueryService.getLot(unit.getLotRrn().longValue());
        Collection bondedLotInfos = queryByLot ? qryBondedLotInfos(lot) : qryBondedLotInfos(unit);
        if (StringUtils.isBlank(unitId) && !queryByLot) {
            lotByUnit = lot;
            bondedLotInfos = qryBondedLotInfos(lot);
        }
        String allPriority = wipQueryService
                .getHotflagSplicingPriority(NumberUtils.toInt(lot.getHotFlag()), lot.getPriority(), facilityRrn);
        request.setAttribute("allPriority", allPriority);
        theform.setUnitId(StringUtils.isBlank(unitId) ? "" : unit.getUnitId());
        theform.setLotId(lotByUnit.getLotId());
        theform.setCarrierId(lotByUnit.getCarrierId());
        theform.setQty1(lotByUnit.getQty1());
        PropertyUtils.copyProperties(theform, lotByUnit);
        theform.setRecipePhysicalId(getRecipePhysicalId(lotByUnit));
        request.setAttribute(SessionNames.LOT_KEY, lotByUnit);
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, theform);
        request.setAttribute(SessionNames.COLLECTION_KEY, bondedLotInfos);
        return mapping.findForward("view");
    }

    public ActionForward viewByUnitBondedHistory(ActionMapping mapping, UnitInfoForm theform,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String unitId = theform.getUnitId().toUpperCase().trim();
        Unit unit = wipQueryService.getUnit(facilityRrn, unitId);

        Assert.isFalse(unit == null || unit.getUnitRrn() <= 0, Errors.create().content("Unit Not Found!").build());

        Lot lot = lotQueryService.getLot(unit.getLotRrn().longValue());

        theform.setUnitId(unit.getUnitId());
        theform.setLotId(lot.getLotId());
        theform.setCarrierId(lot.getCarrierId());
        theform.setQty1(lot.getQty1());
        PropertyUtils.copyProperties(theform, lot);
        theform.setRecipePhysicalId(getRecipePhysicalId(lot));
        request.setAttribute(SessionNames.LOT_KEY, lot);
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, theform);

        Collection bondedLotInfos = qryByBondedLotInfos(unit);
        request.setAttribute(SessionNames.COLLECTION_KEY, bondedLotInfos);
        return mapping.findForward("view");
    }

    private List<Map> qryBondedLotInfos(Unit unit) {

        return lotQueryService.qryBondedLotInfos(unit);
    }

    private List<Map> qryBondedLotInfos(Lot lot) {

        return lotQueryService.qryBondedLotInfos(lot);
    }

    private List<Map> qryByBondedLotInfos(Unit unit) throws Exception {

        return lotQueryService.qryByBondedLotInfos(unit);
    }

    private boolean validateInput(String data) {
        return !(data == null || data.trim().equals(""));
    }

}