SOILS_DB_Factory.java [src/soils/db] 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 soils.db;

import csip.Config;
import csip.SessionLogger;
import csip.api.server.ServiceException;
import csip.api.server.ServiceResources;
import java.sql.Connection;
import java.sql.SQLException;
import static soils.db.DBResources.PROVIDER_TYPE;
import static soils.db.DBResources.SDM;
import static soils.db.DBResources.SDM_REST;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class SOILS_DB_Factory {

  private SOILS_DB_Factory() {

  }

  public static SOILS_DATA createEngine(Class<?> c, SessionLogger LOG) throws ServiceException, SQLException {
    return getEngine(c, LOG, Config.getString(PROVIDER_TYPE, "SSURGO"));
  }

  public static SOILS_DATA createEngine(Class<?> c, SessionLogger LOG, String name) throws ServiceException, SQLException {
    return getEngine(c, LOG, name);
  }

  public static SOILS_DATA createEngine(ServiceResources resources, SessionLogger LOG) throws ServiceException, SQLException {
    return getEngine(resources, LOG, Config.getString(PROVIDER_TYPE, "SSURGO"));
  }

  public static SOILS_DATA createEngine(ServiceResources resources, SessionLogger LOG, String name) throws ServiceException, SQLException {
    return getEngine(resources, LOG, name);
  }

  public static SOILS_DATA createEngine(Connection conn, SessionLogger LOG) throws ServiceException, SQLException {
    return new mssql_sdm(conn, LOG);
  }

  private static SOILS_DATA getEngine(Class<?> c, SessionLogger LOG, String name) throws ServiceException, SQLException {
    SOILS_DATA ret_val;

    if ((null == name) || (name.isEmpty())) {
      throw new ServiceException("The SoilsDbFactory name parameter is either null or empty.  Please specify the type of SDM database desired in the configuration file for this service using the key: \"" + PROVIDER_TYPE + "\".  Choices are: SSURGO, SDM, SDM_REST");
    }

    switch (name.toUpperCase()) {
      case "SDM":
        ret_val = new mssql_sdm(c, LOG);
        break;

      case "SDM_REST":
        ret_val = new mssql_sdm(c, LOG, true);
        break;

      default:
        throw new IllegalArgumentException("Invalid database source name specified to soils_data.createEngine().");

    }
    return ret_val;
  }

  private static SOILS_DATA getEngine(ServiceResources resources, SessionLogger LOG, String name) throws ServiceException, SQLException {
    SOILS_DATA ret_val;

    if ((null == name) || (name.isEmpty())) {
      throw new ServiceException("The SoilsDbFactory name parameter is either null or empty.  Please specify the type of SDM database desired in the configuration file for this service using the key: \"" + PROVIDER_TYPE + "\".  Choices are: SSURGO, SDM, SDM_REST");
    }

    switch (name.toUpperCase()) {
      case "SDM":
        ret_val = new mssql_sdm(resources.getJDBC(SDM), LOG);
        break;

      case "SDM_REST":
        ret_val = new mssql_sdm(resources.getJDBC(SDM_REST), LOG);
        break;

      default:
        throw new IllegalArgumentException("Invalid database source name specified to soils_data.createEngine().");

    }
    return ret_val;
  }
}