package org.dainst.chronontology.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;

/**
 * @author Daniel M. de Oliveira
 */
public class ControllerConfig extends Config {

    static final String MSG_MUST_TYPE_ES= "datastores.0 must be of type \""+
        ConfigConstants.DATASTORE_TYPE_ES+"\".";
    static final String MSG_ES_CLASH= "When both datastores use the same elasticsearch url, the index names must be different.";

    private DatastoreConfig[] datastoreConfigs = new DatastoreConfig[2];
    private boolean useConnect = true;

    @Override
    public boolean validate(Properties props) {
        return (
            _validate(props,"useConnect",true) &
            validateDatastores(props)
        );
    }

    @Override
    public ArrayList<String> getConstraintViolations() {
        ArrayList<String> allViolations= new ArrayList<String>();
        allViolations.addAll(constraintViolations);
        for (DatastoreConfig config: Arrays.asList(datastoreConfigs)) {
            if (config!=null)
                allViolations.addAll(config.getConstraintViolations());
        }
        return allViolations;
    }

    private boolean esConfigsClash() {
        // TODO implement equals
        return ((datastoreConfigs[0].getType().equals(datastoreConfigs[1].getType()))
                && (datastoreConfigs[0].getUrl().equals(datastoreConfigs[1].getUrl()))
                && (datastoreConfigs[0].getIndexName().equals(datastoreConfigs[1].getIndexName())));
    }

    private boolean validateDatastores(Properties props) {
        boolean datastoresValidated= true;
        datastoreConfigs[0]= new DatastoreConfig("0");
        if (!datastoreConfigs[0].validate(props)) return false;
        if (!datastoreConfigs[0].getType().equals(ConfigConstants.DATASTORE_TYPE_ES)) {
            constraintViolations.add(ConfigConstants.MSG_CONSTRAINT_VIOLATION+MSG_MUST_TYPE_ES);
            return false;
        }

        if (useConnect) {
            datastoreConfigs[1]= new DatastoreConfig("1");
            if (!datastoreConfigs[1].validate(props)) datastoresValidated=false;

            if (esConfigsClash()) {
                constraintViolations.add(ConfigConstants.MSG_CONSTRAINT_VIOLATION+MSG_ES_CLASH);
                return false;
            }
        }
        return datastoresValidated;
    }

    void setUseConnect(String useIt) {
        if (useIt.equals("false")) useConnect = false;
    }

    public boolean isUseConnect() {
        return useConnect;
    }

    public DatastoreConfig[] getDatastoreConfigs() {
        return datastoreConfigs;
    }
}

@@ -1543,32 +1543,37 @@
 boolean validate
+First
 Datastores(Prope
@@ -1565,17 +1565,16 @@
 atastore
-s
 (Propert
@@ -1590,51 +1590,8 @@
 ) %7B%0A
-        boolean datastoresValidated= true;%0A
     
@@ -1697,32 +1697,33 @@
 ) return false;%0A
+%0A
         if (!dat
@@ -1931,39 +1931,100 @@
   %7D%0A
+        return true;
 %0A    
+%7D%0A%0A
     
-if (useConnect) %7B%0A    
+private boolean validateSecondDatastore(Properties props) %7B%0A
     
@@ -2078,33 +2078,32 @@
 %22);%0A        
-    if
+return
  (
-!
 datastoreCon
@@ -2130,36 +2130,226 @@
 ps))
- datastoresValidated=false;%0A
+;%0A    %7D%0A%0A%0A%0A    private boolean validateDatastores(Properties props) %7B%0A%0A        if (!validateFirstDatastore(props)) return false;%0A%0A        if (useConnect) %7B%0A            if (!validateSecondDatastore(props)) return false;
 %0A   
@@ -2551,27 +2551,12 @@
 urn 
-datastoresValidated
+true
 ;%0A  

Refactor for readability.