package com.cloudcc.core;

import java.util.HashMap;

import com.alibaba.fastjson.JSONObject;

public class ServiceResult extends HashMap<String, Object> {

    public String success;
    public String message;
    public String Id;

    public void addErrorMessage(String msg) throws Exception {
        this.put("message", msg);
        this.put("success", "false");
        throw new Exception(msg);
    }

    public String getMessage() {
        return (String) this.get("message");
    }

    public String getSuccess() {
        return (String) this.get("success");
    }

    public String getId() {
        return (String) this.get("id");
    }

    /** Whether successful (Boolean semantics, compatible with platform cceg.jar ServiceResult usage). */
    public boolean isSuccess() {
        Object v = this.get("success");
        if (v instanceof Boolean) return (Boolean) v;
        return "true".equals(v);
    }

    public void setSuccess(boolean success) {
        this.put("success", String.valueOf(success));
    }

    /** Return message */
    public String getRtnInfo() {
        return (String) this.get("rtnInfo");
    }

    public void setRtnInfo(String rtnInfo) {
        this.put("rtnInfo", rtnInfo);
    }

    /** Return code */
    public String getRtnCode() {
        return (String) this.get("rtnCode");
    }

    public void setRtnCode(String rtnCode) {
        this.put("rtnCode", rtnCode);
    }

    /** Redirect page */
    public String getRtnPage() {
        return (String) this.get("rtnPage");
    }

    public void setRtnPage(String rtnPage) {
        this.put("rtnPage", rtnPage);
    }

    /** Get the String value of the specified key in the Map */
    public String getString(String key) {
        Object v = this.get(key);
        return v == null ? null : v.toString();
    }

    @Override
    public String toString() {
        JSONObject data = new JSONObject();
        data.putAll(this);
        return data.toString();
    }
}
