package de.uni_potsdam.hpi.bpt.bp2014.jcomparser.xml;

import de.uni_potsdam.hpi.bpt.bp2014.jcomparser.Connector;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.util.ArrayList;
import java.util.List;

public class Fragment implements IDeserialisable, IPersistable {

    private int scenarioID;
    private String fragmentName;
    private Node fragmentXML;
    private int fragmentID;
    private List<ControlNode> controlNodes;
    private List<Edge> edges;
    private int databaseID;

    @Override

    public void initializeInstanceFromXML(Node element) {

        this.fragmentXML = element;
        setFragmentName();
        generateControlNodes();
        generateEdges();

    }

    private void generateEdges() {

        try {
            //get all edges from fragmentXML
            XPath xPath =  XPathFactory.newInstance().newXPath();
            String xPathQuery = "/model/edges/edge";
            NodeList edgeNodes = (NodeList) xPath.compile(xPathQuery).evaluate(this.fragmentXML, XPathConstants.NODESET);
            this.edges = new ArrayList<Edge>(edgeNodes.getLength());

            for(int i = 0; i < edgeNodes.getLength(); i++) {
                Edge currentEdge = new Edge();
                currentEdge.initializeInstanceFromXML(edgeNodes.item(i));
                this.edges.add(currentEdge);
            }
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }

    private void generateControlNodes() {

        try {
            //get all nodes from fragmentXML
            XPath xPath =  XPathFactory.newInstance().newXPath();
            String xPathQuery = "/model/nodes/node";
            NodeList nodes = (NodeList) xPath.compile(xPathQuery).evaluate(this.fragmentXML, XPathConstants.NODESET);
            this.controlNodes = new ArrayList<ControlNode>(nodes.getLength());

            for(int i = 0; i < nodes.getLength(); i++) {
                ControlNode currentControlNode = new ControlNode();
                currentControlNode.initializeInstanceFromXML(nodes.item(i));
                this.controlNodes.add(currentControlNode);
            }
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }

    private void setFragmentName() {

        XPath xPath =  XPathFactory.newInstance().newXPath();
        String xPathQuery = "/model/@name";
        try {
            this.fragmentName = xPath.compile(xPathQuery).evaluate(this.fragmentXML);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }

    public void setScenarioID (int id) {
        this.scenarioID = id;
    }

    public int writeToDatabase() {
        Connector conn = new Connector();
        this.databaseID = conn.insertFragmentIntoDatabase(this.fragmentName, this.scenarioID);
        return databaseID;
    }

    public List<ControlNode> getControlNodes () {
        return this.controlNodes;
    }

    public List<Edge> getEdges () {
        return this.edges;
    }

    public int getDatabaseID() {
        return databaseID;
    }

    public void setDatabaseID(int databaseID) {
        this.databaseID = databaseID;
    }
}

@@ -362,20 +362,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Apubli
@@ -614,21 +614,29 @@
 private 
-List%3C
+Map%3CInteger, 
 ControlN
@@ -727,17 +727,16 @@
 verride%0A
-%0A
     publ
@@ -2033,26 +2033,33 @@
 s = new 
-ArrayList%3C
+HashMap%3CInteger, 
 ControlN
@@ -2320,20 +2320,48 @@
 olNodes.
-add(
+put(currentControlNode.getId(), 
 currentC
@@ -3075,39 +3075,112 @@
 rioID);%0A        
-return 
+for (ControlNode node : controlNodes.values()) %7B%0A            node.setFragmentId(
 databaseID;%0A    
@@ -3177,99 +3177,82 @@
 seID
+)
 ;%0A    
-%7D%0A%0A
     
-public List%3CControlNode%3E getControlNodes () %7B%0A        return this.controlNodes
+    node.writeToDatabase();%0A        %7D%0A        return databaseID
 ;%0A  
@@ -3484,11 +3484,104 @@
 D;%0A    %7D
+%0A%0A    public Map%3CInteger, ControlNode%3E getControlNodes() %7B%0A        return controlNodes;%0A    %7D
 %0A%7D%0A

Set fragmentID of ControlNode, save ControlNodes in a Map