package org.codelibs.elasticsearch.sstmpl.chain;

import java.util.Collections;
import java.util.Map;

import org.codelibs.elasticsearch.sstmpl.ScriptTemplateException;
import org.codelibs.elasticsearch.sstmpl.filter.SearchTemplateFilter;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptService.ScriptType;

public class SearchTemplateChain {

    private ScriptService scriptService;

    private SearchTemplateFilter[] filters;

    int position = 0;

    public SearchTemplateChain(final ScriptService scriptService,
            final SearchTemplateFilter[] filters) {
        this.scriptService = scriptService;
        this.filters = filters;
    }

    public String doCreate(final String lang, final String script,
            final ScriptType scriptType, final Map<String, Object> paramMap) {
        if (position < filters.length) {
            final SearchTemplateFilter filter = filters[position];
            position++;
            return filter.doCreate(lang, script, scriptType, paramMap, this);
        } else {
            final CompiledScript compiledScript = scriptService.compile(lang,
                    script, scriptType);
            final ExecutableScript executable = scriptService.executable(
                    compiledScript,
                    paramMap != null ? paramMap : Collections.emptyMap());
            final Object result = executable.run();
            if (result instanceof String) {
                return (String) result;
            } else if (result instanceof BytesReference) {
                return ((BytesReference) result).toUtf8();
            } else {
                throw new ScriptTemplateException("Query DSL is null.");
            }
        }
    }

}

@@ -1329,16 +1329,211 @@
 tType);%0A
+            final Map%3CString, Object%3E vars;%0A            if (paramMap != null) %7B%0A                vars = paramMap;%0A            %7D else %7B%0A                vars = Collections.emptyMap();%0A            %7D%0A
         
@@ -1637,81 +1637,13 @@
 ipt,
-%0A                    paramMap != null ? paramMap : Collections.emptyMap()
+ vars
 );%0A 

fix compile error