EventSaveAction.java

/*
 *        @ Copyright 2001 FA Software;
 *        All right reserved. No part of this program may be reproduced or
 *        transmitted in any form or by any means, electronic or
 *        mechanical, including photocopying, recording, or by any
 *        information storage or retrieval system without written
 *        permission from FA Software, except for inclusion of brief
 *        quotations in a review.
 */
package com.mycim.webapp.actions.setting.system.event;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.logging.Logger;
import com.mycim.framework.logging.LoggerFactory;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.ems.Event;
import com.mycim.valueobject.ems.EventModel;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.SystemSetupAction;
import com.mycim.webapp.forms.event.EventInfoForm;
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.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 事件定义
 *
 * @author yanbing.chen
 * @date 2019/8/20
 * @since 1.8
 **/
public class EventSaveAction extends SystemSetupAction {

    private static final Logger log = LoggerFactory.getLogger(EventSaveAction.class);

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

        EventInfoForm theform = (EventInfoForm) form;

        String id = theform.getInstanceId().trim().toUpperCase();

        Long facilityRrn = LocalContext.getFacilityRrn();

        Event event = new Event(id, getNamedSpace(ObjectList.EVENT_KEY, facilityRrn), ObjectList.EVENT_KEY);

        long eventRrn = 0;

        eventRrn = getInstanceRrn(id, getNamedSpace(ObjectList.EVENT_KEY, facilityRrn), ObjectList.EVENT_KEY);
        event.setInstanceRrn(eventRrn);

        theform.setInstanceId(id);
        theform.setNamedSpace(getNamedSpace(ObjectList.EVENT_KEY, facilityRrn));

        if (event.getInstanceRrn() == 0) {
            theform.setTransId(Constants.CREATE_KEY);
        } else {
            List<Event> existEvents = baseService.getEvents(event.getInstanceRrn());
            event = existEvents.size() > 0 ? existEvents.get(0) : null;

            List<EventModel> eventModels = baseService.getEventModelofEvent(event);
            event.setEventModels(eventModels);

            PropertyUtils.copyProperties(theform, event);
            theform.setTransId(Constants.MODIFY_KEY);
        }

        request.setAttribute(SessionNames.EVENT_KEY, event);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        request.removeAttribute(SessionNames.EVENT_KEY);
        request.removeAttribute(SessionNames.EVENTS_KEY);
        request.removeAttribute(mapping.getAttribute());

        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward modify(ActionMapping mapping, EventInfoForm form,
                                HttpServletRequest request) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();

        Event event = new Event(form.getInstanceId(), getNamedSpace(ObjectList.EVENT_KEY, facilityRrn),
                                ObjectList.EVENT_KEY);

        // put formbean value to event object
        PropertyUtils.copyProperties(event, form);

        event.setInstanceRrn(getInstanceRrn(form.getInstanceId(), getNamedSpace(ObjectList.EVENT_KEY, facilityRrn),
                                            ObjectList.EVENT_KEY));

        if (form.getRoleId().trim().length() > 0) {
            long roleRrn = getInstanceRrn(form.getRoleId().trim(), getNamedSpace(ObjectList.ROLE_KEY, facilityRrn),
                                          ObjectList.ROLE_KEY);

            Assert.isFalse(roleRrn <= 0,
                           Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
                                 .args("Role").build());
            event.setRoleRrn(new Long(roleRrn));
        } else {
            event.setRoleRrn(null);
        }

        if (request.getParameter(Constants.CREATE_KEY) != null) {
            event.setTransId(Constants.CREATE_KEY);
        } else if (request.getParameter(Constants.MODIFY_KEY) != null) {
            event.setTransId(Constants.MODIFY_KEY);
        }

        String message = updateEventWorkStream(event, facilityRrn, user);

        Assert.isFalse(message != null, Errors.create().content(message).build());

