<commit_before>package org.commcare.api.json;

import org.commcare.modern.util.Pair;
import org.javarosa.core.model.Constants;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.model.SelectChoice;
import org.javarosa.core.model.data.AnswerDataFactory;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.data.SelectMultiData;
import org.javarosa.core.model.data.SelectOneData;
import org.javarosa.core.model.data.UncastData;
import org.javarosa.core.model.data.helper.Selection;
import org.javarosa.form.api.FormEntryController;
import org.javarosa.form.api.FormEntryModel;
import org.javarosa.form.api.FormEntryPrompt;
import org.json.JSONObject;

import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Vector;

/**
 * Created by willpride on 11/18/15.
 */
public class AnswerQuestionJson {

    public static JSONObject deleteRepeatToJson(FormEntryController controller,
                                                 FormEntryModel model, String formIndexString){
        JSONObject ret = new JSONObject();
        FormIndex formIndex = indexFromString(formIndexString, model.getForm());
        controller.deleteRepeat(formIndex);
        ret.put("tree", WalkJson.walkToJSON(model, controller));
        return ret;
    }

    public static JSONObject descendRepeatToJson(FormEntryController controller,
                                                 FormEntryModel model, String formIndexString){
        JSONObject ret = new JSONObject();
        FormIndex formIndex = indexFromString(formIndexString, model.getForm());
        controller.jumpToIndex(formIndex);
        controller.descendIntoNewRepeat();
        ret.put("tree", WalkJson.walkToJSON(model, controller));
        return ret;
    }

    public static JSONObject getCurrentJson(FormEntryController controller,
                                            FormEntryModel model){
        JSONObject ret = new JSONObject();
        ret.put("tree", WalkJson.walkToJSON(model, controller));
        return ret;
    }

    public static JSONObject questionAnswerToJson(FormEntryController controller,
                                                  FormEntryModel model, String answer, FormEntryPrompt prompt){
        JSONObject ret = new JSONObject();
        IAnswerData answerData = null;

        if(answer.equals("None")){
            answerData = null;
        } else {
            try {
                answerData = getAnswerData(prompt, answer);
            } catch (IllegalArgumentException e) {
                ret.put("status", "error");
                ret.put("type", "illegal-argument");
                ret.put("reason", e.getMessage());
                return ret;
            }
        }
        int result = controller.answerQuestion(prompt.getIndex(), answerData);
        if(result == FormEntryController.ANSWER_REQUIRED_BUT_EMPTY) {
            ret.put("status","error");
            ret.put("type", "required");
        }
        else if(result == FormEntryController.ANSWER_CONSTRAINT_VIOLATED){
            ret.put("status", "error");
            ret.put("type", "restraint");
            ret.put("reason", prompt.getConstraintText());
        }
        else if (result == FormEntryController.ANSWER_OK){
            ret.put("status","success");
            //controller.stepToNextEvent();
        }

        ret.put("tree", WalkJson.walkToJSON(model, controller));

        return ret;
    }

    public static JSONObject questionAnswerToJson(FormEntryController controller,
                                                  FormEntryModel model, String answer, String index){
        try {
            FormIndex formIndex = indexFromString(index, model.getForm());
            FormEntryPrompt prompt = model.getQuestionPrompt(formIndex);
            return questionAnswerToJson(controller, model, answer, prompt);
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static JSONObject questionAnswerToJson(FormEntryController controller,
                                                  FormEntryModel model, String answer){
        FormEntryPrompt prompt = model.getQuestionPrompt();
        return questionAnswerToJson(controller, model, answer, prompt);
    }
<commit_msg>tests<commit_after>package org.commcare.api.json;

import org.commcare.modern.util.Pair;
import org.javarosa.core.model.Constants;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.model.SelectChoice;
import org.javarosa.core.model.data.AnswerDataFactory;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.data.SelectMultiData;
import org.javarosa.core.model.data.SelectOneData;
import org.javarosa.core.model.data.UncastData;
import org.javarosa.core.model.data.helper.Selection;
import org.javarosa.form.api.FormEntryController;
import org.javarosa.form.api.FormEntryModel;
import org.javarosa.form.api.FormEntryPrompt;
import org.json.JSONObject;

import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Vector;

/**
 * Created by willpride on 11/18/15.
 */
public class AnswerQuestionJson {

    public static JSONObject deleteRepeatToJson(FormEntryController controller,
                                                 FormEntryModel model, String formIndexString){
        JSONObject ret = new JSONObject();
        FormIndex formIndex = indexFromString(formIndexString, model.getForm());
        controller.deleteRepeat(formIndex);
        ret.put("tree", WalkJson.walkToJSON(model, controller));
        return ret;
    }

    public static JSONObject descendRepeatToJson(FormEntryController controller,
                                                 FormEntryModel model, String formIndexString){
        JSONObject ret = new JSONObject();
        FormIndex formIndex = indexFromString(formIndexString, model.getForm());
        controller.jumpToIndex(formIndex);
        controller.descendIntoNewRepeat();
        ret.put("tree", WalkJson.walkToJSON(model, controller));
        return ret;
    }

    public static JSONObject getCurrentJson(FormEntryController controller,
                                            FormEntryModel model){
        JSONObject ret = new JSONObject();
        ret.put("tree", WalkJson.walkToJSON(model, controller));
        return ret;
    }

    public static JSONObject questionAnswerToJson(FormEntryController controller,
                                                  FormEntryModel model, String answer, FormEntryPrompt prompt){
        JSONObject ret = new JSONObject();
        IAnswerData answerData = null;

        if(answer.equals("None")){
            answerData = null;
        } else {
            try {
                answerData = getAnswerData(prompt, answer);
            } catch (IllegalArgumentException e) {
                ret.put("status", "error");
                ret.put("type", "illegal-argument");
                ret.put("reason", e.getMessage());
                return ret;
            }
        }
        int result = controller.answerQuestion(prompt.getIndex(), answerData);
        System.out.println("RESULT!: " + result);
        if(result == FormEntryController.ANSWER_REQUIRED_BUT_EMPTY) {
            ret.put("status","error");
            ret.put("type", "required");
        }
        else if(result == FormEntryController.ANSWER_CONSTRAINT_VIOLATED){
            ret.put("status", "error");
            ret.put("type", "restraint");
            ret.put("reason", prompt.getConstraintText());
        }
        else if (result == FormEntryController.ANSWER_OK){
            ret.put("tree", WalkJson.walkToJSON(model, controller));
            ret.put("status","success");
            //controller.stepToNextEvent();
        }

        System.out.println("RET: " + ret);

        return ret;
    }

    public static JSONObject questionAnswerToJson(FormEntryController controller,
                                                  FormEntryModel model, String answer, String index){
        FormIndex formIndex = indexFromString(index, model.getForm());
        FormEntryPrompt prompt = model.getQuestionPrompt(formIndex);
        return questionAnswerToJson(controller, model, answer, prompt);
    }

{'fraction_non_alphanumeric': 0.1007541833608296, 'fraction_numerical': 0.0014140938015555031, 'mean_word_length': 2.501237623762376, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6432776', 'n_tokens_mistral': 2222, 'n_tokens_neox': 2160, 'n_words': 435}