EntityPmScheduleViewDetailAction.java

package com.mycim.webapp.actions.equipmentpm;


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.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.TransactionLog;
import com.mycim.valueobject.ems.Checklist;
import com.mycim.valueobject.ems.ChecklistJob;
import com.mycim.valueobject.ems.Entity;
import com.mycim.valueobject.ems.PmSchedule;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.EntityPmScheduleForm;
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.sql.Timestamp;

/**
 * PmViewDetail action
 *
 * @author pinyan.song
 * @version 6.0.0
 * @date 2019-12-5 17:00
 **/
public class EntityPmScheduleViewDetailAction extends EmsSetupAction {

    public ActionForward cancelPm(ActionMapping mapping, EntityPmScheduleForm theform, HttpServletRequest request,
                                  HttpServletResponse response) {
        String comments = theform.getComments();

        Assert.isFalse(comments == null || "".equals(comments.trim()),
                       Errors.create().key(MessageIdList.ENTITYPM_COMMENTS_MISSING).content("Note cannot be empty!")
                             .build());

        PmSchedule pms = new PmSchedule();
        pms.setSequenceNumber(WebUtils.getParameterLong("sequenceNumber", request));
        pms.setEntityRrn(WebUtils.getParameterLong("entityRrn", request));
        pms.setUnit(request.getParameter("unit"));
        pms.setFrequence(request.getParameter("frequence"));
        pms.setNextEventTime(request.getParameter("nextEventTime"));

        pmsService.updatePmScheduleNext(pms);
        modifyCheckListJob(pms, theform.getChecklistId(), theform.getEquipmentId());

        TransactionLog transactionLog = baseService.startTransactionLog(LocalContext.getUserId(), "CANCELPMSCHEDULE");
        transactionLog.setComments(comments);
        baseService.markTransactionLog(transactionLog);

        if ("month".equalsIgnoreCase(request.getParameter("pageFlag"))) {
            return (mapping.findForward("monthnext"));
        } else if (StringUtils.equalsIgnoreCase(theform.getActionType(), "carrier")) {
            return (mapping.findForward("eqptPmDetail"));
        } else {
            return (mapping.findForward("weeknext"));
        }
    }

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        return cancel(mapping, form, request, response);
    }

    public ActionForward detail(ActionMapping mapping, EntityPmScheduleForm theform, HttpServletRequest request,
                                HttpServletResponse response) {

        long checklistRrn = theform.getChecklistRrn();
        long entityRrn = theform.getEntityRrn();
        long sequenceNum = theform.getSequenceNumber();
        PmSchedule pms = pmsService.selectPMScheduleDetail(checklistRrn, entityRrn, sequenceNum);
        setPmscheduleForm(pms, theform);
        request.setAttribute(mapping.getName(), theform);
        return mapping.getInputForward();
    }

    private void modifyCheckListJob(PmSchedule pm, String checklistId, String entityId) {
        Checklist checklist = new Checklist(checklistId,
                                            getNamedSpace(ObjectList.ALARM_KEY, LocalContext.getFacilityRrn()),
                                            ObjectList.CHECKLIST_KEY);
        checklist = emsService.getChecklist(checklist);
        long checklistRrn = checklist.getInstanceRrn();
        long entityRrn = pm.getEntityRrn();

        pm = pmsService.selectPMScheduleDetail(checklistRrn, entityRrn, pm.getSequenceNumber());

        Timestamp startTimestamp = new Timestamp(System.currentTimeMillis());

        ChecklistJob checklistJob = new ChecklistJob();

        checklistJob.setChecklistId(checklistId);
        checklistJob.setEntityRrn(entityRrn);
        checklistJob.setChecklistRrn(checklistRrn);
        checklistJob.setChecklistJobState(STARTED);
        checklistJob.setStartTimestamp(startTimestamp);
        checklistJob.setTransPerformedby(LocalContext.getUserId());
        checklistJob.setTransId(Constants.CREATE_KEY);
        // Entity entity = new Entity();
        // entity.setInstanceRrn(entityRrn);
        // entity.setInstanceId(entityId);
        Entity entity = new Entity(entityId, getNamedSpace(ObjectList.ENTITY_KEY, LocalContext.getFacilityRrn()),
                                   ObjectList.ENTITY_KEY);
        entity = (Entity) getInstance(entity);

        deleteSchedule(pm, entity);
        addSchedule(pm, entity, checklistJob);
    }

    private void setPmscheduleForm(PmSchedule pms, EntityPmScheduleForm form) {
        String nextEventTime = pms.getNextEventTime();
        int i = nextEventTime.indexOf(" ");
        if (i != -1) {
            form.setNextEventTimeDate(nextEventTime.substring(0, i));
            form.setNextEventTimeTime(nextEventTime.substring(i + 1));
        }
        form.setEquipmentId(pms.getEquipmentId());
        form.setEquipmentDesc(pms.getEquipmentDesc());
        form.setNamedSpace(pms.getEquipmentNamedSpace());
        form.setNextEventTime(nextEventTime);
        form.setScheduleId(pms.getScheduleId());
        form.setScheduleDescription(pms.getScheduleDescription());
        form.setChecklistId(pms.getChecklistId());
        form.setChecklistDesc(pms.getChecklistDescription());
        form.setFrequence(pms.getFrequence());
        form.setUnit(pms.getUnit());
        form.setSequenceNumber(pms.getSequenceNumber());
        form.setEntityRrn(pms.getEntityRrn());
    }

}