package siprp.planoactuacao.db;

import siprp.SingletonConstants;

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.sql.Insert;
import com.evolute.utils.strings.UnicodeChecker;

public class PlanoActuacaoDBInit
{
	public static void initDB( boolean web )
		throws Exception
	{
		if( web )
		{
			String url = DBConstants.WEB_URL;
			String user = DBConstants.WEB_USER;
			String pwd = DBConstants.WEB_PASSWORD;
			DBManager webManager = new JDBCManager( url, user, pwd, 10, 8, 8, null );
			Singleton.setInstance( DBConstants.WEB_DBMANAGER, webManager );
			
			url = DBConstants.LOCAL_URL;
			user = DBConstants.LOCAL_USER;
			pwd = DBConstants.LOCAL_PASSWORD;
			DBManager localManager = new JDBCManager( url, user, pwd, 10, 8, 8, null );
			Singleton.setInstance( DBConstants.LOCAL_DBMANAGER, localManager );
		}
		else
		{
			String url = ( String ) Singleton.getInstance( SingletonConstants.WEB_URL_PREFIX );
			url += ( String ) Singleton.getInstance( SingletonConstants.WEB_URL ) + "/";
			url += ( String ) Singleton.getInstance( SingletonConstants.WEB_DB_NAME ) + "/";
			String user = ( String ) Singleton.getInstance( SingletonConstants.WEB_USER );
			String pwd = ( String ) Singleton.getInstance( SingletonConstants.WEB_PASSWORD );
			DBManager webManager = new JDBCManager( url, user, pwd, 10, 8, 8, null );
			Singleton.setInstance( DBConstants.WEB_DBMANAGER, webManager );
			Singleton.setInstance( DBConstants.LOCAL_DBMANAGER, Singleton.getInstance( Singleton.DEFAULT_DBMANAGER ) );
		}
		
		Insert.setDefaultKeyRetriever( PostgresqlAutoKeyRetriever.RETRIEVER );
		UnicodeChecker.setUseDoubleSlash( true );
	}
}
