package com.altra.translatortag.taglib;

import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/**
 * Title:        Language Translation
 * Description:  This project facilitates language translation.
 * Copyright:    Copyright (c) 2000
 * Company:      Altra Energy Technologies
 * @author Michael A. Piwonka
 * @version 1.0
 */

public class LTTagHandler extends TagSupport
{
    private String itemId;
    private static Hashtable dictionary;

    public int doStartTag()
    {

        try
        {
            JspWriter out = pageContext.getOut();
            out.print(findTranslation(itemId));
        }
        catch (Exception e)
        {
        }
        return (SKIP_BODY);
    }

    public void setId(String itemId)
    {
        try
        {
            this.itemId  = itemId;
        }
        catch (Exception exc)
        {
        }
    }

    private String findTranslation(String id)
    {

        String tab = "\t";

        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        HttpSession session = request.getSession(false);

        dictionary = (Hashtable) pageContext.getAttribute("dictionary", PageContext.APPLICATION_SCOPE);
        //dictionary = getDictionary(pageContext);
        String languageId = (String) session.getAttribute("languageId");
        String termSetId = (String) session.getAttribute("termSetId");

        String translation = null;

        if (dictionary == null)
        {
            try
            {
                System.out.println(Thread.currentThread().getName() + " has found a null dictionary.");
                createDictionary(pageContext);
            }
            catch (Exception e)
            {
                return id;
            }
        }

        //First, look for specific itemId for session languageId and termSetId

        String key = id + tab + languageId + tab + termSetId;

        translation = (String)dictionary.get(key);

        if ((translation == null) && !(termSetId.equals("0")))
        {
            //Next, try the session.languageId with no termSetId (i.e., termSetId = 0)
             key = id + tab + languageId + tab + "0";

            translation = (String)dictionary.get(key);
        }

        if ((translation == null) && !(languageId.equals("1")))
            {
                //Final chance:  try the default language and termSetId
                key = id + tab + "1" + tab + "0";
                translation = (String)dictionary.get(key);
            }

        if (translation == null)
        {
            translation = id;
        }
        return translation;
    }

    private static synchronized void createDictionary(PageContext pc)
    {
        //create the dictionary if another thread hasn't already
        dictionary = (Hashtable) pc.getAttribute("dictionary", PageContext.APPLICATION_SCOPE);

        if (dictionary == null)
        {
            System.out.println(Thread.currentThread().getName() + " is creating a dictionary.");
            Translations t = new Translations();
            dictionary = t.createDictionary();
            pc.setAttribute("dictionary",dictionary, PageContext.APPLICATION_SCOPE);
        }
        else
        {
            System.out.println(Thread.currentThread().getName() + " found a dictionary within createDictionary method.");
        }
    }
}