import java.sql.*;
import java.net.*;
import java.io.*;
import java.lang.*;
import java.util.*;

public class actors {
	
	
	public static void main (String[] args) throws Exception
       {
       	
       	
       	
       	String primo,secondo,as;
       	primo = null;
       	secondo = null;
       	as = null;
       	String[]res;
       	int inizio=0;
       	int inizio2=0;
       	String nome = null;
        String cognome = null;
        String year=null;
        String[] arr=null;
        int attoreid=1;
        int movieid=0;
        String nomecognome=null;
          
        FileWriter fi = new FileWriter("G:\\Documents and Settings\\cristiano\\Desktop\\tesina seminario\\tesina\\finalScripts\\actors.sql");
		PrintWriter out=new PrintWriter(fi);
		FileWriter fi3 = new FileWriter("G:\\Documents and Settings\\cristiano\\Desktop\\tesina seminario\\tesina\\finalScripts\\actors2movie.sql");
		PrintWriter out3=new PrintWriter(fi3);
		
		
		FileReader f = new FileReader("G:\\Documents and Settings\\cristiano\\Desktop\\tesina seminario\\tesina\\file IMDB\\actors.txt");
        BufferedReader filebuf = new BufferedReader(f); 
   
      	Connection conn2 = null;
		ResultSet rs3=null;
      
        
           try
           {
               String userName = "root";
               String password = "041524";
               String url = "jdbc:mysql://localhost/imdb";
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               conn2 = DriverManager.getConnection (url, userName, password);
               //System.out.println ("Database connection established");
           }
           catch (Exception e)
           {
           		
           		e.printStackTrace();
               System.err.println ("Cannot connect to database server");
           }
      	
      
           
           
       	
       out.print("INSERT IGNORE INTO actor VALUES\n");
       out3.print("INSERT IGNORE INTO actor2movie VALUES\n");
       	
      	
      	

        String nextStr;
        nextStr = filebuf.readLine();
        
 		  if (args[0].equals("-") && (args[1].startsWith("*"))){
        
 			String num=args[1].substring(1);
 			attoreid=Integer.parseInt(num);
 			       
 		 while (!nextStr.equals("THE ACTORS LIST")) {
 		 	nextStr=filebuf.readLine();
  		}
   
   		nextStr = filebuf.readLine();
        nextStr = filebuf.readLine();
        nextStr = filebuf.readLine();
        nextStr = filebuf.readLine();
        nextStr = filebuf.readLine();
        
   		}
   		
   		if (!args[0].equals("-")){
   			
   			 System.out.println(args[0]);
   			 System.out.println(args[1]);
   			 while (!nextStr.startsWith(args[0])) {
 		 	nextStr=filebuf.readLine();
 		 	}
 		 	attoreid=Integer.parseInt(args[1]);
  		}
  		
  		
   	
   	
   	System.out.println(attoreid);
       		
      
	  while (nextStr!=null){
	  		
	  			try{
	  			
      	
      		  if (nextStr.equals("SUBMITTING UPDATES")){
        			System.out.println("Actors inseriti nel db");
        			break;
        		 }
      	
      			nextStr = nextStr.replace("'","");
      	
      		
      			
		      		
      			if(nextStr.equals("")){
      				
      				attoreid++;
      				
      				nextStr=filebuf.readLine();
      			}
      			
      			
      			
      			if (nextStr.startsWith("\t")){
      				
      				
      				secondo=nextStr;
      				secondo=secondo.replace("'","");
      				secondo=secondo.replace("\t","");
      				
      				if (secondo.contains("(")){
      					
      					int aperta=secondo.indexOf("(");
      					int chiusa=secondo.indexOf(")");
      					
      					year = secondo.substring(aperta+1,chiusa);
      					
      				 }
      				 
      				 else {
      				 	year=null;
      				 }
      					
      				if (secondo.contains("[")){
      					
      					int aperta2=secondo.indexOf("[");
      					int chiusa2=secondo.indexOf("]");
      					
      					as = secondo.substring(aperta2+1,chiusa2);
      					
      				 }
      				 
      				 else {
      				 	as=null;
      				 }
      		}	
      	
      			else {
      				
      				primo=nextStr;
      				primo=primo.replace("'","");
      				int tab=primo.indexOf("\t");
      				
      				nomecognome = primo.substring(0,tab);
      				
      				if (nomecognome.contains(",")){
      					
      					int virgola=nomecognome.indexOf(",");
      					
      					cognome = nomecognome.substring(0,virgola);
      					
      					nome= nomecognome.substring(virgola+2,tab);
      					
      							
      					
      				}
      				
      				else {
      					
      					nome = null;
      					cognome = nomecognome;
      					
      					
      				}
      				
      				
      				
      				out.append("('"+attoreid+"','"+nome+"','"+cognome+"','M'),\n");   	
			
      				
      				secondo=primo.substring(tab);
      				secondo=secondo.replace("'","");
      				secondo=secondo.replace("\t","");
      				
      				if (secondo.contains("(")){
      					
      					int aperta=secondo.indexOf("(");
      					int chiusa=secondo.indexOf(")");
      					
      					year = secondo.substring(aperta+1,chiusa);
      					
      				 }
      				 
      				 else {
      				 	year=null;
      				 }
      					
      				if (secondo.contains("[")){
      					
      					int aperta2=secondo.indexOf("[");
      					int chiusa2=secondo.indexOf("]");
      					
      					as = secondo.substring(aperta2+1,chiusa2);
      					
      				 }
      				 
      				 else {
      				 	as=null;
      				 }
       	}
      	
      	
      	
      	if (secondo.contains("[")){
    		int quadra=secondo.indexOf("[");
    		secondo=secondo.substring(0,quadra);     
   		 }
    
    	if (secondo.contains("(")){
    		int tonda=secondo.indexOf("(");	
    		secondo=secondo.substring(0,tonda);     
   		 }
      	
      	
      
      	/*
      	
 		System.out.println(nome);
 		System.out.println(cognome);
 		System.out.println(secondo);
 		System.out.println(year);
 		System.out.println(as);
 		System.out.println(attoreid);
 		System.out.println(movieid);
 		System.out.println("****************");
 	
    	*/	
    	
    
           
    		try{
    
    
    			
    			
   				
   				Statement s2 = conn2.createStatement ();
  				
  				rs3=s2.executeQuery("SELECT id FROM movies WHERE title='"+secondo+"' and year='"+year+"'");
         				
  			 	rs3.last();
  			 	
  			    movieid =(Integer)rs3.getObject(1);
  			 	
  			 	s2.close ();
   				
   				
           }
           catch(Exception e){
           }
     	   
					
				
				
				out3.append("('"+attoreid+"','"+movieid+"','"+as+"'),\n");   	
		
 		
 		
 			
    
       
   
   
    
     nextStr = filebuf.readLine();// legge una riga del file 
    }
    
    catch(Exception e){
    	e.printStackTrace();
    	break;
    }
        }
           
       
  
           	
       filebuf.close();  // chiude il file    
       out.close();
      
           
           
           
       }
		
		
		
	}
	
	
	
	
	
