NpwCreateLotEditAction.java

package com.mycim.webapp.actions.createlot;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.i18n.Languages;
import com.fa.sesa.threadlocal.LocalContext;
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.consts.TransactionNames;
import com.mycim.valueobject.prp.Item;
import com.mycim.valueobject.sys.ObjectTransAttribute;
import com.mycim.valueobject.wip.PlanLot;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.NpwSetupAction;
import com.mycim.webapp.forms.LotInfoFormNpw;
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.*;

public class NpwCreateLotEditAction extends NpwSetupAction {

    private static final String DEFAULT_LOT_OWNER = "PIE";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        LotInfoFormNpw theform = (LotInfoFormNpw) form;
        boolean createLotFlag = true;
        String screenStatus = createLotFlag == true ? "CREATE_PLAN_LOT" : "LOT_PLAN_START";
        LotInfoFormNpw emptyForm = new LotInfoFormNpw();
        emptyForm.setScreenStatus(screenStatus);
        request.setAttribute("create_lot_flag", String.valueOf(""));
        theform.setScreenStatus("CREATE_PLAN_LOT");
        request.setAttribute("lotInfoForm", emptyForm);
        request.setAttribute("productRrn", 0);
        theform.setLotId("");
        if (createLotFlag == true) {
            theform.setByPlanLotFlag("product");
        }
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward npwCreateLotEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                          HttpServletResponse response) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String language = I18nUtils.getCurrentLanguage().toString();
        LotInfoFormNpw theform = (LotInfoFormNpw) form;

        String id = theform.getCreatedPlanLotId().trim().toUpperCase();
        request.setAttribute("create_lot_flag", String.valueOf(""));
        theform.setScreenStatus("CREATE_PLAN_LOT");
        if ("planLot".equals(theform.getByPlanLotFlag())) {
            PlanLot planLot = new PlanLot(facilityRrn, id);
            planLot = getPlanLot(planLot);

            Assert.isFalse((planLot == null) || (planLot.getLotRrn() == null) ||
                                   (planLot.getLotRrn().compareTo(new Long(0)) <= 0),
                           Errors.create().key(MessageIdList.LOT_ID_EMPTY).content("批次号不能为空!").build());

            theform.setCreatedPlanLotId(planLot.getLotId());
            theform.setCreatedPlanLotRrn(planLot.getLotRrn());
            theform.setProductId(planLot.getProductId());

            if (planLot.getPriority() != null) {
                theform.setPriority(new Integer(planLot.getPriority().intValue()));
            }
            theform.setDueDateS(planLot.getDate());
            theform.setLotOwner(planLot.getLotOwner());
            theform.setHotFlag(planLot.getHotFlag());
            theform.setLotType(planLot.getLotType());
            theform.setQty1(planLot.getQty1());

            request.setAttribute("productRrn", planLot.getProductRrn());

        } else if ("product".equals(theform.getByPlanLotFlag())) {
            theform.setProductId(id);
            String lotId = StringUtils.EMPTY;
            Item item = new Item(theform.getProductId(), getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn),
                                 ObjectList.PRODUCT_KEY);

            item = prpService.getItem(item);
            Assert.notNull(item, Errors.create().key(MessageIdList.PRODUCT_PRODUCT_MISSING).content("产品不存在!").build());
            Assert.isFalse(StringUtils.equalsIgnoreCase("DUMMY", item.getObjectType()) != true,
                           Errors.create().key(MessageIdList.NPWCREATELOT_TYPE_NOT_BE_DUMMY)
                                 .content("非DUMMY产品不能使用DUMMY投片!").build());
            Collection reticleGroups = prpService.getProductReticleGroups(item.getInstanceRrn());
            request.setAttribute(SessionNames.RETICLE_GROUP, reticleGroups);
            request.setAttribute("productRrn", new Long(item.getInstanceRrn()));

            ObjectTransAttribute objectTransAttribute = new ObjectTransAttribute("LOT",
                                                                                 getNamedSpace("LOT", facilityRrn),
                                                                                 TransactionNames.CREATE_KEY, null);
            Collection fields = null;

            fields = getObjectTransAttributeFields(objectTransAttribute, language);

            if ((fields != null) && (fields.size() > 0)) {
                request.setAttribute("attributes", fields);
            }

            theform.setHotFlag("1");
            String createCategory = theform.getCreateCategory();
            List<Map<String, String>> lotTypeOptions = new ArrayList<Map<String, String>>();
            Map<String, String> map = new HashMap<String, String>();
            map.put("value", "M");
            map.put("text", "M");
            map.put("selected", "true");
            lotTypeOptions.add(map);

            if (lotTypeOptions.size() > 0) {
                request.setAttribute("lotTypeOptions", lotTypeOptions);
            } else {
                request.removeAttribute("lotTypeOptions");
            }
            theform.setCreateCategory(
                    StringUtils.isNotBlank(createCategory) ? createCategory : getDefaultOptionValue(lotTypeOptions));
            createCategory = theform.getCreateCategory();
            lotId = baseService.generateLotId(item.getObjectType(), createCategory).toUpperCase();
            String createCategoryDesc = sysService
                    .referenceDetailExchange("$LOT_CREATE_CATEGORY", createCategory, null, "data_1_value");
            theform.setLotType(createCategoryDesc);

            theform.setLotOwner(DEFAULT_LOT_OWNER);
            theform.setLotId(lotId);
            theform.setCreateCategory(createCategory);
            String isshow = request.getParameter("isshow");
            request.setAttribute("isshow", isshow);

        }
        return mapping.findForward("modify");
    } // end of perform()

    // -----------------------------------------------------------------------private
    // methods
    private Collection getObjectTransAttributeFields(ObjectTransAttribute objectTransAttribute,
                                                     String language) throws Exception {
        objectTransAttribute.setFieldRrn(null);

        return sysService.getObjectTransAttributes(objectTransAttribute, Languages.valueOf(language));
    }

}