/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlpackage;


import CommonFunctions.debugPrinter;
import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import structures.Constants;
import applications.CarbonControl;
import applications.Trip;
import mdot.Main;
/**
 *
 * @author student
 */
public class CarbonEmissionsReader {

    public static void ExtractFromFile() {
        try {
            File xmlFile = new File(Main.carbon);
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(xmlFile);
            ExtractTripData(doc);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

     private static void ExtractTripData(Document doc) {
        /*
        <trips>
        <trip>
        <date>11/13/2009</date>
        <fuel>32</fuel>
        <miles>640</miles>
        <carbon>2000</carbon>
        </trip>
        </trips>*/

        try {

            //get the root element -- trips
            Element rootElement = doc.getDocumentElement();

            //get the list of all trip element
            NodeList tripList = rootElement.getElementsByTagName(Constants.XML_CARBON_TRIP);

            if (tripList != null && tripList.getLength() > 0) {
                for (int i = 0; i < tripList.getLength(); i++) {

                    //Vector tripRecord = new Vector();
                    Trip tripRecord = new Trip();

                    //get the trip element
                    Node fstTrip = tripList.item(i);

                    //if trip is an element
                    if (fstTrip.getNodeType() == Node.ELEMENT_NODE) {

                        Element tripElmnt = (Element) fstTrip;

                        NodeList dateElementList = tripElmnt.getElementsByTagName(Constants.XML_CARBON_DATE);
                        Element dateElement = (Element) dateElementList.item(0);
                        NodeList dateChildren = dateElement.getChildNodes();
                        debugPrinter.printLine("Date: " + ((Node) dateChildren.item(0)).getNodeValue());
                        //tripRecord.add(((Node) dateChildren.item(0)).getNodeValue());
                        tripRecord.setDate(((Node) dateChildren.item(0)).getNodeValue());

                        NodeList fuelElementList = tripElmnt.getElementsByTagName(Constants.XML_CARBON_FUEL);
                        Element fuelElement = (Element) fuelElementList.item(0);
                        NodeList fuelChildren = fuelElement.getChildNodes();
                        debugPrinter.printLine("Fuel : " + ((Node) fuelChildren.item(0)).getNodeValue());
                        //tripRecord.add(((Node) fuelChildren.item(0)).getNodeValue());
                        tripRecord.setGallonsused(Double.parseDouble(((Node) fuelChildren.item(0)).getNodeValue()));

                        NodeList milesElementList = tripElmnt.getElementsByTagName(Constants.XML_CARBON_MILES);
                        Element milesElement = (Element) milesElementList.item(0);
                        NodeList milesChildren = milesElement.getChildNodes();
                        debugPrinter.printLine("Miles : " + ((Node) milesChildren.item(0)).getNodeValue());
                        //tripRecord.add(((Node) milesChildren.item(0)).getNodeValue());
                        tripRecord.setMilesTraveled(Double.parseDouble(((Node) milesChildren.item(0)).getNodeValue()));

                        NodeList carbonElementList = tripElmnt.getElementsByTagName(Constants.XML_CARBON_CARBON);
                        Element carbonElement = (Element) carbonElementList.item(0);
                        NodeList carbonChildren = carbonElement.getChildNodes();
                        debugPrinter.printLine("Carbon : " + ((Node) carbonChildren.item(0)).getNodeValue());
                        //tripRecord.add(((Node) carbonChildren.item(0)).getNodeValue());
                        tripRecord.setCarbonemission(Double.parseDouble(((Node) carbonChildren.item(0)).getNodeValue()));
                    }
                    CarbonControl.tripsDatabase.add(tripRecord);
                }
            }

            debugPrinter.printLine(String.valueOf(CarbonControl.tripsDatabase.size()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
