DayHours.java

/*
  Copyright 2001, Pajato Systems Group
  Copyright 2001, Allaire Corporation
*/

package allaire.samples.invoice.taglib;

import java.io.IOException;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * An <inv:getDayHours> tag outputs the hours invoiceable for the
 * current day index.  The tag must be located within a
 * <inv:foreachDay> tag.
 */

public class DayHours extends TagSupport {

    /**
     * Output the number of hours worked for the current day index.
     */
    public int doStartTag() throws JspException, JspTagException {

	// Validate that the tag is inside a <inv:foreachDay> tag.
	DayIterator parent = (DayIterator)
	    findAncestorWithClass( this, DayIterator.class );
        if ( parent == null ) {

	    // It is not.  Throw an invoice exception.
	    String message = 
		"The condition tag must be inside a <foreachDay> tag.";
	    throw new JspTagException( message );
	}

	// Output the hours as maintained in the parent tag.
	try {
	    pageContext.getOut().print( parent.getDayHours() );
	} catch ( IOException exc ) {
	    exc.printStackTrace();
	    throw new JspException( exc.getMessage() );
	}

	return SKIP_BODY;
    }

}