Search in sources :

Example 1 with LetterSoundCorrespondenceGson

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;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) LetterSoundCorrespondenceGson(ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson) Gson(com.google.gson.Gson) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) LetterSoundCorrespondenceGson(ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with LetterSoundCorrespondenceGson

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;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) LetterSoundCorrespondenceGson(ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson) Gson(com.google.gson.Gson) WordGson(ai.elimu.model.v2.gson.content.WordGson) WordContributionEventGson(ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) LetterSoundCorrespondenceGson(ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with LetterSoundCorrespondenceGson

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;
    }
}
Also used : Letter(ai.elimu.model.content.Letter) ArrayList(java.util.ArrayList) SoundGson(ai.elimu.model.v2.gson.content.SoundGson) Sound(ai.elimu.model.content.Sound) LetterGson(ai.elimu.model.v2.gson.content.LetterGson) LetterSoundCorrespondenceGson(ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson)

Example 4 with 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;
    }
}
Also used : WordGson(ai.elimu.model.v2.gson.content.WordGson) ArrayList(java.util.ArrayList) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) LetterSoundCorrespondenceGson(ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson)

Example 5 with LetterSoundCorrespondenceGson

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;
}
Also used : Word(ai.elimu.model.content.Word) WordContributionEventGson(ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson) ArrayList(java.util.ArrayList) Contributor(ai.elimu.model.contributor.Contributor) LetterSoundCorrespondenceGson(ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson) Gson(com.google.gson.Gson) WordGson(ai.elimu.model.v2.gson.content.WordGson) WordContributionEventGson(ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson) LetterSoundCorrespondenceGson(ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson) WordGson(ai.elimu.model.v2.gson.content.WordGson) JSONObject(org.json.JSONObject) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) WordContributionEvent(ai.elimu.model.contributor.WordContributionEvent) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

LetterSoundCorrespondenceGson (ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson)5 LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)4 WordGson (ai.elimu.model.v2.gson.content.WordGson)3 Gson (com.google.gson.Gson)3 ArrayList (java.util.ArrayList)3 JSONObject (org.json.JSONObject)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 WordContributionEventGson (ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson)2 JSONArray (org.json.JSONArray)2 Letter (ai.elimu.model.content.Letter)1 Sound (ai.elimu.model.content.Sound)1 Word (ai.elimu.model.content.Word)1 Contributor (ai.elimu.model.contributor.Contributor)1 WordContributionEvent (ai.elimu.model.contributor.WordContributionEvent)1 LetterGson (ai.elimu.model.v2.gson.content.LetterGson)1 SoundGson (ai.elimu.model.v2.gson.content.SoundGson)1