package fr.insee.pogues.transforms;

import fr.insee.pogues.conversion.JSONToXMLTranslator;
import fr.insee.pogues.utils.json.JSONFunctions;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.xml.bind.JAXBException;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Map;

@Service
public class JSONToXMLImpl implements JSONToXML {

    private JSONToXMLTranslator translator = new JSONToXMLTranslator(true);

    @PostConstruct
    public void onInit(){
        System.setProperty("javax.xml.bind.context.factory","org.eclipse.persistence.jaxb.JAXBContextFactory");
    }

    public void transform(InputStream input, OutputStream output, Map<String, Object> params) throws JAXBException, IOException {
        if(null == input){
            throw new NullPointerException("Null input");
        }
        if(null == output){
            throw new NullPointerException("Null output");
        }
        try {
            StreamSource source = new StreamSource(input);
            byte[] out = translator.translate(source).getBytes(Charset.forName("UTF-8"));
            output.write(out, 0, out.length);
        } catch (Exception e) {
            throw e;
        }
    }

    public String transform(InputStream input, Map<String, Object> params) throws Exception {
        if(null == input){
            throw new NullPointerException("Null input");
        }
        try {
            StreamSource source = new StreamSource(input);
            return translator.translate(source);
        } catch (Exception e) {
            throw e;
        }
    }

    public String transform(String input, Map<String, Object> params) throws Exception {
        if(null == input){
            throw new NullPointerException("Null input");
        }
        try {
            // why do we need to wrap our object ? just because pogues-model tells us to do so !
            JSONParser parser = new JSONParser();
            JSONObject questionnaire = (JSONObject) parser.parse(input);
            questionnaire = JSONFunctions.renameQuestionnairePlural(questionnaire);
            JSONObject wrapper = new JSONObject();
            wrapper.put("Questionnaire", questionnaire);
            return translator.translate(wrapper.toString());
        } catch (Exception e) {
            throw e;
        }
    }

}

@@ -134,17 +134,16 @@
 ctions;%0A
-%0A
 import o
@@ -1380,32 +1380,65 @@
 (Exception e) %7B%0A
+            e.printStackTrace();%0A
             thro
@@ -2042,105 +2042,8 @@
 y %7B%0A
-            // why do we need to wrap our object ? just because pogues-model tells us to do so !%0A
     
@@ -2084,24 +2084,24 @@
 ONParser();%0A
+
             
@@ -2249,116 +2249,8 @@
 e);%0A
-            JSONObject wrapper = new JSONObject();%0A            wrapper.put(%22Questionnaire%22, questionnaire);%0A
     
@@ -2289,18 +2289,28 @@
 ate(
-wrapper.to
+questionnaire.toJSON
 Stri
@@ -2340,32 +2340,65 @@
 (Exception e) %7B%0A
+            e.printStackTrace();%0A
             thro

Improve error logging