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

import CommonFunctions.debugPrinter;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.File;
import java.net.InetAddress;
import structures.Constants;
import database.CoreDatabase;
import structures.SuperNodeObject;

/*class NeighborsSuperNodeRead
 * Parses Supernode xml file and puts the supernodes into the CoreDatabase
 */
public class NeighborsSuperNodeReader {

    private String myComputerName = null;

    public void ExtractFromFile(File xmlFile) {
        try {
            try {
              
                myComputerName = InetAddress.getLocalHost().getHostName();
                debugPrinter.printLine("computer name = " + myComputerName);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(xmlFile);
            ExtractSuperNodes(doc);

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

    private void ExtractSuperNodes(Document doc) {
        try {
            NodeList listCentroidSets = doc.getElementsByTagName(Constants.XML_SUPERNODES);
            Node sections = listCentroidSets.item(0);
            if (sections.getNodeType() == Node.ELEMENT_NODE) {
                Element singleSection = (Element) sections;
                NodeList listCentroidSet = singleSection.getElementsByTagName(Constants.XML_SUPERNODES_SUPERNODE);
                for (int i = 0; i < listCentroidSet.getLength(); i++) {
                    Node subSections = listCentroidSet.item(i);
                    if (subSections.getNodeType() == Node.ELEMENT_NODE) {
                        Element singleSubSection = (Element) subSections;
                        NodeList IDList = singleSubSection.getElementsByTagName(Constants.XML_SUPERNODES_SUPERNODE_ID);
                        String ID = ExtractSuperNodeInfo(IDList);
                        NodeList IPadressList = singleSubSection.getElementsByTagName(Constants.XML_SUPERNODES_SUPERNODE_IP);
                        String IPAddress = ExtractSuperNodeInfo(IPadressList);
                        NodeList RM_TCPPortList = singleSubSection.getElementsByTagName(Constants.XML_SUPERNODES_SUPERNODE_RM_TCP_PORT);
                        String rm_tcp_port = ExtractSuperNodeInfo(RM_TCPPortList);
                        NodeList NODE_TCPPortList = singleSubSection.getElementsByTagName(Constants.XML_SUPERNODES_SUPERNODE_NODE_TCP_PORT);
                        String node_tcp_port = ExtractSuperNodeInfo(NODE_TCPPortList);

                        NodeList SuperNodePortList = singleSubSection.getElementsByTagName(Constants.XML_SUPERNODES_SUPERNODE_TCP_PORT);
                        String superNode_TCP_port = ExtractSuperNodeInfo(SuperNodePortList);
                        SuperNodeObject tmp = new SuperNodeObject();
                        tmp.setID(ID);
                        tmp.setIPAddress(IPAddress);
                        tmp.setRM_Tcp_portNo(rm_tcp_port);
                        tmp.setNode_tcp_portNo(node_tcp_port);
                        tmp.setSuperNode_TCP_portNo(superNode_TCP_port);
                        CoreDatabase.superNodes.add(tmp);

                    }
                }
            }
            debugPrinter.printLine("done");
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private String ExtractSuperNodeInfo(NodeList typeList) {
        if (typeList.getLength() > 0) {
            Element termname = (Element) typeList.item(0);
            NodeList type = termname.getChildNodes();
            return ((Node) type.item(0)).getNodeValue();
        }
        return null;
    }
}
