From 2c4240e0a43457d3f73eccdb23a5d8729101d2a4 Mon Sep 17 00:00:00 2001 From: Harrison Shu Date: Thu, 9 May 2024 10:15:23 -0700 Subject: [PATCH] refactored response map using the Break out Method Object --- .../ui/controllers/MedicalController.java | 23 ++------ .../translation/TranslationResponseMap.java | 56 +++++++++++++++++++ 2 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 app/femr/util/translation/TranslationResponseMap.java diff --git a/app/femr/ui/controllers/MedicalController.java b/app/femr/ui/controllers/MedicalController.java index 4d7221b20..78744ff59 100644 --- a/app/femr/ui/controllers/MedicalController.java +++ b/app/femr/ui/controllers/MedicalController.java @@ -19,6 +19,7 @@ import femr.util.DataStructure.Mapping.TabFieldMultiMap; import femr.util.DataStructure.Mapping.VitalMultiMap; import femr.util.stringhelpers.StringUtils; +import femr.util.translation.TranslationResponseMap; import play.data.Form; import play.data.FormFactory; import play.libs.Json; @@ -267,32 +268,19 @@ public Result translateGet() { // retrieve current patient encounter encounter int patientId = Integer.parseInt(request().getQueryString("patientId")); ServiceResponse currentEncounterByPatientId = searchService.retrieveRecentPatientEncounterItemByPatientId(patientId); - if (currentEncounterByPatientId.hasErrors()) { throw new RuntimeException(); } PatientEncounterItem patientEncounter = currentEncounterByPatientId.getResponseObject(); String fromLanguage = patientEncounter.getLanguageCode(); - // add whether the language is rtl to response - List rtlLanguages = Arrays.asList("he", "ar"); - Map responseMap = new HashMap<>(); - responseMap.put("toLanguageIsRtl", rtlLanguages.contains(toLanguage)); - responseMap.put("fromLanguageIsRtl", rtlLanguages.contains(fromLanguage)); - - // if same to same (like en to en) don't translate - if (Objects.equals(toLanguage, fromLanguage)) { - responseMap.put("translation", "SameToSame"); - } else { - responseMap.put("translation", translate(text, fromLanguage,toLanguage)); - } + // Harrison Shu: Handles the creation of the response map and figures out whether or not to translate + TranslationResponseMap responseMapObject = new TranslationResponseMap(fromLanguage, toLanguage, text); - return ok(Json.toJson(responseMap)); + return ok(responseMapObject.getResponseJson()); } -// Calls Python Script to translate - private String translate(String text, String fromLanguage, String toLanguage) { - + public String translate(String text, String fromLanguage, String toLanguage) { String data = ""; try { data = BackEndControllerHelper.translate(text, fromLanguage, toLanguage); @@ -302,6 +290,7 @@ private String translate(String text, String fromLanguage, String toLanguage) { return data; } + /** * Get the populated partial view that represents 1 row of new prescription fields * - meant to be an AJAX call diff --git a/app/femr/util/translation/TranslationResponseMap.java b/app/femr/util/translation/TranslationResponseMap.java new file mode 100644 index 000000000..d67ada956 --- /dev/null +++ b/app/femr/util/translation/TranslationResponseMap.java @@ -0,0 +1,56 @@ +package femr.util.translation; +import femr.ui.controllers.BackEndControllerHelper; +import java.util.*; +import play.libs.Json; + + +// Harrison Shu +// represents the Response item that is returned from the backend Java GET Endpoint +public class TranslationResponseMap { + List rtlLanguages = Arrays.asList("he", "ar"); + private String toLanguage; + private String fromLanguage; + private String text; + Map responseMap = new HashMap<>(); + + public TranslationResponseMap(String fromLanguage, String toLanguage, String text) { + this.fromLanguage = fromLanguage; + this.toLanguage = toLanguage; + this.text = text; + this.populateResponseMap(); + } + + private void populateResponseMap() { + populateIsRtl(); + populateTranslation(); + } + + private void populateIsRtl() { + responseMap.put("toLanguageIsRtl", rtlLanguages.contains(toLanguage)); + responseMap.put("fromLanguageIsRtl", rtlLanguages.contains(fromLanguage)); + } + + private void populateTranslation() { + // if same to same (like en to en) don't translate + if (Objects.equals(toLanguage, fromLanguage)) { + responseMap.put("translation", "SameToSame"); + } else { + String data = ""; + try { + data = BackEndControllerHelper.translate(text, fromLanguage, toLanguage); + } catch (Exception e) { + e.printStackTrace(); + } + responseMap.put("translation", data); + } + } + + private Map getResponseMap() { + return this.responseMap; + } + + public com.fasterxml.jackson.databind.JsonNode getResponseJson() { + return Json.toJson(responseMap); + } + +}