COSUImpl.java [src/csip] Revision:   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.COSU;
import csip.cosu.ObjFunc;
import csip.utils.JSONUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 * COSU support for services.
 *
 * @author od
 */
class COSUImpl extends ModelDataServiceAPI
    implements COSU, ModelDataServiceConstants {

  private Map<String, JSONObject> c;


  COSUImpl(ModelDataService mds) {
    super(mds);
  }


  synchronized private Map<String, JSONObject> cosuMeta() {
    if (c == null) {
      if (mds.metainfo != null && mds.metainfo.has(KEY_COSU)) {
        try {
          // get all cosu entries
          c = JSONUtils.preprocess(mds.metainfo.get(KEY_COSU));
        } catch (JSONException ex) {
          throw new RuntimeException(ex);
        }
      } else {
        throw new RuntimeException("No COSU information found.");
      }
    }
    return c;
  }


  @Override
  public boolean isRequested() {
    if (mds.metainfo == null)
      return false;
    if (!mds.metainfo.has(KEY_COSU))
      return false;

    Map<String, JSONObject> cosu = cosuMeta();
    return cosu != null && !cosu.isEmpty();
  }


  JSONArray getCOSUMetainfo() {
    if (!isRequested())
      throw new UnsupportedOperationException();

    JSONArray arr = new JSONArray();
    Map<String, JSONObject> cosu = cosuMeta();
    SortedSet<String> keys = new TreeSet<>(cosu.keySet());
    for (String key : keys) {
      arr.put(cosu.get(key));
    }
    return arr;
  }


  @Override
  public List<String> getNames() {
    if (!isRequested())
      throw new UnsupportedOperationException();

    return new ArrayList<>(cosuMeta().keySet());
  }


  @Override
  public ObjFunc getObjFunc(String name) {
    if (!isRequested())
      throw new UnsupportedOperationException();

    JSONObject p = cosuMeta().get(name);
    return ObjFunc.of(p.optString(KEY_OF, null));
  }


  @Override
  public String[] getData(String name) {
    if (!isRequested())
      throw new UnsupportedOperationException();

    JSONObject p = cosuMeta().get(name);
    try {
      return JSONUtils.toStringArray(p.getJSONArray(KEY_DATA));
    } catch (JSONException ex) {
      throw new RuntimeException(ex);
    }
  }


  @Override
  public void setValue(String name, double value) {
    if (!isRequested())
      throw new UnsupportedOperationException();

    JSONObject p = cosuMeta().get(name);
    try {
      p.put(KEY_VALUE, value);
    } catch (JSONException ex) {
      throw new RuntimeException(ex);
    }
  }


  @Override
  public String toString() {
    return !isRequested() ? "Not requested." : c.toString();
  }

}