use of ai.elimu.model.v2.gson.content.WordGson 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;
}
use of ai.elimu.model.v2.gson.content.WordGson in project webapp by elimu-ai.
the class JpaToGsonConverter method getImageGson.
public static ImageGson getImageGson(Image image) {
if (image == null) {
return null;
} else {
ImageGson imageGson = new ImageGson();
// BaseEntity
imageGson.setId(image.getId());
// Content
imageGson.setRevisionNumber(image.getRevisionNumber());
imageGson.setUsageCount(image.getUsageCount());
// Image
imageGson.setTitle(image.getTitle());
imageGson.setImageFormat(image.getImageFormat());
imageGson.setBytesUrl("/image/" + image.getId() + "_r" + image.getRevisionNumber() + "." + image.getImageFormat().toString().toLowerCase());
imageGson.setBytesSize(image.getBytes().length / 1024);
Set<WordGson> wordGsons = new HashSet<>();
for (Word word : image.getWords()) {
WordGson wordGson = new WordGson();
wordGson.setId(word.getId());
wordGsons.add(wordGson);
}
imageGson.setWords(wordGsons);
return imageGson;
}
}
use of ai.elimu.model.v2.gson.content.WordGson in project webapp by elimu-ai.
the class JpaToGsonConverter method getVideoGson.
public static VideoGson getVideoGson(Video video) {
if (video == null) {
return null;
} else {
VideoGson videoGson = new VideoGson();
// BaseEntity
videoGson.setId(video.getId());
// Content
videoGson.setRevisionNumber(video.getRevisionNumber());
videoGson.setUsageCount(video.getUsageCount());
// Video
videoGson.setTitle(video.getTitle());
videoGson.setVideoFormat(video.getVideoFormat());
videoGson.setBytesUrl("/video/" + video.getId() + "_r" + video.getRevisionNumber() + "." + video.getVideoFormat().toString().toLowerCase());
videoGson.setBytesSize(video.getBytes().length / 1024);
Set<WordGson> wordGsons = new HashSet<>();
for (Word word : video.getWords()) {
WordGson wordGson = new WordGson();
wordGson.setId(word.getId());
wordGsons.add(wordGson);
}
videoGson.setWords(wordGsons);
return videoGson;
}
}
Aggregations