package de.tblsoft.solr.pipeline;

import de.tblsoft.solr.pipeline.bean.Reader;
import org.apache.commons.lang3.text.StrSubstitutor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tblsoft on 11.02.16.
 */
public abstract class AbstractReader implements ReaderIF {

    protected PipelineExecuter executer;

    protected Reader reader;

    protected String baseDir;

    protected Map<String,String> variables = new HashMap<String, String>();


    @Override
    public void setVariables(Map<String,String> variables) {
        if(variables == null) {
            return;
        }
        for(Map.Entry<String,String> entry: variables.entrySet()) {
            this.variables.put("variables." + entry.getKey(), entry.getValue());
        }
    }

    @Override
    public void setPipelineExecuter(PipelineExecuter executer) {
        this.executer = executer;
    }

    @Override
    public void end() {

    }

    @Override
    public void setReader(Reader reader) {
        this.reader = reader;
    }

    public List<String> getPropertyAsList(String name, List<String> defaultValue) {
        if(reader.getProperty() == null) {
            return defaultValue;
        }
        List<String> value = (List<String>) reader.getProperty().get(name);
        if(value != null) {
            return value;
        }
        return defaultValue;
    }

    public String[] getPropertyAsArray(String name, String[] defaultValue) {
        List<String> list = getPropertyAsList(name, null);
        if(list == null) {
            return defaultValue;
        }
        return list.toArray(new String[list.size()]);
    }

    public String getProperty(String name, String defaultValue) {
        String value = (String) reader.getProperty().get(name);
        if(value != null) {
            StrSubstitutor strSubstitutor = new StrSubstitutor(variables);
            value = strSubstitutor.replace(value);
            return value;
        }
        return defaultValue;
    }

    public Long getPropertyAsInteger(String name, Long defaultValue) {
        String value = getProperty(name, null);
        if(value == null) {
            return defaultValue;
        }

        return Long.valueOf(value);

    }

    public Boolean getPropertyAsBoolean(String name, Boolean defaultValue) {
        String value = getProperty(name, null);
        if(value == null) {
            return defaultValue;
        }

        return Boolean.valueOf(value);

    }

    public String getBaseDir() {
        return baseDir;
    }

    public void setBaseDir(String baseDir) {
        this.baseDir = baseDir;
    }
}

@@ -127,16 +127,44 @@
 tutor;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -1266,24 +1266,25 @@
 tring%3E value
+s
  = (List%3CStr
@@ -1329,32 +1329,33 @@
         if(value
+s
  != null) %7B%0A    
@@ -1358,36 +1358,344 @@
             
-return value
+List%3CString%3E substitutedValues = new ArrayList%3C%3E();%0A            StrSubstitutor strSubstitutor = new StrSubstitutor(variables);%0A            for(String value : values) %7B%0A                value = strSubstitutor.replace(value);%0A                substitutedValues.add(value);%0A            %7D%0A%0A            return substitutedValues
 ;%0A        %7D%0A

allow the usage of variables in reader properties