Search in sources :

Example 16 with Contributor

use of ai.elimu.model.contributor.Contributor in project webapp by elimu-ai.

the class ContributorDaoTest method testConstraintViolation.

@Ignore
@Test(expected = ConstraintViolationException.class)
public void testConstraintViolation() {
    Contributor contributor = new Contributor();
    contributorDao.create(contributor);
    logger.info("contributor: " + contributor);
    assertThat(contributor, nullValue());
}
Also used : Contributor(ai.elimu.model.contributor.Contributor) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with Contributor

use of ai.elimu.model.contributor.Contributor 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)

Example 18 with Contributor

use of ai.elimu.model.contributor.Contributor in project webapp by elimu-ai.

the class ContributorsRestController method handleGetRequest.

@RequestMapping(method = RequestMethod.POST)
public String handleGetRequest(@RequestBody String requestBody, HttpServletResponse response) {
    logger.info("handlePostRequest");
    logger.info("requestBody: " + requestBody);
    JSONObject contributorJSONObject = new JSONObject(requestBody);
    logger.info("contributorJSONObject: " + contributorJSONObject);
    String providerIdGoogle = contributorJSONObject.getString("providerIdGoogle");
    String email = contributorJSONObject.getString("email");
    String firstName = contributorJSONObject.getString("firstName");
    String lastName = contributorJSONObject.getString("lastName");
    String imageUrl = contributorJSONObject.getString("imageUrl");
    JSONObject jsonObject = new JSONObject();
    // Look for existing Contributor with matching e-mail address
    Contributor existingContributor = contributorDao.read(email);
    logger.info("existingContributor: " + existingContributor);
    if (existingContributor == null) {
        // Store the Contributor in the database
        Contributor contributor = new Contributor();
        contributor.setRegistrationTime(Calendar.getInstance());
        contributor.setProviderIdGoogle(providerIdGoogle);
        contributor.setEmail(email);
        contributor.setFirstName(firstName);
        contributor.setLastName(lastName);
        contributor.setImageUrl(imageUrl);
        contributor.setRoles(new HashSet<>(Arrays.asList(Role.CONTRIBUTOR)));
        contributorDao.create(contributor);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/contributor/" + contributor.getId();
        String embedThumbnailUrl = null;
        if (StringUtils.isNotBlank(contributor.getImageUrl())) {
            embedThumbnailUrl = contributor.getImageUrl();
        }
        DiscordHelper.sendChannelMessage("Contributor joined via the Crowdsource app: " + contentUrl, contributor.getFirstName() + " " + contributor.getLastName(), null, null, embedThumbnailUrl);
        jsonObject.put("result", "success");
        jsonObject.put("successMessage", "The Contributor was stored in the database");
    } else {
        // Return error message saying that the Contributor has already been created
        logger.warn("The Contributor has already been stored in the database");
        // Update existing contributor with latest values fetched from provider
        if (StringUtils.isNotBlank(providerIdGoogle)) {
            existingContributor.setProviderIdGoogle(providerIdGoogle);
        }
        if (StringUtils.isNotBlank(imageUrl)) {
            existingContributor.setImageUrl(imageUrl);
        }
        if (StringUtils.isNotBlank(firstName)) {
            existingContributor.setFirstName(firstName);
        }
        if (StringUtils.isNotBlank(lastName)) {
            existingContributor.setLastName(lastName);
        }
        contributorDao.update(existingContributor);
        jsonObject.put("result", "error");
        jsonObject.put("errorMessage", "The Contributor has already been stored in the database");
    // response.setStatus(HttpStatus.CONFLICT.value());
    }
    String jsonResponse = jsonObject.toString();
    logger.info("jsonResponse: " + jsonResponse);
    return jsonResponse;
}
Also used : JSONObject(org.json.JSONObject) Contributor(ai.elimu.model.contributor.Contributor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 19 with Contributor

use of ai.elimu.model.contributor.Contributor 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)

Example 20 with Contributor

use of ai.elimu.model.contributor.Contributor in project webapp by elimu-ai.

the class AudioPeerReviewsRestController method uploadAudioPeerReview.

/**
 * Note: The logic in this method is similar to the one used at {@link AudioPeerReviewEventCreateController#handleSubmit}
 */
