/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.servlet.template;

import org.apache.log4j.Logger;

import java.io.File;
import java.util.Hashtable;
import java.util.StringTokenizer;

import org.bodington.database.PrimaryKey;
import org.bodington.server.*;
import org.bodington.server.realm.User;
import org.bodington.server.realm.Group;

/**
 * The class Template. <p>
 * <i>(WebLearn modification: 18/05/2004 Colin Tatham <br />
 * Added ability to load templates specific to the user's group.
 * (Template files need to be stored in a named directory, e.g: 'group_sysadmins')
 * Rewritten/reorganised some code, and moved some existing code out into methods, for simplicity.
 * <p>
 * NB: Temporarily removed ability to load templates for specified resource ID -- not being used at Oxford.
 * (Uncomment code in get() method to re-instate.))</i>
 */
public class Template
{
    private static Logger log = Logger.getLogger(Template.class);
    private File       file;

    private String    facility;
    private Integer    style;
    private String     name;
    private String	  url;
    private PrimaryKey resource_id;

    private XmlTemplate xml_template;
    private String class_name;

    private GifTemplate gif_template;

    private boolean redirected=false;
    private long file_timestamp;
    private long template_timestamp;

    private static String class_path, template_dir, class_dir;
    private static Hashtable by_resource_id=new Hashtable();
    private static String[] group_names_in_order;
    private static Hashtable groups;
    private static Hashtable by_group=new Hashtable();
    private static Hashtable by_style=new Hashtable();
    private static Integer minus_one = new Integer( -1 );

    public static Template get( String f, Integer style, PrimaryKey r, String n )
    {
      String group_name;
      group_name = getSpecifiedGroupName();

//  NB: Temporarily removed ability to load templates for specified resource ID -- not being used at Oxford:
//      return recursiveGet(f, style, group_name, r, n);
      return recursiveGet(f, style, group_name, null, n);
    }

    public static Template recursiveGet( String f, Integer style, String group_name, PrimaryKey r, String n )
    {
      Template template;

      if ( r!=null )
      {
	template = privateGet( r, n );
	if ( template!=null )
	  return template;
	// if a specific resource was specified and not found try
	// checking with null resource
	// (this one is done recursively to apply other rules

	return recursiveGet( f, style, group_name, null, n );
      }

      if ( style!=null )
      {
	if ( f!=null )
	{
	  template = privateGet( f, style, n );
	  if ( template!=null )
	    return template;
	}

	template = privateGet( "default", style, n );
	if ( template!=null )
	  return template;

	// didn't find file within style so check default style
	return recursiveGet( f, null, group_name, null, n );
      }

      if ( group_name!=null )
      {
	// check if this user belongs to one of the user groups that have specific templates defined:
	if ( f!=null )
	{
	  template = privateGet( f, group_name, n );
	  if ( template!=null )
	    return template;
	}

	template = privateGet( "default", group_name, n );
	if ( template!=null )
	  return template;

	// didn't find file within group dir, so set group_name to null:
	return recursiveGet( f, null, null, null, n );
      }

      // no resource, style or group specified so check default style

      if ( f!=null )
      {
	template = privateGet( f, n );
	if ( template!=null )
	  return template;
      }

      template = privateGet( "default", n );

      return template;
    }

	private static Template privateGet( String f, Integer style, String n )
	{
	  Hashtable templates;
	  Template t;

	  templates = getStyleSpecificTemplates( style, f );

	  t = (Template)templates.get( n );
	  //always create a template object even if there isn't a file
	  if ( t==null )
	  {
	    t = createNewTemplate( f, style, null, null, n );
	    templates.put( n, t );
	  }

	  if ( t.exists() )
	    return t;

	  //forget it there isn't a template file.
	  return null;
	}

	private static Template privateGet( String f, String n )
	{
	  Hashtable templates;
	  Template t;

	  templates = getStyleSpecificTemplates( minus_one, f );

	  t = (Template)templates.get( n );
	  //always create a template object even if there isn't a file
	  if ( t==null )
	  {
	    t = createNewTemplate( f, minus_one, null, null, n );
	    templates.put( n, t );
	  }

	  if ( t.exists() )
	    return t;

	  //forget it there isn't a template file.
	  return null;
	}

	private static Template privateGet( String f, String group_name, String n )
	{
	  Hashtable templates;
	  Template t;

	  templates = getGroupSpecificTemplates( f, group_name );

	  t = (Template)templates.get( n );
	  //always create a template object even if there isn't a file
	  if ( t==null )
	  {
	    t = createNewTemplate( f, minus_one, group_name, null, n );
	    templates.put( n, t );
	  }

	  if ( t.exists() )
	    return t;

	  //forget it there isn't a template file.
	  return null;
	}

