package extractor.ui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.sql.*;
import extractor.SCA_API.*;
import ui.*;

public class InitialUI extends JPanel
{
  	BorderLayout borderLayout1 = new BorderLayout();
  	JPanel jPanel1 = new JPanel();
  	JTree jTree1 = null;
  	JButton jButton1 = new JButton();
  	JButton jButton2 = new JButton();
  	JLabel jLabel1 = new JLabel();
  	JLabel jLabel2 = new JLabel();

  	DBConnection db = new DBConnection();
  	Vector entity = null;
  	Vector session = null;
  	ExtractResultsPanel ejbName = null;
  
  	/**Construct the frame*/
  	public InitialUI(ExtractResultsPanel ejbName) 
  	{
		this.ejbName = ejbName;
    		try 
    		{
      			jbInit();
    		}
    		catch(Exception e) 
    		{
      			e.printStackTrace();
    		}
  	}
  
  
  	/**Component initialization*/
  	private void jbInit() throws Exception  
  	{
    		Vector v = new Vector();
		Vector vv = new Vector();
		Statement stmt = db.createStatement();
		Statement stmtt = db.createStatement();
		System.out.println(stmt);

  		ResultSet rs = stmt.executeQuery("select clss_name from t_classx");
  		ResultSet rss = stmtt.executeQuery("select clss_name from t_classx where clss_prnt = 'HttpServlet'");
  	
		while(rs.next())
		{
			String str = rs.getString("clss_name"); 	
			System.out.println(str);
			v.addElement(str);
  		}
	
		while(rss.next())
		{
			String strr = rss.getString("clss_name");
			System.out.println(strr);
			vv.addElement(strr);
  		}
  	
  		for(int i=0; i<vv.size();i++)
  		{
  			v.remove(vv.get(i));
  		}

		DefaultMutableTreeNode top = new DefaultMutableTreeNode("Target classes"); 
		createNodes(top, v);
			
    		jTree1 = new JTree(top);
		DefaultTreeSelectionModel dTM = new DefaultTreeSelectionModel();
    		dTM.setSelectionMode(dTM.DISCONTIGUOUS_TREE_SELECTION);
		DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
		renderer.setLeafIcon(new ImageIcon("images/class.gif"));
		jTree1.setCellRenderer(renderer);


    		setLayout(borderLayout1);
    		this.addMouseListener(new java.awt.event.MouseAdapter() 
    		{
      			public void mouseClicked(MouseEvent e) 
      			{
        			this_mouseClicked(e);
      			}
    		});
    
    		jPanel1.setLayout(null);
    		jTree1.setBounds(new Rectangle(57, 62, 337, 258));
    		jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() 
    		{
      			public void valueChanged(TreeSelectionEvent e) 
      			{
        			jTree1_valueChanged(e);
      			}
    		});
   
    		jLabel1.setFont(new java.awt.Font("Serif", 1, 20));
    		jLabel1.setToolTipText("");
    		jLabel1.setText("Target Classes : ");
    		jLabel1.setBounds(new Rectangle(57, 26, 234, 31));
    		jLabel2.setFont(new java.awt.Font("Serif", 0, 18));
    		jLabel2.setText("Please select some classes!!");
    		jLabel2.setBounds(new Rectangle(59, 335, 261, 27));
    
    		add(jPanel1, BorderLayout.CENTER);
    
    		jPanel1.add(jTree1, null);
    		jPanel1.add(jLabel1, null);
    		jPanel1.add(jLabel2, null);
	}

	private void createNodes(DefaultMutableTreeNode top, Vector values)	
	{
		DefaultMutableTreeNode classNode = null;
		Object obj = null;
  		for(int i=0; i<values.size();i++)
  		{
  			classNode = new DefaultMutableTreeNode(values.get(i));
  			top.add(classNode);
  		}
	}	
	
  	void jTree1_mouseClicked(TreeSelectionEvent e)
  	{
  		System.out.println("");
  	}
  	
  	void jTree1_valueChanged(TreeSelectionEvent e) 
  	{
     		// System.out.println(jTree1.getLastSelectedPathComponent());
      		String str = jTree1.getLastSelectedPathComponent().toString();
  	}

  	void this_mouseClicked(MouseEvent e) 
  	{

  	}

  	public void execute() 
  	{
  		if ( jTree1.getLastSelectedPathComponent() == null)
  		{
  			return;
  		}
  		
      		String str = jTree1.getLastSelectedPathComponent().toString();
	   	Vector selected = new Vector();
  		entity = new Vector();
  		session = new Vector();

      		
      		TreePath[] tps = jTree1.getSelectionPaths();
      		
      		int i = 0;
      		for(i = 0; i<tps.length; i++)
      		{
        		selected.addElement(tps[i].getLastPathComponent().toString());
        		//System.out.println(objs);
      		}  
      		
		getEntityValues(selected);
  	}
  	
  
  	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// new extractor.ui.expected.Application1(entity, session, ejbName);      
  	public Vector getEntity()
  	{
  		return entity;
  	}
  	
  	public Vector getSession()
  	{
  		return session;
  	}
  	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  
  	
  	private void getEntityValues(Vector selected)
  	{
  		System.out.println(selected);
  		Vector v = new Vector();
  		Vector se = new Vector();
 		Statement stmt = db.createStatement();
		
		try
		{
 			ResultSet rs = stmt.executeQuery("select sql_id from tq_insert");
  	
			while(rs.next())
			{
				String str = rs.getString("sql_id"); 	
				System.out.println("str = "+ str);
				StringTokenizer st = new StringTokenizer(str, ".");
				
				st.nextToken();
				v.addElement(st.nextToken());
			}

 			Statement stmtt = db.createStatement();
  			ResultSet rss = stmtt.executeQuery("select sql_id from tq_delete");
  	
			while(rss.next())
			{
				String strr = rss.getString("sql_id"); 	
				System.out.println("strr = "+ strr);
				StringTokenizer stt = new StringTokenizer(strr, ".");
				stt.nextToken();
				v.addElement(stt.nextToken());
				System.out.println("Hey " + v);
			}

 			Statement stmttt = db.createStatement();
  			ResultSet rsss = stmttt.executeQuery("select sql_id from tq_update");
  	
			while(rsss.next())
			{
				String strrr = rsss.getString("sql_id"); 	
				System.out.println("strrr = "+ strrr);
				StringTokenizer sttt = new StringTokenizer(strrr, ".");
				sttt.nextToken();
				v.addElement(sttt.nextToken());
				System.out.println(v);
			}

 			Statement stmtttt = db.createStatement();
  			ResultSet rssss = stmtttt.executeQuery("select sql_id from tq_select");
  	
			while(rssss.next())
			{
				String strrrr = rssss.getString("sql_id"); 	
				System.out.println("strrr = "+ strrrr);
				StringTokenizer stttt = new StringTokenizer(strrrr, ".");
				stttt.nextToken();
				se.addElement(stttt.nextToken());
				System.out.println("select = " + se);
			}
	
			for(int i=0; i< selected.size(); i++)
			{
				if(v.contains(selected.get(i))) 
				{
					entity.addElement(selected.get(i)); 
					System.out.println(selected.get(i));			
				}
				else if(se.contains(selected.get(i)))
				{
					session.addElement(selected.get(i)); 
					System.out.println(selected.get(i));
				}
			}	
		}
		catch(SQLException e)
		{
			e.getMessage();
		}
	
		System.out.println("entity = " + entity);
		System.out.println("session = " + session);
	
	}
}