use of ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson in project webapp by elimu-ai.
the class LetterSoundCorrespondencesRestController method handleGetRequest.
@RequestMapping(method = RequestMethod.GET)
public String handleGetRequest() {
logger.info("handleGetRequest");
JSONArray letterSoundCorrespondencesJsonArray = new JSONArray();
for (LetterSoundCorrespondence letterSoundCorrespondence : letterSoundCorrespondenceDao.readAllOrderedByUsage()) {
LetterSoundCorrespondenceGson letterSoundCorrespondenceGson = JpaToGsonConverter.getLetterSoundCorrespondenceGson(letterSoundCorrespondence);
String json = new Gson().toJson(letterSoundCorrespondenceGson);
letterSoundCorrespondencesJsonArray.put(new JSONObject(json));
}
String jsonResponse = letterSoundCorrespondencesJsonArray.toString();
logger.info("jsonResponse: " + jsonResponse);
return jsonResponse;
}
use of ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson in project webapp by elimu-ai.
the class WordContributionRestController method getLetterSoundCorrespondences.
/**
* Returns a list of {@link LetterSoundCorrespondence}s that will be used to construct a {@link Word}.
*/
@RequestMapping(value = "/letter-sound-correspondences", method = RequestMethod.GET)
public String getLetterSoundCorrespondences(HttpServletRequest request, HttpServletResponse response) {
logger.info("getLetterSoundCorrespondences");
JSONArray letterSoundCorrespondencesJsonArray = new JSONArray();
for (LetterSoundCorrespondence letterSoundCorrespondence : letterSoundCorrespondenceDao.readAllOrderedByUsage()) {
LetterSoundCorrespondenceGson letterSoundCorrespondenceGson = JpaToGsonConverter.getLetterSoundCorrespondenceGson(letterSoundCorrespondence);
String json = new Gson().toJson(letterSoundCorrespondenceGson);
letterSoundCorrespondencesJsonArray.put(new JSONObject(json));
}
String jsonResponse = letterSoundCorrespondencesJsonArray.toString();
logger.info("jsonResponse: " + jsonResponse);
return jsonResponse;
}
use of ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson in project webapp by elimu-ai.
the class JpaToGsonConverter method getLetterSoundCorrespondenceGson.
public static LetterSoundCorrespondenceGson getLetterSoundCorrespondenceGson(LetterSoundCorrespondence letterSoundCorrespondence) {
if (letterSoundCorrespondence == null) {
return null;
} else {
LetterSoundCorrespondenceGson letterSoundCorrespondenceGson = new LetterSoundCorrespondenceGson();
// BaseEntity
letterSoundCorrespondenceGson.setId(letterSoundCorrespondence.getId());
// LetterSoundCorrespondence
List<LetterGson> letters = new ArrayList<>();
for (Letter letter : letterSoundCorrespondence.getLetters()) {
LetterGson letterGson = getLetterGson(letter);
letters.add(letterGson);
}
letterSoundCorrespondenceGson.setLetters(letters);
List<SoundGson> sounds = new ArrayList<>();
for (Sound sound : letterSoundCorrespondence.getSounds()) {
SoundGson soundGson = getSoundGson(sound);
sounds.add(soundGson);
}
letterSoundCorrespondenceGson.setSounds(sounds);
letterSoundCorrespondenceGson.setUsageCount(letterSoundCorrespondence.getUsageCount());
return letterSoundCorrespondenceGson;
}
}
use of ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson in project webapp by elimu-ai.
the class JpaToGsonConverter method getWordGson.
public static WordGson getWordGson(Word word) {
if (word == null) {
return null;
} else {
WordGson wordGson = new WordGson();
// BaseEntity
wordGson.setId(word.getId());
// Content
wordGson.setRevisionNumber(word.getRevisionNumber());
wordGson.setUsageCount(word.getUsageCount());
// Word
wordGson.setText(word.getText());
List<LetterSoundCorrespondenceGson> letterSoundCorrespondences = new ArrayList<>();
for (LetterSoundCorrespondence letterSoundCorrespondence : word.getLetterSoundCorrespondences()) {
LetterSoundCorrespondenceGson letterSoundCorrespondenceGson = getLetterSoundCorrespondenceGson(letterSoundCorrespondence);
letterSoundCorrespondences.add(letterSoundCorrespondenceGson);
}
wordGson.setLetterSoundCorrespondences(letterSoundCorrespondences);
wordGson.setWordType(word.getWordType());
return wordGson;
}
}
use of ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson in project webapp by elimu-ai.
the class WordContributionRestController method postWordContribution.
/**
* Handles the creation of a new {@link Word} & the corresponding {@link WordContributionEvent}.
*
* @param requestBody JSON should contain fields required for the creation of a {@link WordContributionEventGson}
* object.
*/
@RequestMapping(value = "/word", method = RequestMethod.POST)
public String postWordContribution(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestBody) {
logger.info("postWordContribution");
// Validate the Contributor.
JSONObject jsonObject = new JSONObject();
String providerIdGoogle = request.getHeader("providerIdGoogle");
logger.info("providerIdGoogle: " + providerIdGoogle);
if (StringUtils.isBlank(providerIdGoogle)) {
jsonObject.put("result", "error");
jsonObject.put("errorMessage", "Missing providerIdGoogle");
response.setStatus(HttpStatus.BAD_REQUEST.value());
String jsonResponse = jsonObject.toString();
logger.info("jsonResponse: " + jsonResponse);
return jsonResponse;
}
// Lookup the Contributor by ID
Contributor contributor = contributorDao.readByProviderIdGoogle(providerIdGoogle);
logger.info("contributor: " + contributor);
if (contributor == null) {
jsonObject.put("result", "error");
jsonObject.put("errorMessage", "The Contributor was not found.");
response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());
String jsonResponse = jsonObject.toString();
logger.info("jsonResponse: " + jsonResponse);
return jsonResponse;
}
logger.info("requestBody: " + requestBody);
// Convert the request body to a WordContributionEventGson
WordContributionEventGson wordContributionEventGson = new Gson().fromJson(requestBody, WordContributionEventGson.class);
logger.info("wordContributionEventGson: " + wordContributionEventGson);
// Extract the WordGson from the WordContributionEventGson
WordGson wordGson = wordContributionEventGson.getWord();
logger.info("wordGson: " + wordGson);
// Check if the word is already existing.
Word existingWord = wordDao.readByText(wordGson.getText().toLowerCase());
if (existingWord != null) {
jsonObject.put("result", "error");
jsonObject.put("errorMessage", "NonUnique");
response.setStatus(HttpStatus.CONFLICT.value());
String jsonResponse = jsonObject.toString();
logger.info("jsonResponse: " + jsonResponse);
return jsonResponse;
}
try {
// Convert the WordGson to Word POJO.
Word word = new Word();
word.setWordType(wordGson.getWordType());
word.setText(wordGson.getText().toLowerCase());
List<LetterSoundCorrespondenceGson> letterSoundCorrespondencesGsons = wordGson.getLetterSoundCorrespondences();
List<LetterSoundCorrespondence> letterSoundCorrespondences = new ArrayList<>();
for (LetterSoundCorrespondenceGson letterSoundCorrespondenceGson : letterSoundCorrespondencesGsons) {
LetterSoundCorrespondence letterSoundCorrespondence = letterSoundCorrespondenceDao.read(letterSoundCorrespondenceGson.getId());
letterSoundCorrespondences.add(letterSoundCorrespondence);
}
word.setLetterSoundCorrespondences(letterSoundCorrespondences);
wordDao.create(word);
WordContributionEvent wordContributionEvent = new WordContributionEvent();
wordContributionEvent.setContributor(contributor);
wordContributionEvent.setTime(wordContributionEventGson.getTime());
wordContributionEvent.setWord(word);
wordContributionEvent.setRevisionNumber(word.getRevisionNumber());
wordContributionEvent.setComment(StringUtils.abbreviate(wordContributionEventGson.getComment(), 1000));
wordContributionEvent.setTimeSpentMs(System.currentTimeMillis() - wordContributionEvent.getTime().getTimeInMillis());
// TODO: wordContributionEvent.setTimeSpentMs(wordContributionEventGson.getTimeSpentMs());
// refer to: https://github.com/elimu-ai/webapp/pull/1289#discussion_r642024541
wordContributionEvent.setPlatform(Platform.CROWDSOURCE_APP);
wordContributionEventDao.create(wordContributionEvent);
String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/word/edit/" + word.getId();
DiscordHelper.sendChannelMessage("Word created: " + contentUrl, "\"" + wordContributionEvent.getWord().getText() + "\"", "Comment: \"" + wordContributionEvent.getComment() + "\"", null, null);
response.setStatus(HttpStatus.CREATED.value());
} catch (Exception ex) {
logger.error(ex);
jsonObject.put("result", "error");
jsonObject.put("errorMessage", ex.getMessage());
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
}
String jsonResponse = jsonObject.toString();
logger.info("jsonResponse: " + jsonResponse);
return jsonResponse;
}
Aggregations