	private static Template privateGet( PrimaryKey r, String n )
	{
	  Hashtable templates;
	  Template t;

	  templates = getResourceSpecificTemplates( r );

	  t = (Template)templates.get( n );
	  //always create a template object even if there isn't a file
	  if ( t==null )
	  {
	    t = createNewTemplate( null, null, null, r, n );
	    templates.put( n, t );
	  }

	  if ( t.exists() )
	    return t;

	  //forget it there isn't a template file.
	  return null;
	}


    private static String getSpecifiedGroupName()
    {
/**
 * See if this user belongs to a group that has specific templates.
 * Groups are searched in the *reverse* order that they appear in the properties file,
 * i.e. from more specific groups to more general groups.
 */
      User user;
      String group_name;
      Group group;

      if ( groups == null )
	loadGroups();

      user = (User)BuildingContext.getContext().getUser();
      /** @todo special groups??? */

      if ( user!=null )
      {
	for ( int i=group_names_in_order.length - 1; i >= 0; i-- )
	{
	  group_name = group_names_in_order[i];
	  group = (Group)groups.get( group_name );
	  if ( user.isMember( group ) )
	    return group_name;
	}
      }

      return null;
    }


    private static Hashtable getResourceSpecificTemplates( PrimaryKey r )
    {
      Hashtable templates;

      templates = (Hashtable)by_resource_id.get( r );
      if ( templates == null )
      {
	templates = new Hashtable();
	by_resource_id.put( r, templates );
      }

      return templates;
    }

    private static Hashtable getGroupSpecificTemplates( String f, String group_name )
    {
      /** @todo possible optimization: remove creation of all these Hashtables!! */
      Hashtable by_facility, templates;

      by_facility = (Hashtable)by_group.get( group_name );
      if ( by_facility==null )
      {
	by_facility = new Hashtable();// new Hashtable for every group....
	by_group.put( group_name, by_facility );
      }

      templates = (Hashtable)by_facility.get( f );
      if ( templates == null )
      {
	templates = new Hashtable();// new Hashtable for every facility name, in each group Hashtable
	by_facility.put( f, templates );
      }

      return templates;
    }


    private static Hashtable getStyleSpecificTemplates( Integer style, String f )
    {
      /** @todo possible optimization: remove creation of all these Hashtables!! */
      Hashtable by_facility, templates;

      by_facility = (Hashtable)by_style.get( style );
      if ( by_facility==null )
      {
	by_facility = new Hashtable();// new Hashtable for every style....
	by_style.put( style, by_facility );
      }

      templates = (Hashtable)by_facility.get( f );
      if ( templates == null )
      {
	templates = new Hashtable();// new Hashtable for every facility name, in each style Hashtable
	by_facility.put( f, templates );
      }

      return templates;
    }

    private static Template createNewTemplate( String f, Integer style, String group_name, PrimaryKey r, String n )
    {
      Template t;
      StringBuffer file_name, cname, url;
      File file;

      file_name = new StringBuffer();
      cname = new StringBuffer();
      url = new StringBuffer();
      url.append( "/" );

      if( r!=null )
      {
	createPaths( r, file_name, cname, n );
      }
      else if( group_name != null )
      {
	createPaths( group_name, f, file_name, cname, n );
      }
      else
      {
	createPaths( style, f, file_name, cname, n );
      }

      url.append( file_name.toString() );

//	  Logger.getLogger( "org.bodington" ).finer( file_name.toString() );
//	  Logger.getLogger( "org.bodington" ).finer( url.toString() );
      file = new File(
	  Template.getTemplateDirectory(),
	  file_name.toString()
	  );
      t = new Template( file, url.toString(), cname.toString().replace( '-', '_' ), f, style, r, n );

      return t;
    }


    private static void createPaths( PrimaryKey r, StringBuffer file_name, StringBuffer cname, String n )
    {
      file_name.append( "res_" );
      file_name.append( r.toString() );
      file_name.append( "/" );
      file_name.append( n );

      cname.append( "working." );
      cname.append( "res_" );
      cname.append( r.toString() );
      cname.append( ".template_" );
      cname.append( n.substring( 0, n.indexOf( '.' ) ) );
    }

    private static void createPaths( String group_name, String f, StringBuffer file_name, StringBuffer cname, String n )
    {
      file_name.append( "group_" );
      file_name.append( group_name );
      file_name.append( "/" );
      file_name.append( f );
      file_name.append( "/" );
      file_name.append( n );

      cname.append( "working." );
      cname.append( "group_" );
      cname.append( group_name );
      cname.append( "_" );
      cname.append( f );
      cname.append( ".template_" );
      cname.append( n.substring( 0, n.indexOf( '.' ) ) );
    }

