PayloadMetaInfoImpl.java [src/csip] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2022, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip;

import csip.api.server.ServiceException;
import csip.api.server.PayloadMetaInfo;
import csip.utils.JSONUtils;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 * Payload Metainfo access.
 *
 * @author od
 */
class PayloadMetaInfoImpl implements PayloadMetaInfo {

  private JSONObject metainfo;


  PayloadMetaInfoImpl(JSONObject metainfo) {
    this.metainfo = metainfo;
  }


  public String getString(String name) throws ServiceException {
    try {
      return metainfo.getString(name);
    } catch (JSONException ex) {
      throw new ServiceException(ex);
    }
  }


  public String getString(String name, String def) {
    return metainfo.optString(name, def);
  }


  public String[] getStringArray(String name) throws ServiceException {
    try {
      JSONArray a = metainfo.getJSONArray(name);
      String[] s = new String[a.length()];
      for (int i = 0; i < s.length; i++) {
        s[i] = a.getString(i);
      }
      return s;
    } catch (JSONException ex) {
      throw new ServiceException(ex);
    }
  }


  public String[] getStringArray(String name, String[] def) {
    try {
      JSONArray a = metainfo.getJSONArray(name);
      String[] s = new String[a.length()];
      for (int i = 0; i < s.length; i++) {
        s[i] = a.getString(i);
      }
      return s;
    } catch (JSONException ex) {
      return def;
    }
  }


  public int getInt(String name) throws ServiceException {
    try {
      return metainfo.getInt(name);
    } catch (JSONException ex) {
      throw new ServiceException(ex);
    }
  }


  public int getInt(String name, int def) throws ServiceException {
    return metainfo.optInt(name, def);
  }


  public double getDouble(String name) throws ServiceException {
    try {
      return metainfo.getDouble(name);
    } catch (JSONException ex) {
      throw new ServiceException(ex);
    }
  }


  public double getDouble(String name, double def) throws ServiceException {
    return metainfo.optDouble(name, def);
  }


  public boolean getBoolean(String name) throws ServiceException {
    try {
      return metainfo.getBoolean(name);
    } catch (JSONException ex) {
      throw new ServiceException(ex);
    }
  }


  public boolean getBoolean(String name, boolean def) {
    return metainfo.optBoolean(name, def);
  }


  public boolean hasName(String name) {
    return metainfo.has(name);
  }


  public PayloadMetaInfoImpl require(String... names) throws ServiceException {
    for (String name : names) {
      if (!hasName(name))
        throw new ServiceException("Metainfo key not found :" + names);

    }
    return this;
  }


  public Collection<String> getNames() {
    Set<String> s = new TreeSet<>();
    Iterator<?> i = metainfo.keys();
    while (i.hasNext()) {
      s.add(i.next().toString());
    }
    return s;
  }


  public int getCount() {
    return metainfo.length();
  }


  public void setWarning(String msg) throws ServiceException {
    try {
      metainfo.put("warning", msg);
    } catch (JSONException ex) {
      throw new ServiceException("Warning failed.");
    }
  }


  public void put(String key, String value) throws ServiceException {
    try {
      metainfo.put(key, value);
    } catch (JSONException ex) {
      throw new ServiceException("Put metainfo entry failed for: " + key);
    }
  }


  public void put(String key, int value) throws ServiceException {
    try {
      metainfo.put(key, value);
    } catch (JSONException ex) {
      throw new ServiceException("Put metainfo entry failed for: " + key);
    }
  }


  public void put(String key, long value) throws ServiceException {
    try {
      metainfo.put(key, value);
    } catch (JSONException ex) {
      throw new ServiceException("Put metainfo entry failed for: " + key);
    }
  }


  public void put(String key, double value) throws ServiceException {
    try {
      metainfo.put(key, value);
    } catch (JSONException ex) {
      throw new ServiceException("Put metainfo entry failed for: " + key);
    }
  }


  public void put(String key, String[] value) throws ServiceException {
    try {
      metainfo.put(key, Arrays.asList(value));
    } catch (JSONException ex) {
      throw new ServiceException("Put metainfo entry failed for: " + key);
    }
  }


  public void appendWarning(String msg) throws ServiceException {
    try {
      String prevmsg = JSONUtils.getJSONString(metainfo, "warning", "");
      prevmsg += "|" + msg;
      metainfo.put("warning", prevmsg);
    } catch (JSONException ex) {
      throw new ServiceException("Warning failed.");
    }
  }


  public String toString() {
    return metainfo.toString();
  }

}