PMCounterByRecipeAction.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.CollectionUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.prp.Recipe;
import com.mycim.valueobject.prp.RecipeVersion;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.RootForm;
import com.mycim.webapp.utils.TriggerAlarmHelper;
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.*;

/**
 * @author Johnson.Wang
 * @version 6.0.0
 * @date 2019/10/10
 **/
public class PMCounterByRecipeAction extends WipSetupAction {

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

        request.getAttribute("runcardLotId");
        Map parameters = (Map) request.getAttribute(SessionNames.PARAMETERSINFO_KEY);
        RootForm theform = (RootForm) form;
        if (parameters == null) {
            parameters = (Map) WebUtils.getCacheString2Obj(theform.getCacheParametersInfo());
            request.setAttribute(SessionNames.PARAMETERSINFO_KEY, parameters);
        }

        Long recipeRrn = 0L;
        Long eqptRrn = 0L;
        Long eventRrn = 0L;
        Long jobRrn = 0L;

        if (parameters.get("recipeRrn") != null) {
            recipeRrn = new Long((String) parameters.get("recipeRrn"));
        }

        if (parameters.get("eqptRrn") != null) {
            eqptRrn = new Long((String) parameters.get("eqptRrn"));
        }

        if (parameters.get("eventId") != null) {

            eventRrn = this.getInstanceRrn((String) parameters.get("eventId"),
                                           this.getNamedSpace(ObjectList.EVENT_KEY, LocalContext.getFacilityRrn()),
                                           ObjectList.EVENT_KEY);
        }

        if (parameters.get("jobRrn") != null) {
            jobRrn = new Long((String) parameters.get("jobRrn"));
        }

        if (!(recipeRrn == 0L || eqptRrn == 0L)) {
            List<String> chambers = getSubChambleListByRecipe(recipeRrn, this.getNamedSpace(ObjectList.RECIPE_KEY,
                                                                                            LocalContext
                                                                                                    .getFacilityRrn()),
                                                              ObjectList.RECIPE_KEY);

            if (CollectionUtils.isNotEmpty(chambers)) {
                Iterator iter = chambers.iterator();
                while (iter != null && iter.hasNext()) {
                    String subChamberId = this.getInstanceId(eqptRrn) + iter.next();
                    Long subChambleRrn = this.getInstanceRrn(subChamberId, this.getNamedSpace(ObjectList.ENTITY_KEY,
                                                                                              LocalContext
                                                                                                      .getFacilityRrn()),
                                                             ObjectList.ENTITY_KEY);
                    updateEqptPMCounter(subChambleRrn, eventRrn, recipeRrn, jobRrn, LocalContext.getUserRrn());

                }
            }
        }

        parameters.put(SessionNames.RUNSTEP_FLAG, "0");
        request.getRequestDispatcher(mapping.findForward("workflow").getPath()).forward(request, response);
        return null;
    }

    private List<String> getSubChambleListByRecipe(Long recipeRrn, String nameSpace, String object) {
        if (recipeRrn == null || nameSpace == null || object == null) {
            return null;
        }

        RecipeVersion recipeVersion;
        String chambers = "";

        Recipe recipe = recipeService.getRecipe(recipeRrn);
        Assert.isFalse(recipe == null, Errors.create().key(MessageIdList.RECIPE_NOT_DEFINED)
                                             .content("The recipe is not defined in the system!").build());

        recipeVersion = recipeService.getActiveRecipeVersion(recipe);
        chambers = recipeVersion.getSetupId();
        Assert.isFalse(chambers == null, Errors.create().content("This recipe version has not document!").build());

        chambers = chambers.toUpperCase().trim();

        if (chambers.length() > 0) {

            int l = chambers.length();
            int i = 0;

            char[] ch0 = new char[256];
            char[] ch1 = chambers.toCharArray();

            while (i < 2 * l) {
                ch0[i] = i % 2 == 0 ? ch1[i / 2] : ',';
                i++;
            }

            chambers = new String(ch0);
            chambers = chambers.substring(0, i);

        }
        return this.getSubChambleList(chambers);
    }

    private List<String> getSubChambleList(String strChambles) {

        List<String> subChambleList = new ArrayList<>();
        int lastFlag;
        String subChamble;
        while (true) {

            if ("".equalsIgnoreCase(strChambles) || strChambles == null) {
                break;
            }

            lastFlag = strChambles.lastIndexOf(',');

            if (lastFlag >= 0) {
                subChamble = strChambles.substring(lastFlag + 1);
                if (StringUtils.isNotEmpty(subChamble)) {
                    subChambleList.add(subChamble.startsWith("CH") ? subChamble : "CH" + subChamble);
                }

                strChambles = strChambles.substring(0, lastFlag);

            } else {
                subChamble = strChambles;
                subChambleList.add(subChamble.startsWith("CH") ? subChamble : "CH" + subChamble);
                break;
            }

        }

        return subChambleList;
    }


    private void updateEqptPMCounter(Long entityRrn, Long eventRrn, Long recipeRrn, Long jobRrn, Long userRrn) {

        List<Map> alarmList = emsService.updateEqptPMCounter(entityRrn, recipeRrn, eventRrn, jobRrn);
        Iterator alarmIt = alarmList.iterator();

        HashMap map;
        String alarmId;
        Long checkListRrn;

        while (alarmIt.hasNext()) {
            map = (HashMap) alarmIt.next();

            alarmId = (String) map.get("alarmId");
            checkListRrn = (Long) map.get("checkListRrn");

            Map triggerMap = new HashMap();

            triggerMap.put("eqptRrn", entityRrn);

            triggerMap.put("alarmId", alarmId);

            triggerMap.put("fromUserRrn", userRrn);

            Map mapInfo = alarmService.getTriggerAlarmWithWorkFlowInfo(entityRrn, alarmId);

            triggerMap.putAll(mapInfo);
            triggerMap.put("userRrn", userRrn);

            String subject = "" + getInstanceId(entityRrn);

            String text = "The equipment " + subject;

            triggerMap.put("userRrn", userRrn);

            if (checkListRrn != null) {
                text = text + " need do checklist " + getInstanceId(checkListRrn) + " !";
            } else {
                text = text + " will need to check!";
            }
            triggerMap.put("alarmText", text);
            triggerMap.put("subject", subject);

            TriggerAlarmHelper helper = new TriggerAlarmHelper();
            helper.triggerAlarmWithWorkFlow(triggerMap);
        }

    }

}