Search in sources :

Example 1 with WordContributionEventGson

use of ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson in project webapp by elimu-ai.

the class WordPeerReviewsRestController method handleGetRequest.

/**
 * Get {@link WordContributionEvent}s pending a {@link WordPeerReviewEvent} for the current {@link Contributor}.
 * <p>
 * Note: Currently The list of Emojis are not delivered in this method compared to the handleGetRequest method in
 * {@link WordPeerReviewsController}
 */
@RequestMapping(method = RequestMethod.GET)
public String handleGetRequest(HttpServletRequest request, HttpServletResponse response) {
    logger.info("handleGetRequest");
    // 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;
    }
    // Get the most recent WordContributionEvent for each Word, including those made by the current Contributor
    List<WordContributionEvent> mostRecentWordContributionEvents = wordContributionEventDao.readMostRecentPerWord();
    logger.info("mostRecentWordContributionEvents.size(): " + mostRecentWordContributionEvents.size());
    // For each WordContributionEvent, check if the Contributor has already performed a peer-review.
    // If not, add it to the list of pending peer reviews.
    JSONArray wordContributionEventsPendingPeerReviewJsonArray = new JSONArray();
    for (WordContributionEvent mostRecentWordContributionEvent : mostRecentWordContributionEvents) {
        // Ignore WordContributionEvents made by the current Contributor
        if (mostRecentWordContributionEvent.getContributor().getId().equals(contributor.getId())) {
            continue;
        }
        // Check if the current Contributor has already peer-reviewed this Word contribution
        List<WordPeerReviewEvent> wordPeerReviewEvents = wordPeerReviewEventDao.readAll(mostRecentWordContributionEvent, contributor);
        if (wordPeerReviewEvents.isEmpty()) {
            WordContributionEventGson mostRecentWordContributionEventGson = JpaToGsonConverter.getWordContributionEventGson(mostRecentWordContributionEvent);
            String json = new Gson().toJson(mostRecentWordContributionEventGson);
            wordContributionEventsPendingPeerReviewJsonArray.put(new JSONObject(json));
        }
    }
    logger.info("wordContributionEventsPendingPeerReviewJsonArray.size(): " + wordContributionEventsPendingPeerReviewJsonArray.length());
    String jsonResponse = wordContributionEventsPendingPeerReviewJsonArray.toString();
    logger.info("jsonResponse: " + jsonResponse);
    return jsonResponse;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) WordContributionEventGson(ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson) Gson(com.google.gson.Gson) WordPeerReviewEventGson(ai.elimu.model.v2.gson.crowdsource.WordPeerReviewEventGson) WordContributionEventGson(ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with WordContributionEventGson

use of ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson in project webapp by elimu-ai.

the class JpaToGsonConverter method getWordContributionEventGson.

public static WordContributionEventGson getWordContributionEventGson(WordContributionEvent wordContributionEvent) {
    if (wordContributionEvent == null) {
        return null;
    } else {
        WordContributionEventGson wordContributionEventGson = new WordContributionEventGson();
        // BaseEntity
        wordContributionEventGson.setId(wordContributionEvent.getId());
        // WordContributionEvent
        wordContributionEventGson.setWord(getWordGson(wordContributionEvent.getWord()));
        wordContributionEventGson.setComment(wordContributionEvent.getComment());
        wordContributionEventGson.setTime(wordContributionEvent.getTime());
        return wordContributionEventGson;
    }
}
Also used : WordContributionEventGson(ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson)

Example 3 with WordContributionEventGson

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

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