        if (request.getParameter(Constants.CREATE_KEY) != null) {

            message = updateEventStatusWorkStream(event, facilityRrn, user);

            Assert.isFalse(message != null, Errors.create().content(message).build());
        }
        process(event);
        event.setEventModels(baseService.getEventModelofEvent(event));
        request.setAttribute(SessionNames.EVENT_KEY, event);
        form.setTransId(Constants.MODIFY_KEY);
        WebUtils.setSuccessMsg(request);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward members(ActionMapping mapping, EventInfoForm form,
                                 HttpServletRequest request) throws Exception {
        Event event = new Event(form.getInstanceId(),
                                getNamedSpace(ObjectList.EVENT_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.EVENT_KEY);

        event.setInstanceRrn(
                getInstanceRrn(form.getInstanceId(), getNamedSpace(ObjectList.EVENT_KEY, LocalContext.getFacilityRrn()),
                               ObjectList.EVENT_KEY));
        PropertyUtils.copyProperties(event, form);

        EventModel eventModel = new EventModel();

        eventModel.setStatusGroupId(form.getStatusGroupId());
        eventModel.setValidStatus(form.getValidStatus());
        eventModel.setTargetStatus(form.getTargetStatus());
        eventModel.setModifiableFlag(form.getModifiableFlag());

        processDetails(event.getInstanceRrn(), eventModel);

        event.setEventModels(baseService.getEventModelofEvent(event));

        request.setAttribute(SessionNames.EVENT_KEY, event);
        form.setTransId(Constants.MODIFY_KEY);
        //form.setOperationSuccess("true");
        WebUtils.setSuccessMsg(request);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward changetype(ActionMapping mapping, EventInfoForm form,
                                    HttpServletRequest request) throws Exception {
        Event event = new Event(form.getInstanceId(),
                                getNamedSpace(ObjectList.EVENT_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.EVENT_KEY);

        event.setInstanceRrn(
                getInstanceRrn(form.getInstanceId(), getNamedSpace(ObjectList.EVENT_KEY, LocalContext.getFacilityRrn()),
                               ObjectList.EVENT_KEY));
        PropertyUtils.copyProperties(event, form);

        String entityType = form.getEntityType();

        List<Event> events = baseService.getEvents(event.getInstanceRrn());

        Iterator it = events.iterator();
        Event _temp = null;
        boolean selected = false;

        while (it.hasNext()) {
            _temp = (Event) it.next();

            if (_temp.getEntityType().equalsIgnoreCase(entityType)) {
                selected = true;
                event = _temp;
            }
        }

        if (selected) {
            form.setTransId(Constants.MODIFY_KEY);

            event.setEventModels(baseService.getEventModelofEvent(event));
            request.setAttribute(SessionNames.EVENT_KEY, event);

        } else {
            form.setTransId(Constants.CREATE_KEY);
        }
        WebUtils.setSuccessMsg(request);
        // put event into formBean
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward delete(ActionMapping mapping, EventInfoForm form,
                                HttpServletRequest request) throws Exception {
        Event event = new Event(form.getInstanceId(),
                                getNamedSpace(ObjectList.EVENT_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.EVENT_KEY);

        PropertyUtils.copyProperties(event, form);

        event.setInstanceRrn(
                getInstanceRrn(form.getInstanceId(), getNamedSpace(ObjectList.EVENT_KEY, LocalContext.getFacilityRrn()),
                               ObjectList.EVENT_KEY));

        event.setTransId(Constants.DELETE_KEY);
        String dest = "setup";

        String message = updateEventWorkStream(event, LocalContext.getFacilityRrn(), LocalContext.getUserId());

        Assert.isFalse(message != null, Errors.create().content(message).build());

        process(event);

        List<Event> events = baseService.getEvents(event.getInstanceRrn());

        if (events.size() < 1) {
            form.setInstanceId("");
        } else {
            event = events.iterator().next();

            List<EventModel> eventModels = baseService.getEventModelofEvent(event);
            event.setEventModels(eventModels);

            PropertyUtils.copyProperties(form, event);
            dest = "modify";
        }

        request.setAttribute(SessionNames.EVENT_KEY, event);
        form.setTransId(Constants.MODIFY_KEY);
        WebUtils.setSuccessMsg(request);
        return mapping.findForward(dest);
    }

    public ActionForward copy(ActionMapping mapping, EventInfoForm form, HttpServletRequest request) throws Exception {

        Event event = new Event(form.getInstanceId(),
                                getNamedSpace(ObjectList.EVENT_KEY, LocalContext.getFacilityRrn()),
                                ObjectList.EVENT_KEY);
        PropertyUtils.copyProperties(event, form);

        String id = request.getParameter(Constants.COPY_KEY);

        event.setInstanceId(id);

        doCopy(form, event);

        // for logic equal tag, copy is the same action as create
        form.setTransId(Constants.CREATE_KEY);

        return mapping.findForward(Constants.MODIFY_KEY);
    }

    private void processDetails(long eventRrn, EventModel eventModel) {
        baseService.updateEventModel(eventRrn, eventModel);
    }

    private String updateEventWorkStream(Event event, Long facility, String user) {
        String message = null;
        HashMap map = new HashMap(2);

        map.put("event", event);
        map.put("wsname", "updateEvent");
        message = connectMesSystem(map, facility.longValue(), user);

        return message;
    }

    private String updateEventStatusWorkStream(Event event, Long facility, String user) {
        String message = null;
        HashMap map = new HashMap(2);
        event.setEventModels(null);

        map.put("event", event);
        map.put("wsname", "updateEventStatus");
        message = connectMesSystem(map, facility.longValue(), user);

        return message;
    }

    private String connectMesSystem(Map objectInfo, long facilityRrn, String user) {
        // todo 接口相关的待完成
        return null;
    }

}