Database.java [src/java/m/gis] Revision: 0c08525488fc95bc69a8bc986cb676eefeefddc6  Date: Mon Jun 22 11:53:53 MDT 2015
/*
 * 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.gis;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author ktraff
 */
public abstract class Database {

    public static enum DataType {NUMERIC, STRING};
    public static final String SCHEMA = "csip_gis";
    public final String SESSION_ID;

    protected Connection conn;
    protected final Logger LOG = Logger.getLogger(getClass().getSimpleName());

    public Database(String hostname, String port, String dbName, String username, String password) {
        SESSION_ID = new RandomString(8).nextString();
        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 abstract boolean exists(String schema, String tableName) throws csip.ServiceException;

    public abstract ArrayList<String> getColumns(String schema, String tableName) throws csip.ServiceException;

    public abstract void addColumn(String schema, String tableName, String columnName, DataType type) throws csip.ServiceException;
    
    public abstract void createSchema(String schemaName) throws csip.ServiceException;

    public abstract Layer createLayer(String tableName, JSONObject layerJSON) throws csip.ServiceException;

    public abstract Feature uploadFeature(JSONObject featureJSON, Layer layer) throws csip.ServiceException;

    public abstract int deleteTable(String tableName) throws csip.ServiceException;

    public abstract int deleteLayer(Layer lyr) throws csip.ServiceException;

    public abstract Layer intersect(Layer one, Layer two) throws csip.ServiceException;

    public abstract Layer union(Layer one, Layer two) throws csip.ServiceException;

    public abstract Layer buffer(Layer lyr, int distance) throws csip.ServiceException;

    public abstract Layer minimumBoundingCircle(Layer lyr, int numSegsPerQtCirc) throws csip.ServiceException;

    public abstract Layer minimumBoundingRectangle(Layer lyr) throws csip.ServiceException;

    public abstract Layer extent(Layer lyr) throws csip.ServiceException;

    public abstract Layer envelope(Layer lyr) throws csip.ServiceException;

    public abstract Layer area(Layer lyr) throws csip.ServiceException;

    public abstract JSONObject toJSON(Layer lyr) throws csip.ServiceException;

    public abstract String toWKT(Layer lyr) throws csip.ServiceException;
}