package es.ejie.frmk.presentacion.iu.taglib.html.table;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;

import org.displaytag.exception.TagStructureException;

import es.ejie.frmk.infraestructura.traza.Q70TraceLevel;
import es.ejie.frmk.infraestructura.traza.Q70Traza;
import es.ejie.frmk.presentacion.iu.taglib.base.Q70Constants;

/**
 * @author fgiust
 * @version 1.0
 */
public class Q70TableFooterTag extends BodyTagSupport
{

	/**
	 * Instancia del log de trazas
	 */
	
	private static Q70Traza traza = Q70Traza.getSingleton();

    /**
     * private boo
     */
    private boolean firstIteration = false;

    /**
     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
     */
    public int doEndTag() throws JspException
    {
        if (firstIteration)
        {
            Q70TableTag tableTag = (Q70TableTag) findAncestorWithClass(this, Q70TableTag.class);

            if (tableTag == null)
            {
                throw new TagStructureException(getClass(), "footer", "table");
            }

            // add column header only once
            
            if (traza.isTraceActive()) {
            	traza.trace(Q70Constants.COMPONENTE_CUSTOM_TAG, Q70TraceLevel.DEBUG, "first call to doEndTag, setting footer");
            }

            if (getBodyContent() != null)
            {
                tableTag.setFooter(getBodyContent().getString());

            }

            firstIteration = false;

        }

        return TagSupport.EVAL_PAGE;
    }

    /**
     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
     */
    public int doStartTag() throws JspException
    {
        Q70TableTag tableTag = (Q70TableTag) findAncestorWithClass(this, Q70TableTag.class);

        if (tableTag == null)
        {
            throw new TagStructureException(getClass(), "footer", "table");
        }

        // add column header only once
        if (tableTag.isFirstIteration())
        {
            firstIteration = true;
            // using int to avoid deprecation error in compilation using j2ee 1.3 (EVAL_BODY_TAG)
            return 2;
        }
        else
        {
            firstIteration = false;
            return TagSupport.SKIP_BODY;
        }

    }

}
