package de.tblsoft.solr.pipeline.filter;

import de.tblsoft.solr.pipeline.AbstractFilter;
import de.tblsoft.solr.pipeline.bean.Document;
import de.tblsoft.solr.pipeline.bean.DocumentBuilder;
import de.tblsoft.solr.util.IOUtils;
import org.apache.commons.io.FileUtils;

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tblsoft 30.03.18.
 */
public class JavaScriptFilter extends AbstractFilter {


    private String filename;

    private String script;

    private ScriptEngine engine;

    private CompiledScript compiledScript;

    @Override
    public void init() {
        String internalFilename = getProperty("filename", null);
        verify(internalFilename, "For the JavaScriptFilter a filename property must be defined.");
        filename = IOUtils.getAbsoluteFile(getBaseDir(),internalFilename);


        ScriptEngineManager mgr = new ScriptEngineManager();
        engine = mgr.getEngineByName("JavaScript");


        try {
            script = FileUtils.readFileToString(new File(filename));

            Compilable compEngine = (Compilable) engine;
            compiledScript = compEngine.compile(script);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        super.init();

    }

    @Override
    public void document(Document document) {
        try {
            engine.put("input", document);
            DocumentBuilder documentBuilder = new DocumentBuilder();
            engine.put("documentBuilder", documentBuilder);

            List<Document> output = new ArrayList<>();
            engine.put("output", output);
            compiledScript.eval();

            for(Document d : output) {
                super.document(d);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public void end() {
        super.end();
    }
}

@@ -989,16 +989,62 @@
 ename);%0A
+        script = getProperty(%22script%22, null);%0A
 %0A%0A      
@@ -1158,32 +1158,71 @@
 %0A%0A        try %7B%0A
+            if(filename != null) %7B%0A    
             scri
@@ -1274,16 +1274,29 @@
 name));%0A
+            %7D
 %0A       

read javascript from script var instead of only from filename