package com.inmobi.hive.test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.text.StrSubstitutor;

import com.google.common.collect.Lists;

/*
 * This class is used to model a Hive QL script.  It will allow for parameters
 * to be substituted using the Hive convention of ${VAR_NAME}, where you would 
 * ${VAR_NAME} in your script, and then create a hashmap with VAR_NAME as the 
 * key and the substitution value as the data.  
 * 
 * In addition, you may also exclude entire lines in your script by placing 
 * the entire line in the excludes list.  
 */
public class HiveScript {

    private String scriptFile;
    private Map<String, String> params;
    private List<String> excludes;
    private StrSubstitutor substitutor;

    public HiveScript(String scriptFile, Map<String, String> params, List<String> excludes) {
        this.scriptFile = scriptFile;
        this.params = params;
        this.excludes = excludes;
        if (this.params != null) {
            substitutor = new StrSubstitutor(this.params);
        }
    }

    public List<String> getStatements() {
        List<String> commands = Lists.newArrayList();
        BufferedReader in = null;
        try {
          in = new BufferedReader(new FileReader(scriptFile));
          String line;
          StringBuilder command = new StringBuilder();
          while ((line = in.readLine()) != null) {
            if (!line.isEmpty() && !line.startsWith("--")) {
              if (excludeLine(line)) {
                  continue;
              }
              if (line.endsWith(";")) {
                command.append(replaceParams(line.replace(";", "")));
                commands.add(command.toString());
                command = new StringBuilder();
              }
              else {
                  command.append(replaceParams(line));
                  //need to make sure there is a space between lines
                  command.append(" ");
              }
            }
          }
        } catch (FileNotFoundException e) {
          throw new RuntimeException(e);
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return commands;
      }

    private boolean excludeLine(String line) {
        if (excludes == null) {
            return false;
        }
        for (String excludeLine : excludes) {
            if (line.equals(excludeLine)) {
                return true;
            }
        }
        return false;
    }

    private String replaceParams(String line) {
       if (substitutor == null) {
           return line;
       }
       return substitutor.replace(line);
    }

}

@@ -59,46 +59,8 @@
 er;%0A
-import java.io.FileNotFoundException;%0A
 impo
@@ -1304,16 +1304,18 @@
         
+  
 in = new
@@ -1355,24 +1355,26 @@
 riptFile));%0A
+  
           St
@@ -1390,24 +1390,26 @@
 ;%0A          
+  
 StringBuilde
@@ -1451,16 +1451,18 @@
         
+ 
+ 
 while ((
@@ -1510,75 +1510,39 @@
     
-if (!line.isEmpty() && !line.startsWith(%22--%22)) %7B%0A              if (
+    if (skippableLine(line) %7C%7C 
 excl
@@ -1550,32 +1550,34 @@
 deLine(line)) %7B%0A
+  
                 
@@ -1594,34 +1594,38 @@
 ;%0A              
-%7D%0A
+  %7D%0A  
               if
@@ -1656,32 +1656,36 @@
                 
+   
+ 
 command.append(r
@@ -1734,24 +1734,28 @@
             
+    
 commands.add
@@ -1788,24 +1788,28 @@
             
+ 
+   
 command = ne
@@ -1833,32 +1833,34 @@
 ;%0A              
+  
 %7D%0A              
@@ -1863,15 +1863,19 @@
     
+ 
+ 
 else %7B%0A
+  
     
@@ -1943,16 +1943,18 @@
         
+  
 //need t
@@ -2006,32 +2006,34 @@
                 
+ 
+ 
 command.append(%22
@@ -2037,36 +2037,24 @@
 d(%22 %22);%0A    
-          %7D%0A
             
@@ -2069,94 +2069,11 @@
     
-%7D%0A        %7D catch (FileNotFoundException e) %7B%0A          throw new RuntimeException(e);
+  %7D
 %0A   
@@ -2078,34 +2078,32 @@
        %7D catch (
-IO
 Exception e) %7B%0A 
@@ -2093,32 +2093,34 @@
 (Exception e) %7B%0A
+  
           throw 
@@ -2420,18 +2420,16 @@
 ds;%0A    
-  
 %7D%0A%0A    p
@@ -2702,32 +2702,200 @@
 n false;%0A    %7D%0A%0A
+    private boolean skippableLine(String line) %7B%0A        if (line.isEmpty() %7C%7C line.startsWith(%22--%22)) %7B%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A
     private Stri
@@ -2922,24 +2922,25 @@
 ing line) %7B%0A
+ 
        if (s
@@ -2953,32 +2953,33 @@
 utor == null) %7B%0A
+ 
            retur
@@ -2989,26 +2989,28 @@
 ine;%0A       
+ 
 %7D%0A
+ 
        retur

clean up