@RequestMapping(value = "/words", method = RequestMethod.POST)
public String uploadAudioPeerReview(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestBody) {
    logger.info("uploadAudioPeerReview");
    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;
    }
    try {
        // Convert from Gson (POJO) to JPA/Hibernate
        logger.info("requestBody: " + requestBody);
        AudioPeerReviewEventGson audioPeerReviewEventGson = new Gson().fromJson(requestBody, AudioPeerReviewEventGson.class);
        logger.info("audioPeerReviewEventGson: " + audioPeerReviewEventGson);
        AudioContributionEventGson audioContributionEventGson = audioPeerReviewEventGson.getAudioContributionEvent();
        logger.info("audioContributionEventGson: " + audioContributionEventGson);
        Long audioContributionEventGsonId = audioContributionEventGson.getId();
        logger.info("audioContributionEventGsonId: " + audioContributionEventGsonId);
        AudioContributionEvent audioContributionEvent = audioContributionEventDao.read(audioContributionEventGsonId);
        logger.info("audioContributionEvent: " + audioContributionEvent);
        AudioPeerReviewEvent audioPeerReviewEvent = new AudioPeerReviewEvent();
        audioPeerReviewEvent.setContributor(contributor);
        audioPeerReviewEvent.setAudioContributionEvent(audioContributionEvent);
        audioPeerReviewEvent.setApproved(audioPeerReviewEventGson.isApproved());
        audioPeerReviewEvent.setComment(StringUtils.abbreviate(audioPeerReviewEventGson.getComment(), 1000));
        audioPeerReviewEvent.setTime(audioPeerReviewEventGson.getTime());
        audioPeerReviewEvent.setPlatform(Platform.CROWDSOURCE_APP);
        audioPeerReviewEventDao.create(audioPeerReviewEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/multimedia/audio/edit/" + audioContributionEvent.getAudio().getId();
        DiscordHelper.sendChannelMessage("Audio peer-reviewed: " + contentUrl, "\"" + audioContributionEvent.getAudio().getTranscription() + "\"", "Comment: \"" + audioPeerReviewEvent.getComment() + "\"", audioPeerReviewEvent.isApproved(), null);
        // Update the audio's peer review status
        int approvedCount = 0;
        int notApprovedCount = 0;
        for (AudioPeerReviewEvent peerReviewEvent : audioPeerReviewEventDao.readAll(audioContributionEvent)) {
            if (peerReviewEvent.isApproved()) {
                approvedCount++;
            } else {
                notApprovedCount++;
            }
        }
        logger.info("approvedCount: " + approvedCount);
        logger.info("notApprovedCount: " + notApprovedCount);
        Audio audio = audioContributionEvent.getAudio();
        if (approvedCount >= notApprovedCount) {
            audio.setPeerReviewStatus(PeerReviewStatus.APPROVED);
        } else {
            audio.setPeerReviewStatus(PeerReviewStatus.NOT_APPROVED);
        }
        audioDao.update(audio);
    } 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 : JSONObject(org.json.JSONObject) AudioPeerReviewEventGson(ai.elimu.model.v2.gson.crowdsource.AudioPeerReviewEventGson) AudioContributionEventGson(ai.elimu.model.v2.gson.crowdsource.AudioContributionEventGson) Contributor(ai.elimu.model.contributor.Contributor) Gson(com.google.gson.Gson) AudioPeerReviewEventGson(ai.elimu.model.v2.gson.crowdsource.AudioPeerReviewEventGson) AudioContributionEventGson(ai.elimu.model.v2.gson.crowdsource.AudioContributionEventGson) AudioContributionEvent(ai.elimu.model.contributor.AudioContributionEvent) Audio(ai.elimu.model.content.multimedia.Audio) AudioPeerReviewEvent(ai.elimu.model.contributor.AudioPeerReviewEvent) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Contributor (ai.elimu.model.contributor.Contributor)38 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)32 StoryBookContributionEvent (ai.elimu.model.contributor.StoryBookContributionEvent)10 WordContributionEvent (ai.elimu.model.contributor.WordContributionEvent)8 ArrayList (java.util.ArrayList)8 JSONObject (org.json.JSONObject)8 StoryBook (ai.elimu.model.content.StoryBook)7 Word (ai.elimu.model.content.Word)7 Audio (ai.elimu.model.content.multimedia.Audio)6 AudioContributionEvent (ai.elimu.model.contributor.AudioContributionEvent)6 NumberContributionEvent (ai.elimu.model.contributor.NumberContributionEvent)5 HashMap (java.util.HashMap)5 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)4 Gson (com.google.gson.Gson)4 LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)3 AudioPeerReviewEvent (ai.elimu.model.contributor.AudioPeerReviewEvent)3 LetterSoundCorrespondenceContributionEvent (ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 Letter (ai.elimu.model.content.Letter)2