/*
 * Created on 2004-1-25
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package workflow.store;

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

import workflow.WorkflowInterface;

public  class SchemaContext implements WorkflowInterface
{
//---------------------------------------------------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" ;
	
  /**
   *  workflow database user settting including table name and column name
   */
  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" ;
	
  /**
   *  workflow database case table name setting
   */
  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" ;
	
  protected  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" ;
   
  protected  static String SCHEMA_CASE_DEFAULT                   = "rt_p_case" ;
  protected  static String SCHEMA_WORK_DEFAULT                   = "rt_p_work" ;
  protected  static String SCHEMA_PATH_DEFAULT                   = "rt_p_path" ;
  protected  static String SCHEMA_CASE_EVENT_DEFAULT             = "rt_p_caseevent" ;
  protected  static String SCHEMA_WORK_EVENT_DEFAULT             = "rt_p_workevent" ;
  protected  static String SCHEMA_CASE_DATAFIELD_DEFAULT         = "rt_p_datafield" ;
	protected  static String SCHEMA_ACTIVITY_DISPATCH_DEFAULT      = "rt_p_activity_dispatch" ;
	
  protected   Properties schemaProps=new Properties();
  protected   Hashtable  defaultTableProps=new Hashtable();
  protected   Hashtable  defaultColumnProps=new Hashtable();

  protected String pathname = null;
	
	public SchemaContext()
	{
		this(null);
	}
  
  public SchemaContext(String pathname)
  {
    this.pathname=pathname;

    loadDefaultConfig();
    loadSchemaConfig();
  }

  public  String getConfigFile()
  {
    return pathname;
  }

  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 loadDefaultConfig()
  {	
		defaultTableProps.put(SCHEMA_RANK,SCHEMA_RANK_DEFAULT);
		defaultTableProps.put(SCHEMA_CATEGORY,SCHEMA_CATEGORY_DEFAULT);
		defaultTableProps.put(SCHEMA_ADMIN,SCHEMA_ADMIN_DEFAULT);
			
    /**
     * schema name setting
     */		
    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 loadSchemaConfig()
  {
    if(pathname==null || "".equals(pathname))
    {
    	return;
    }
    
    try
    {
       File file = new File(pathname);
       
       if (!file.exists())
       {
         return;
       }
       FileInputStream fis = new FileInputStream(file);
       schemaProps.load(fis);
    }
    catch( Exception ex )
    {
      //-- nothing --
      ex.printStackTrace();
    }

  }
}
