/* bcwti
 *
 * Copyright (c) 1997-2002 Parametric Technology Corporation (PTC). All Rights Reserved.
 *
 * This software is the confidential and proprietary information of PTC.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement.
 *
 * ecwti
 */


import java.io.*;


/**
 * This program Searches the DDL Log file for any error and report the status.
 * 
 */
public class CreateDDLResultProcessor
{
   private static final String OracleKeyWord1 = "SP2";
   private static final String OracleKeyWord2 = "ERROR";
   
   private static final String SqlServerKeyWord1 = "Cannot open database";
   private static final String SqlServerKeyWord2 = "[DBNETLIB]SQL Server does not exist";
   private static final String SqlServerKeyWord3 = "Login fail";
   private static final String SqlServerKeyWord4 = "Cannot open input file";
   private static final String SqlServerKeyWord5 = "Msg ";
   private static final String SqlServerKeyWord6 = "Level ";
   
      
   String[] oracleKeywords = new String[] {OracleKeyWord1, OracleKeyWord2};
   String[] sqlServerKeywords = new String[] {SqlServerKeyWord1, SqlServerKeyWord2,
   SqlServerKeyWord3, SqlServerKeyWord4, SqlServerKeyWord5, SqlServerKeyWord6};
   
   private static int errorLine=0;
   private static int numberOfErrors=0;
   private static boolean errorFound=false;
   
   //Procedure to scan the log file for errors
      
   private void parseLogFile(String[] keywords, String fileName) throws IOException {
    //System.out.println("fileName    "+fileName);
   	int linesRead=0;
  	
  	try {
  	    BufferedReader br =  new BufferedReader(new FileReader(fileName));                
		while( true ) {
		    String s = br.readLine();		    
			if (s == null) {
				break;
			}else{
			    for (int i=0; i < keywords.length; i++) {
			        if (s.startsWith(keywords[i])) {				
						errorFound=true;
						errorLine=linesRead;
						numberOfErrors++;						
						System.out.println("Line"+"["+errorLine+"] "+s);
					}				
				}
			}
			linesRead++;
		}
	} catch( IOException e ) {
		throw e;
	}		 
  } 
   
   
   private void processOracle(String fileName) throws IOException {
    parseLogFile(oracleKeywords, fileName);
   }
   
   private void processSQLServer(String fileName) throws IOException {
    parseLogFile(sqlServerKeywords, fileName);
   }
   
   public static void main(final String[] args)
   {      
      try
      {
         if(args.length == 0)
         {
            System.err.println("");
            System.err.println("Please provide the datastore, file_name ...");
            System.exit(-1);
         }
         CreateDDLResultProcessor resultProcesor = new CreateDDLResultProcessor();
         
         String datastore = args[0];         
         if (datastore.equals("Oracle")) {
            resultProcesor.processOracle(args[1]);
         } else if (datastore.equals("SQLServer")) {
            resultProcesor.processSQLServer(args[1]);
         }
         
         
         if (errorFound) {
             System.out.println("                         ");
             System.out.println("/////////////////////////");
             System.out.println("DDL Creation Fails...");
             System.out.println("There are errors creating database objects, please check the log file for details.");
             System.out.println("/////////////////////////");
         }else {
             System.out.println("                         ");
             System.out.println("/////////////////////////");
             System.out.println("DDL Creation SUCCESSFUL");
             System.out.println("/////////////////////////");
         }
         
      }
      catch(Throwable t)
      {
         t.printStackTrace();
      }
   }
   
   
}
