<commit_before>package com.muzima.service;

import android.util.Log;
import com.muzima.api.model.Concept;
import com.muzima.api.model.Encounter;
import com.muzima.api.model.Observation;
import com.muzima.api.model.Patient;
import com.muzima.controller.ConceptController;
import com.muzima.controller.EncounterController;
import com.muzima.controller.ObservationController;
import com.muzima.controller.PatientController;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import static com.muzima.utils.DateUtils.parse;
import static java.util.Arrays.asList;

public class HTMLFormObservationCreator {

    private PatientController patientController;
    private ConceptController conceptController;
    private EncounterController encounterController;
    private ObservationController observationController;
    private ObservationParserUtility observationParserUtility;

    private Patient patient;
    private Encounter encounter;
    private List<Observation> observations;
    private String TAG = "HTMLFormObservationCreator";

    public HTMLFormObservationCreator(PatientController patientController, ConceptController conceptController,
                                      EncounterController encounterController, ObservationController observationController) {
        this.patientController = patientController;
        this.conceptController = conceptController;
        this.encounterController = encounterController;
        this.observationController = observationController;
        this.observationParserUtility = new ObservationParserUtility(conceptController);
    }

    public void createAndPersistObservations(String jsonResponse) {
        parseJSONResponse(jsonResponse);
        try {
            saveObservationsAndRelatedEntities();
        } catch (ConceptController.ConceptSaveException e) {
            Log.e(TAG, "Error while saving concept");
        } catch (EncounterController.SaveEncounterException e) {
            Log.e(TAG, "Error while saving Encounter");
        } catch (ObservationController.SaveObservationException e) {
            Log.e(TAG, "Error while saving Observation");
        } catch (Exception e) {
            Log.e(TAG, "Unexpected Exception occurred" + e);
        }
    }

    public List<Observation> getObservations() {
        return observations;
    }

    private void parseJSONResponse(String jsonResponse) {
        try {
            JSONObject responseJSON = new JSONObject(jsonResponse);
            patient = getPatient(responseJSON.getJSONObject("patient"));
            encounter = createEncounter(responseJSON.getJSONObject("encounter"));
            observations = extractObservationFromJSONObject(responseJSON.getJSONObject("observation"));
        } catch (PatientController.PatientLoadException e) {
            Log.e(TAG, "Error while fetching Patient");
        } catch (ConceptController.ConceptFetchException e) {
            Log.e(TAG, "Error while fetching Concept");
        } catch (JSONException e) {
            Log.e(TAG, "Error while parsing response JSON");
        } catch (ParseException e) {
            Log.e(TAG, "Error while parsing response JSON");
        } catch (ConceptController.ConceptSaveException e) {
            Log.e(TAG, "Error while saving newly created concept");
        }
    }

    private void saveObservationsAndRelatedEntities() throws EncounterController.SaveEncounterException,
            ObservationController.SaveObservationException, ConceptController.ConceptSaveException {
        encounterController.saveEncounters(asList(encounter));
        conceptController.saveConcepts(observationParserUtility.getNewConceptList());
        observationController.saveObservations(observations);
    }

    private List<Observation> extractObservationFromJSONObject(JSONObject jsonObject) throws JSONException, ConceptController.ConceptFetchException, ConceptController.ConceptSaveException {
        List<Observation> observations = new ArrayList<Observation>();
        Iterator keys = jsonObject.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            observations.addAll(extractBasedOnType(jsonObject, key));
        }
        return observations;
    }

    private List<Observation> extractBasedOnType(JSONObject jsonObject, String key) throws JSONException, ConceptController.ConceptFetchException, ConceptController.ConceptSaveException {
        if (jsonObject.get(key) instanceof JSONArray) {
            return createMultipleObservation(key, jsonObject.getJSONArray(key));
        } else if (jsonObject.get(key) instanceof JSONObject) {
            return extractObservationFromJSONObject(jsonObject.getJSONObject(key));
        }
        ArrayList<Observation> observations = new ArrayList<Observation>();
        observations.add(createObservation(key, jsonObject.getString(key)));
        observations.removeAll(Collections.singleton(null));
        return observations;
    }

    private List<Observation> createMultipleObservation(String conceptName, JSONArray jsonArray) throws JSONException,
            ConceptController.ConceptFetchException, ConceptController.ConceptSaveException {
        List<Observation> observations = new ArrayList<Observation>();
        for (int i = 0; i < jsonArray.length(); i++) {
            if (jsonArray.get(i) instanceof JSONObject) {
                observations.addAll(extractObservationFromJSONObject(jsonArray.getJSONObject(i)));
            } else {
                observations.add(createObservation(conceptName, jsonArray.getString(i)));
            }
        }
        return observations;
    }

    private Observation createObservation(String conceptName, String value) throws JSONException, ConceptController.ConceptFetchException, ConceptController.ConceptSaveException {
        Concept concept = observationParserUtility.getConceptEntity(conceptName);
        if(concept == null){
            return null;
        }
        Observation observation = observationParserUtility.getObservationEntity(concept, value);
        observation.setEncounter(encounter);
        observation.setPerson(patient);
        observation.setObservationDatetime(encounter.getEncounterDatetime());
        return observation;
    }

    private Encounter createEncounter(JSONObject encounterJSON) throws JSONException, ParseException {
        return observationParserUtility.getEncounterEntity(parse(encounterJSON.getString("encounter.encounter_datetime")), patient);
    }
<commit_msg>added a catch clause to catch unexpected exceptions while parsing and saving observations<commit_after>package com.muzima.service;

