// $Id: amex.java,v 1.3 2001/10/06 08:13:47 mdean Exp $


/**
 * convert American Express statement in .csv format to DAML.
 */
class amex
{
    static com.hp.hpl.mesa.rdf.jena.model.Model model = new com.hp.hpl.mesa.rdf.jena.mem.ModelMem();

    static String getValue(String line)
    {
	int equal = line.indexOf('=');
	return line.substring(equal + 1);
    }

    /**
     * return the Item number iff it matches the specified Field, otherwise return null.
     */
    static String getItem(String line, String field)
    {
	if (! line.startsWith("Item"))
	    return null;
	int start = line.indexOf(field + "=");
	if (start == (-1))
	    return null;
	return line.substring(4, start);
    }

    static java.text.DecimalFormat nf = new java.text.DecimalFormat("0.00");

    /**
     * round to even dollars.cents
     */
    static String round(String amount)
    {
	double d = Double.parseDouble(amount);
	return nf.format(d);
    }

    static void processFile(java.io.File file)
	throws Exception
    {
	String filename = file.getName();

	// process only .csv's
	if (! filename.endsWith(".csv"))
	    return;

	String stmtDate = filename.substring(0, filename.indexOf('.'));

	java.io.BufferedReader stream = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file)));
	String line;
	int index = 0;
	while ((line = stream.readLine()) != null)
	    {
		com.hp.hpl.mesa.rdf.jena.model.Resource form = null;
		int endDate = line.indexOf(',');
		String date = line.substring(0, endDate);
		int endId = line.indexOf(',', endDate + 1);
		String id = line.substring(endDate + 1, endId);
		int endAmount = line.indexOf(',', endId + 1);
		String amount = line.substring(endId + 1, endAmount);
		int endDescription = line.indexOf('"', endAmount + 2);
		String description = line.substring(endAmount + 2, endDescription);
		int endMemo = line.indexOf('"', endDescription + 3);
		String memo = line.substring(endDescription + 3, endMemo);

		// add transaction
		com.hp.hpl.mesa.rdf.jena.model.Resource transaction = new com.hp.hpl.mesa.rdf.jena.common.ResourceImpl("#" + stmtDate + "-" + index);
		model.add(transaction,
			  com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
			  amex_ont.Transaction);
		model.add(transaction,
			  amex_ont.date,
			  date.substring(6, 10) + "-" + date.substring(0, 2) + "-" + date.substring(3, 5));
		model.add(transaction,
			  amex_ont.id,
			  id);
		model.add(transaction,
			  amex_ont.amount,
			  amount);
		model.add(transaction,
			  amex_ont.description,
			  description);
		model.add(transaction,
			  amex_ont.memo,
			  memo);
		index++;
	    }
    }

    static void usage()
    {
	System.err.println("Usage:  amex-directory");
	System.exit(1);
    }
    
    public static void main(String args[])
	throws Exception
    {
	// argument parsing
	if (args.length != 1)
	    usage();
	String directory = args[0];

	java.io.File[] files = (new java.io.File(directory)).listFiles();
	for (int i = 0; i < files.length; i++)
	    {
		processFile(files[i]);
	    }

	// write model
	java.io.PrintWriter stream = new java.io.PrintWriter(System.out);
	model.write(stream);
    }
}
