package org.dainst.redirector;

import java.io.*;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author Daniel de Oliveira
 */
public class App {

    private static String PROPS_PATH="config/config.properties";
    private static String REDIRECTS_PATH="config/redirects.csv";
    private static String JDBC_DRIVER="com.mysql.jdbc.Driver";

    private static Properties props = null;

    public static void main(String [] args) throws FileNotFoundException {

        loadConfiguration(PROPS_PATH);
        Map<String,String> redirects = readRedirects(REDIRECTS_PATH);

        Connection conn = getConnection(get("dbJdbcUrl"),get("username"),get("password"));
        new Controller(new DAO(conn),get("targetUrl"),redirects);
    }



    private static Connection getConnection(String dbJdbcUrl,String username,String password) {
        try
        {
            Class.forName(JDBC_DRIVER).newInstance();
            Connection conn = DriverManager.getConnection(dbJdbcUrl,
                    username, password);
            return conn;
//            conn.close(); // TODO implement proper tear down
        }
        catch (Exception ex) {
            System.err.println("Error: "+ex.getMessage());
            System.exit(1);
            return null; // dead code
        }
    }




    private static Map<String,String> readRedirects(String redirectsPath) {

        Map<String,String> m = new HashMap<>();
        FileReader in;
        try {

            in = new FileReader(REDIRECTS_PATH);
            BufferedReader br = new BufferedReader(in);

            String l;
            while ((l = br.readLine()) != null) {
                m.put(l.split(",")[0],l.split(",")[1]);
            }
            in.close();
            return m;
        } catch (Exception e) {
            System.exit(1);
            return null; // dead code
        }
    }


    private static String get(String propertyName)  {

        String s = props.getProperty(propertyName);
        if (s==null) {
            System.out.println(propertyName+" not defined in "+PROPS_PATH+".");
            System.exit(1);
        }
        return s;
    }


    /**
     * @param propertiesFileRelativePath
     * @return null if an error occured
     */
    private static void loadConfiguration(String propertiesFileRelativePath) {
        props = new Properties();
        try (
                FileInputStream is =new FileInputStream(new File(propertiesFileRelativePath)))
        {
            props.load(is);
        } catch (IOException e) {
            System.out.println("Could not load properties from file: "+propertiesFileRelativePath);
            System.out.println(e.getMessage());
            System.exit(1);
        }
    }
}

@@ -631,34 +631,56 @@
         
-Connection conn = 
+new Controller(%0A                new DAO(
 getConne
@@ -734,26 +734,29 @@
 sword%22))
-;%0A
+)%0A   
         
 new Cont
@@ -751,36 +751,13 @@
     
-new Controller(new DAO(conn)
+     
 ,get
@@ -770,16 +770,33 @@
 etUrl%22),
+%0A                
 redirect
@@ -1214,17 +1214,16 @@
 eption e
-x
 ) %7B%0A    
@@ -1260,17 +1260,16 @@
 ror: %22+e
-x
 .getMess
@@ -1843,32 +1843,90 @@
 (Exception e) %7B%0A
+            System.err.println(%22Error: %22+e.getMessage());%0A
             Syst
@@ -2134,35 +2134,35 @@
          System.
-out
+err
 .println(propert
@@ -2271,105 +2271,8 @@
 %7D%0A%0A%0A
-    /**%0A     * @param propertiesFileRelativePath%0A     * @return null if an error occured%0A     */%0A
     
@@ -2572,35 +2572,35 @@
          System.
-out
+err
 .println(%22Could 
@@ -2684,11 +2684,11 @@
 tem.
-out
+err
 .pri

Clean up a bit.