Skip to content

Commit

Permalink
refactored response map using the Break out Method Object
Browse files Browse the repository at this point in the history
  • Loading branch information
Har150n committed May 9, 2024
1 parent 5e01adf commit 2c4240e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
23 changes: 6 additions & 17 deletions app/femr/ui/controllers/MedicalController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -267,32 +268,19 @@ public Result translateGet() {
// retrieve current patient encounter encounter
int patientId = Integer.parseInt(request().getQueryString("patientId"));
ServiceResponse<PatientEncounterItem> 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<String> rtlLanguages = Arrays.asList("he", "ar");
Map<String, Object> 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);
Expand All @@ -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
Expand Down
56 changes: 56 additions & 0 deletions app/femr/util/translation/TranslationResponseMap.java
Original file line number Diff line number Diff line change
@@ -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<String> rtlLanguages = Arrays.asList("he", "ar");
private String toLanguage;
private String fromLanguage;
private String text;
Map<String, Object> 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<String, Object> getResponseMap() {
return this.responseMap;
}

public com.fasterxml.jackson.databind.JsonNode getResponseJson() {
return Json.toJson(responseMap);
}

}

0 comments on commit 2c4240e

Please sign in to comment.