PMCounterAction.java

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

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.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
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.HashMap;
import java.util.List;
import java.util.Map;

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

    @Override
    public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        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 = null;
        Long eventRrn = null;
        Long jobRrn = null;
        int flag = -1;

        if (StringUtils.isNotEmpty(parameters.get("recipeRrn").toString())) {
            String recipeString = (String) parameters.get("recipeRrn");
            flag = recipeString.indexOf('|');
            if (flag >= 0) {
                recipeString = recipeString.substring(0, flag);
                recipeRrn = new Long(recipeString);
            } else {
                recipeRrn = new Long((String) parameters.get("recipeRrn"));
            }
        }

        eqptRrn = new Long((String) parameters.get("eqptRrn"));

        String eventId = (String) parameters.get("eventId");

        eventRrn = this.getInstanceRrn(eventId, this.getNamedSpace(ObjectList.EVENT_KEY, LocalContext.getFacilityRrn()),
                                       ObjectList.EVENT_KEY);

        jobRrn = new Long((String) parameters.get("jobRrn"));

        // step 1
        updateEqptPMCounter(eqptRrn, eventRrn, recipeRrn, jobRrn, LocalContext.getUserRrn());

        // step 2
        List<Map<String, Object>> cascadingEvent = baseService.getCascadingEvent(eqptRrn, eventRrn);


        if (CollectionUtils.isNotEmpty(cascadingEvent)) {
            for (Map<String, Object> cascadingMap : cascadingEvent) {
                Long entityRrn = (Long) cascadingMap.get("entityRrn");
                if (entityRrn != null) {
                    updateEqptPMCounter(entityRrn, eventRrn, recipeRrn, jobRrn, LocalContext.getUserRrn());
                }
            }

        }

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

        request.getRequestDispatcher(mapping.findForward("workflow").getPath()).forward(request, response);
        return null;
    }


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

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

        // if alarmList is not null,trigger the alarm

        String alarmId = "";
        Long checkListRrn = null;

        for (Map map : alarmList) {
            alarmId = (String) map.get("alarmId");
            checkListRrn = (Long) map.get("checkListRrn");

            Map<String, Object> triggerMap = new HashMap<>();

            triggerMap.put("equipmentRrn", entityRrn);

            triggerMap.put("alarmId", alarmId);

            triggerMap.put("fromUserRrn", userRrn);

            if ("COUNTER_ALARM".equalsIgnoreCase(alarmId)) {// CSEC:触发COUNTER_ALARM警报时,hold设备
                emsService.holdEntity4PM(entityRrn);
            }

            Map<String, Object> 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);

        }

    }

}