All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rt.logic.beans.LogicBean Maven / Gradle / Ivy

There is a newer version: 1.1.17
Show newest version
package com.rt.logic.beans;

import com.json.JSONArray;
import com.json.JSONObject;
import com.rt.core.beans.Property;
import com.rt.core.util.RTUtil;

import java.io.Serializable;
import java.util.Map;

/**
 * json对象与主要操作方法
 *
 * @author msc
 */
public class LogicBean extends Property {

    public static final String SUCCESS = "success";
    public static final String SUCCESS_STR = "execution succeed";

    /**
     * 默认构造函数
     */
    public LogicBean() {
        super();
    }

    /**
     * 通过json字符串构造
     *
     * @param string json string
     * @throws Exception json format exception
     */
    public LogicBean(String string) throws Exception {
        super(string);
    }

    /**
     * 通过Map构造
     *
     * @param map Serializable map
     */
    public LogicBean(Map map) {
        super(map);
    }

    /**
     * 构造函数
     *
     * @param success success
     * @param code    message code
     * @param message message
     * @param data    data
     */
    public LogicBean(boolean success, String code, String message, Serializable data) {
        setSuccess(success);
        setCode(code);
        setMessage(message);
        setData(data);
    }

    /**
     * 构造函数
     *
     * @param success success
     * @param code    message code
     * @param message message
     */
    public LogicBean(boolean success, String code, String message) {
        this(success, code, message, null);
    }

    /**
     * 应用ID
     *
     * @return String
     */
    public String getAppId() {
        return getString("appId");
    }

    /**
     * 应用ID
     *
     * @param appId appId
     */
    public void setAppId(String appId) {
        put("appId", appId);
    }

    /**
     * 业务逻辑名称
     *
     * @return String
     */
    public String getLogicId() {
        return getString("logicId");
    }

    /**
     * 业务逻辑名称
     *
     * @param logicId logic id
     */
    public void setLogicId(String logicId) {
        put("logicId", logicId);
    }

    /**
     * 业务追踪ID
     *
     * @return String
     */
    public String getTraceId() {
        return getString("traceId");
    }

    /**
     * 业务追踪ID
     *
     * @param traceId traceId
     */
    public void setTraceId(String traceId) {
        put("traceId", traceId);
    }

    /**
     * 获取签名
     *
     * @return String
     */
    public String getSign() {
        return getString("sign");
    }

    /**
     * 设置签名
     *
     * @param sign sign
     */
    public void setSign(String sign) {
        put("sign", sign);
    }

    /**
     * 获得消息队列
     *
     * @return JSONObject
     */
    public JSONObject getMessageMap() {
        JSONObject map = getJSONObject("messageMap");
        if (map == null) {
            map = new JSONObject();
            put("messageMap", map);
        }
        return map;
    }

    /**
     * 添加消息
     *
     * @param code    code
     * @param type    type
     * @param message message
     */
    public void putMessage(String code, String type, String message) {
        JSONObject msg = new JSONObject();
        msg.put("type", type);
        msg.setValue(message);
        getMessageMap().put(code, msg);
    }

    /**
     * 消息
     *
     * @return String
     */
    public String getMessage() {
        return getString("message");
    }

    /**
     * 消息
     *
     * @param message message value
     */
    public void setMessage(String message) {
        put("message", message);
    }

    /**
     * 是否设置了成功或失败
     *
     * @return boolean
     */
    public boolean hasSuccess() {
        return has("success");
    }

    /**
     * 执行成功或失败
     *
     * @return String
     */
    public boolean isSuccess() {
        return getBoolean("success");
    }

    /**
     * 执行中是否存在错误
     *
     * @return String
     */
    public boolean isError() {
        return !isSuccess();
    }

    /**
     * 消息
     *
     * @param success is success boolean tag
     */
    public void setSuccess(boolean success) {
        put("success", success);
    }

    /**
     * count查询
     *
     * @return String the countQuery
     */
    public String getCountQuery() {
        return getString("countQuery");
    }

    /**
     * count查询
     *
     * @param countQuery the countQuery to set
     */
    public void setCountQuery(String countQuery) {
        put("countQuery", countQuery);
    }

    /**
     * remove count查询
     */
    public void removeCountQuery() {
        remove("countQuery");
    }

