TerminateLotAction.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.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.TransReason;
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.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;

public class TerminateLotAction extends WipSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        String lotInfoFlag = StringUtils.trimToEmpty(request.getParameter("lotInfoFlag"));
        DummyForm theform = (DummyForm) form;
        if (StringUtils.isEmpty(lotInfoFlag)) {
            theform.setLotId("");
            return mapping.findForward("init");

        } else {
            request.setAttribute("lotId", theform.getLotId());
            return mapping.findForward("lotlocation");
        }
    }

    // --------------------------------------------------------- Public Methods
    public ActionForward terminateLot(ActionMapping mapping, DummyForm theform, HttpServletRequest request,
                                      HttpServletResponse response) {
        String user = LocalContext.getUserId();
        Long facilityRrn = LocalContext.getFacilityRrn();
        Long userRrn = LocalContext.getUserRrn();
        String lotInfoFlag = StringUtils.trimToEmpty(request.getParameter("lotInfoFlag"));
        Map<String, Object> cachePageInfo = (Map<String, Object>) WebUtils
                .getCacheString2Obj(theform.getCachePageInfo());
        if (cachePageInfo == null) {
            cachePageInfo = new HashMap<String, Object>();
        }

        // Lot lot = (Lot) session.getAttribute(SessionNames.LOT_KEY);
        Lot lot = (Lot) WebUtils.getCacheString2Obj(theform.getCacheLot());
        checkLotInfo(request, lot, theform.getLotId(), theform.getCarrierId());
        wipQueryService.checkActiveInlineOcapId(lot.getLotRrn());
        // if user click the Cancel button
        if (request.getParameter(Constants.CANCEL_KEY) != null) {
            if (StringUtils.isEmpty(lotInfoFlag)) {
                theform.setLotId("");
                return mapping.findForward("init");
            } else {
                request.setAttribute("lotId", theform.getLotId());
                return mapping.findForward("lotlocation");
            }
        } else if (request.getParameter(Constants.MODIFY_KEY) != null) {
            String reason = theform.getReasonCode() + " " + theform.getDeptExt().trim().toUpperCase() + " " +
                    request.getParameter("reason");
            TransReason transReason = new TransReason();
            transReason.setReasonCode(theform.getReasonCode());
            transReason.setReasonCategory(TransactionNames.TERMINATELOT_KEY);
            transReason.setReason(reason);
            String userName = "";
            userName = lotQueryService.getUserName(user);
            if (userName != null) {
                transReason.setResponsibility(user + " " + userName);
            } else {
                transReason.setResponsibility(user);
            }
            transReason.setAccountCode(request.getParameter("accountCode"));
            transReason.setTransQty1(lot.getQty1());
            transReason.setTransQty2(lot.getQty2());

            HashMap transInfo = new HashMap();
            transInfo.put("transReason", transReason);
            transInfo.put("reasonCode", theform.getReasonCode());

            transInfo.put("lotRrn", new Long(lot.getLotRrn()));
            transInfo.put("lotId", lot.getLotId());
            transInfo.put("operationId", lot.getOperationId());
            transInfo.put("user", user);
            transInfo.put("transId", TransactionNames.TERMINATELOT_KEY);
            transInfo.put("comments", request.getParameter("comments"));
            transInfo.put("facilityRrn", facilityRrn);
            transInfo.put("areaDepartment", request.getParameter("areaDepartment"));

            //erpService.lotInfoForShip(lot.getLotId(), user, facilityRrn);
            // List<Lot> lockLots = new ArrayList<Lot>();
            // lockLots.add(lot);
            // checkAndCreateLotsTransLock(userRrn, TransactionNames.LOCK_TERMINATE, lockLots,
            //                             "Terminate lot in TerminateLotAction by: " + user);
            lotService.terminateLot(transInfo);
            removeEqptlotCache(lot, facilityRrn, userRrn);
            // TODO runcard暂时不考虑
                /*List<LotRunCard> runcardList = wipQueryService.getEffectiveLotRuncardByLot(lot.getLotRrn());
                for(LotRunCard runcard : runcardList) {
                    lotRuncardService.forceWithdrawRunCard(facilityRrn, user, userRrn, runcard
                    .getRunCardRrn());
                }*/

        }

        this.setTheFormSession(theform, request, null, null, null);
        if (StringUtils.isEmpty(lotInfoFlag)) {
            theform.setLotId("");
            return new ActionForward(mapping.getInput());

        } else {
            request.setAttribute("lotId", theform.getLotId());
            return mapping.findForward("lotlocation");
        }

    } // end of perform()

    private 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));
        }
    }

    private void checkLotInfo(HttpServletRequest request, Lot lot, String formLotId, String formCarrierId) {
        String lotInfoFlag = WebUtils.getParameter("lotInfoFlag", request);
        String formCarrierIdTemp = com.mycim.framework.utils.lang.StringUtils.equals(lotInfoFlag, "1") ? WebUtils
                .getParameter("baseCarrierId", request) : com.mycim.framework.utils.lang.StringUtils
                .trimToUpperCase(formCarrierId);
        Assert.isFalse(!com.mycim.framework.utils.lang.StringUtils
                               .equals(lot.getLotId(),
                                       com.mycim.framework.utils.lang.StringUtils.trimToUpperCase(formLotId)) ||
                               !com.mycim.framework.utils.lang.StringUtils
                                       .equals(com.mycim.framework.utils.lang.StringUtils
                                                       .defaultString(lot.getCarrierId()), formCarrierIdTemp),
                       Errors.create().key(MessageIdList.SCRAPLOT_LOTID_ERROR).content("Invalid ID!").build());
    }

    protected void removeEqptlotCache(Lot lot, long facilityRrn, Long userRrn) {
        //TODO CacheHelper相关更新
            /*Collection availableEquipmentInfos = buildAvailableEquipInfosForLot(facilityRrn, lot, userRrn);
            CacheHelper cacheHelper = AppContext.getBean("cacheHelper", CacheHelper.class);
            for (Object eqpts : availableEquipmentInfos) {
                cacheHelper.remove(MapUtils.getLongValue((Map) eqpts, "equipmentRrn") + "");
            }*/
    }

}

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