/*
 * Created on 2004-10-19
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.huihoo.willow.store;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Enumeration;

import org.apache.commons.digester.Digester;
import org.huihoo.willow.startup.DigesterFactory;
import org.xml.sax.InputSource;

import workflow.store.spi.SpiCaseDatabase;
import workflow.store.spi.SpiUserDatabase;
import workflow.store.spi.SpiWorkDispatcher;

import com.zosatapo.workflow.store.DatabaseRegistry;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class DatabaseLoader
{
	public static void loadOnStartup(String databaseXml)
	{
		File databaseFile = new File(databaseXml);
		if (!databaseFile.isAbsolute())
		{
			databaseFile = new File(System.getProperty("willow.home"), databaseXml);
		}

		if (!databaseFile.exists() || !databaseFile.isFile())
		{
			databaseFile=new File(System.getProperty(org.huihoo.willow.deploy.Constants.PROPS_WILLOW_DATABASE));
		}

		if (!databaseFile.exists() || !databaseFile.isFile())
		{
			return;
		}		
		
		Digester dbDigester = DigesterFactory.newDigester(false, false, new DatabaseRuleSet());
		ClassLoader clazzLoader = DatabaseLoader.class.getClassLoader();
		dbDigester.setClassLoader(clazzLoader);
		dbDigester.setUseContextClassLoader(false);
		InputSource inputSource = null;
		InputStream inputStream = null;
		try
		{
			inputStream = new FileInputStream(databaseFile);
			inputSource = new InputSource("file://" + databaseFile.getAbsolutePath());
		}
		catch (Exception e)
		{
			;
		}

		if (inputStream == null)
		{
			return;
		}

		DatabaseEntries entries = new DatabaseEntries();
		try
		{
			inputSource.setByteStream(inputStream);
			dbDigester.push(entries);
			dbDigester.parse(inputSource);
			inputStream.close();
		}
		catch (Exception e)
		{
			System.out.println("DatabaseLoader.loadOnStartup: " + e);
			e.printStackTrace(System.out);
			return;
		}

		Enumeration databases = entries.entries();
		while (databases.hasMoreElements())
		{
			DatabaseEntry database =
				(DatabaseEntry) entries.findDatabaseEntry((String) databases.nextElement());
			registerDatabase(database);
		}
	}

	private static void registerDatabase(DatabaseEntry databaseEntry)
	{
		String driver = databaseEntry.getDriver();
		if (driver == null)
		{
			return;
		}

		ClassLoader clazzLoader = DatabaseLoader.class.getClassLoader();

		String userdatabaseClass = databaseEntry.getUserDatabaseClass();
		String casedatabaseClass = databaseEntry.getCaseDatabaseClass();
		String dispatcherClass = databaseEntry.getDispatcherClass();

		if (userdatabaseClass != null)
		{
			try
			{
				Class clazz = clazzLoader.loadClass(userdatabaseClass);
				SpiUserDatabase spiUserDatabase = (SpiUserDatabase) clazz.newInstance();
				DatabaseRegistry.registerUserDB(driver, spiUserDatabase);
			}
			catch (Throwable e)
			{
				;
			}
		}

		if (casedatabaseClass != null)
		{
			try
			{
				Class clazz = clazzLoader.loadClass(casedatabaseClass);
				SpiCaseDatabase spiCaseDatabase = (SpiCaseDatabase) clazz.newInstance();
				DatabaseRegistry.registerCaseDB(driver, spiCaseDatabase);
			}
			catch (Throwable e)
			{
				;
			}
		}

		if (dispatcherClass != null)
		{
			try
			{
				Class clazz = clazzLoader.loadClass(dispatcherClass);
				SpiWorkDispatcher spiWorkDispatcher = (SpiWorkDispatcher) clazz.newInstance();
				DatabaseRegistry.registerDispDB(driver, spiWorkDispatcher);
			}
			catch (Throwable e)
			{
				;
			}
		}
	}
}
