InvoiceIterator.java
package allaire.samples.invoice.taglib;
import allaire.samples.invoice.InvoiceDataModel;
import allaire.samples.invoice.ParameterUtil;
import java.io.IOException;
import java.util.Iterator;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class InvoiceIterator extends BodyTagSupport {
public static final int SCOPE = PageContext.APPLICATION_SCOPE;
private InvoiceDataModel dataModel;
private String item;
private Iterator iterator;
private String selectedValue;
private String collectionName;
public String getCollectionName() {
return collectionName;
}
public void setCollectionName( String name ) {
collectionName = name;
}
private String parameterName;
public String getParameterName() {
return parameterName;
}
public void setParameterName( String name ) {
parameterName = name;
}
public String getItem() {
return item;
}
public boolean isSelectedItem() {
return selectedValue != null && selectedValue.equals( item );
}
public int doStartTag() throws JspException {
try {
dataModel = (InvoiceDataModel)
pageContext.getAttribute( "invoiceDataModel", SCOPE );
if ( dataModel == null ) {
dataModel = new InvoiceDataModel( pageContext );
pageContext.setAttribute( "invoiceDataModel", dataModel, SCOPE );
}
ServletRequest request = pageContext.getRequest();
ServletResponse response = pageContext.getResponse();
selectedValue = ParameterUtil.getParameterWithEncoding( request, response, parameterName );
iterator = dataModel.getIterator( collectionName );
HttpSession session = pageContext.getSession();
String clientCompany = ParameterUtil.getParameterWithEncoding( request, response, "clientCompany" );
if ( clientCompany == null || iterator == null ) {
session.setAttribute( "displayWeeks", new Boolean( false ) );
} else {
dataModel.setClientCompany( clientCompany );
session.setAttribute( "displayWeeks", new Boolean( true ) );
}
} catch (Exception exc) {
exc.printStackTrace();
throw new JspException( exc.getMessage() );
}
return getNext();
}
public int doAfterBody () throws JspException {
int result = SKIP_BODY;
BodyContent body = getBodyContent();
try {
body.writeOut( getPreviousOut() );
} catch (IOException exc) {
throw new JspTagException( "unexpected IO error" );
}
body.clearBody();
return getNext();
}
private int getNext() {
int result = SKIP_BODY;
if ( iterator != null && iterator.hasNext() ) {
result = EVAL_BODY_TAG;
item = (String) iterator.next();
}
return result;
}
}