-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #724 from FEMR/temr-refactor-harrison
refactored response map in TranslateGet() using the Break out Method Object
- Loading branch information
Showing
2 changed files
with
62 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |