DatabaseBuilder.java [src/java/m/gis] Revision: 70467d69a58db7d081f87586decf837e5a6df84b  Date: Mon May 11 17:10:11 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.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 *
 * @author ktraff
 */
public class DatabaseBuilder {

    /**
     * Represents all possible target database connections. 
     */
    public static enum DB { POSTGRES };

    private DB type;
    private String port;
    private String dbName;
    private String hostname;
    private String username;
    private String password;

    public DatabaseBuilder() {
    }

    public DatabaseBuilder setAll(String filename) throws IOException {
        Properties prop = new Properties();
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(filename);
 
		if (inputStream != null) {
			prop.load(inputStream);
		} else {
			throw new FileNotFoundException("property file '" + filename + "' not found in the classpath");
		}

        setType(DB.POSTGRES)
            .setHostname(prop.getProperty("hostname"))
            .setPort(prop.getProperty("port"))
            .setDbName(prop.getProperty("dbName"))
            .setUsername(prop.getProperty("username"))
            .setPassword(prop.getProperty("password"));
        
        return this;
    }

    public DatabaseBuilder setType(DB type) {
        this.type = type;
        return this;
    }

    public DatabaseBuilder setHostname(String hostname) {
        this.hostname = hostname;
        return this;
    }

    public DatabaseBuilder setPort(String port) {
        this.port = port;
        return this;
    }

    public DatabaseBuilder setDbName(String dbName) {
        this.dbName = dbName;
        return this;
    }

    public DatabaseBuilder setUsername(String username) {
        this.username = username;
        return this;
    }

    public DatabaseBuilder setPassword(String password) {
        this.password = password;
        return this;
    }

    public Database create() {
        switch (type) {
            case POSTGRES:
                return new PostgresqlDatabase(hostname, port, dbName, username, password);
            default:
                return new PostgresqlDatabase(hostname, port, dbName, username, password);
        }
    }
    
    
}