MessageSaveAction.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.message;

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.framework.utils.lang.StringUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.sys.Message;
import com.mycim.valueobject.sys.MessageDetail;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.SystemSetupAction;
import com.mycim.webapp.forms.system.MessageInfoForm;
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.List;

/**
 * 消息定义
 *
 * @author yanbing.chen
 * @date 2019/8/19
 * @since 1.8
 **/
public class MessageSaveAction extends SystemSetupAction {

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

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

        MessageInfoForm theform = (MessageInfoForm) form;
        // Populate the new valid format id
        String id = "";
        String formId = theform.getInstanceId();
        if(StringUtils.isNotEmptyTrim(formId)){
            id = theform.getInstanceId().trim().toUpperCase();
        }
        String flag = request.getParameter("flag");
        String type = request.getParameter("type");
        if(StringUtils.isNotEmptyTrim(flag)){
            request.setAttribute("flag",flag);
        }
        if(StringUtils.isNotEmptyTrim(type)){
            request.setAttribute("type",type);
        }

        // Construct a new value object that stores the id,
        Message message = new Message(id, getNamedSpace(ObjectList.MESSAGE_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.MESSAGE_KEY);

        message = (Message) getInstance(message);

        // Copy value object properties to form bean.
        PropertyUtils.copyProperties(theform, message);
        // if a value object does not exist, its instanceRrn will be set to 0
        if (message.getInstanceRrn() == 0) {
            theform.setTransId(Constants.CREATE_KEY);

            return (mapping.findForward(Constants.MODIFY_KEY));
        }
        // the following step will occur when in a one2many relation
        List<MessageDetail> messageDetails = null;

        messageDetails = getMessageDetails(message);

        message.setMessageDetails(messageDetails);

        theform.setTransId(Constants.MODIFY_KEY);

        request.setAttribute(SessionNames.MESSAGE_KEY, message);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        MessageInfoForm theform = (MessageInfoForm) form;

        Message message = new Message(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.MESSAGE_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.MESSAGE_KEY);

        message = (Message) getInstance(message);
        theform.setInstanceId("");

        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward copy(ActionMapping mapping, MessageInfoForm theform,
                              HttpServletRequest request) throws Exception {
        Message message = new Message(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.MESSAGE_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.MESSAGE_KEY);

        message = (Message) getInstance(message);

        message.setTransPerformedby(LocalContext.getUserId());

        // the new Id is stored in the url COPY key
        String id = request.getParameter(Constants.COPY_KEY);

        message.setInstanceId(id);
        log.debug("the message is:" + message.getInstanceDesc());

        doCopy(theform, message);

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

        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward members(ActionMapping mapping, MessageInfoForm theform,
                                 HttpServletRequest request) throws Exception {
        Message message = new Message(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.MESSAGE_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.MESSAGE_KEY);

        message = (Message) getInstance(message);

        message.setTransPerformedby(LocalContext.getUserId());

        theform.setTransId(Constants.CREATE_KEY);
        if (request.getParameter(Constants.ITEM_KEY) != null) {
            String language = request.getParameter(Constants.ITEM_KEY);
            List<MessageDetail> details = getMessageDetails(message);

            for (MessageDetail item : details) {
                if (item.getLanguage().equals(language)) {
                    theform.setLanguage(item.getLanguage());
                    theform.setFullLanguage(item.getFullLanguage());
                    theform.setContentText(item.getContentText());
                    theform.setFullContent(item.getFullContent());
                    theform.setHelpText(item.getHelpText());
                }
            }

            message.setMessageDetails(details);
            request.setAttribute(SessionNames.MESSAGE_KEY, message);
            theform.setTransId(Constants.MODIFY_KEY);
        }

        return mapping.findForward(Constants.MEMBERS_KEY);
    }

    public ActionForward delete(ActionMapping mapping, MessageInfoForm theform,
                                HttpServletRequest request) throws Exception {
        Message message = new Message(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.MESSAGE_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.MESSAGE_KEY);

        message = (Message) getInstance(message);

        message.setTransPerformedby(LocalContext.getUserId());

        // when inserting or updating, populate the value object for process method
        PropertyUtils.copyProperties(message, theform);

        // execute the process method that will understand action
        // thru value object property transId
        message.setTransId(Constants.DELETE_KEY);
        theform.setInstanceId("");

        process(message);

        message = (Message) getInstance(message);

        // differet from other one2many model, label has fixed items
        message.setMessageDetails(getMessageDetails(message));

        theform.setTransId(Constants.MODIFY_KEY);

        request.setAttribute(SessionNames.MESSAGE_KEY, message);
        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward create(ActionMapping mapping, MessageInfoForm theform,
                                HttpServletRequest request) throws Exception {
        Message message = new Message(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.MESSAGE_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.MESSAGE_KEY);

        message = (Message) getInstance(message);

        message.setTransPerformedby(LocalContext.getUserId());

        // when inserting or updating, populate the value object for process method
        PropertyUtils.copyProperties(message, theform);

        message.setTransId(Constants.CREATE_KEY);

        // no matter add or modify, they all execute the same function
        process(message);

        message = (Message) getInstance(message);

        // differet from other one2many model, label has fixed items
        message.setMessageDetails(getMessageDetails(message));

        // just for logic equals tag
        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute(SessionNames.MESSAGE_KEY, message);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward modify(ActionMapping mapping, MessageInfoForm theform,
                                HttpServletRequest request) throws Exception {
        Message message = new Message(theform.getInstanceId(),
                                      getNamedSpace(ObjectList.MESSAGE_KEY, LocalContext.getFacilityRrn()),
                                      ObjectList.MESSAGE_KEY);

        message = (Message) getInstance(message);

        message.setTransPerformedby(LocalContext.getUserId());

        PropertyUtils.copyProperties(message, theform);

        message.setTransId(Constants.MODIFY_KEY);

        process(message);

        message = (Message) getInstance(message);

        // differet from other one2many model, label has fixed items
        message.setMessageDetails(getMessageDetails(message));

        // just for logic equals tag
        theform.setTransId(Constants.MODIFY_KEY);

        request.setAttribute(SessionNames.MESSAGE_KEY, message);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

}