    /**
     * 查询
     *
     * @return the query
     */
    public String getQuery() {
        return getString("query");
    }

    /**
     * query查询
     *
     * @param query the query to set
     */
    public void setQuery(String query) {
        put("query", query);
    }

    /**
     * remove query查询
     */
    public void removeQuery() {
        remove("query");
    }

    /**
     * 获取Data列表中获取第一个数据对象
     *
     * @param  object
     * @return T object
     */
    public  T getDataItem() {
        Object data = getData();
        if (data == null) {
            return null;
        }
        if (data instanceof JSONObject) {
            JSONObject map = (JSONObject) data;
            if (map.isEmpty()) {
                return null;
            }
            for (Object o : map.keySet()) {
                Object o1 = map.get(o);
                if (o1 instanceof JSONObject || o1 instanceof JSONArray) {
                    return (T) o1;
                } else {
                    return (T) data;
                }
            }
        }
        if (data instanceof JSONArray) {
            JSONArray array = (JSONArray) data;
            if (array.isEmpty()) {
                return null;
            }
            return (T) array.get(0);
        }
        return null;
    }

    /**
     * 数据
     *
     * @return the Array data
     */
    public JSONArray getListData() {
        if (getData() == null) {
            return null;
        }
        if (getData() instanceof JSONArray) {
            return (JSONArray) getData();
        }
        return null;
    }

    /**
     * 获取JSONObject格式数据
     *
     * @return the Map data
     */
    public JSONObject getMapData() {
        if (getData() instanceof JSONObject) {
            return (JSONObject) getData();
        }
        return null;
    }

    /**
     * 获取data数据
     *
     * @param  object
     * @return T object
     */
    public  T getData() {
        return (T) get("data");
    }

    /**
     * 数据
     *
     * @param data the data to set
     */
    public void setData(Object data) {
        put("data", data);
    }

    /**
     * 业务请求参数
     *
     * @return the param
     */
    public JSONObject getHeadParam() {
        JSONObject param = getJSONObject("headParam");
        if (param == null) {
            param = new JSONObject();
            setHeadParam(param);
        }
        return param;
    }

    /**
     * 业务请求参数
     *
     * @param param the param to set
     */
    public void setHeadParam(Map param) {
        put("headParam", param);
    }

    /**
     * 获取查询参数
     *
     * @param key key
     * @return String value
     */
    public String getHeadParam(String key) {
        return RTUtil.val(getHeadParam().get(key));
    }

    /**
     * 业务请求参数
     *
     * @return the param
     */
    public JSONObject getParam() {
        JSONObject param = getJSONObject("param");
        if (param == null) {
            param = new JSONObject();
            setParam(param);
        }
        return param;
    }

    /**
     * 业务请求参数
     *
     * @param param the param to set
     */
    public void setParam(Map param) {
        put("param", param);
    }

    /**
     * 添加参数
     *
     * @param key   key
     * @param value value
     */
    public void putParam(String key, Serializable value) {
        if (value == null) {
            getParam().put(key, RTUtil.EMPTY);
        } else {
            getParam().put(key, value);
        }
    }

    /**
     * 获取查询参数
     *
     * @param key key
     * @return String value
     */
    public String getParam(String key) {
        return RTUtil.val(getParam().get(key));
    }

    /**
     * 业务返回参数
     *
     * @return JSONObject the result param
     */
    public JSONObject getResultParam() {
        JSONObject resultParam = getJSONObject("resultParam");
        if (resultParam == null) {
            resultParam = new JSONObject();
            setResultParam(resultParam);
            ;
        }
        return resultParam;
    }

    /**
     * 业务返回参数
     *
     * @param resultParam the result param to set
     */
    public void setResultParam(Map resultParam) {
        put("resultParam", resultParam);
    }

    /**
     * 添加返回参数
     *
     * @param key   key
     * @param value value
     */
    public void putResultParam(String key, Serializable value) {
        if (value == null) {
            getResultParam().put(key, RTUtil.EMPTY);
        } else {
            getResultParam().put(key, value);
        }
    }

    /**
     * 获取返回参数
     *
     * @param key key
     * @return String value
     */
    public String getResultParam(String key) {
        return RTUtil.val(getResultParam().get(key));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy