Search in sources :

Example 1 with WordGson

use of ai.elimu.model.v2.gson.content.WordGson in project webapp by elimu-ai.

the class WordsRestController method handleGetRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleGetRequest() {
    logger.info("handleGetRequest");
    JSONArray wordsJsonArray = new JSONArray();
    for (Word word : wordDao.readAllOrdered()) {
        WordGson wordGson = JpaToGsonConverter.getWordGson(word);
        String json = new Gson().toJson(wordGson);
        wordsJsonArray.put(new JSONObject(json));
    }
    String jsonResponse = wordsJsonArray.toString();
    logger.info("jsonResponse: " + jsonResponse);
    return jsonResponse;
}
Also used : WordGson(ai.elimu.model.v2.gson.content.WordGson) Word(ai.elimu.model.content.Word) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) WordGson(ai.elimu.model.v2.gson.content.WordGson) Gson(com.google.gson.Gson) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with WordGson

use of ai.elimu.model.v2.gson.content.WordGson in project webapp by elimu-ai.

the class JpaToGsonConverter method getStoryBookParagraphGson.

public static StoryBookParagraphGson getStoryBookParagraphGson(StoryBookParagraph storyBookParagraph) {
    if (storyBookParagraph == null) {
        return null;
    } else {
        StoryBookParagraphGson storyBookParagraphGson = new StoryBookParagraphGson();
        // BaseEntity
        storyBookParagraphGson.setId(storyBookParagraph.getId());
        // StoryBookParagraph
        storyBookParagraphGson.setSortOrder(storyBookParagraph.getSortOrder());
        storyBookParagraphGson.setOriginalText(storyBookParagraph.getOriginalText());
        List<WordGson> wordGsons = new ArrayList<>();
        for (Word word : storyBookParagraph.getWords()) {
            WordGson wordGson = null;
            if (word != null) {
                wordGson = new WordGson();
                wordGson.setId(word.getId());
            }
            wordGsons.add(wordGson);
        }
        storyBookParagraphGson.setWords(wordGsons);
        return storyBookParagraphGson;
    }
}
Also used : WordGson(ai.elimu.model.v2.gson.content.WordGson) Word(ai.elimu.model.content.Word) StoryBookParagraphGson(ai.elimu.model.v2.gson.content.StoryBookParagraphGson) ArrayList(java.util.ArrayList)

Example 3 with WordGson

use of ai.elimu.model.v2.gson.content.WordGson in project webapp by elimu-ai.

the class JpaToGsonConverter method getEmojiGson.

public static EmojiGson getEmojiGson(Emoji emoji) {
    if (emoji == null) {
        return null;
    } else {
        EmojiGson emojiGson = new EmojiGson();
        // BaseEntity
        emojiGson.setId(emoji.getId());
        // Content
        emojiGson.setRevisionNumber(emoji.getRevisionNumber());
        emojiGson.setUsageCount(emoji.getUsageCount());
        // Emoji
        emojiGson.setGlyph(emoji.getGlyph());
        emojiGson.setUnicodeVersion(emoji.getUnicodeVersion());
        emojiGson.setUnicodeEmojiVersion(emoji.getUnicodeEmojiVersion());
        Set<WordGson> wordGsons = new HashSet<>();
        for (Word word : emoji.getWords()) {
            WordGson wordGson = new WordGson();
            wordGson.setId(word.getId());
            wordGsons.add(wordGson);
        }
        emojiGson.setWords(wordGsons);
        return emojiGson;
    }
}
Also used : EmojiGson(ai.elimu.model.v2.gson.content.EmojiGson) WordGson(ai.elimu.model.v2.gson.content.WordGson) Word(ai.elimu.model.content.Word) HashSet(java.util.HashSet)

Example 4 with WordGson

use of ai.elimu.model.v2.gson.content.WordGson 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 WordGson

use of ai.elimu.model.v2.gson.content.WordGson in project webapp by elimu-ai.

the class AudioContributionsRestController method getWordsPendingRecording.

/**
 * Get {@link Word}s pending {@link Audio} recording for the current {@link Contributor}.
 */
@RequestMapping(value = "/words", method = RequestMethod.GET)
public String getWordsPendingRecording(HttpServletRequest request, HttpServletResponse response) {
    logger.info("getWordsPendingRecording");
    JSONObject jsonObject = new JSONObject();
    // Lookup the Contributor by ID
    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;
    }
    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;
    }
    // Get the IDs of Words that have already been recorded by the Contributor
    List<AudioContributionEvent> audioContributionEvents = audioContributionEventDao.readAll(contributor);
    logger.info("audioContributionEvents.size(): " + audioContributionEvents.size());
    HashMap<Long, Void> idsOfRecordedWordsHashMap = new HashMap<>();
    for (AudioContributionEvent audioContributionEvent : audioContributionEvents) {
        Audio audio = audioContributionEvent.getAudio();
        Word word = audio.getWord();
        if (word != null) {
            idsOfRecordedWordsHashMap.put(word.getId(), null);
        }
    }
    logger.info("idsOfRecordedWordsHashMap.size(): " + idsOfRecordedWordsHashMap.size());
    // For each Word, check if the Contributor has already contributed a
    // corresponding Audio recording. If not, add it to the list of pending recordings.
    List<Word> wordsPendingAudioRecording = new ArrayList<>();
    for (Word word : wordDao.readAllOrderedByUsage()) {
        if (!idsOfRecordedWordsHashMap.containsKey(word.getId())) {
            wordsPendingAudioRecording.add(word);
        }
    }
    logger.info("wordsPendingAudioRecording.size(): " + wordsPendingAudioRecording.size());
    // Convert to JSON
    JSONArray wordsJsonArray = new JSONArray();
    for (Word word : wordsPendingAudioRecording) {
        WordGson wordGson = JpaToGsonConverter.getWordGson(word);
        String json = new Gson().toJson(wordGson);
        wordsJsonArray.put(new JSONObject(json));
    }
    String jsonResponse = wordsJsonArray.toString();
    logger.info("jsonResponse: " + jsonResponse);
    return jsonResponse;
}
Also used : Word(ai.elimu.model.content.Word) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Contributor(ai.elimu.model.contributor.Contributor) Gson(com.google.gson.Gson) WordGson(ai.elimu.model.v2.gson.content.WordGson) WordGson(ai.elimu.model.v2.gson.content.WordGson) JSONObject(org.json.JSONObject) AudioContributionEvent(ai.elimu.model.contributor.AudioContributionEvent) Audio(ai.elimu.model.content.multimedia.Audio) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WordGson (ai.elimu.model.v2.gson.content.WordGson)8 Word (ai.elimu.model.content.Word)7 ArrayList (java.util.ArrayList)4 Gson (com.google.gson.Gson)3 HashSet (java.util.HashSet)3 JSONObject (org.json.JSONObject)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)2 Contributor (ai.elimu.model.contributor.Contributor)2 LetterSoundCorrespondenceGson (ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson)2 JSONArray (org.json.JSONArray)2 Audio (ai.elimu.model.content.multimedia.Audio)1 AudioContributionEvent (ai.elimu.model.contributor.AudioContributionEvent)1 WordContributionEvent (ai.elimu.model.contributor.WordContributionEvent)1 EmojiGson (ai.elimu.model.v2.gson.content.EmojiGson)1 ImageGson (ai.elimu.model.v2.gson.content.ImageGson)1 StoryBookParagraphGson (ai.elimu.model.v2.gson.content.StoryBookParagraphGson)1 VideoGson (ai.elimu.model.v2.gson.content.VideoGson)1 WordContributionEventGson (ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson)1 HashMap (java.util.HashMap)1