V1_0.java [src/java/m/weppws/impounddb] 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.impounddb;

import csip.ModelDataService;
import csip.annotations.*;
import csip.api.server.ServiceException;
import java.io.File;
import java.io.IOException;
import javax.ws.rs.*;
import org.codehaus.jettison.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;

@Name("weppws-impounddb")
@Description("WEPPWS-impounddb - impoundment database records")
@Path("m/impounddb/1.0")
@Polling(first = 2000, next = 2000)
@Resource(file = "/data/impoundmentDatabase.json", id = "impoundmentdatabase")
@Resource(file = "/data/impoundmentDatabaseDataDictionary.json", id = "impoundmentdatabasedictionary")
public class V1_0 extends ModelDataService {

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

  boolean namesOnly;
  String recName;
  JSONArray impoundmentParameterData;
  JSONArray impoundmentDataDictionary;
  boolean allData;
  boolean dataDictionary;


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


  /**
   * doProcess() Get input parameters from WEPP request.
   *
   * @throws Exception
   */
  @Override
  protected void doProcess() throws Exception {
    if (namesOnly || (!recName.isEmpty()))
      readImpoundmentDatabaseJSON(resources().getFile("impoundmentdatabase"));

    if (dataDictionary)
      readImpoundmentDatabaseDict(resources().getFile("impoundmentdatabasedictionary"));
  }


  /**
   * 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 impoundment names
      JSONArray names = new JSONArray();
      for (int i = 0; i < impoundmentParameterData.length(); i++) {
        JSONObject node = impoundmentParameterData.getJSONObject(i);
        JSONObject imp = new JSONObject();
        imp.put("name", node.getString("name"));
        imp.put("id", node.getString("id"));
        names.put(imp);
      }
      results().put("impoundments", names, "Impoundment names", "");
    }

    if (!recName.isEmpty()) {
      // JSON parameters for single channel database record
      for (int i = 0; i < impoundmentParameterData.length(); i++) {
        JSONObject imp = impoundmentParameterData.getJSONObject(i);
        if (imp.getString("name").equals(recName)) {
          results().put("impoundmentParameters", imp, "Impoundment parameters", "");
          break;
        }
      }
    }
    if (allData)
      results().put("allChannelParameters", impoundmentParameterData, "All Impoundment Parameter Data", "");

    if (dataDictionary)
      results().put("dataDictionary", impoundmentDataDictionary, "Data dictionary for channel parameters", "");

  }


  void readImpoundmentDatabaseJSON(File dbFile) throws ServiceException {
    try {
      String impounddata = FileUtils.readFileToString(dbFile, "UTF-8");
      JSONObject st = new JSONObject(impounddata);
      impoundmentParameterData = st.getJSONArray("impoundments");
    } catch (IOException | JSONException e) {
      throw new ServiceException("Could not read impoundment database file: " + dbFile.getName());
    }
  }


  void readImpoundmentDatabaseDict(File dbFile) throws ServiceException {
    try {
      String impounddata = FileUtils.readFileToString(dbFile, "UTF-8");
      JSONObject st = new JSONObject(impounddata);
      impoundmentDataDictionary = st.getJSONArray("impoundmentDatabaseDataDictionary");
    } catch (IOException | JSONException e) {
      throw new ServiceException("Could not read impoundment database dictionary file: " + dbFile.getName());
    }
  }

}