    private static void createPaths( Integer style, String f, StringBuffer file_name, StringBuffer cname, String n )
    {
      file_name.append( "style_" );
      if ( style.equals( minus_one ) )
	file_name.append( "default" );
      else
	file_name.append( style.toString() );
      file_name.append( "/" );

      file_name.append( f );
      file_name.append( "/" );
      file_name.append( n );

      cname.append( "working." );
      cname.append( "style_" );
      if ( style.equals( minus_one ) )
	cname.append( "default" );
      else
	cname.append( style.toString() );
      cname.append( "_" );

      cname.append( f );
      cname.append( ".template_" );
   
      cname.append( n.substring( 0, (n.indexOf( '.' ) == -1)? n.length(): n.indexOf('.')) );
    }


    private static void loadGroups()
    {
/**
 * Groups are loaded into Hashtable to avoid repeated database access.
 * Group names are loaded into the String[] in the order that they appear in the properties file,
 * but will be searched in the reverse order.
 */

/** @todo Update group names from property file on the fly, to avoid restarting Tomcat? */
/** @todo Can end up with values in group_names_in_order, but null Groups in Hashtable.
 * (Doesn't cause problems, but worth fixing?) */

      String loaded, group_name;
      StringTokenizer tokenizer;
      Group group;

      groups = new Hashtable();
      BuildingContext context = BuildingContext.getContext();
      loaded = context.getProperty( "servlet.template.groupnames" );
      if ( loaded == null )
      {
	group_names_in_order = new String[0];
	return;
      }

      tokenizer = new StringTokenizer( loaded, "," );
      group_names_in_order = new String[ tokenizer.countTokens() ];

      for ( int i=0; i<group_names_in_order.length; i++ )
      {
	group_name = tokenizer.nextToken();
	group_names_in_order[i] = group_name;
	try
	{
	  group = Group.findGroupByName(group_name);
	  if ( group == null )
	  {
	    log.info( "Group not found: " + group_name );
	    continue;
	  }
	}
	catch (BuildingServerException ex)
	{
	  log.info( "Group not loaded: " + group_name );
	  continue;
	}
	groups.put( group_name, group );
      }
    }

    public static void setClassPath( String str )
    {
	class_path = str;
    }

    public static String getClassPath()
    {
	if ( class_path == null )
	    class_path = System.getProperty( "java.class.path" );

	return class_path;
    }

    public static void setTemplateDirectory( String str )
    {
	template_dir = str;
    }

    public static String getTemplateDirectory()
    {
	return template_dir;
    }

    public static void setClassDirectory( String str )
    {
	class_dir = str;
    }

    public static String getClassDirectory()
    {
	return class_dir;
    }

    public Template( File f, String url, String cname, String fac, Integer s, PrimaryKey res, String n )
    {
	file=f;
	facility = fac;
	style = s;
	resource_id = res;
	name = n;
	this.class_name = cname;
	this.url = url;

	redirected = !name.toLowerCase().endsWith( ".html" ) && !name.toLowerCase().endsWith( ".gif" );
	file_timestamp = file.lastModified();
        template_timestamp = System.currentTimeMillis();

	if ( !redirected )
          {
            if (name.toLowerCase().endsWith(".gif")) {
              gif_template = new GifTemplate();
            }
            else {
              File class_dir_file = new File(getClassDirectory());
              xml_template = new XmlTemplate(f, class_dir_file, class_name,
                                             getClassPath());
            }
          }
    }

    public boolean exists()
    {
	return (file.exists() && file.isFile() );
    }

    public boolean isRedirected()
    {
	return redirected;
    }

    public String getUrl()
    {
	return url;
    }

    public File getFile()
    {
        return file;
    }

    public long getTemplateTimestamp()
    {
        long current_file_timestamp = file.lastModified();
        if ( current_file_timestamp != file_timestamp )
        {
            template_timestamp = System.currentTimeMillis();
            file_timestamp = current_file_timestamp;
        }
        return template_timestamp;
    }

    public XmlTemplateProcessor getXmlTemplateProcessor()
    {
        if ( gif_template != null )
            return gif_template.getProcessor();
        try
        {
        	if ( xml_template != null )
        	    return xml_template.getProcessor();
        } catch (Exception e)
        {
            log.error( e.getMessage(), e );
            return new XmlTemplateErrorProcessor( e.getMessage() );
        }
        return null;
    }

}
