Database.java [src/m/utils] 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.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ktraff
 */
public abstract class Database {
    
    protected Connection conn;
    protected String hostname;
    protected String port;
    protected String dbName;
    protected String username;
    protected String password;
    
    public static enum DataType {NUMERIC, STRING};
    public final String SESSION_ID;
    protected final Logger LOG = Logger.getLogger(getClass().getSimpleName());
    public static final String DEFAULT_CRS = "EPSG:4326";
    public static final String DEFAULT_SCHEMA = "csip";
    
    public Database(String hostname, String port, String dbName, String username, String password) {
        SESSION_ID = new RandomString(8).nextString();
        
        setConnection(hostname, port, dbName, username, password);
    }
    
    public Database(String filePath) throws FileNotFoundException, IOException {
        SESSION_ID = new RandomString(8).nextString();
        Properties prop = new Properties();
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(filePath);

        if (inputStream != null) {
            prop.load(inputStream);
        } else {
            throw new FileNotFoundException("property file '" + filePath + "' not found in the classpath");
        }

        setConnection(hostname, port, dbName, username, password);
    }
    
    public Database(Connection conn) throws Exception {
        SESSION_ID = new RandomString(8).nextString();
        this.conn = conn;
    }
    
    public void setConnection(String hostname, String port, String dbName, String username, String password) {
        this.hostname = hostname;
        this.port = port;
        this.dbName = dbName;
        this.username = username;
        this.password = password;
        conn = getConnection(hostname, port, dbName, username, password);
        try {
            conn.setAutoCommit(true);
        } catch (SQLException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    }
    
    public static Connection getConnection(String hostname, String port, String dbName, String username, String password) {
        try {
            Class.forName("org.postgresql.Driver");
            String conn = "jdbc:postgresql://" + hostname + ":" + port + "/" + dbName;
            Logger.getLogger(Database.class.getName()).log(Level.INFO, "Connection: " + conn);
            return DriverManager.getConnection(conn, username, password);
        } catch (SQLException|ClassNotFoundException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public String getConnectionString() {
        String connStr = "PG:host="+this.hostname+" user="+this.username+" dbname="+this.dbName+
                " password="+this.password+" port="+this.port;
        Logger.getLogger(Database.class.getName()).log(Level.INFO, "Connection string: " + connStr);
        return connStr;
    }
    
    public String getSessionID() {
        return SESSION_ID;
    }
    
    public String getDefaultCRS() {
        return DEFAULT_CRS;
    }
    
    public String getDefaultSchema() {
        return DEFAULT_SCHEMA;
    }
    
}