package com.evolute.siprp.server.db;

import java.util.Date;
import java.util.Properties;

import com.evolute.utils.jdo.JDOProvider;

/**
 * 
 * @author jneto
 */
public abstract class JDOProviderFactory
{
	public static JDOProvider createInstance(String url, String user, String password)
	{
		Date d = new Date();
		System.out.println("INIT: " + d);

		Properties jpoxProps;
		jpoxProps = new Properties();
		jpoxProps.setProperty("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.jdo.JDOPersistenceManagerFactory");
		jpoxProps.setProperty("javax.jdo.option.ConnectionDriverName", "org.postgresql.Driver");
		jpoxProps.setProperty("datanucleus.identifier.fullyQualifiedNames", "false");
		jpoxProps.setProperty("datanucleus.identifier.case", "PreserveCase");
		jpoxProps.setProperty("javax.jdo.option.ConnectionURL", url );
		jpoxProps.setProperty("javax.jdo.option.ConnectionUserName", user);
		jpoxProps.setProperty("javax.jdo.option.ConnectionPassword", password);
		jpoxProps.setProperty("javax.jdo.option.IgnoreCache", "false");
		jpoxProps.setProperty("javax.jdo.option.NontransactionalRead", "true");
		jpoxProps.setProperty("javax.jdo.option.RetainValues", "true");
		jpoxProps.setProperty("datanucleus.autoCreateTables", "false");
		jpoxProps.setProperty("datanucleus.autoCreateSchema", "false");
		jpoxProps.setProperty("datanucleus.autoCreateConstraints", "false");
		jpoxProps.setProperty("datanucleus.autoCreateColumns", "false");
		jpoxProps.setProperty("datanucleus.validateTables", "false");
		jpoxProps.setProperty("datanucleus.validateColumns", "false");
		jpoxProps.setProperty("datanucleus.validateConstraints", "false");
		jpoxProps.setProperty("datanucleus.rdbms.CheckExistTablesOrViews", "false");
		jpoxProps.setProperty("datanucleus.rdbms.initializeColumnInfo", "None");
//		jpoxProps.setProperty("datanucleus.fixedDatastore", "true");
		jpoxProps.setProperty("datanucleus.identifier.defaultSchemaName", "public");
		jpoxProps.setProperty("datanucleus.autoStartMechanismMode", "Ignored");
		jpoxProps.setProperty("datanucleus.autoStartMechanism", "None");
		jpoxProps.setProperty("datanucleus.poid.transactionAttribute", "UsePM");		
		jpoxProps.setProperty("datanucleus.connectionPoolingType", "C3P0");
		jpoxProps.setProperty("datanucleus.connectionPool.maxPoolSize", "5");
		jpoxProps.setProperty("datanucleus.connectionPool.minPoolSize", "1");
		jpoxProps.setProperty("datanucleus.connectionPool.initialPoolSize", "1");
		jpoxProps.setProperty("datanucleus.catalogName", "siprp");
		
		// System.setProperty( "datanucleus.schemaName", "public" );
		// PersistenceManagerFactory pmf =
		// JDOHelper.getPersistenceManagerFactory( jpoxProps );
		// // pmf.setOptimistic( true );
		// System.out.print( "Loading PM: " + new Date() );
		// // ( ( PersistenceManagerFactoryImpl )pmf ).setTransactionIsolation(
		// Connection.TRANSACTION_READ_UNCOMMITTED );
		// // Retrieve a PersistenceManager from the PersistenceManagerFactory.
		// PersistenceManager pm = pmf.getPersistenceManager();
		// JDOObject.setPersistentManager( pm );
		// System.out.println( "PM done " + new Date() );
		JDOProvider defaultProvider = new JDOProvider( jpoxProps );
		return defaultProvider;
	}
}
