/*
 * Copyright 2008-2013 Microarray Informatics Team, EMBL-European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

package uk.ac.ebi.atlas.trader;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import uk.ac.ebi.atlas.model.ExperimentConfiguration;
import uk.ac.ebi.atlas.model.baseline.BaselineExperimentConfiguration;
import uk.ac.ebi.atlas.model.differential.microarray.MicroarrayExperimentConfiguration;

import javax.inject.Named;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;

@Named
public class ConfigurationTrader {

    private static final Logger LOGGER = Logger.getLogger(ConfigurationTrader.class);

    @Value("#{configuration['experiment.factors.path.template']}")
    private String baselineFactorsConfigurationPathTemplate;

    @Value("#{configuration['experiment.configuration.path.template']}")
    private String configurationPathTemplate;

    public BaselineExperimentConfiguration getFactorsConfiguration(String experimentAccession) {
        XMLConfiguration xmlConfiguration = getXmlConfiguration(baselineFactorsConfigurationPathTemplate, experimentAccession);
        return new BaselineExperimentConfiguration(xmlConfiguration);
    }

    public ExperimentConfiguration getExperimentConfiguration(String experimentAccession) {
        return getExperimentConfiguration(experimentAccession, false);
    }

    private ExperimentConfiguration getExperimentConfiguration(String experimentAccession, boolean isMicroarray) {

        XMLConfiguration xmlConfiguration = getXmlConfiguration(configurationPathTemplate, experimentAccession);
        xmlConfiguration.setExpressionEngine(new XPathExpressionEngine());
        Document document = parseConfigurationXml(experimentAccession);
        if (isMicroarray) {
            return new MicroarrayExperimentConfiguration(xmlConfiguration, document);
        }
        return new ExperimentConfiguration(xmlConfiguration, document);
    }


    public MicroarrayExperimentConfiguration getMicroarrayExperimentConfiguration(String experimentAccession) {
        return (MicroarrayExperimentConfiguration) getExperimentConfiguration(experimentAccession, true);
    }

    private Document parseConfigurationXml(String experimentAccession) {
        Path path = Paths.get(MessageFormat.format(configurationPathTemplate, experimentAccession));
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse(Files.newInputStream(path));
        } catch (ParserConfigurationException | SAXException | IOException e) {
            LOGGER.error(e.getMessage(), e);
            throw new IllegalStateException("Problem parsing configuration file: " + path.toString(), e);
        }
    }

    private XMLConfiguration getXmlConfiguration(String pathTemplate, String experimentAccession) {
        Path path = Paths.get(MessageFormat.format(pathTemplate, experimentAccession));

        File file = path.toFile();

        if (!file.exists()) {
            throw new IllegalStateException("Configuration file " + path.toString() + " does not exist");
        }

        try {
            XMLConfiguration xmlConfiguration = new XMLConfiguration();
            xmlConfiguration.setDelimiterParsingDisabled(true);
            xmlConfiguration.load(path.toFile());
            return xmlConfiguration;
        } catch (ConfigurationException cex) {
            LOGGER.error(cex.getMessage(), cex);
            throw new IllegalStateException("Cannot read configuration from path " + path.toString(), cex);
        }

//        try {
//            XMLConfiguration xmlConfiguration = new XMLConfiguration(path.toFile());
//            xmlConfiguration.setDelimiterParsingDisabled(true);
//            return xmlConfiguration;
//        } catch (ConfigurationException cex) {
//            LOGGER.error(cex.getMessage(), cex);
//            throw new IllegalStateException("Cannot read configuration from path " + path.toString(), cex);
//        }
    }

}
@@ -2339,32 +2339,38 @@
 erimentAccession
+, true
 );%0A        retur
@@ -2821,32 +2821,39 @@
 erimentAccession
+, false
 );%0A        xmlCo
@@ -4148,32 +4148,54 @@
 erimentAccession
+, boolean splitOnComma
 ) %7B%0A        Path
@@ -4536,24 +4536,61 @@
 guration();%0A
+            if (!splitOnComma) %7B%0A    
             
@@ -4637,24 +4637,38 @@
 bled(true);%0A
+            %7D%0A
             

allow comma parsing into list when reading factors