InvoiceBean.java

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

package allaire.samples.invoice;

import allaire.samples.invoice.InvoiceException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import javax.servlet.jsp.PageContext;

/**
 * The InvoiceBean class encapsulates invoice information.  The
 * information is extracted from files generated by an Emacs time
 * management tool (timecard-mode.el).  The keys used for the
 * extraction process are a client company name and a week ending
 * date.
 *
 * @author Paul Reilly, Pajato Systems Group <pmr@pajato.com>
 * @version 0.1
 */
public class InvoiceBean implements Serializable {

    // Formatting constants
    private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
    
    // Data model constants

    // Miscellaneous constants.
    private static final String[] DAYS =
    {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

    // Container for the hours worked on a given week.
    private String[] hours = new String[7];

    // Properties

    // Other, obvious, properties.
    private String dayName;
    private String invoiceDate;
    private String item;

    // The data model object.
    private InvoiceDataModel dataModel;

    /**
     * Construct a bean to use.
     */
    public InvoiceBean() throws InvoiceException {

	// Create the data model.
	dataModel = new InvoiceDataModel();
    }

    /**
     * Get the total balance due.
     */
    public String getBalanceDue() {

	String result = "";

	try {
	    double rate = Double.parseDouble( getDeveloperRate() );
	    double hours = Double.parseDouble( getTotalHours() );
	    double balance = rate * hours;
	    result = String.valueOf( balance );
	} catch ( Exception exc ) {
	    exc.printStackTrace();
	}

	return result;
    }

    /**
     * Client address accessor.
     */
    public String getClientAddress() throws InvoiceException {
	return dataModel.getProperty( "clientAddress" );
    }

    /**
     * Client company accessor.
     */
    public String getClientCompany() throws InvoiceException {
	return dataModel.getProperty( "clientCompany" );
    }

    /**
     * Client purchase order (PO) accessor.
     */
    public String getClientPO() throws InvoiceException {
	return dataModel.getProperty( "clientPO" );
    }

    /**
     * Returns a list of company names for which data has been
     * collected.
     */
    public String[] getCompanies() throws InvoiceException {
	return dataModel.getCompanies();
    }

    /**
     * Day of week accessor.
     */
    public String[] getDays() {
	return DAYS;
    }

    /**
     * Developer name accessor.
     */
    public String getDeveloperName() throws InvoiceException {
	return dataModel.getProperty( "developerName" );
    }

    /**
     * Developer rate accessor.
     */
    public String getDeveloperRate() throws InvoiceException {
	return dataModel.getProperty( "developerRate" );
    }

    /**
     * Day name accessor.
     */
    public String getDayName() {
	return dayName;
    }

    /**
     * Return the number of hours worked on a particular day.
     */
    public String getDayHours() {

	String result = null;

	// Map the day name to an index.
	int index;
	if ( dayName.equals( "Mon" ) ) {
	    index = 0;
	} else if ( dayName.equals( "Tue" ) ) {
	    index = 1;
	} else if ( dayName.equals( "Wed" ) ) {
	    index = 2;
	} else if ( dayName.equals( "Thu" ) ) {
	    index = 3;
	} else if ( dayName.equals( "Fri" ) ) {
	    index = 4;
	} else if ( dayName.equals( "Sat" ) ) {
	    index = 5;
	} else {
	    index = 6;
	}

	return hours[index];
    }

    /**
     * Invoice date accessor.
     */
    public String getInvoiceDate() {

	// Initialize the invoice date with today's date if it is not
	// already initialized.
	if ( invoiceDate == null ) {
	    String dateFormat = "MMM d, yyyy";
	    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
	    invoiceDate = formatter.format(new Date());
	}
	return invoiceDate;
    }

    /**
     * Item accessor.
     */
    public String getItem() {
	return item;
    }

    /**
     * Selected accessor.  Return the string "selected" if the item
     * property matches either the client company property or the week
     * ending property, otherwise return the empty string.  Context
     * should assure correctness of the result.
     */
    public String getSelected() {
	String result = dataModel.getSelected( item );
	return result;
    }

    /**
     * Access the total number of hours worked.
     */
    public String getTotalHours()
	throws Exception {

	String result = null;

	// Sum the entered hours for the given week.
	double sum = 0.0;
	for (int i = 0; i < 7; i++) {
	    if ( hours[i] != null ) {
		sum += Double.parseDouble( hours[i] );
	    }
	}
	result = String.valueOf( sum );
	return result;
    }

    /**
     * Access the week ending property.
     */
    public String getWeekEnding() throws InvoiceException {
	return dataModel.getProperty( "weekEnding" );
    }

    /**
     * Access a set of weeks.  This is a client specific function.
     */
    public String[] getWeeks() {
	return dataModel.getWeeks();
    }

    /**
     * Client company mutator.  Set the company and establish the set
     * of weeks for that company.
     */
    public void setClientCompany( String company ) {
	dataModel.setClientCompany( company );
    }

    /**
     * Day name mutator.
     */
    public void setDayName( String name ) {
	dayName = name;
    }

    /**
     * Item mutator.
     */
    public void setItem( String item ) {
	this.item = item;
    }

    /**
     * Provide a mutator for the page context.
     */
    public void setPageContext( PageContext pageContext ) {
	dataModel.setPageContext( pageContext );
    }

    /**
     * Week ending mutator.
     */
    public void setWeekEnding( String date ) throws InvoiceException {

	// Load the number of hours worked on the selected week.
	dataModel.setWeekEnding( date );
	hours = dataModel.getHours();
    }

}