InvoiceProperty.java
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;
public class InvoiceProperty extends TagSupport {
private String name;
public String getName() {
return name;
}
public void setName( String name ) {
this.name = name;
}
public int doStartTag() throws JspException {
int SCOPE = PageContext.APPLICATION_SCOPE;
int result = SKIP_BODY;
try {
InvoiceDataModel dataModel = (InvoiceDataModel)
pageContext.getAttribute( "invoiceDataModel", SCOPE );
if ( dataModel == null ) {
String message = "Internal error: no data model.";
throw new JspException( message );
}
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 {
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 String getTotalHours( String[] hours )
throws JspException {
String result = null;
try {
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;
}
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;
}
public String getDate() {
String result;
String dateFormat = "MMM d, yyyy";
SimpleDateFormat formatter = new SimpleDateFormat( dateFormat );
result = formatter.format( new Date() );
return result;
}
}