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

csip.PayloadMetaInfo Maven / Gradle / Ivy

Go to download

The Cloud Services Integration Platform is a SoA implementation to offer a Model-as-a-Service framework, Application Programming Interface, deployment infrastructure, and service implementations for environmental modeling.

There is a newer version: 2.6.30
Show newest version
/*
 * $Id: PayloadMetaInfo.java 9e04709703fa 2020-04-27 od $
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2019, 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.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
 */
public class PayloadMetaInfo {
  
  private final JSONObject metainfo;
  
  
  PayloadMetaInfo(JSONObject metainfo) {
    this.metainfo = metainfo;
  }


  /**
   * Get the metainfo value as String.
   * @param name the name of the metainfo entry
   * @return the value of a metainfo entry
   * @throws ServiceException if there is a JSON error.
   */
  public String getString(String name) throws ServiceException {
    try {
      return metainfo.getString(name);
    } catch (JSONException ex) {
      throw new ServiceException(ex);
    }
  }


  /**
   * Get the metainfo value as StringArray.
   * @param name the name of the metainfo entry
   * @return the value of a metainfo entry
   * @throws ServiceException if there is a JSON error.
   */
  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);
    }
  }


  /**
   * Get metainfo value as int.
   * @param name the name of the metainfo entry
   * @return the int value of a metainfo entry.
   * @throws ServiceException if there is a JSON error.
   */
  public int getInt(String name) throws ServiceException {
    try {
      return metainfo.getInt(name);
    } catch (JSONException ex) {
      throw new ServiceException(ex);
    }
  }


  /**
   * Get metainfo value as int.
   * @param name the name of the metainfo entry
   * @return the int value of a metainfo entry.
   * @throws ServiceException if there is a JSON error.
   */
  public int getInt(String name, int def) throws ServiceException {
    return metainfo.optInt(name, def);
  }


  /**
   * Get the metainfo value as double.
   * @param name the name of the metainfo entry
   * @return the metainfo value.
   * @throws ServiceException if there is a JSON error.
   */
  public double getDouble(String name) throws ServiceException {
    try {
      return metainfo.getDouble(name);
    } catch (JSONException ex) {
      throw new ServiceException(ex);
    }
  }


  /**
   * Get a metainfo value as boolean.
   * @param name the name of the metainfo entry
   * @return the metainfo value
   * @throws ServiceException if there is a JSON error.
   */
  public boolean getBoolean(String name) throws ServiceException {
    try {
      return metainfo.getBoolean(name);
    } catch (JSONException ex) {
      throw new ServiceException(ex);
    }
  }


  /**
   * Check if a metainfo entry exists.
   * @param name the name of a metainfo name.
   * @return true if a metainfo entry exists, false otherwise
   */
  public boolean hasName(String name) {
    return metainfo.has(name);
  }


  /**
   * Check is a required key exists.
   *
   * @param names the parameter name
   * @return this object
   * @throws ServiceException if the parameter is not found.
   */
  public PayloadMetaInfo require(String... names) throws ServiceException {
    for (String name : names) {
      if (!hasName(name)) {
        throw new ServiceException("Metainfo key not found :" + names);
      }
    }
    return this;
  }


  /**
   * Get all metainfo names.
   * @return the set of metainfo names.
   */
  public Collection getNames() {
    Set s = new TreeSet<>();
    Iterator i = metainfo.keys();
    while (i.hasNext()) {
      s.add(i.next().toString());
    }
    return s;
  }


  /**
   * Get the number of metainfo entries.
   * @return the number of entries.
   */
  public int getCount() {
    return metainfo.length();
  }


  /**
   * Set a warning message.
   *
   * @param msg the message
   * @throws ServiceException if warning cannot be added.
   */
  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, String[] value) throws ServiceException {
    try {
      metainfo.put(key, Arrays.asList(value));
    } catch (JSONException ex) {
      throw new ServiceException("Put metainfo entry failed for: " + key);
    }
  }


  /**
   * Append a warning.
   *
   * @param msg the message to append
   * @throws ServiceException if json operation fails
   */
  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.");
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy