/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * published by the Free Software Foundation; either version 2.1 of
 * the license, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite
 * 330, Boston, MA  02111-1307, USA.
 * 
 */
package com.k_int.xrtree;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * Title:		XRDbManager
 * @author:		Rob Tice (rob.tice@k-int.com)
 * @version:	$Id: $
 * Copyright:	Copyright 1999-2006 Knowledge Integration Ltd
 * Company: 	Knowledge Integration Ltd
 * Description:
 * 
 * 
 *
 * Created:		Jun 14, 2006
 * 
 * 
 * History
 *				$Log: $
 * 		
 */

public class XRDbManager
{
    @SuppressWarnings("unused")
	private Connection conn;
    
    private PreparedStatement add_data_stmt;
    private PreparedStatement add_xr_stmt;
    private PreparedStatement add_rel_stmt;
    private PreparedStatement get_data_stmt;
    
    /**
     * 
     */
    public XRDbManager(Connection conn) throws SQLException
    {
        this.conn=conn;
        add_xr_stmt     = conn.prepareStatement("INSERT INTO xr_tree  VALUES(?,?.?)");
        add_data_stmt   = conn.prepareStatement("INSERT INTO data  VALUES(?,?,?,?,?)");           
        get_data_stmt   = conn.prepareStatement("SELECT COUNT(*) FROM data WHERE id=? and source=?"); 
        add_rel_stmt    = conn.prepareStatement("INSERT INTO relationships  VALUES(?,?,?)");       
    }
    
    public void addData(DataNode node) throws SQLException
    {
        get_data_stmt.setString(1, node.getId());
        get_data_stmt.setString(2, node.getSource());
        ResultSet result = get_data_stmt.executeQuery();
        
        while(result.next())
        {              
            int count = result.getInt(1);
           // System.out.println(count);
            if(count>0)
            {
                //throw new SQLEXception("Data exists");
                //System.out.println("-------- node "+node.getId()+" "+node.getName()+" exists already");
                return;
            }
        }
      
        try
        {
            add_data_stmt.setString(1,node.getId());
            add_data_stmt.setString(2,node.getName());
            add_data_stmt.setString(3,node.getSource());
            add_data_stmt.setInt(4,node.getType());
            add_data_stmt.setString(5,node.getDescription());
            add_data_stmt.execute();
        }
        catch(SQLException e)
        {
            System.out.println("Exception Adding data "+node.getName()+": "+node.getId());
            throw new SQLException(e.getMessage());
        }
    }
    
    
    public void addXRNode(XRNode node) throws SQLException
    {
    	//System.out.println("Adding xr node "+node.getXRIndex());
        add_xr_stmt.setString(1,node.getXRIndex());
        add_xr_stmt.setString(2,node.getDataId());
        add_xr_stmt.setString(3,node.getDataSource());
        add_xr_stmt.execute();
    }
    
    public void addXRNodeRelationships(XRNodeRelationship node_rel) throws SQLException
    {
        add_rel_stmt.setString(1,node_rel.getSourceId());
        add_rel_stmt.setString(2,node_rel.getDestinationId());
        add_rel_stmt.setInt(3,node_rel.getType());
        add_rel_stmt.execute();
    }
}
