CreateChecklistJobAction.java

package com.mycim.webapp.actions.checklist;

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.consts.SessionNames;
import com.mycim.valueobject.ems.Checklist;
import com.mycim.valueobject.ems.ChecklistJob;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.DoChecklistInfoForm;
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;

/**
 * 创建检查单
 *
 * @author pinyan.song
 * @version 6.0.0
 * @date 2019-11-29 14:48
 **/
public class CreateChecklistJobAction extends EmsSetupAction {

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

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        form = new DoChecklistInfoForm();
        return super.cancel(mapping, form, request, response);
    }

    public ActionForward view(ActionMapping mapping, DoChecklistInfoForm theform, HttpServletRequest request,
                              HttpServletResponse response) {
        ChecklistJob instance = this.getChecklistJob(theform);
        return setTheForm(instance, theform, mapping, request);
    }

    public ActionForward create(ActionMapping mapping, DoChecklistInfoForm theform, HttpServletRequest request,
                                HttpServletResponse response) {
        String checklistId = theform.getChecklistId().trim().toUpperCase();
        Checklist checklist = new Checklist(checklistId,
                                            getNamedSpace(ObjectList.CHECKLIST_KEY, LocalContext.getFacilityRrn()),
                                            ObjectList.CHECKLIST_KEY);
        checklist = (Checklist) this.getInstance(checklist);

        Assert.isFalse(checklist == null || checklist.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
                             .args("checkList").build());

        ChecklistJob instance = new ChecklistJob();
        instance.setEntityRrn(theform.getEntityRrn());
        instance.setChecklistJobId(theform.getChecklistJobId());
        instance.setComments(theform.getComments());
        instance.setChecklistRrn(checklist.getInstanceRrn());
        instance.setStartTimestamp(new Timestamp(System.currentTimeMillis()));
        instance.setTriggerTimestamp(new Timestamp(System.currentTimeMillis()));
        instance.setChecklistJobState(STARTED);
        instance.setTransId(Constants.CREATE_KEY);
        instance.setTransPerformedby(LocalContext.getUserId());
        instance.setTriggerPerformedBy(LocalContext.getUserId());

        instance.setStartEventRrn(checklist.getStartEventRrn());
        instance.setEndEventRrn(checklist.getEndEventRrn());

        instance.setAttri1(theform.getAttri1());
        instance.setAttri2(theform.getAttri2());
        instance.setAttri3(theform.getAttri3());
        instance.setAttri4(theform.getAttri4());
        instance.setCreatePerformedBy(LocalContext.getUserId());
        instance.setChecklistJobRrn(emsService.insertChecklistJob(instance));

        return setTheForm(instance, theform, mapping, request);
    }

    private ActionForward setTheForm(ChecklistJob instance, DoChecklistInfoForm theform, ActionMapping mapping,
                                     HttpServletRequest request) {
        instance = emsService.getChecklistJob(instance);
        theform.setChecklistId(instance.getChecklistId());
        theform.setChecklistJobRrn(instance.getChecklistJobRrn());
        theform.setEntityId(instance.getEntityId());
        theform.setChecklistJobState(instance.getChecklistJobState());
        theform.setComments(instance.getComments());

        theform.setAttri1(instance.getAttri1());
        theform.setAttri2(instance.getAttri2());
        theform.setAttri3(instance.getAttri3());
        theform.setAttri4(instance.getAttri4());

        theform.setCompletePerformedBy(instance.getCompletePerformedBy());
        theform.setCreatePerformedBy(instance.getCreatePerformedBy());

        theform.setStartEventId(instance.getStartEventId());
        theform.setEndEventId(instance.getEndEventId());

        instance.setTriggerPerformedBy(LocalContext.getUserId());
        instance.setTriggerTimestamp(new Timestamp(System.currentTimeMillis()));

        if (!StringUtils.equalsIgnoreCase(Constants.CANCEL_KEY, theform.getActionType())) {
            logEventWhenPmStart(instance);
        }

        request.setAttribute(SessionNames.CHECKLISTJOB_KEY, instance);
        theform.setPages(1);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    private void logEventWhenPmStart(ChecklistJob instance) {
        String status = instance.getChecklistJobState();
        if ("STARTED".equalsIgnoreCase(status) && !instance.getFirstLogFlag()) {
            boolean logRights = false;
            long userrrn = LocalContext.getUserRrn();
            if (StringUtils.equalsIgnoreCase(ObjectList.CARRIER_KEY, instance.getEntityType())) {
                logRights = true;
            } else {
                logRights = emsService
                        .checkLogRights(instance.getEntityId(), getInstanceId(instance.getStartEventRrn()), userrrn,
                                        ObjectList.EQUIPMENT_KEY);
            }
            Assert.isTrue(logRights, Errors.create().key(MessageIdList.ENTITYPM_NO_ACCESS_OR_STATUS_ERROR)
                                           .content("你无权登录该事件或设备状态与该事件不符,不能进行检查作业").build());
            // log event when first check the job
            instance.setFirstLogFlag(true);
            emsService.logEventWhenPMStart(instance);
        }

    }

}