// $Id: eecr.java,v 1.4 2002/09/06 07:48:29 mdean Exp $


class eecr
{
    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 java.text.DecimalFormat monthFormat = new java.text.DecimalFormat("00");

    /**
     * translate ...-YYYYDDM where M is A-L for Jan-Dec
     */
    static String getSubmissionDate(String transid)
    {
	// possibly no submission date
	if (transid.length() == 0)
	    return null;

	int dash = transid.indexOf('-');
	String date = transid.substring(dash + 1);
	int month = (int) date.charAt(6) - (int) 'A' + 1;
	
	return date.substring(0, 4) + "-" + monthFormat.format(month) + "-" + date.substring(4, 6);
    }

    static void processFile(java.io.File file)
	throws Exception
    {
	// process only expense reports
	if (! file.toString().endsWith(".EE"))
	    return;

	java.io.BufferedReader stream = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file)));
	String line;
	String transID = null;
	String total;
	String itemNumber;
	String date = null;
	String description = null;
	String amount;
	com.hp.hpl.mesa.rdf.jena.model.Resource form = null;
	while ((line = stream.readLine()) != null)
	    {
		if (line.startsWith("TransID"))
		    transID = getValue(line);
		else if (line.startsWith("Description"))
		    description = getValue(line);
		else if (line.startsWith("Total"))
		    {
			total = round(getValue(line));

			// add form
			form = model.createResource("#" + transID);
			model.add(form,
				  com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
				  eecr_ont.EmployeeExpenseForm);
			model.add(form,
				  eecr_ont.transid,
				  transID);
			model.add(form,
				  eecr_ont.description,
				  description);
			model.add(form,
				  eecr_ont.total,
				  total);
			String submissionDate = getSubmissionDate(transID);
			if (submissionDate != null)
			    model.add(form,
				      eecr_ont.submissionDate,
				      submissionDate);
		    }
		else if (getItem(line, "Date") != null)
		    {
			String value = getValue(line);
			
			date = value.substring(6) + '-' + value.substring(0, 2) + '-' + value.substring(3, 5);
		    }
		else if (getItem(line, "Description") != null)
		    description = getValue(line);
		else if ((itemNumber = getItem(line, "Amount")) != null)
		    {
			amount = round(getValue(line));
			
			// add item
			com.hp.hpl.mesa.rdf.jena.model.Resource item = model.createResource("#" + transID + "-" + itemNumber);
			model.add(item,
				  com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
				  eecr_ont.Item);
			model.add(item,
				  eecr_ont.date,
				  date);
			model.add(item,
				  eecr_ont.description,
				  description);
			model.add(item,
				  eecr_ont.amount,
				  amount);
			/* model.add(form,
				  eecr_ont.item,
				  item); */
		    }
	    }
    }

    static void usage()
    {
	System.err.println("Usage:  eecr-directory");
    }
    
    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);
    }
}
