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

csip.PayloadResults 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: PayloadResults.java 48d8b8f5790c 2019-11-13 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 static csip.Config.LOG;
import csip.utils.JSONUtils;
import csip.utils.ZipFiles;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

/**
 * Access to return payload results (and report data, same API).
 *
 * @author od
 */
public class PayloadResults {

  /**
   * Functional Interface for Result and Report Data
   *
   * @param  The data structure to manage
   */
  @FunctionalInterface
  interface Redata {

    T fetch();
  }

  Redata> rdata;
  Redata>> rmeta;
  Redata> fresults;
  Redata> fdesc;


  PayloadResults(Redata> rd,
      Redata>> rm,
      Redata> fres, Redata> fd) {
    this.rdata = rd;
    this.rmeta = rm;
    this.fresults = fres;
    this.fdesc = fd;
  }


  /**
   * Put any meta data into a result.
   *
   * @param name the result name
   * @param metaKey the meta data key
   * @param metaValue the meta data value
   * @return this payload object
   */
  public PayloadResults putMetaInfo(String name, String metaKey, Object metaValue) {
    Map meta = rmeta.fetch().get(name);
    if (meta == null) {
      meta = new LinkedHashMap<>();
      rmeta.fetch().put(name, meta);
    }
    meta.put(metaKey, metaValue);
    return this;
  }


