PayloadResultsImpl.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.PayloadResults;
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
 */
class PayloadResultsImpl implements PayloadResults {

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

    T fetch();
  }

  Redata<Map<String, JSONObject>> rdata;
  Redata<Map<String, Map<String, Object>>> rmeta;
  Redata<List<File>> fresults;
  Redata<Map<File, String>> fdesc;


  PayloadResultsImpl(Redata<Map<String, JSONObject>> rd,
      Redata<Map<String, Map<String, Object>>> rm,
      Redata<List<File>> fres, Redata<Map<File, String>> fd) {
    this.rdata = rd;
    this.rmeta = rm;
    this.fresults = fres;
    this.fdesc = fd;
  }


  @Override
  public PayloadResultsImpl putMetaInfo(String name, String metaKey, Object metaValue) {
    Map<String, Object> meta = rmeta.fetch().get(name);
    if (meta == null) {
      meta = new LinkedHashMap<>();
      rmeta.fetch().put(name, meta);
    }
    meta.put(metaKey, metaValue);
    return this;
  }


  public PayloadResultsImpl put(String name, String val, String descr, String unit) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, unit, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, String val, String descr) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, String val) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, null));
    return this;
  }


  public PayloadResultsImpl put(String name, int val, String descr, String unit) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, unit, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, int val, String descr) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, int val) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, null));
    return this;
  }


  public PayloadResultsImpl put(String name, double val, String descr, String unit) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, unit, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, double val, String descr) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, double val) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, null));
    return this;
  }


  public PayloadResultsImpl put(String name, boolean val, String descr, String unit) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, unit, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, boolean val, String descr) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, boolean val) {
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, val, null, null));
    return this;
  }


  public PayloadResultsImpl 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;
  }


  public PayloadResultsImpl 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;
  }


  public PayloadResultsImpl 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;
  }


  public PayloadResultsImpl 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;
  }


  public PayloadResultsImpl put(String name, boolean[] val, String descr) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, boolean[] val) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, null));
    return this;
  }


  public PayloadResultsImpl 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;
  }


  public PayloadResultsImpl put(String name, double[] val, String descr) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, double[] val) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, null));
    return this;
  }


  public PayloadResultsImpl 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;
  }


  public PayloadResultsImpl put(String name, int[] val, String descr) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, int[] val) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, null));
    return this;
  }


  public PayloadResultsImpl 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;
  }


  public PayloadResultsImpl put(String name, long[] val, String descr) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, long[] val) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, null));
    return this;
  }


  public PayloadResultsImpl 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;
  }


  public PayloadResultsImpl put(String name, String[] val, String descr) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, descr));
    return this;
  }


  public PayloadResultsImpl put(String name, String[] val) {
    JSONArray v = JSONUtils.toArray(val);
    rdata.fetch().put(name, JSONUtils.dataUnitDesc(name, v, null, null));
    return this;
  }


  public PayloadResultsImpl 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;
  }


  public PayloadResultsImpl put(File file, String descr) {
    put(file);
    fdesc.fetch().put(file, descr);
    return this;
  }


  public PayloadResultsImpl put(File... files) {
    for (File file : files) {
      put(file);
    }
    return this;
  }

}