/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2015 Rodrigo de Bona Sartor
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.sartor.javaping.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

import com.sartor.javaping.db.entity.Host;

public class DbManager {
	
	private static final DbManager instance = new DbManager();
	private final String driver;
	private final String connectionUrl;
	private final String user;
	private final String password;
	private final ConcurrentHashMap<Thread,Connection> threadConMap;

	private DbManager() {
		// TODO: read from file
		driver = "org.h2.Driver";
		connectionUrl = "jdbc:h2:tcp://localhost/~/ping";
		user = "admin";
		password = "admin";
		threadConMap = new ConcurrentHashMap<Thread, Connection>();
	}

	public static DbManager getInstance() {
		return instance;
	}
	
	public void closeAll() {
		Collection<Connection> cons = new ArrayList<Connection>( threadConMap.values() );
		threadConMap.clear();
		for( Connection con : cons ){
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public Connection getConnection() throws SQLException {
		Thread t = Thread.currentThread();
		Connection con = threadConMap.get(t);
		if( con == null ){
			try {
                con = createNewConnection();
            } catch (ClassNotFoundException e) {
                throw new SQLException(e);
            }
			threadConMap.put(t, con);
		}
		return con;
	}

	private Connection createNewConnection() throws ClassNotFoundException, SQLException {
		Class.forName( driver );
		Connection conn = DriverManager.getConnection(connectionUrl,user,password);
		return conn;
	}

    public String createPingId( Host host ) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String id = sdf.format(new Date()) + host.getId();
        return id;
    }

    public String createHostStatusId(Host host) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String id = sdf.format(new Date()) + host.getId();
        return id;
    }
}

@@ -1208,24 +1208,57 @@
 ping.db;%0D%0A%0D%0A
+import java.io.FileInputStream;%0D%0A
 import java.
@@ -1456,16 +1456,46 @@
 .Date;%0D%0A
+import java.util.Properties;%0D%0A
 import j
@@ -1677,30 +1677,24 @@
 ;%0D%0A%09private 
-final 
 String drive
@@ -1702,30 +1702,24 @@
 ;%0D%0A%09private 
-final 
 String conne
@@ -1734,30 +1734,24 @@
 ;%0D%0A%09private 
-final 
 String user;
@@ -1761,22 +1761,16 @@
 private 
-final 
 String p
@@ -1877,65 +1877,285 @@
 %7B%0D%0A%09
-%09// TODO: read from file%0D%0A%09%09driver = %22org.h2.D
+    this.threadConMap = new ConcurrentHashMap%3CThread, Connection%3E();%0D%0A%09    try%7B%0D%0A    %09    Properties propFile = new Properties();%0D%0A    %09    propFile.load( new FileInputStream( %22config.properties%22 ) );%0D%0A    %09    %0D%0A    %09%09this.driver = propFile.getProperty(%22d
 river%22
+)
 ;%0D%0A
-%09%09
+    %09%09this.
 conn
@@ -2170,146 +2170,219 @@
 l = 
-%22jdbc:h2:tcp://localhost/~/ping%22;%0D%0A%09%09user = %22admin%22;%0D%0A%09%09password = %22admin%22;%0D%0A%09%09threadConMap = new ConcurrentHashMap%3CThread, Connection%3E();
+propFile.getProperty(%22url%22);%0D%0A    %09%09this.user = propFile.getProperty(%22user%22);%0D%0A    %09%09this.password = propFile.getProperty(%22password%22);%0D%0A%09    %7D%0D%0A%09    catch( Exception ex )%7B%0D%0A%09        ex.printStackTrace();%0D%0A%09    %7D
 %0D%0A%09%7D

Read connection parameters from file