V1_0.java [src/java/m/weppws/channeldb] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package m.weppws.channeldb;

import csip.ModelDataService;
import csip.api.server.Executable;
import csip.annotations.*;
import csip.api.server.ServiceException;
import static csip.annotations.ResourceType.*;
import java.io.File;
import javax.ws.rs.*;
import org.codehaus.jettison.json.JSONObject;
import org.json.XML;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import static m.weppws.ApplicationResources.*;
import org.apache.commons.io.FileUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;

@Name("weppws-channeldb")
@Description("WEPPWS-channeldb - channel database records")
@Path("m/channeldb/1.0")
@Polling(first = 2000, next = 2000)

@Resource(file = "/data/channelDatabase.json", id = "channeldatabase")
@Resource(file = "/data/channelDatabaseDataDictionary.json", id = "channeldatabasedictionary")

// Executables & File parameter
public class V1_0 extends ModelDataService {

  static final String NAMES_ONLY = "namesonly";
  static final String SINGLE_NAME = "name";
  static final String ORDERS = "channelOrderData";
  static final String ALL_DATA = "allrecords";
  static final String DATA_DICTIONARY = "dataDictionary";

  JSONArray channelOrderData;
  JSONArray channelParameterData;
  JSONArray channelDataDictionary;

  boolean namesOnly;
  String recName;
  boolean ordersOnly;
  boolean allData;
  boolean dataDictionary;


  @Override
  protected void preProcess() throws Exception {
    namesOnly = parameter().getBoolean(NAMES_ONLY, true);
    recName = parameter().getString(SINGLE_NAME, "");
    ordersOnly = parameter().getBoolean(ORDERS, false);
    allData = parameter().getBoolean(ALL_DATA, false);
    dataDictionary = parameter().getBoolean(DATA_DICTIONARY, false);
  }


  /**
   * doProcess() Get input parameters from WEPP request.
   *
   * @throws Exception
   */
  @Override
  protected void doProcess() throws Exception {
    readChannelDatabaseJSON(resources().getFile("channeldatabase"));

    if (dataDictionary) {
      readChannelDatabaseDict(resources().getFile("channeldatabasedictionary"));
    }

  }


  /**
   * postProcess() Extract some WEPP outputs and include links to model input
   * and output files.
   *
   * @throws Exception
   */
  @Override
  protected void postProcess() throws Exception {
    if (namesOnly) {
      // Just a list of the channel names
      JSONArray names = new JSONArray();
      for (int i = 0; i < channelParameterData.length(); i++) {
        JSONObject chan = channelParameterData.getJSONObject(i);
        JSONObject ch = new JSONObject();
        ch.put("name", chan.getString("name"));
        ch.put("id", chan.getString("id"));
        names.put(ch);
      }
      results().put("channels", names, "Channel names", "");
    }
    if (ordersOnly) {
      // JSON information for the channel orders
      results().put("channelOrders", channelOrderData, "Channel Order Data", "");
    }
    if (!recName.isEmpty()) {
      // JSON parameters for single channel database record
      for (int i = 0; i < channelParameterData.length(); i++) {
        JSONObject chan = channelParameterData.getJSONObject(i);
        if (chan.getString("name").equals(recName)) {
          results().put("channelParameters", chan, "Channel parameters", "");
          break;
        }
      }
    }
    if (allData) {
      // return everything
      results().put("allChannelOrders", channelOrderData, "All Channel Order Data", "");
      results().put("allChannelParameters", channelParameterData, "All Channel Parameter Data", "");
    }
    if (dataDictionary) {
      results().put("dataDictionary", channelDataDictionary, "Data dictionary for channel parameters", "");
    }
  }


  void readChannelDatabaseJSON(File dbFile) throws ServiceException {
    try {
      String chandata = FileUtils.readFileToString(dbFile, "UTF-8");
      JSONObject st = new JSONObject(chandata);
      JSONObject chdb = st.getJSONObject("channelDatabase");
      channelOrderData = chdb.getJSONArray("channelOrders");
      channelParameterData = chdb.getJSONArray("channels");
    } catch (IOException | JSONException e) {
      throw new ServiceException("Could not read channel database file: " + dbFile.getName());
    }
  }


  void readChannelDatabaseDict(File dbFile) throws ServiceException {
    try {
      // read in the JSON data for channel orders
      String chandata = FileUtils.readFileToString(dbFile, "UTF-8");
      JSONObject st = new JSONObject(chandata);
      channelDataDictionary = st.getJSONArray("channelDatabaseDataDictionary");
    } catch (IOException | JSONException e) {
      throw new ServiceException("Could not read channel database dictionary file: " + dbFile.getName());
    }
  }

}