package ir.piana.dev.server.config;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @author Mohammad Rahmati, 5/3/2017 8:01 AM
 */
public class PianaConfigReader {
    private static final Logger logger =
            Logger.getLogger(
                    PianaConfigReader.class);

    public static PianaConfig createFromJson(
            String configFilePath)
            throws Exception {
        return createFromJson(
                new FileInputStream(
                        new File(configFilePath)));
    }

    public static PianaConfig createFromJson(
            File configFile)
            throws Exception {
        return createFromJson(new FileInputStream(configFile));
    }

    public static PianaConfig createFromJson(
            InputStream inputStream)
            throws Exception {
        if(inputStream == null)
            throw new Exception("input stream is null");

        PianaConfig pianaConfig =
                new PianaConfig();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            pianaConfig.configMap = objectMapper
                    .readValue(
                            inputStream, Map.class);
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return pianaConfig;
    }

    public static PianaConfig createFromJsonNode(
            InputStream inputStream)
            throws Exception {
        if(inputStream == null)
            throw new Exception("input stream is null");

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            PianaConfig pianaConfig = new PianaConfig(
                    objectMapper.readTree(inputStream));
            return pianaConfig;
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return new PianaConfig();
    }

    public static PianaConfig createFromMap(
            Map<String, Object> configMap)
            throws Exception {
        if(configMap == null)
            throw new Exception("input map is null");

        PianaConfig pianaConfig =
                new PianaConfig();
        pianaConfig.configMap = configMap;
        return pianaConfig;
    }

    public static PianaServiceConfig createPianaServiceConfig(
            InputStream inputStream)
            throws Exception {
        if(inputStream == null)
            throw new Exception("input stream is null");

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            PianaServiceConfig config = new PianaServiceConfig(
                    objectMapper.readTree(inputStream));
            return config;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw e;
        }
    }
}

@@ -605,24 +605,527 @@
 er.class);%0A%0A
+    public static JsonNode createJsonNode(%0A            InputStream inputStream)%0A            throws Exception %7B%0A        if(inputStream == null)%0A            throw new Exception(%22input stream is null%22);%0A%0A        ObjectMapper objectMapper = new ObjectMapper();%0A        JsonNode jsonNode = null;%0A        try %7B%0A            jsonNode = objectMapper.readTree(%0A                    inputStream);%0A        %7D catch (IOException e) %7B%0A            logger.error(e.getMessage());%0A        %7D%0A        return jsonNode;%0A    %7D%0A%0A
     public s
@@ -3573,11 +3573,351 @@
  %7D%0A    %7D
+%0A%0A    public static PianaServiceConfig createPianaServiceConfig(%0A            JsonNode jsonNode)%0A            throws Exception %7B%0A        if(jsonNode == null)%0A            throw new Exception(%22input json node is null%22);%0A%0A        PianaServiceConfig config = new PianaServiceConfig(%0A                    jsonNode);%0A            return config;%0A    %7D
 %0A%7D%0A

change to piana config reader