Fuel.java [src/java/crlmod/nodes] Revision:   Date:
/*
 * $Id: 1.0+62 Fuel.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 Fuel {

  /**
   * The file key in LMOD which uniquely identifies this fuel
   */
  public String file_key;
  /**
   * The name of this fuel
   */
  public String name;
  /**
   * The path of this fuel (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;
  /**
   * Short description of this fuel
   */
  public String descrip;
  /**
   * The cost per volume of this fuel in USD per gallon.
   */
  public String cost;
  /**
   * The energy per volume of this fuel in BTU per gallon.
   */
  public String energy;
  /**
   * A date which represents the versions of Rusle2 which was used to create
   * this data.
   */
  public String scienceVersion;
  /**
   * A ratio which indicates how to convert from Diesel to this fuel.
   */
  public String convert;
  /**
   * An XML string which holds the RUSLE2 specific representation of this fuel
   */
  public String r2Data;


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


  public Fuel(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 fuel 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 = 'ful'";

      try (ResultSet resultSet = statement.executeQuery(query)) {
        if (!resultSet.next()) {
          return "no fuel 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 cost
      query = "SELECT param_name,param_value \n"
          + "FROM file_data \n"
          + "WHERE param_name = 'FUEL_COST_PER_UNIT_VOLUME' \n"
          + "AND file_key = " + file_key + "\n";

      try (ResultSet resultSet = statement.executeQuery(query)) {
        if (resultSet.next()) {
          cost = resultSet.getString("param_value");
        }
      }
      statement.clearBatch();

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

      //get description
      query = "SELECT param_name,param_value \n"
          + "FROM file_data \n"
          + "WHERE param_name = 'FUEL_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 width
      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 convert
      query = "SELECT param_name,param_value \n"
          + "FROM file_data \n"
          + "WHERE param_name = 'FUEL_CONVERT_DIESEL_TO_THIS_FUEL' \n"
          + "AND file_key = " + file_key + "\n";
      try (ResultSet resultSet = statement.executeQuery(query)) {
        if (resultSet.next()) {
          convert = resultSet.getString("param_value");
        }
      }
      statement.clearBatch();
    }
    return null;
  }


  public String getFuelData(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.getFuelR2XML(this, doc);
      if (error != null) {
        return error;
      }
      r2Data = crlmod.utils.Utils.getStringFromDocument(doc);
    }
    return null;
  }
}