/**  
 *  Copyright 2013 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 *
 */
package xcmailrstarter;

import java.io.FileReader;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.h2.tools.RunScript;
import org.mortbay.log.Log;

/**
 * @author Patrick Thum, Xceptance Software Technologies GmbH, Germany
 */
public class ScriptRunner
{

    public ScriptRunner(StarterConf config)
    {
        // Handle the connection
        Connection conn;
        try
        {
            Class.forName(config.XCM_DB_DRIVER);
            Log.info("Driver Loaded.");

            conn = DriverManager.getConnection(config.XCM_DB_URL, config.XCM_DB_USER, config.XCM_DB_PASS);
            Log.info("Got Connection.");

            boolean usrTable = false;
            boolean mbxTable = false;
            boolean mtxTable = false;
            // if xcmstarter has been started with the parameter "-Dxcmailr.xcmstart.droptables=true"
            // drop all tables
            if (!(System.getProperty("xcmailr.xcmstart.droptables") == null))
            {
                RunScript.execute(conn, new FileReader("conf/default-drop.sql"));
                Log.info("Executed Drop Table.");
            }
            DatabaseMetaData md = conn.getMetaData();
            ResultSet rs;
            rs = md.getTables(null, null, "%", null);
            // check if the tables exist
            while (rs.next())
            {
                if (rs.getString(3).equals("USERS"))
                {
                    usrTable = true;
                }
                if (rs.getString(3).equals("MAILBOXES"))
                {
                    mbxTable = true;
                }
                if (rs.getString(3).equals("MAILTRANSACTIONS"))
                {
                    mtxTable = true;
                }
            }

            // create the tables if they're not existing
            if (!(usrTable && mbxTable && mtxTable))
            {
                RunScript.execute(conn, new FileReader("conf/default-create.sql"));
                Log.info("Executed Create Table.");
            }

            rs = md.getColumns(null, "PUBLIC", "USERS", null);

            String columns = "";
            while (rs.next())
            {
                columns += rs.getString("COLUMN_NAME");
                columns += "; ";
            }
            // alter the user-table if it has no language-attribute
            if (!columns.contains("LANGUAGE"))
            {
                Statement stmnt = conn.createStatement();
                stmnt.execute("ALTER TABLE USERS ADD LANGUAGE VARCHAR");
                Log.info("Altered table users, added language");
            }

            conn.close();

        }
        catch (Exception e)
        {
            System.err.println("Got an exception! Connection error or Script execution failed.");
            e.printStackTrace();
            System.exit(0);
        }

    }
}

@@ -797,16 +797,46 @@
 ultSet;%0A
+import java.sql.SQLException;%0A
 import j
@@ -1926,24 +1926,25 @@
 MetaData();%0A
+%0A
             
@@ -1957,24 +1957,8 @@
 Set 
-rs;%0A            
 rs =
@@ -2754,33 +2754,32 @@
 ;%0A            %7D%0A
-%0A
             rs =
@@ -2773,16 +2773,563 @@
         
+ alterTable(%22USERS%22, %22LANGUAGE%22, conn, md);%0A            alterTable(%22MAILTRANSACTIONS%22, %22RELAYADDR%22, conn, md);%0A%0A            conn.close();%0A%0A        %7D%0A        catch (Exception e)%0A        %7B%0A            System.err.println(%22Got an exception! Connection error or Script execution failed.%22);%0A            e.printStackTrace();%0A            System.exit(0);%0A        %7D%0A%0A    %7D%0A%0A    public void alterTable(String tableName, String columnName, Connection conn, DatabaseMetaData md)%0A        throws SQLException%0A    %7B%0A        // get the user-table%0A        ResultSet
  rs = md
@@ -3356,23 +3356,25 @@
 UBLIC%22, 
-%22USERS%22
+tableName
 , null);
@@ -3371,32 +3371,77 @@
 me, null);%0A%0A    
+    // get all column-names of the usertable%0A
         String c
@@ -3453,36 +3453,32 @@
 s = %22%22;%0A        
-    
 while (rs.next()
@@ -3483,34 +3483,26 @@
 ())%0A        
-  
-  %7B%0A    
+%7B%0A
             
@@ -3545,36 +3545,32 @@
 %22);%0A            
-    
 columns += %22; %22;
@@ -3578,32 +3578,26 @@
         
-    %7D%0A  
+%7D%0A
         
   // alt
@@ -3588,18 +3588,16 @@
         
-  
 // alter
@@ -3648,28 +3648,24 @@
 ute%0A        
-    
 if (!columns
@@ -3674,26 +3674,26 @@
 ontains(
-%22LANGUAGE%22
+columnName
 ))%0A     
@@ -3691,34 +3691,26 @@
 e))%0A        
-    %7B%0A    
+%7B%0A
             
@@ -3763,20 +3763,16 @@
         
-    
 stmnt.ex
@@ -3794,26 +3794,48 @@
 BLE 
-USERS ADD LANGUAGE
+%22 + tableName + %22 ADD %22 + columnName + %22
  VAR
@@ -3838,28 +3838,24 @@
  VARCHAR%22);%0A
-    
             
@@ -3882,279 +3882,54 @@
 ble 
-users, added language%22);%0A            %7D%0A%0A            conn.close();%0A%0A        %7D%0A        catch (Exception e)%0A        %7B%0A            System.err.println(%22Got an exception! Connection error or Script execution failed.%22);%0A            e.printStackTrace();%0A            System.exit(0
+%22 + tableName + %22, added column %22 + columnName
 );%0A 
@@ -3933,21 +3933,20 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%7D%0A

prepare the database by letting xcmailr-jetty-starter alter the database if necessary