TerminateLotSelectAction.java

/*
 *        @ Copyright 2001 FA Software;
 *        All right reserved. No part of this program may be reproduced or
 *        transmitted in any form or by any means, electronic or
 *        mechanical, including photocopying, recording, or by any
 *        information storage or retrieval system without written
 *        permission from FA Software, except for inclusion of brief
 *        quotations in a review.
 */
package com.mycim.webapp.actions.lot.terminatelot;

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.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.prp.Operation;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.DummyForm;
import com.mycim.webapp.forms.lot.LotInfoForm;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.compress.utils.Sets;
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.HashMap;
import java.util.List;
import java.util.Map;

public class TerminateLotSelectAction extends WipSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        String language = I18nUtils.getCurrentLanguage().toString();
        long facilityRrn = LocalContext.getFacilityRrn();
        String username = LocalContext.getUserId();
        String lotInfoFlag = request.getParameter("lotInfoFlag");
        boolean readOnly = StringUtils.isNotBlank(lotInfoFlag) ? true : false;
        initLotBaseInfoForJsp(request, readOnly, LotStatus.HOLD, null);

        if (WebUtils.getParameterBoolean(Constants.NAV_KEY, request)) {
            request.setAttribute("lotInfo", new LotInfoForm());
            return new ActionForward(mapping.getInput());
        }

        DummyForm theform = (DummyForm) form;

        Map<String, Object> cachePageInfo = (Map<String, Object>) WebUtils
                .getCacheString2Obj(theform.getCachePageInfo());
        if (cachePageInfo == null) {
            cachePageInfo = new HashMap<String, Object>();
        }

        // Populate the new valid format id
        String lotId = StringUtils.EMPTY;
        lotId = getLotIdFromRequest(request, "byCarrierId");
        String msg = "";
        if (StringUtils.equalsIgnoreCase("CN", language)) {
            msg = WebUtils.getParameter("byCarrierId", request) != null ? "晶舟号" : "批次号";
        } else {
            msg = WebUtils.getParameter("byCarrierId", request) != null ? "Cassette Id" : "lot Id";
        }
        Assert.isFalse((lotId == null) || lotId.trim().equalsIgnoreCase(""),
                       Errors.create().key(MessageIdList.LOT_LOTID_OR_CARRIERID_INCORRECT).content("批次号或晶舟号不正确!")
                             .args(msg).build());

        // Construct a new value object that stores the id,named space(implicit, derived),object(hard coded)
        lotId = lotId.toUpperCase();

        Lot lot = lotQueryService.getLot(lotId, facilityRrn);

        //卡控post future hold被hold住的lot
        checkPostFutureHold(lot.getLotRrn());

        super.checkCstRemoved(Sets.newHashSet(lot.getLotRrn()));

        Assert.isFalse(lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.LOT_MISSING_ID).content("No such lotId").build());

        Assert.isFalse(
                lot.getLotStatus() == null || lot.getLotStatus() != null && !lot.getLotStatus().equals(LotStatus.HOLD),
                Errors.create().key(MessageIdList.LOT_LOT_STATUS_MUSTBE_HOLD).content("批次必须为暂停状态!").build());

        Assert.isFalse(StringUtils.equalsIgnoreCase("P", lot.getLotType()),
                       Errors.create().content("The lot type should not be" + " P!").build());

        Assert.isFalse(diffBatchQueryService.checkLotInBatch(lot.getLotRrn()),
                       Errors.create().key(MessageIdList.SCRAPLOT_LOT_BATCH).content("Lot in batch!").build());
        String lossTable = null;
        Operation operation = new Operation();
        Long operRrn = lot.getOperationRrn();

        //check sortJob
        checkWaitJobs(lot.getCarrierRrn(), 0L, 0L, null);

        Assert.isTrue(operRrn != null && operRrn.intValue() != 0,
                      Errors.create().key(MessageIdList.LOT_LOT_PARAMETER_ERROR).content("批次参数错误!").build());
        operation = prpService.getOperation(operRrn);
        if (operation.getLossTableId() != null && operation.getLossTableId() != "") {
            lossTable = operation.getLossTableId();
        }
        // populate lot information for next page
        LotInfoForm lotInfoForm = new LotInfoForm();

        PropertyUtils.copyProperties(lotInfoForm, lot);
        lotInfoForm.setRecipePhysicalId(getRecipePhysicalId(lot));

        request.setAttribute("lotInfoFlag", StringUtils.trimToEmpty(lotInfoFlag));
        cachePageInfo.put("lossTable", lossTable);

        this.setTheFormSession(theform, request, lot, lotInfoForm, cachePageInfo);

        return new ActionForward(mapping.getInput());
    }

    /**
     * 在request中获得LotId,如果请求是一个carrier Id查询的额请求,那么会通过carrier Id去返回lotId
     *
     * @param request
     * @param byCarrierFlag
     * @return
     * @throws Exception
     */
    public String getLotIdFromRequest(HttpServletRequest request, String byCarrierFlag) {
        String result = null;
        boolean carrierFlag = request.getParameter(byCarrierFlag) != null;
        String carrierId = carrierFlag ? request.getParameter("carrierId") : StringUtils.EMPTY;
        carrierId = carrierId != null ? carrierId.trim().toUpperCase() : StringUtils.EMPTY;
        Assert.isFalse(carrierFlag == true && carrierId.length() < 1,
                       Errors.create().key(MessageIdList.CARRIER_CASSETTE_ID_MISSING).content("晶舟号不存在!").build());
        if (carrierFlag == true) {
            Long carrierRrn = this.getInstanceRrn(carrierId, LocalContext.getFacilityRrn(), ObjectList.ENTITY_KEY);
            if (carrierRrn > 0L) {
                List<String> lotIds = carrierService.getInUseCarrierLotIdList(carrierRrn);
                Assert.isFalse(lotIds == null || lotIds.size() != 1,
                               Errors.create().key(MessageIdList.CARRIER_CASSETTE_HAVE_SOME_LOTS).content("该晶舟内有批次")
                                     .args(lotIds == null ? 0 : lotIds.size()).build());
                result = lotIds.get(0);
            }
        } else {
            result = request.getParameter("lotId") == null ? request.getParameter("instanceId") : request
                    .getParameter("lotId");
        }
        return result != null ? result.trim().toUpperCase() : StringUtils.EMPTY;
    }

    protected void setTheFormSession(DummyForm theform, HttpServletRequest request, Lot lot, LotInfoForm lotInfo,
                                   Map<String, Object> cachePageInfo) {
        if (lot == null) {
            lot = (Lot) WebUtils.getCacheString2Obj(theform.getCacheLot());
        }
        theform.setCacheLot(WebUtils.getCacheObj2String(lot));
        request.setAttribute(SessionNames.LOT_KEY, lot);


        if (lotInfo == null) {
            lotInfo = (LotInfoForm) WebUtils.getCacheString2Obj(theform.getCacheLotInfo());
        }
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, lotInfo);
        theform.setCacheLotInfo(WebUtils.getCacheObj2String(lotInfo));

        if (cachePageInfo == null) {
            cachePageInfo = (Map<String, Object>) WebUtils.getCacheString2Obj(theform.getCachePageInfo());
        }
        if (cachePageInfo == null) {
            cachePageInfo = new HashMap<String, Object>();
        }
        theform.setCachePageInfo(WebUtils.getCacheObj2String(cachePageInfo));
        for (String key : cachePageInfo.keySet()) {
            request.setAttribute(key, MapUtils.getObject(cachePageInfo, key));
        }
    }

}

/*
 * Modification Log Log No : Name : Modified Date: Description :
 */