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

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.huihoo.willow.Globals;

import com.zosatapo.commons.store.Store;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class WorkflowProperties
{
//---------------------------------------------------
	private static Store xpdlStore=null;
	private static Store caseStore=null;
	private static Store userStore=null;
	
	private static String schemaContextFile=null;
	
	private static Properties props=null;
	
//---------------------------------------------------	
	static
	{
		load();
	}
	
	private WorkflowProperties(){}
	
	public static Store getXPDLStore()
	{
		return xpdlStore;
	}
	
	public static Store getCaseStore()
	{
		return caseStore;
	}
	
	public static Store getUserStore()
	{
		return userStore;
	}
	
	public static String getSchemaContextFile()
	{
		return schemaContextFile;
	}
	
	public static String getProperty(String key)
	{
		return props.getProperty(key);
	}
//---------------------------------------------------------------
//----------------------------------------------------------------	
	private static void load()
	{
		File schemaFile=new File(System.getProperty(Globals.PROPS_WILLOW_HOME),Constants.SchemaProperties);
		if(schemaFile.exists())
		{
			schemaContextFile=schemaFile.getAbsolutePath();
		}
		
		File storeFile=new File(System.getProperty(Globals.PROPS_WILLOW_HOME),Constants.StoreProperties);
		
		if(!storeFile.exists() || !storeFile.isFile())
		{
			storeFile=new File(System.getProperty(org.huihoo.willow.deploy.Constants.PROPS_WILLOW_STORE));
		}
		
		if(storeFile.exists())
		{
			FileInputStream fin=null;
			try
			{
				fin=new FileInputStream(storeFile);
				props=new  Properties(); 
				props.load(fin);
				
				xpdlStore=loadStore(props,"xpdl");
				caseStore=loadStore(props,"case");
				userStore=loadStore(props,"user");	
			}
			catch (Exception e)
			{
				;
			}
			finally
		  {
				try
				{
					fin.close();
				}
				catch (IOException e1)
				{
					;
				}
		  }
		}
	}
	
	private static Store  loadStore(Properties props,String prefix)
	{
		String connURL=props.getProperty(prefix+"."+Store.STORE_URL);		
		String connType=props.getProperty(prefix+"."+Store.STORE_TYPE);
		if(connURL==null || connURL.length()==0 ||connType==null ||connType.length()==0)
		{
			return null;
		}	
		String connDriver=props.getProperty(prefix+"."+Store.STORE_DRIVER);
		String connUSR=props.getProperty(prefix+"."+Store.STORE_USERNAME);
		String connPWD=props.getProperty(prefix+"."+Store.STORE_PASSWORD);
		
		return new Store(connType,connURL,connUSR,connPWD,connDriver);
	}
}
