InvoiceDataModel.java
package allaire.samples.invoice;
import allaire.samples.invoice.InvoiceException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import javax.servlet.jsp.PageContext;
import javax.servlet.ServletContext;
public class InvoiceDataModel implements Serializable {
private static final String COMPANIES_FILE_NAME = "companies.txt";
private static final String INVOICE_DATA = "/data/invoice/";
private static final String CLIENT_ADDRESS_EXT = ".address";
private static final String CLIENT_PO_EXT = ".po";
private static final String DEVELOPER_NAME_EXT = ".developer";
private static final String DEVELOPER_RATE_EXT = ".rate";
private static final String WEEKS_EXT = ".weeks";
private String clientCompany;
private Map collectionNameToObjectMap;
private Map companyNameToIDMap;
private Map companyIDToWeekEndingMap;
private PageContext pageContext;
private Map propertyNameToExtensionMap;
private String weekEnding;
public InvoiceDataModel( PageContext pageContext )
throws InvoiceException {
this.pageContext = pageContext;
createMaps();
}
public InvoiceDataModel() throws InvoiceException {
createMaps();
}
public String[] getCompanies() throws InvoiceException {
String[] result;
if ( companyNameToIDMap == null ) {
loadCompanies();
}
int index = 0;
Set companies = companyNameToIDMap.keySet();
result = new String[companies.size()];
Iterator iterator = companies.iterator();
while ( iterator.hasNext() ) {
result[index++] = (String) iterator.next();
}
return result;
}
public String[] getHours() throws InvoiceException {
String[] result = new String[7];
try {
String id = (String) companyNameToIDMap.get( clientCompany );
String path = INVOICE_DATA + id + ".hours" + "/" + weekEnding;
BufferedReader reader = getReader( path );
StringTokenizer tokenizer =
new StringTokenizer( reader.readLine(), "," );
int index = 0;
while ( tokenizer.hasMoreTokens() ) {
result[index++] = tokenizer.nextToken();
}
} catch (Exception exc) {
throw new InvoiceException( exc.getMessage() );
}
return result;
}
public Iterator getIterator( String name ) throws InvoiceException {
Iterator result = null;
if ( !((HashMap)collectionNameToObjectMap).containsKey( name ) ) {
throw new InvoiceException( "Invalid collection name" );
}
Object[] array = (Object[]) collectionNameToObjectMap.get( name );
if ( array == null ) {
if ( name.equals( "clientCompanies" ) ) {
collectionNameToObjectMap.put( name, getCompanies() );
} else if ( name.equals( "weeks" ) ) {
collectionNameToObjectMap.put( name, getWeeks() );
}
array = (Object[]) collectionNameToObjectMap.get( name );
}
List list = Arrays.asList( array );
result = list.iterator();
return result;
}
public String getProperty( String name ) throws InvoiceException {
String result = null;
if ( name.equals( "clientCompany" ) ) {
result = clientCompany;
} else if ( name.equals( "weekEnding" ) ) {
result = weekEnding;
} else {
try {
String extension =
(String) propertyNameToExtensionMap.get( name );
String id =
(String) companyNameToIDMap.get( clientCompany );
String path = INVOICE_DATA + id + extension;
BufferedReader reader = getReader( path );
result = reader.readLine();
reader.close();
} catch (Exception exc) {
exc.printStackTrace();
throw new InvoiceException( exc.getMessage() );
}
}
return result;
}
public String[] getWeeks() {
String[] result;
String id = (String) companyNameToIDMap.get( clientCompany );
ArrayList weeks = (ArrayList) companyIDToWeekEndingMap.get( id );
if ( weeks == null ) {
result = new String[1];
result[0] = "No data collected";
} else {
int N = weeks.size();
result = new String[ N ];
for ( int i = 0; i < N; i++ ) {
result[i] = (String) weeks.get(i);
}
}
return result;
}
public void initialize( String company, String date ) {
clientCompany = company;
weekEnding = date;
}
public String getSelected( String item ) {
String result = "";
if ( item.equals( clientCompany ) ||
item.equals( weekEnding ) ) {
result = "selected";
}
return result;
}
public void setClientCompany( String company ) {
clientCompany = company;
String id = (String) companyNameToIDMap.get( company );
ArrayList weeks = (ArrayList) companyIDToWeekEndingMap.get( id );
if ( weeks == null ) {
String index;
String key;
String week;
weeks = new ArrayList();
index = INVOICE_DATA + id + WEEKS_EXT;
try {
BufferedReader reader = getReader( index );
while ( (week = reader.readLine()) != null ) {
week = week.trim();
if ( ! ("".equals( week ) || week.startsWith( "#" ) ) ) {
weeks.add( week );
}
}
} catch ( Exception exc ) {
exc.printStackTrace();
weeks.clear();
weeks.add( "No data collected." );
}
companyIDToWeekEndingMap.put( id, weeks );
}
}
public void setPageContext( PageContext pageContext ) {
this.pageContext = pageContext;
}
public void setWeekEnding( String date ) {
weekEnding = date;
}
private void createMaps() {
collectionNameToObjectMap = new HashMap();
collectionNameToObjectMap.put( "clientCompanies", null );
collectionNameToObjectMap.put( "weeks", null );
propertyNameToExtensionMap = new HashMap();
propertyNameToExtensionMap.put( "clientAddress", CLIENT_ADDRESS_EXT );
propertyNameToExtensionMap.put( "clientPO", CLIENT_PO_EXT );
propertyNameToExtensionMap.put( "developerName", DEVELOPER_NAME_EXT );
propertyNameToExtensionMap.put( "developerRate", DEVELOPER_RATE_EXT );
}
private BufferedReader getReader( String path ) throws IOException {
ServletContext context = pageContext.getServletContext();
InputStream stream = context.getResourceAsStream( path );
InputStreamReader reader = new InputStreamReader( stream );
return new BufferedReader( reader );
}
private void loadCompanies() throws InvoiceException {
try {
String name;
String id;
String path = INVOICE_DATA + COMPANIES_FILE_NAME;
BufferedReader reader = getReader( path );
String company = reader.readLine();
StringTokenizer tokenizer;
companyNameToIDMap = new HashMap();
companyIDToWeekEndingMap = new HashMap();
while ( company != null ) {
tokenizer = new StringTokenizer( company, ":" );
id = tokenizer.nextToken();
name = tokenizer.nextToken();
companyNameToIDMap.put( name, id );
companyIDToWeekEndingMap.put( id, null );
company = reader.readLine();
}
reader.close();
} catch (FileNotFoundException fnfExc) {
fnfExc.printStackTrace();
throw new InvoiceException( fnfExc.getMessage() );
} catch (IOException ioExc) {
ioExc.printStackTrace();
throw new InvoiceException( ioExc.getMessage() );
} catch (Exception exc) {
exc.printStackTrace();
throw new InvoiceException( exc.getMessage() );
}
Object[] clientCompanies = companyNameToIDMap.keySet().toArray();
setClientCompany( (String) clientCompanies[0] );
}
}