package fr.insee.rmes.config;

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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Configuration {
    private static Logger logger = LogManager.getLogger(Configuration.class);

    private static String sparqlEndPoint = "";

    private static String baseHost = "";
    private static String fileStorage = "";
    private static String fileStorageLocation = "";

    private static String swaggerHost = "";
    private static String swaggerBasepath = "";
    private static String swaggerUrl = "";
    private static Boolean requiresSsl = false;

    private Properties getProperties() throws IOException {
        Properties props = new Properties();
        props.load(this.getClass().getClassLoader().getResourceAsStream("rmes-api.properties"));
        this.loadIfExists(props, "rmes-api.properties");
        this.loadIfExists(props, "rmeswnci.properties");
        this.loadIfExists(props, "rmeswncz.properties\"");
        return props;
    }

    private void loadIfExists(Properties props, String filename) throws IOException {
        File f;
        f = new File(String.format("%s/webapps/%s", System.getProperty("catalina.base"), filename));
        if (f.exists() && ! f.isDirectory()) {
            FileReader r = new FileReader(f);
            props.load(r);
            r.close();
        }
    }

    public Configuration() {
        Properties props = null;
        try {
            props = this.getProperties();

            sparqlEndPoint = props.getProperty("fr.insee.rmes.api.sparqlEndpoint");
            baseHost = props.getProperty("fr.insee.rmes.api.baseHost");
            fileStorage = props.getProperty("fr.insee.rmes.api.fileStorage");
            fileStorageLocation = props.getProperty("fr.insee.rmes.storage.document");

            swaggerHost = props.getProperty("fr.insee.rmes.api.host");
            swaggerBasepath = props.getProperty("fr.insee.rmes.api.basepath");
            swaggerUrl = (requiresSsl ? "https" : "http") + "://" + swaggerHost + "/" + swaggerBasepath;
            requiresSsl = Boolean.valueOf(props.getProperty("fr.insee.rmes.api.force.ssl"));

        }
        catch (IOException e) {
            logger.error("Configuration error, can't read properties", e);
        }

    }

    public static String getSparqlEndPoint() {
        return sparqlEndPoint;
    }

    public static void setSparqlEndPoint(String sparqlEndPoint) {
        Configuration.sparqlEndPoint = sparqlEndPoint;
    }

    public static String getBaseHost() {
        return baseHost;
    }

    public static void setBaseHost(String baseHost) {
        Configuration.baseHost = baseHost;
    }

    public static String getFileStorage() {
        return fileStorage;
    }

    public static void setFileStorage(String fileStorage) {
        Configuration.fileStorage = fileStorage;
    }

    public static String getFileStorageLocation() {
        return fileStorageLocation;
    }

    public static void setFileStorageLocation(String fileStorageLocation) {
        Configuration.fileStorageLocation = fileStorageLocation;
    }

    public static String getSwaggerHost() {
        return swaggerHost;
    }

    public static void setSwaggerHost(String swaggerHost) {
        Configuration.swaggerHost = swaggerHost;
    }

    public static String getSwaggerBasepath() {
        return swaggerBasepath;
    }

    public static void setSwaggerBasepath(String swaggerBasepath) {
        Configuration.swaggerBasepath = swaggerBasepath;
    }

    public static String getSwaggerUrl() {
        return swaggerUrl;
    }

    public static void setSwaggerUrl(String swaggerUrl) {
        Configuration.swaggerUrl = swaggerUrl;
    }

    public static Boolean getRequiresSsl() {
        return requiresSsl;
    }

    public static void setRequiresSsl(Boolean requiresSsl) {
        Configuration.requiresSsl = requiresSsl;
    }

}

@@ -379,44 +379,128 @@
     
-private static String baseHost = %22%22;
+//Base for all uri in our database%0A    private static String baseHost = %22%22;%0A    %0A    //folder where documents are stored
 %0A   
@@ -536,24 +536,64 @@
 orage = %22%22;%0A
+    //Server where documents are stored%0A
     private 
@@ -629,24 +629,41 @@
 tion = %22%22;%0A%0A
+    //API Server%0A
     private 
@@ -690,24 +690,39 @@
 rHost = %22%22;%0A
+    //API name%0A
     private 
@@ -753,24 +753,59 @@
 epath = %22%22;%0A
+    //Build with host and basepath%0A
     private 
@@ -831,24 +831,44 @@
 erUrl = %22%22;%0A
+    //Https or Http%0A
     private 
@@ -1277,10 +1277,8 @@
 ties
-%5C%22
 %22);%0A
@@ -1302,24 +1302,79 @@
 ops;%0A    %7D%0A%0A
+    /*%0A     * load properties on catalina base%0A     */%0A
     private 

Improve comments