import android.util.Log;
import com.muzima.api.model.Concept;
import com.muzima.api.model.Encounter;
import com.muzima.api.model.Observation;
import com.muzima.api.model.Patient;
import com.muzima.controller.ConceptController;
import com.muzima.controller.EncounterController;
import com.muzima.controller.ObservationController;
import com.muzima.controller.PatientController;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import static com.muzima.utils.DateUtils.parse;
import static java.util.Arrays.asList;

public class HTMLFormObservationCreator {

    private PatientController patientController;
    private ConceptController conceptController;
    private EncounterController encounterController;
    private ObservationController observationController;
    private ObservationParserUtility observationParserUtility;

    private Patient patient;
    private Encounter encounter;
    private List<Observation> observations;
    private String TAG = "HTMLFormObservationCreator";

    public HTMLFormObservationCreator(PatientController patientController, ConceptController conceptController,
                                      EncounterController encounterController, ObservationController observationController) {
        this.patientController = patientController;
        this.conceptController = conceptController;
        this.encounterController = encounterController;
        this.observationController = observationController;
        this.observationParserUtility = new ObservationParserUtility(conceptController);
    }

    public void createAndPersistObservations(String jsonResponse) {
        parseJSONResponse(jsonResponse);
        try {
            saveObservationsAndRelatedEntities();
        } catch (ConceptController.ConceptSaveException e) {
            Log.e(TAG, "Error while saving concept");
        } catch (EncounterController.SaveEncounterException e) {
            Log.e(TAG, "Error while saving Encounter");
        } catch (ObservationController.SaveObservationException e) {
            Log.e(TAG, "Error while saving Observation");
        } catch (Exception e) {
            Log.e(TAG, "Unexpected Exception occurred" + e);
        }
    }

    public List<Observation> getObservations() {
        return observations;
    }

    private void parseJSONResponse(String jsonResponse) {
        try {
            JSONObject responseJSON = new JSONObject(jsonResponse);
            patient = getPatient(responseJSON.getJSONObject("patient"));
            encounter = createEncounter(responseJSON.getJSONObject("encounter"));
            observations = extractObservationFromJSONObject(responseJSON.getJSONObject("observation"));
        } catch (PatientController.PatientLoadException e) {
            Log.e(TAG, "Error while fetching Patient");
        } catch (ConceptController.ConceptFetchException e) {
            Log.e(TAG, "Error while fetching Concept");
        } catch (JSONException e) {
            Log.e(TAG, "Error while parsing response JSON");
        } catch (ParseException e) {
            Log.e(TAG, "Error while parsing response JSON");
        } catch (ConceptController.ConceptSaveException e) {
            Log.e(TAG, "Error while saving newly created concept");
        }
    }

    private void saveObservationsAndRelatedEntities() throws EncounterController.SaveEncounterException,
            ObservationController.SaveObservationException, ConceptController.ConceptSaveException {
        try {
            encounterController.saveEncounters(asList(encounter));
            conceptController.saveConcepts(observationParserUtility.getNewConceptList());
            observationController.saveObservations(observations);
        } catch (Exception e) {
            Log.e(TAG, "Error while parsing and storing Observations.");
        }
    }

    private List<Observation> extractObservationFromJSONObject(JSONObject jsonObject) throws JSONException, ConceptController.ConceptFetchException, ConceptController.ConceptSaveException {
        List<Observation> observations = new ArrayList<Observation>();
        Iterator keys = jsonObject.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            observations.addAll(extractBasedOnType(jsonObject, key));
        }
        return observations;
    }

    private List<Observation> extractBasedOnType(JSONObject jsonObject, String key) throws JSONException, ConceptController.ConceptFetchException, ConceptController.ConceptSaveException {
        if (jsonObject.get(key) instanceof JSONArray) {
            return createMultipleObservation(key, jsonObject.getJSONArray(key));
        } else if (jsonObject.get(key) instanceof JSONObject) {
            return extractObservationFromJSONObject(jsonObject.getJSONObject(key));
        }
        ArrayList<Observation> observations = new ArrayList<Observation>();
        observations.add(createObservation(key, jsonObject.getString(key)));
        observations.removeAll(Collections.singleton(null));
        return observations;
    }

    private List<Observation> createMultipleObservation(String conceptName, JSONArray jsonArray) throws JSONException,
            ConceptController.ConceptFetchException, ConceptController.ConceptSaveException {
        List<Observation> observations = new ArrayList<Observation>();
        for (int i = 0; i < jsonArray.length(); i++) {
            if (jsonArray.get(i) instanceof JSONObject) {
                observations.addAll(extractObservationFromJSONObject(jsonArray.getJSONObject(i)));
            } else {
                observations.add(createObservation(conceptName, jsonArray.getString(i)));
            }
        }
        return observations;
    }

    private Observation createObservation(String conceptName, String value) throws JSONException, ConceptController.ConceptFetchException, ConceptController.ConceptSaveException {
        Concept concept = observationParserUtility.getConceptEntity(conceptName);
        if (concept == null) {
            return null;
        }
        Observation observation = observationParserUtility.getObservationEntity(concept, value);
        observation.setEncounter(encounter);
        observation.setPerson(patient);
        observation.setObservationDatetime(encounter.getEncounterDatetime());
        return observation;
    }

    private Encounter createEncounter(JSONObject encounterJSON) throws JSONException, ParseException {
        return observationParserUtility.getEncounterEntity(parse(encounterJSON.getString("encounter.encounter_datetime")), patient);
    }

{'fraction_non_alphanumeric': 0.08004717328812559, 'fraction_numerical': 0.0001474165253924965, 'mean_word_length': 3.7045769764216367, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7320654', 'n_tokens_mistral': 3292, 'n_tokens_neox': 3240, 'n_words': 686}