com.nec.harvest.helper.MessageHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.nec.harvest.helper.MessageHelper.java

Source

/**
 * Copyright(C) 2014
 * NEC Corporation All rights reserved.
 * 
 * No permission to use, copy, modify and distribute this software
 * and its documentation for any purpose is granted.
 * This software is provided under applicable license agreement only.
 */
package com.nec.harvest.helper;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nec.core.container.ContextAwareContainer;
import com.nec.core.exception.ObjectNotFoundException;
import com.nec.harvest.bean.mapping.json.JSONMessage;
import com.nec.harvest.cache.CacheService;
import com.nec.harvest.constant.MsgConstants;
import com.nec.harvest.exception.ConnectionException;
import com.nec.harvest.exception.ServiceException;
import com.nec.harvest.model.Message;
import com.nec.harvest.service.MessageService;

/**
 * This is a helper for messages that can be used to get a message for the given
 * message code.
 * 
 * @author sondn
 * @version MessageHelper.java May 4, 2014
 * 
 */
public final class MessageHelper {

    private static final Logger logger = LoggerFactory.getLogger(MessageHelper.class);

    private static CacheService<String, Message> cacheService;

    /** ??????????? */
    private static JSONMessage systemErrorMsg;

    /** ????????? */
    private static JSONMessage closeCfmMsg;

    public MessageHelper(CacheService<String, Message> hazelcastConcurrentLRUCache, String systemError,
            String closeCfm) {
        cacheService = hazelcastConcurrentLRUCache;

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // 
            systemErrorMsg = objectMapper.readValue(systemError, new TypeReference<JSONMessage>() {
            });
            closeCfmMsg = objectMapper.readValue(closeCfm, new TypeReference<JSONMessage>() {
            });
        } catch (IOException ex) {
            logger.warn(ex.getMessage());
        }
    }

    /**
     * Get a message for the given message's code
     * 
     * @param msgCode
     *            A message's code
     * @return A message
     * @throws NullPointerException
     */
    public static Message get(final String msgCode) {
        try {
            Message message = cacheService.get(msgCode);
            if (message != null) {
                logger.info("Trying to load message " + msgCode + " from Harvest LRU Cache");
                // 
                return message;
            }
            //
            MessageService messageService = ContextAwareContainer.getInstance().getComponent(MessageService.class);
            message = messageService.findByMsgCode(msgCode);
            if (message != null) {
                cacheService.put(msgCode, message);
            }
            return message;
        } catch (IllegalArgumentException | ObjectNotFoundException ex) {
            logger.warn(ex.getMessage());

            //
            return cacheService.get(msgCode);
        } catch (ConnectionException | ServiceException ex) {
            logger.error(ex.getMessage(), ex);

            // An error occurred while trying to get the message from underlying database
            return getSystemError();
        }
    }

    /**
     * Get a message for the given message's code
     * 
     * @param msgCode
     *            A message's code
     * @return A message's body
     */
    public static String getBody(String msgCode) {
        Message message = cacheService.get(msgCode);
        if (message != null) {
            logger.info("Trying to load message " + msgCode + " from Harvest LRU Cache");
            // 
            return message.getMsg();
        }
        // 
        message = get(msgCode);
        if (message == null) {
            return StringUtils.EMPTY;
        }
        // 
        return message.getMsg();
    }

    /**
     * Get message's information for a shop based on message's code
     * 
     * @param msgCode
     *            A message's code
     * @param strCode
     *            A shop's code
     * @return A message's body
     */
    public static String getBody(String msgCode, String strCode) {
        Message message = cacheService.get(msgCode);
        if (message != null) {
            logger.info("Trying to load message " + msgCode + " from Harvest LRU Cache");
            // 
            return message.getMsg();
        }
        // 
        message = get(msgCode);
        if (message == null) {
            return StringUtils.EMPTY;
        }
        String body = message.getMsg();
        body = body.replace("{1}", strCode);
        return body;
    }

    /**
     * Get message's information for a shop and month for the given message's
     * code
     * 
     * @param msgCode
     *            A message's code
     * @param strCode
     *            A shop's code
     * @param monthly
     *            A string of month
     * @return A message's body
     */
    public static String getBody(String msgCode, String strCode, String monthly) {
        Message message = cacheService.get(msgCode);
        if (message != null) {
            logger.info("Trying to load message " + msgCode + " from Harvest LRU Cache");
            // 
            return message.getMsg();
        }
        // 
        message = get(msgCode);
        if (message == null) {
            return StringUtils.EMPTY;
        }
        String body = message.getMsg();
        body = body.replace("{1}", strCode);
        body = body.replace("{2}", monthly);
        return body;
    }

    /**
     * Get the system error message from the EHCache, if it does not exist in
     * the cache then generate by default based system properties
     * 
     * @return System error message {@value ???????????}
     */
    public static Message getSystemError() {
        Message message = cacheService.get(MsgConstants.CM_SYS_ERR01);
        if (message != null) {
            logger.info("Trying to load message " + MsgConstants.CM_SYS_ERR01 + " from Harvest LRU Cache");
            //
            return message;
        }

        message = new Message(systemErrorMsg.getMsgID(), systemErrorMsg.getMsg(), systemErrorMsg.getBtnKbn(),
                systemErrorMsg.getIcoKbn(), systemErrorMsg.getBtnKbnD());
        // Store in the LRU Cache
        cacheService.put(MsgConstants.CM_SYS_ERR01, message);
        return message;
    }

    /**
      * Get the confirm close application message from underlying database
      * 
      * @return Message
      */
    public static Message getCloseAppMsg() {
        // ?????????
        Message message = MessageHelper.get(MsgConstants.AF001_CLOSE_CFM_M01);
        if (message == null) {
            message = new Message(closeCfmMsg.getMsgID(), closeCfmMsg.getMsg(), closeCfmMsg.getBtnKbn(),
                    closeCfmMsg.getIcoKbn(), closeCfmMsg.getBtnKbnD());
        }
        // Store in the LRU Cache
        cacheService.put(MsgConstants.AF001_CLOSE_CFM_M01, message);
        return message;
    }

}