//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.workflow;

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

public class SchemaContext implements WorkflowSerializable
{
	//---------------------------------------------------
	//Property Keys
	//---------------------------------------------------
	public final static String SCHEMA_RANK = "schema.rank";
	public final static String SCHEMA_CATEGORY = "schema.category";
	public final static String SCHEMA_ADMIN = "schema.admin";

	public final static String SCHEMA_USER = "schema.user";
	public final static String SCHEMA_GROUP = "schema.group";
	public final static String SCHEMA_ROLE = "schema.role";
	public final static String SCHEMA_DEPARTMENT = "schema.department";

	public final static String SCHEMA_USER_GROUP = "schema.user.group";
	public final static String SCHEMA_USER_ROLE = "schema.user.role";
	public final static String SCHEMA_USER_DEPARTMENT = "schema.user.department";

	public final static String SCHEMA_CASE = "schema.case";
	public final static String SCHEMA_WORK = "schema.work";
	public final static String SCHEMA_PATH = "schema.path";
	public final static String SCHEMA_CASE_EVENT = "schema.case.event";
	public final static String SCHEMA_WORK_EVENT = "schema.work.event";
	public final static String SCHEMA_CASE_DATAFIELD = "schema.case.datafield";
	public final static String SCHEMA_ACTIVITY_DISPATCH = "schema.activity.dispatch";

	//---------------------------------------------------
	//Property Default Values
	//---------------------------------------------------
	public final static String SCHEMA_RANK_DEFAULT = "om_p_rank";
	public final static String SCHEMA_CATEGORY_DEFAULT = "om_p_category";
	public final static String SCHEMA_ADMIN_DEFAULT = "om_p_admin";

	public final static String SCHEMA_USER_DEFAULT = "om_p_user";
	public final static String SCHEMA_GROUP_DEFAULT = "om_p_group";
	public final static String SCHEMA_ROLE_DEFAULT = "om_p_role";
	public final static String SCHEMA_DEPARTMENT_DEFAULT = "om_p_department";

	public final static String SCHEMA_USER_GROUP_DEFAULT = "om_p_user_group";
	public final static String SCHEMA_USER_ROLE_DEFAULT = "om_p_user_role";
	public final static String SCHEMA_USER_DEPARTMENT_DEFAULT = "om_p_user_department";

	public final static String SCHEMA_CASE_DEFAULT = "rt_p_case";
	public final static String SCHEMA_WORK_DEFAULT = "rt_p_work";
	public final static String SCHEMA_PATH_DEFAULT = "rt_p_path";
	public final static String SCHEMA_CASE_EVENT_DEFAULT = "rt_p_caseevent";
	public final static String SCHEMA_WORK_EVENT_DEFAULT = "rt_p_workevent";
	public final static String SCHEMA_CASE_DATAFIELD_DEFAULT = "rt_p_datafield";
	public final static String SCHEMA_ACTIVITY_DISPATCH_DEFAULT = "rt_p_activity_dispatch";

	//---------------------------------------------------
	//Instant Variables
	//---------------------------------------------------
	protected Properties schemaProps = new Properties();
	protected Hashtable defaultTableProps = new Hashtable();
	protected Hashtable defaultColumnProps = new Hashtable();

	protected String configFile = null;

	public SchemaContext()
	{
		this(null);
	}

	public SchemaContext(String configFile)
	{
		this.configFile = configFile;

		initializeDefault();
		installSchemaContext();
	}

	public String getConfigFile()
	{
		return configFile;
	}

	public String getTableName(String key)
	{
		String value = schemaProps.getProperty(key);
		if (value == null || value.trim().length() == 0)
		{
			value = (String) defaultTableProps.get(key);
		}

		return value;
	}

	public String getColumnName(String key)
	{
		String value = schemaProps.getProperty(key);
		if (value == null || value.trim().length() == 0)
		{
			value = (String) defaultColumnProps.get(key);
		}

		return value;
	}

	protected void initializeDefault()
	{
		defaultTableProps.put(SCHEMA_RANK, SCHEMA_RANK_DEFAULT);
		defaultTableProps.put(SCHEMA_CATEGORY, SCHEMA_CATEGORY_DEFAULT);
		defaultTableProps.put(SCHEMA_ADMIN, SCHEMA_ADMIN_DEFAULT);

		defaultTableProps.put(SCHEMA_USER, SCHEMA_USER_DEFAULT);
		defaultTableProps.put(SCHEMA_GROUP, SCHEMA_GROUP_DEFAULT);
		defaultTableProps.put(SCHEMA_ROLE, SCHEMA_ROLE_DEFAULT);
		defaultTableProps.put(SCHEMA_DEPARTMENT, SCHEMA_DEPARTMENT_DEFAULT);
		defaultTableProps.put(SCHEMA_USER_GROUP, SCHEMA_USER_GROUP_DEFAULT);
		defaultTableProps.put(SCHEMA_USER_ROLE, SCHEMA_USER_ROLE_DEFAULT);
		defaultTableProps.put(SCHEMA_USER_DEPARTMENT, SCHEMA_USER_DEPARTMENT_DEFAULT);

		defaultTableProps.put(SCHEMA_CASE, SCHEMA_CASE_DEFAULT);
		defaultTableProps.put(SCHEMA_WORK, SCHEMA_WORK_DEFAULT);
		defaultTableProps.put(SCHEMA_PATH, SCHEMA_PATH_DEFAULT);
		defaultTableProps.put(SCHEMA_CASE_EVENT, SCHEMA_CASE_EVENT_DEFAULT);
		defaultTableProps.put(SCHEMA_WORK_EVENT, SCHEMA_WORK_EVENT_DEFAULT);
		defaultTableProps.put(SCHEMA_CASE_DATAFIELD, SCHEMA_CASE_DATAFIELD_DEFAULT);
		defaultTableProps.put(SCHEMA_ACTIVITY_DISPATCH, SCHEMA_ACTIVITY_DISPATCH_DEFAULT);
	}

	protected void installSchemaContext()
	{
		if (configFile == null || configFile.trim().length() == 0)
		{
			return;
		}

		File schemaFile = new File(configFile);
		if (!schemaFile.exists())
		{
			return;
		}

		FileInputStream fis = null;
		try
		{
			fis = new FileInputStream(schemaFile);
			schemaProps.load(fis);
		}
		catch (Exception ex)
		{
			;
		}
		finally
		{
			if (fis != null)
			{
				try
				{
					fis.close();
				}
				catch (IOException e)
				{
					;
				}
				fis = null;
			}
		}

	}
}
