/*
 * This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation.

 *  There is no copyright.

 *  MockwareTeam (C) 2009

 */

package core;

import java.sql.*;



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


/**
 *
 * @author yetkin
 */
public class FactParser extends util {

    public FactParser(){}

    public static void InsertFact() {

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

		String nodeValue_tablenamef = "";//database tablosunun ismini alıyo
        String insertQueryf = "INSERT INTO " ;
        String appendQueryf = "VALUES (" ;

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

        NodeList objefs = docEle.getChildNodes();
        Node obfNode;
        NamedNodeMap obfcolumns;
        for(int i=1;i<objefs.getLength();i += 2)
         {
            obfNode = objefs.item(i);
            obfcolumns = obfNode.getAttributes();
            String str = "";
            str = obfcolumns.item(0).getNodeValue();
            insertQueryf += str + ", ";
            appendQueryf += "'" + obfNode.getTextContent() + "'" + ", ";
        }

        String makeQueryf;
        makeQueryf = insertQueryf.substring(0, (insertQueryf.length()-2));
        makeQueryf += ")" ;
        String takeQueryf;
        takeQueryf = appendQueryf.substring(0, (appendQueryf.length()-2));
        takeQueryf += ")" ;
        makeQueryf += " " + takeQueryf ;
        System.out.println(makeQueryf) ;
       /*
        NodeList objes = docEle.getChildNodes();
        Node objeNode;
        objeNode = objes.item(1);
        NamedNodeMap columnf = objeNode.getAttributes();
        String columnNamef = "";
        columnNamef = columnf.item(0).getNodeValue();
        insertQueryf += columnNamef + ")";
        appendQueryf += "'" + objeNode.getTextContent() + "'" + ")";

        String makeQueryf;
        makeQueryf = insertQueryf + " " + appendQueryf ;
        System.out.println(makeQueryf) ;*/

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

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

        stmt.executeUpdate(makeQueryf);


    } catch (SQLException e) {
    }
        
    }


}
