package org.commcare.api.process;

import org.commcare.core.interfaces.UserSandbox;
import org.commcare.core.process.XmlFormRecordProcessor;
import org.javarosa.xml.util.InvalidStructureException;
import org.javarosa.xml.util.UnfullfilledRequirementsException;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 *  * Convenience methods, mostly for Touchforms so we don't have to deal with Java IO
 * in Jython which is terrible
 *
 * Created by wpride1 on 8/20/15.
 */
public class FormRecordProcessorHelper extends XmlFormRecordProcessor {
    public static void processXML(UserSandbox sandbox, String fileText) throws IOException, XmlPullParserException, UnfullfilledRequirementsException, InvalidStructureException {
        InputStream stream = new ByteArrayInputStream(fileText.getBytes("UTF-8"));
        FormRecordProcessorThread thread = new FormRecordProcessorThread(sandbox, stream);
        thread.start();
    }

    public static void processFile(UserSandbox sandbox, File record) throws IOException, XmlPullParserException, UnfullfilledRequirementsException, InvalidStructureException {
        InputStream stream = new FileInputStream(record);
        FormRecordProcessorThread thread = new FormRecordProcessorThread(sandbox, stream);
        thread.start();
    }

    static class FormRecordProcessorThread extends Thread{
        public FormRecordProcessorThread(UserSandbox sandbox, InputStream stream) throws IOException, XmlPullParserException, UnfullfilledRequirementsException, InvalidStructureException {
            process(sandbox, stream);
        }
    }
}

@@ -134,16 +134,71 @@
 cessor;%0A
+import org.commcare.data.xml.TransactionParserFactory;%0A
 import o
@@ -237,32 +237,32 @@
 ctureException;%0A
-
 import org.javar
@@ -737,16 +737,415 @@
 cessor %7B
+%0A%0A    public static void processXML(TransactionParserFactory factory, String fileText) throws IOException, XmlPullParserException, UnfullfilledRequirementsException, InvalidStructureException %7B%0A        InputStream stream = new ByteArrayInputStream(fileText.getBytes(%22UTF-8%22));%0A        FormRecordProcessorThread thread = new FormRecordProcessorThread(factory, stream);%0A        thread.start();%0A    %7D%0A%0A
 %0A    pub
@@ -1934,16 +1934,17 @@
 s Thread
+ 
 %7B%0A      
@@ -2118,32 +2118,32 @@
 tureException %7B%0A
-
             proc
@@ -2173,16 +2173,267 @@
        %7D
+%0A%0A        public FormRecordProcessorThread(TransactionParserFactory factory, InputStream stream) throws IOException, XmlPullParserException, UnfullfilledRequirementsException, InvalidStructureException %7B%0A            process(stream, factory);%0A        %7D
 %0A    %7D%0A%7D

Add polymorphic process() function