UnTerminateLotAction.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.unterminatelot;

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.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 UnTerminateLotAction 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 unTerminateLot(ActionMapping mapping, DummyForm theform, HttpServletRequest request,
                                        HttpServletResponse response) {

        String user = LocalContext.getUserId();
        Long facilityRrn = LocalContext.getFacilityRrn();
        String language = I18nUtils.getCurrentLanguage().toString();
        long userRRN = WebUtils.getParameterLong("userId", request);
        String userName = userRRN > 0 ? securityService.getUser(userRRN).getUserName() : "";

        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());

        // if user click the Cancel button
        if (request.getParameter(Constants.CANCEL_KEY) != null) {
            request.setAttribute("lotId", theform.getLotId());
            return mapping.findForward("lotlocation");
        } else if (request.getParameter(Constants.MODIFY_KEY) != null) {
            String reason = TransactionNames.UN_SCRAP_FINISH_TERMINATED + " " + theform.getReasonCode() + " " +
                    StringUtils.trimToEmpty(WebUtils.getParameterUpperCase("deptExt", request)) + " " +
                    StringUtils.trimToEmpty(userName) + " " + request.getParameter("reason");
            TransReason transReason = new TransReason();
            transReason.setReasonCode(theform.getReasonCode());
            transReason.setReasonCategory(TransactionNames.UNTERMINATELOT_KEY);
            transReason.setReason(reason);
            transReason.setResponsibility(userName);
            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.UNTERMINATELOT_KEY);
            transInfo.put("comments", request.getParameter("comments"));
            transInfo.put("areaDepartment", request.getParameter("areaDepartment"));

            String targetCarrierId = StringUtils.trimToUpperCase(theform.getTargetCarrierId());
            Assert.isFalse(StringUtils.isEmpty(targetCarrierId),
                           Errors.create().key(MessageIdList.CARRIER_CASSETTE_EMPTY).content("晶舟号不能为空!").build());

            this.carrierService.compareRealAndDummyCarrierAndCheckRealCarrierAvailabile(facilityRrn, lot.getCarrierId(),
                                                                                        targetCarrierId);

            Long targetCarrierRrn = carrierService.getCarrierRrnForLot(facilityRrn, lot.getQty1(), targetCarrierId);
            transInfo.put("targetCarrierRrn", targetCarrierRrn);

            // List<Lot> lockLots = new ArrayList<Lot>();
            // erpService.lotInfoForShip(lot.getLotId(), user, facilityRrn);
            // lockLots.add(lot);
            // checkAndCreateLotsTransLock(LocalContext.getUserRrn(), TransactionNames.LOCK_UNTERMINATE, lockLots,
            //                             "UnTerminate lot in UnTerminateLotAction by: " + user);
            lotService.unTerminateLot(transInfo);
        }

        this.setTheFormSession(theform, request, null, null, null);

        String lotInfoFlag = StringUtils.trimToEmpty(request.getParameter("lotInfoFlag"));
        if (StringUtils.isEmpty(lotInfoFlag)) {
            theform.setLotId("");
            return new ActionForward(mapping.getInput());

        } else {
            /*request.getRequestDispatcher(
                    "/lotLocation4CSECAction.do?qry=lotlocation&lotId=" + WebUtils.getParameterUpperCase(
                            "baseLotId", request)).forward(request, response);*/
            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));
        }
    }

}

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