package org.dainst.chronontology;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.dainst.chronontology.model.DocumentModel;
import org.dainst.chronontology.model.DocumentModelFactory;
import org.dainst.chronontology.store.ESRestSearchableKeyValueStore;
import org.dainst.chronontology.store.FileSystemKeyValueStore;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import static spark.Spark.port;

import org.dainst.chronontology.connect.JsonRestClient;

/**
 * Main class. Handles application setup.
 *
 * @author Daniel M. de Oliveira
 */
public class App {

    final static Logger logger = Logger.getLogger(App.class);
    private static final String DEFAULT_PROPERTIES_FILE_PATH = "config.properties";

    private static FileSystemKeyValueStore initDS(String datastorePath) {

        if (!(new File(datastorePath).exists())) {
            logger.error("The specified path " + datastorePath + " does not exist.");
            return null;
        }
        return new FileSystemKeyValueStore(datastorePath);
    }

    private static Properties loadProps(String propertiesFilePath) {
        Properties props = new Properties();
        try (
                FileInputStream is =new FileInputStream(new File(propertiesFilePath)))
        {
            props.load(is);
        } catch (IOException e) {
            logger.error(e.getMessage());
            return null;
        }
        return props;
    }

    public static void main(String [] args) {

        Properties props= loadProps(DEFAULT_PROPERTIES_FILE_PATH);
        if (props==null) System.exit(1);

        final FileSystemKeyValueStore store=
                initDS((String)props.get("datastorePath"));
        if (store==null) {
            System.exit(1);
        }

        int serverPort= Integer.parseInt((String)props.get("serverPort"));
        port(serverPort);

        String[] typeNames= ((String)props.get("typeNames")).split(",");
        for (String typeName:typeNames) {
            DocumentModel dm= DocumentModelFactory.create(typeName, "1", new ObjectMapper().createObjectNode());
            if (dm==null) {
                logger.error("No document model found for "+typeName);
                System.exit(1);
            }
        }

        String[] credentials= ((String)props.get("credentials")).split(",");

        JsonRestClient jrc= new JsonRestClient((String)props.get("esUrl"));

        Controller controller= new Controller(
                store,new ESRestSearchableKeyValueStore(jrc,(String)props.get("esIndexName")));

        new Router(
                controller,
                typeNames,
                credentials);
    }
}

@@ -1032,35 +1032,38 @@
             
-return null
+System.exit(1)
 ;%0A        %7D%0A
@@ -1473,19 +1473,22 @@
     
-return null
+System.exit(1)
 ;%0A  
@@ -1528,26 +1528,29 @@
  %7D%0A%0A
+%0A%0A
     p
-ublic
+rivate
  static 
 void
@@ -1549,479 +1549,340 @@
 tic 
-void main(String %5B%5D args) %7B%0A%0A        Properties props= loadProps(DEFAULT_PROPERTIES_FILE_PATH);%0A        if (props==null) System.exit(1);%0A%0A        final FileSystemKeyValueStore store=%0A                initDS((String)props.get(%22datastorePath%22));%0A        if (store==null) %7B%0A            System.exit(1);%0A        %7D%0A%0A        int serverPort= Integer.parseInt((String)props.get(%22serverPort%22))
+Properties _validate(final Properties props,final String name) %7B%0A        if (props.get(name)==null) %7B%0A            logger.error(%22Property %22+name+%22 does not exist%22);%0A            System.exit(1);%0A        %7D%0A%0A        return props
 ;%0A    
+%7D%0A%0A
     p
-ort(serverPort);%0A%0A        String%5B%5D typeNames= ((String)props.get(%22typeNames%22))
+rivate static String%5B%5D getTypes(final String typesString) %7B%0A        String%5B%5D types= typesString
 .spl
@@ -1923,20 +1923,16 @@
 ame:type
-Name
 s) %7B%0A   
@@ -2196,25 +2196,24 @@
       %7D%0A
-%0A
         
 String%5B%5D
@@ -2208,122 +2208,513 @@
     
-String%5B%5D credentials= ((String)props.get(%22credentials%22)).split(%22,%22);%0A
+return types;%0A    %7D%0A%0A    private static Properties validate(Properties props) %7B%0A        _validate(props,%22serverPort%22);%0A        _validate(props,%22esIndexName%22);%0A        _validate(props,%22datastorePath%22);%0A        _validate(props,%22esUrl%22);%0A        _validate(props,%22credentials%22);%0A        _validate(props,%22typeNames%22);%0A        return props;
 %0A    
+%7D%0A%0A
     
-JsonRestClient jrc= new JsonRestClie
+public static void main(String %5B%5D args) %7B%0A%0A        Properties props= validate(loadProps(DEFAULT_PROPERTIES_FILE_PATH));%0A%0A        int serverPort= Integer.parseI
 nt((
@@ -2735,27 +2735,31 @@
 et(%22
-esUrl
+serverPort
 %22));%0A
-%0A
         
 Cont
@@ -2758,208 +2758,398 @@
     
-Controller controller= new Controller(%0A                store,new ESRestSearchableKeyValueStore(jrc,(String)props.get(%22esIndexName%22)));%0A%0A        new Router(%0A                controller,%0A                
+port(serverPort);%0A%0A        new Router(%0A                new Controller(%0A                        initDS((String)props.get(%22datastorePath%22)),%0A                        new ESRestSearchableKeyValueStore(%0A                                new JsonRestClient((String)props.get(%22esUrl%22)),%0A                                (String)props.get(%22esIndexName%22))),%0A                getTypes((String)props.get(%22
 type
@@ -3153,16 +3153,19 @@
 ypeNames
+%22))
 ,%0A      
@@ -3174,27 +3174,61 @@
         
-credentials
+((String)props.get(%22credentials%22)).split(%22,%22)
 );%0A    %7D

Add properties file validation.