ShowInstructionAction.java

package com.mycim.webapp.actions.operation.run;

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.collections.MapUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.prp.ContextValue;
import com.mycim.valueobject.wip.Job;
import com.mycim.valueobject.wip.Lot;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.RootForm;
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 final class ShowInstructionAction extends WipSetupAction {

    @Override
    public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {

        RootForm theform = (RootForm) form;

        HashMap parameters = (HashMap) request.getAttribute(SessionNames.PARAMETERSINFO_KEY);
        if (parameters == null) {
            parameters = (HashMap) WebUtils.getCacheString2Obj(theform.getCacheParametersInfo());
        }
        Assert.isFalse(parameters == null, Errors.create().content("Parameters are not enough!").build());
        if (request.getParameter("show") != null) {

            validateTask(parameters, "showinstruction");

            return createRemark(mapping, theform, request, response);
        }

        Job job = (Job) request.getAttribute("job");
        if (job == null) {
            job = (Job) WebUtils.getCacheString2Obj(theform.getCacheJob());
        }
        Collection lots = (Collection) request.getAttribute(SessionNames.COLLECTION_KEY);
        if (lots == null) {
            lots = (Collection) WebUtils.getCacheString2Obj(theform.getCacheCollection());
        }
        Assert.isFalse(job == null || lots == null, Errors.create().content("Parameters are not enough!").build());

        boolean isInstruction = false;
        ContextValue contextValue = new ContextValue();
        Collection contextValues = new ArrayList();


        HashMap item = new HashMap();
        String actionPoint = MapUtils.getStringCheckNull(parameters, "actionPoint");
        for (Iterator it = lots.iterator(); it.hasNext(); ) {
            item = (HashMap) it.next();


            Collection<ContextValue> instructionByProducts = getInstruction("product", item, actionPoint);
            Collection<ContextValue> instructionByProcesss = getInstruction("process", item, actionPoint);
            Collection<ContextValue> instructionByLots = getInstruction("lot", item, actionPoint);
            Map instruction = new HashMap();
            StringBuffer productSb = new StringBuffer();
            StringBuffer processSb = new StringBuffer();
            StringBuffer lotSb = new StringBuffer();
            String notice = "";
            Iterator<ContextValue> _it = null;
            if (instructionByProducts != null && instructionByProducts.size() > 0) {
                for (_it = instructionByProducts.iterator(); _it.hasNext(); ) {
                    ContextValue instructionByProduct = _it.next();
                    notice = instructionByProduct != null ? instructionByProduct.getResultValue1() : "";
                    productSb.append(notice + "; ");
                }
            }
            if (instructionByProcesss != null && instructionByProcesss.size() > 0) {
                for (_it = instructionByProcesss.iterator(); _it.hasNext(); ) {
                    ContextValue instructionByProcess = _it.next();
                    notice = instructionByProcess != null ? instructionByProcess.getResultValue1() : "";
                    processSb.append(notice + "; ");
                }
            }
            if (instructionByLots != null && instructionByLots.size() > 0) {
                for (_it = instructionByLots.iterator(); _it.hasNext(); ) {
                    ContextValue instructionByLot = _it.next();
                    notice = instructionByLot != null ? instructionByLot.getResultValue1() : "";
                    lotSb.append(notice + "; ");
                }
            }

            if ((productSb != null && productSb.length() > 0) || (processSb != null && processSb.length() > 0) ||
                    (lotSb != null && lotSb.length() > 0)) {
                isInstruction = true;

                instruction.put("instructionByProduct", productSb.toString());
                instruction.put("instructionByProcess", processSb.toString());
                instruction.put("instructionByLot", lotSb.toString());
                contextValues.add(instruction);
            }
        }

        if (isInstruction == true) {
            theform.setCacheJob(WebUtils.getCacheObj2String(job));
            theform.setCacheCollection(WebUtils.getCacheObj2String(lots));
            theform.setCacheParametersInfo(WebUtils.getCacheObj2String(parameters));
            request.setAttribute("instructions", contextValues);
            return new ActionForward(mapping.getInput());
        }
        parameters.put(SessionNames.RUNSTEP_FLAG, "0");
        request.setAttribute(SessionNames.PARAMETERSINFO_KEY, parameters);
        request.setAttribute("currentlots", WebUtils.getCacheString2Obj(theform.getCacheCurrentlots()));
        request.setAttribute(SessionNames.JOB_KEY, job);
        request.setAttribute(SessionNames.COLLECTION_KEY, lots);
        request.getRequestDispatcher(mapping.findForward("workflow").getPath()).forward(request, response);
        return null;
    }

    private ActionForward createRemark(ActionMapping mapping, RootForm theform, HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {
        Collection lots = (Collection) request.getAttribute(SessionNames.COLLECTION_KEY);
        if (lots == null) {
            lots = (Collection) WebUtils.getCacheString2Obj(theform.getCacheCollection());
        }
        HashMap parameters = (HashMap) request.getAttribute(SessionNames.PARAMETERSINFO_KEY);
        if (parameters == null) {
            parameters = (HashMap) WebUtils.getCacheString2Obj(theform.getCacheParametersInfo());
        }

        HashMap item = new HashMap();
        String actionPoint = MapUtils.getStringCheckNull(parameters, "actionPoint");
        for (Iterator it = lots.iterator(); it.hasNext(); ) {
            item = (HashMap) it.next();

            List<ContextValue> instructionByProducts = getInstruction("product", item, actionPoint);
            List<ContextValue> instructionByProcesss = getInstruction("process", item, actionPoint);
            List<ContextValue> instructionByLots = getInstruction("lot", item, actionPoint);

            Lot lot = lotQueryService.getLot(MapUtils.getString(item, "lotId"), LocalContext.getFacilityRrn());
            if (instructionByProducts != null && instructionByProducts.size() > 0) {
                for (int i = 0; i < instructionByProducts.size(); i++) {
                    ContextValue instructionByProduct = instructionByProducts.get(i);
                    Assert.isFalse(StringUtils.isNotEmpty(instructionByProduct.getResultValue3()) &&
                                           StringUtils.equalsIgnoreCase("Y", instructionByProduct.getResultValue3()) &&
                                           StringUtils.isEmpty(WebUtils.getParameter("remark", request)),
                                   Errors.create().key(MessageIdList.SYSTEM_FILL_NOTES).content("Fill in the notes")
                                         .build());
                }
            }
            if (instructionByProcesss != null && instructionByProcesss.size() > 0) {
                for (int i = 0; i < instructionByProcesss.size(); i++) {
                    ContextValue instructionByProcess = instructionByProcesss.get(i);
                    Assert.isFalse(StringUtils.isNotEmpty(instructionByProcess.getResultValue3()) &&
                                           StringUtils.equalsIgnoreCase("Y", instructionByProcess.getResultValue3()) &&
                                           StringUtils.isEmpty(WebUtils.getParameter("remark", request)),
                                   Errors.create().key(MessageIdList.SYSTEM_FILL_NOTES).content("Fill in the notes")
                                         .build());
                }
            }
            if (instructionByLots != null && instructionByLots.size() > 0) {
                for (int i = 0; i < instructionByLots.size(); i++) {
                    ContextValue instructionByLot = instructionByLots.get(i);
                    Assert.isFalse(StringUtils.isNotEmpty(instructionByLot.getResultValue3()) &&
                                           StringUtils.equalsIgnoreCase("Y", instructionByLot.getResultValue3()) &&
                                           StringUtils.isEmpty(WebUtils.getParameter("remark", request)),
                                   Errors.create().key(MessageIdList.SYSTEM_FILL_NOTES).content("Fill in the notes")
                                         .build());
                }
            }

            if (StringUtils.isNotEmpty(WebUtils.getParameter("remark", request))) {
                lotService.addLotStepHistoryComment(MapUtils.getString(item, "lotRrn"),
                                                    StringUtils.toString(lot.getStepSequence()),
                                                    WebUtils.getParameter("remark", request), null,
                                                    LocalContext.getUserId(), LocalContext.getFacilityRrn());
            }

        }

        parameters.put(SessionNames.RUNSTEP_FLAG, "0");

        request.setAttribute(SessionNames.PARAMETERSINFO_KEY, parameters);
        request.setAttribute("currentlots", WebUtils.getCacheString2Obj(theform.getCacheCurrentlots()));
        request.setAttribute(SessionNames.JOB_KEY, WebUtils.getCacheString2Obj(theform.getCacheJob()));
        request.setAttribute(SessionNames.COLLECTION_KEY, lots);
        request.getRequestDispatcher(mapping.findForward("workflow").getPath()).forward(request, response);
        return null;
    }

}