package @package@;

import javax.jdo.PersistenceManagerFactory;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * FireStorm/DAO uses this source file as a template when generating code for JDO. You can modify
 * this template to suit your own needs so long as you preserve the method signatures that exist.
 *
 * @author Andy Grove
 */
public class ResourceManager
{
	/**
	 * Obtain a PersistenceManager based on the default properties.
	 *
	 * @return
	 */
	public static PersistenceManager getPersistenceManager()
	{
		Properties prop = getDefaultProperties();
		PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory( prop );
		PersistenceManager pm = pmf.getPersistenceManager();
		return pm;
	}

	/**
	 * Obtain a PersistenceManager based on the supplied properties.
	 *
	 * @param prop
	 * @return
	 */
	public static PersistenceManager getPersistenceManager(Properties prop)
	{
		PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory( prop );
		PersistenceManager pm = pmf.getPersistenceManager();
		return pm;
	}

	/**
	 * Obtain a PersistenceManager based on the supplied properties file.
	 *
	 * @param propertiesFile
	 * @return
	 */
	public static PersistenceManager getPersistenceManager(File propertiesFile)
		throws FileNotFoundException, IOException
	{
		Properties prop = new Properties();
		FileInputStream is = new FileInputStream( propertiesFile );
		prop.load( is );
		PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory( prop );
		PersistenceManager pm = pmf.getPersistenceManager();
		return pm;
	}

	/**
	 * Default JDO properties.
	 *
	 * @return
	 */
	public static Properties getDefaultProperties()
	{
		Properties prop = new Properties();
		prop.put( "javax.jdo.PersistenceManagerFactoryClass", "com.sun.jdori.fostore.FOStorePMF" );
		prop.put( "javax.jdo.option.ConnectionURL", "" );
		prop.put( "javax.jdo.option.ConnectionUserName", "" );
		prop.put( "javax.jdo.option.ConnectionPassword", "" );
		prop.put( "javax.jdo.option.Optimistic", "false" );
		return prop;
	}

}