  /**
   * Provide a string as a result.
   * @param name the result name
   * @param val the value to store
   * @param unit the physical unit
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, String val, String descr, String unit) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, unit, descr));
    return this;
  }


  /**
   * Provide a string as a result.
   * @param name the result name
   * @param val the value to store
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, String val, String descr) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, descr));
    return this;
  }


  /**
   * Provide a string as a result.
   * @param name the result name
   * @param val the value to store
   * @return this payload object
   */
  public PayloadResults put(String name, String val) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, null));
    return this;
  }


  /**
   * Provide an int as a result.
   * @param name the result name
   * @param val the value to store
   * @param unit the physical unit
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, int val, String descr, String unit) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, unit, descr));
    return this;
  }


  /**
   * Provide an int as a result.
   * @param name the result name
   * @param val the value to store
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, int val, String descr) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, descr));
    return this;
  }


  /**
   * Provide an int as a result.
   * @param name the result name
   * @param val the value to store
   * @return this payload object
   */
  public PayloadResults put(String name, int val) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, null));
    return this;
  }


  /**
   * Provide a double as a result.
   * @param name the result name
   * @param val the value to store
   * @param unit the physical unit
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, double val, String descr, String unit) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, unit, descr));
    return this;
  }


  /**
   * Provide a double as a result.
   * @param name the result name
   * @param val the value to store
   * @param descr the result description.
   * @return this payload object
   */
  public PayloadResults put(String name, double val, String descr) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, descr));
    return this;
  }


  /**
   * Provide a double as a result.
   * @param name the result name
   * @param val the value to store
   * @return this payload object
   */
  public PayloadResults put(String name, double val) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, null));
    return this;
  }


  /**
   * Provide a boolean as a result.
   * @param name the result name
   * @param val the value to store
   * @param unit the physical unit
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, boolean val, String descr, String unit) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, unit, descr));
    return this;
  }


  /**
   * Provide a boolean as a result.
   * @param name the result name
   * @param val the value to store
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, boolean val, String descr) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, descr));
    return this;
  }


  /**
   * Provide a boolean as a result.
   * @param name the result name
   * @param val the value to store
   * @return this payload object
   */
  public PayloadResults put(String name, boolean val) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, null));
    return this;
  }


  /**
   * Provide an Object as a result.
   * @param name the result name
   * @param val the value to store
   * @param descr the description
   * @param unit the unit
   * @return this payload object
   */
  public PayloadResults put(String name, Object val, String descr, String unit) {
    if (val instanceof Collection) {
      val = JSONUtils.toArray((Collection) val);
    }
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, unit, descr));
    return this;
  }


  /**
   * Provide an Object as a result.
   * @param name the result name
   * @param val the value to store
   * @param descr the description
   * @return this payload object
   */
  public PayloadResults put(String name, Object val, String descr) {
    if (val instanceof Collection) {
      val = JSONUtils.toArray((Collection) val);
    }
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, descr));
    return this;
  }


  /**
   * Provide an Object as a result.
   * @param name the result name
   * @param val the value to store
   * @return this payload object
   */
  public PayloadResults put(String name, Object val) {
    if (val instanceof Collection) {
      val = JSONUtils.toArray((Collection) val);
    }
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, null));
    return this;
  }


  /**
   * Provide a boolean array as a result.
   * @param name the result name
   * @param val the value to store
   * @param unit the physical unit
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, boolean[] val, String descr, String unit) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, unit, descr));
    return this;
  }


  /**
   * Provide a boolean array as a result.
   * @param name the result name
   * @param val the value to store
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, boolean[] val, String descr) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, descr));
    return this;
  }


  /**
   * Provide a boolean array as a result.
   * @param name the result name
   * @param val the value to store
   * @return this payload object
   */
  public PayloadResults put(String name, boolean[] val) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, null));
    return this;
  }


  /**
   * Provide a double array as a result.
   * @param name the result name
   * @param val the value to store
   * @param unit the physical unit
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, double[] val, String descr, String unit) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, unit, descr));
    return this;
  }


  /**
   * Provide a double array as a result.
   * @param name the result name
   * @param val the value to store
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, double[] val, String descr) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, descr));
    return this;
  }


  /**
   * Provide a double array as a result.
   * @param name the result name
   * @param val the value to store
   * @return this payload object
   */
  public PayloadResults put(String name, double[] val) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, null));
    return this;
  }


  /**
   * Provide a int array as a result.
   * @param name the result name
   * @param val the value to store
   * @param unit the physical unit
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, int[] val, String descr, String unit) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, unit, descr));
    return this;
  }


  /**
   * Provide a int array as a result.
   * @param name the result name
   * @param val the value to store
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, int[] val, String descr) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, descr));
    return this;
  }


  /**
   * Provide a int array as a result.
   * @param name the result name
   * @param val the value to store
   * @return this payload object
   */
  public PayloadResults put(String name, int[] val) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, null));
    return this;
  }


  /**
   * Provide a long array as a result.
   * @param name the result name
   * @param val the value to store
   * @param unit the physical unit
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, long[] val, String descr, String unit) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, unit, descr));
    return this;
  }


  /**
   * Provide a long array as a result.
   * @param name the result name
   * @param val the value to store
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, long[] val, String descr) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, descr));
    return this;
  }


  /**
   * Provide a long array as a result.
   * @param name the result name
   * @param val the value to store
   * @return this payload object
   */
  public PayloadResults put(String name, long[] val) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, null));
    return this;
  }


  /**
   * Provide a String array as a result.
   * @param name the result name
   * @param val the value to store
   * @param unit the physical unit
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, String[] val, String descr, String unit) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, unit, descr));
    return this;
  }


  /**
   * Provide a String array as a result.
   * @param name the result name
   * @param val the value to store
   * @param descr a description
   * @return this payload object
   */
  public PayloadResults put(String name, String[] val, String descr) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, descr));
    return this;
  }


  /**
   * Provide a String array as a result.
   * @param name the result name
   * @param val the value to store
   * @return this payload object
   */
  public PayloadResults put(String name, String[] val) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, null));
    return this;
  }


  /**
   * Provide a file as a result. If the files represents a folder, it gets added
   * to the result as zip file.
   *
   * @param file the file to add as result.
   * @return this payload object
   */
  public PayloadResults put(File file) {
    if (file == null || !file.exists() || !file.canRead()) {
      throw new IllegalArgumentException("Cannot acces File/Folder: " + file);
    }
    if (file.isDirectory()) {
      try {
        // can only add zipped folder as output.
        file = ZipFiles.zip(file);
      } catch (IOException ex) {
        LOG.log(Level.SEVERE, null, ex);
        return this;
      }
    }
    fresults.fetch().add(file);
    return this;
  }


  /**
   * Provide a file with description as a result.
   * @param file the result file
   * @param descr the file description
   * @return this payload object
   */
  public PayloadResults put(File file, String descr) {
    put(file);
    fdesc.fetch().put(file, descr);
    return this;
  }


  /**
   * Provide multiple files as a result.
   * @param files the files to add as a result
   * @return this payload object
   */
  public PayloadResults put(File... files) {
    for (File file : files) {
      put(file);
    }
    return this;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy