package com.evolute.siprp.server.db;

import com.evolute.utils.Singleton;
import com.evolute.utils.db.DBManager;
import com.evolute.utils.db.JDBCManager;
import com.evolute.utils.db.keyretrievers.PostgresqlAutoKeyRetriever;
import com.evolute.utils.jdbc.DBStatementExecuter;
import com.evolute.utils.jdbc.StatementExecuterFactory;
import com.evolute.utils.jdo.JDOProvider;
import com.evolute.utils.sql.Insert;
import com.evolute.utils.sql.SQLQuery;
import com.evolute.utils.strings.UnicodeChecker;


public class InitDB 
{
	private static DBManager manager;
	private static JDOProvider jdoProvider;
	
	private static String dbUsername = "postgres";
	private static String dbPassword = "Typein";
	private static String URL = "jdbc:postgresql://storage:5432/siprp_20091203";
	
	public static void init() throws Exception
	{
		if ( manager == null && jdoProvider == null )
		{
			System.out.println( "Initializing db manager ..." );
			
			DBManager manager = new JDBCManager( URL, dbUsername, dbPassword, 100, 80, 80, new SQLQuery[]{} );
			Singleton.setInstance( Singleton.DEFAULT_DBMANAGER, manager );
			Insert.setDefaultKeyRetriever( PostgresqlAutoKeyRetriever.RETRIEVER );
			StatementExecuterFactory.initialize( new DBStatementExecuter( manager.getSharedExecuter( InitDB.class ) ) );
			UnicodeChecker.setUseDoubleSlash( true );
			
			initJDOConnection( dbUsername, dbPassword );		
		}
	}
	
	private static void initJDOConnection( String username, String password )
	{
		jdoProvider = JDOProviderFactory.createInstance( URL, username, password );
		Singleton.setInstance( Singleton.DEFAULT_JDO_PROVIDER, jdoProvider );
	}
	
}
