/*
 * 
 */

package core;

import java.sql.*;



import org.w3c.dom.NodeList;
import org.w3c.dom.Node ;
import org.w3c.dom.NamedNodeMap ;



/**
 *
 * @author ULAN
 */
public class RuleParser extends util implements RuleParserInt{

    //static Document doc ;
	//static File xmlFile ;

    //static Node docEle ;

    public RuleParser(){}

    public static void InsertRule() {

        doc.normalizeDocument(); //root elementi alıyo
        docEle = doc.getDocumentElement() ;
		NamedNodeMap dbNode = docEle.getAttributes() ;//attribute olan nodelar için named node map kullanıo

		String nodeValue_tablename = "";//database tablosunun ismini alıyo
        String insertQuery = "INSERT INTO " ;
        String appendQuery = "VALUES (" ;

        nodeValue_tablename = dbNode.item(0).getNodeValue();//tablo ismi
        //System.out.println(nodeValue_tablename);
        insertQuery += nodeValue_tablename + " (" ;

        NodeList condNodes = docEle.getChildNodes() ; //con ve then
        Node condNode;
        NodeList logicexist;

        condNode = condNodes.item(1); // cond bölümünde şu an
        //System.out.println(condNode.getNodeName());
        logicexist = condNode.getChildNodes() ;

        Node objectNode;
        objectNode = logicexist.item(1); // logical operator mu object mi
        String objectnodeName = objectNode.getNodeName();

        if (objectnodeName.equals("object"))
        {
            NamedNodeMap columnnode1 = objectNode.getAttributes();
            String columnname1 = "";
            columnname1 = columnnode1.item(0).getNodeValue();
            insertQuery += columnname1 + ", ";
            appendQuery += "'" + objectNode.getTextContent() + "'" + ", " ;
            //System.out.println(columnname1);
            //System.out.println(objectNode.getTextContent());
        }
        else {
            NodeList obNodes = objectNode.getChildNodes();
            Node columnname ;
            NamedNodeMap columnnode ;
            for (int i = 1;i < obNodes.getLength(); i += 2)
            {
                columnname = obNodes.item(i);
                columnnode = columnname.getAttributes();
                String columnname2 = "";
                columnname2 = columnnode.item(0).getNodeValue();
                insertQuery += columnname2 + ", ";
                appendQuery += "'" + columnname.getTextContent() + "'" + ", " ;
                //System.out.println(columnname2);
                //System.out.println(columnname.getTextContent());

            }

        }

        Node thenNode;
        thenNode = condNodes.item(3); // cond bölümünde şu an
        //System.out.println(thenNode.getNodeName());
        NodeList logicexist2;

        logicexist2 = thenNode.getChildNodes() ;
        Node objectNode2;
        objectNode2 = logicexist2.item(1); // logical operator mu object mi
        String objectnodeName2 = objectNode2.getNodeName();

        if (objectnodeName2.equals("object"))
        {
            NamedNodeMap columnnode2 = objectNode2.getAttributes();
            String columnname3 = "";
            columnname3 = columnnode2.item(0).getNodeValue();
            insertQuery += columnname3 + ", ";
            appendQuery += "'" + objectNode2.getTextContent() + "'" + ", " ;
            //System.out.println(columnname3);
            //System.out.println(objectNode2.getTextContent());
        }
        else {
            NodeList obNodes2 = objectNode2.getChildNodes();
            Node columnname4 ;
            NamedNodeMap columnnode2 ;
            for (int i = 1;i < obNodes2.getLength(); i += 2)
            {
                columnname4 = obNodes2.item(i);
                columnnode2 = columnname4.getAttributes();
                String columnname5 = "";
                columnname5 = columnnode2.item(0).getNodeValue();
                insertQuery += columnname5 + ", ";
                appendQuery += "'" + columnname4.getTextContent() + "'" + ", " ;
                //System.out.println(columnname5);
                //System.out.println(columnname4.getTextContent());

            }

        }
        String makeQuery;
        makeQuery = insertQuery.substring(0, (insertQuery.length()-2));
        makeQuery += ")" ;
        String takeQuery;
        takeQuery = appendQuery.substring(0, (appendQuery.length()-2));
        takeQuery += ")" ;
        makeQuery += " " + takeQuery ;
        System.out.println(makeQuery) ;
        //System.out.println(takeQuery) ;

        Connection conn = db.dbConnect("jdbc:mysql://localhost:3306/Mockware", "root", "qropadb") ;

try {
        // Create a result set containing all data from my_table
        Statement stmt = conn.createStatement();

stmt.executeUpdate(makeQuery);

     
    } catch (SQLException e) {
    }
    }

}

