HydraulicElementSystem.java [src/java/crlmod/nodes] Revision:   Date:
/*
 * $Id: 1.0+62 HydraulicElementSystem.java a170e1637ffa 2021-12-20 od $
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2024, 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 crlmod.nodes;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import crlmod.utils.NodeTranslator;
import org.codehaus.jettison.json.JSONException;
import org.w3c.dom.Document;

/**
 * @author Lucas Yaege
 */
public class HydraulicElementSystem {

  /**
   * The file key in LMOD which uniquely identifies this hydraulic element
   * system
   */
  public String file_key;
  /**
   * The name of this hydraulic element system
   */
  public String name;
  /**
   * The path of this hydraulic element system (From RUSLE2 historical
   * file-based system)
   */
  public String path;
  /**
   * The date on which this file was list changed.
   */
  public String date;
  /**
   * The name of the creator or publisher of this data.
   */
  public String owner;
  /**
   * The group which is permitted to alter this data
   */
  public String password;
  /**
   * The LMOD status of this object. (pending, approved, published)
   */
  public String status;
  /**
   * The r2 specific format for this Hydraulic Element System
   */
  public String r2Data;
  /**
   * Short description of this hydraulic element system
   */
  public String descrip;
  /**
   * The number of flow paths in this system.
   */
  public String numFlowPaths;
  /**
   * Boolean is the flow path at the bottom of the system.
   */
  public String flowPathAtBottom;
  /**
   * A date which represents the versions of Rusle2 which was used to create
   * this data.
   */
  public String scienceVersion;
  /**
   * Type of flow path used in this system
   */
  public String flowPathType;


  public HydraulicElementSystem(String file_key) {
    this.file_key = file_key;
  }


  public HydraulicElementSystem(String filekey, String name, String path, String date, String owner, String password) {
    this.file_key = filekey;
    this.name = name;
    this.path = path;
    this.date = date;
    this.owner = owner;
    this.password = password;
  }


  public String getFile_key() {
    return file_key;
  }


  public String getName() {
    return name;
  }


  public String getPath() {
    return path;
  }


  public String getOwner() {
    return owner;
  }


  public String getDate() {
    return date;
  }


  public String getPassword() {
    return password;
  }


  public String getR2Data() {
    return r2Data;
  }


  /**
   * Populates name and path of the hydraulic element system indicated by the
   * file_key with which it was instantiated.
   *
   * @param connection Connection to the LMOD Database
   * @return String indicating the details of an error that has occurred.
   * @throws SQLException
   */
  public String popPrev(Connection connection) throws SQLException {
    try (Statement statement = connection.createStatement()) {
      String query = "SELECT file_name, file_path,\n"
          + "CASE \n"
          + "WHEN published_date is not null THEN 'published' \n"
          + "WHEN approved_date is not null THEN 'approved' \n"
          + "WHEN imported_date is not null THEN 'pending' \n"
          + "END as status \n"
          + "FROM file_metadata \n"
          + "WHERE file_key = " + file_key + "\n"
          + "AND object_key = 'hes'";

      try (ResultSet resultSet = statement.executeQuery(query)) {
        if (!resultSet.next()) {
          return "no hydraulic element system with file key " + file_key;
        }
        name = resultSet.getString("file_name");
        path = resultSet.getString("file_path");
        status = resultSet.getString("status");
      }
    }
    return null;
  }


  public String populate(Connection connection) throws SQLException {
    try (Statement statement = connection.createStatement()) {
      String query;
      //get numPaths
      query = "SELECT param_name,param_value \n"
          + "FROM file_data \n"
          + "WHERE param_name = 'NUM_FLOW_PATH_IN_SYSTEM' \n"
          + "AND file_key = " + file_key + "\n";
      try (ResultSet resultSet = statement.executeQuery(query)) {
        if (resultSet.next()) {
          numFlowPaths = resultSet.getString("param_value");
        }
      }
      statement.clearBatch();

      //get path at bottom
      query = "SELECT param_name,param_value \n"
          + "FROM file_data \n"
          + "WHERE param_name = 'FLOW_PATH_AT_BOTTOM' \n"
          + "AND file_key = " + file_key + "\n";
      try (ResultSet resultSet = statement.executeQuery(query)) {
        if (resultSet.next()) {
          flowPathAtBottom = resultSet.getString("param_value");
        }
      }
      statement.clearBatch();

      //get description
      query = "SELECT param_name,param_value \n"
          + "FROM file_data \n"
          + "WHERE param_name = 'HYD_ELEMENT_SYSTEM_DESCRIP' \n"
          + "AND file_key = " + file_key + "\n";
      try (ResultSet resultSet = statement.executeQuery(query)) {
        if (resultSet.next()) {
          descrip = resultSet.getString("param_value");
        }
      }
      statement.clearBatch();

      //get science version
      query = "SELECT param_name,param_value \n"
          + "FROM file_data \n"
          + "WHERE param_name = 'RUSLE2:SCIENCEVERSION' \n"
          + "AND file_key = " + file_key + "\n";
      try (ResultSet resultSet = statement.executeQuery(query)) {
        if (resultSet.next()) {
          scienceVersion = resultSet.getString("param_value");
        }
      }
      statement.clearBatch();

      //get flow path type
      query = "SELECT param_name,param_value \n"
          + "FROM file_data \n"
          + "WHERE param_name = 'HYD_SYSTEM_FLOW_PATH_TYPE' \n"
          + "AND file_key = " + file_key + "\n";
      try (ResultSet resultSet = statement.executeQuery(query)) {
        if (resultSet.next()) {
          flowPathType = resultSet.getString("param_value");
        }
      }
      statement.clearBatch();
    }
    return null;
  }


  public String getHydraulicElementSystemData(boolean r2) throws SQLException, JSONException, ParserConfigurationException {
    String error = null;
    if (r2) {
      DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

      // root elements
      Document doc = docBuilder.newDocument();
      error = NodeTranslator.getHydraulicElementSystemR2XML(this, doc);
      if (error != null) {
        return error;
      }
      r2Data = crlmod.utils.Utils.getStringFromDocument(doc);
    }
    return null;
  }
}