InvoiceProperty.java

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

package allaire.samples.invoice.taglib;

import allaire.samples.invoice.InvoiceDataModel;
import allaire.samples.invoice.ParameterUtil;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * The <inv:getProperty> tag accesses a named property.  The
 * <i>name</i> attribute specifies a property.
 */

public class InvoiceProperty extends TagSupport {

    // Name property.
    private String name;

    /**
     * Name property accessor.
     */
    public String getName() {
	return name;
    }

    /**
     * Name property mutator.
     */
    public void setName( String name ) {
	this.name = name;
    }

    /**
     * Validate and map a name to obtain a property value.
     */
    public int doStartTag() throws JspException {

	int SCOPE = PageContext.APPLICATION_SCOPE;

	// Ignore any body content.
	int result = SKIP_BODY;

	try {
	    // The name is valid.  Initialize and save the data model.
	    // after making sure that it has been loaded.
	    InvoiceDataModel dataModel = (InvoiceDataModel)
		pageContext.getAttribute( "invoiceDataModel", SCOPE );
	    if ( dataModel == null ) {

		// This is impossible.  A data model had to exist to
		// get this far.
		String message = "Internal error: no data model.";
		throw new JspException( message );
	    }

	    // Output the value.
	    String value;
	    ServletRequest request;
            ServletResponse response;

	    if ( name.equals( "date" ) ) {
		value = getDate();
	    } else if ( name.equals( "clientCompany" ) ) {
		request = pageContext.getRequest();
		response = pageContext.getResponse();
		value = ParameterUtil.getParameterWithEncoding(request, response, "clientCompany" );
	    } else if ( name.equals( "weekEndingDate" ) ) {
		request = pageContext.getRequest();
		response = pageContext.getResponse();                
		value = ParameterUtil.getParameterWithEncoding(request, response, "weekEndingDate" );
	    } else if ( name.equals( "totalHours" ) ) {
		value = getTotalHours( dataModel.getHours() );
	    } else if ( name.equals( "balanceDue" ) ) {
		value = getBalanceDue( dataModel.getHours(),
				       dataModel.getProperty( "developerRate" ) );
	    } else {
		// Setup the data model with the client company and
		// week ending date.  Then fetch the given property.
		request = pageContext.getRequest();
		response = pageContext.getResponse();                
		String clientCompany = ParameterUtil.getParameterWithEncoding(request, response, "clientCompany" );
		String weekEndingDate = ParameterUtil.getParameterWithEncoding(request, response, "weekEndingDate" );
		dataModel.initialize( clientCompany, weekEndingDate );
		value = dataModel.getProperty( name );
	    }
	    pageContext.getOut().print( value );

	} catch (Exception exc) {
	    exc.printStackTrace();
	    throw new JspException( exc.getMessage() );
	}


	return result;
    }


    // Private methoods

    // Return the total number of hours worked.
    private String getTotalHours( String[] hours )
	throws JspException {

	String result = null;

	try {
	    // Loop through all seven days.
	    double sum = 0.0;
	    for ( int i = 0; i < 7; i++ ) {
		sum += Double.parseDouble( hours[i] );
	    }
	    result = String.valueOf( sum );
	} catch (Exception exc) {
	    exc.printStackTrace();
	    throw new JspException( exc.getMessage() );
	}

	return result;
    }

    // Return the balance due.
    private String getBalanceDue( String[] hours, String rate )
	throws JspException {

	String result = null;

	try {
	    double multiplier = Double.parseDouble( rate );
	    double sum = Double.parseDouble( getTotalHours( hours ) );
	    result = String.valueOf( multiplier * sum );
	} catch ( Exception exc ) {
	    exc.printStackTrace();
	    throw new JspException( exc.getMessage() );
	}

	return result;
    }

    // Get today's date.
    public String getDate() {

	String result;

	// Initialize the invoice date with today's date.
	String dateFormat = "MMM d, yyyy";
	SimpleDateFormat formatter = new SimpleDateFormat( dateFormat );
	result = formatter.format( new Date() );

	return result;
    }

}