Search in sources :

Example 1 with AudioContributionEventGson

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

the class AudioPeerReviewsRestController method listWordRecordingsPendingPeerReview.

/**
 * Get {@link AudioContributionEvent}s pending a {@link AudioPeerReviewEvent} for the current {@link Contributor}.
 */
@RequestMapping(value = "/words", method = RequestMethod.GET)
public String listWordRecordingsPendingPeerReview(HttpServletRequest request, HttpServletResponse response) {
    logger.info("listWordRecordingsPendingPeerReview");
    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;
    }
    List<AudioContributionEvent> allAudioContributionEvents = audioContributionEventDao.readAll();
    logger.info("allAudioContributionEvents.size(): " + allAudioContributionEvents.size());
    // Get the most recent AudioContributionEvent for each Audio
    List<AudioContributionEvent> mostRecentAudioContributionEvents = new ArrayList<>();
    HashMap<Long, Void> idsOfAudiosWithContributionEventHashMap = new HashMap<>();
    for (AudioContributionEvent audioContributionEvent : allAudioContributionEvents) {
        // Ignore AudioContributionEvent if one has already been added for this Audio
        if (idsOfAudiosWithContributionEventHashMap.containsKey(audioContributionEvent.getAudio().getId())) {
            continue;
        }
        // Keep track of the Audio ID
        idsOfAudiosWithContributionEventHashMap.put(audioContributionEvent.getAudio().getId(), null);
        // Ignore AudioContributionEvents made by the current Contributor
        if (audioContributionEvent.getContributor().getId().equals(contributor.getId())) {
            continue;
        }
        mostRecentAudioContributionEvents.add(audioContributionEvent);
    }
    logger.info("mostRecentAudioContributionEvents.size(): " + mostRecentAudioContributionEvents.size());
    // For each AudioContributionEvent, check if the Contributor has already performed a peer review.
    // If not, add it to the list of pending peer reviews.
    List<AudioContributionEvent> audioContributionEventsPendingPeerReview = new ArrayList<>();
    for (AudioContributionEvent mostRecentAudioContributionEvent : mostRecentAudioContributionEvents) {
        AudioPeerReviewEvent audioPeerReviewEvent = audioPeerReviewEventDao.read(mostRecentAudioContributionEvent, contributor);
        if (audioPeerReviewEvent == null) {
            audioContributionEventsPendingPeerReview.add(mostRecentAudioContributionEvent);
        }
    }
    logger.info("audioContributionEventsPendingPeerReview.size(): " + audioContributionEventsPendingPeerReview.size());
    // Convert to JSON
    JSONArray audioContributionEventsJsonArray = new JSONArray();
    for (AudioContributionEvent audioContributionEvent : audioContributionEventsPendingPeerReview) {
        AudioContributionEventGson audioContributionEventGson = JpaToGsonConverter.getAudioContributionEventGson(audioContributionEvent);
        String json = new Gson().toJson(audioContributionEventGson);
        audioContributionEventsJsonArray.put(new JSONObject(json));
    }
    String jsonResponse = audioContributionEventsJsonArray.toString();
    logger.info("jsonResponse: " + jsonResponse);
    return jsonResponse;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) 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) AudioPeerReviewEvent(ai.elimu.model.contributor.AudioPeerReviewEvent) JSONObject(org.json.JSONObject) AudioContributionEventGson(ai.elimu.model.v2.gson.crowdsource.AudioContributionEventGson) AudioContributionEvent(ai.elimu.model.contributor.AudioContributionEvent) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with AudioContributionEventGson

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

Example 3 with AudioContributionEventGson

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

the class JpaToGsonConverter method getAudioContributionEventGson.

public static AudioContributionEventGson getAudioContributionEventGson(AudioContributionEvent audioContributionEvent) {
    if (audioContributionEvent == null) {
        return null;
    } else {
        AudioContributionEventGson audioContributionEventGson = new AudioContributionEventGson();
        // BaseEntity
        audioContributionEventGson.setId(audioContributionEvent.getId());
        // AudioContributionEvent
        audioContributionEventGson.setAudio(getAudioGson(audioContributionEvent.getAudio()));
        audioContributionEventGson.setComment(audioContributionEvent.getComment());
        audioContributionEventGson.setTime(audioContributionEvent.getTime());
        return audioContributionEventGson;
    }
}
Also used : AudioContributionEventGson(ai.elimu.model.v2.gson.crowdsource.AudioContributionEventGson)

Aggregations

AudioContributionEventGson (ai.elimu.model.v2.gson.crowdsource.AudioContributionEventGson)3 AudioContributionEvent (ai.elimu.model.contributor.AudioContributionEvent)2 AudioPeerReviewEvent (ai.elimu.model.contributor.AudioPeerReviewEvent)2 Contributor (ai.elimu.model.contributor.Contributor)2 AudioPeerReviewEventGson (ai.elimu.model.v2.gson.crowdsource.AudioPeerReviewEventGson)2 Gson (com.google.gson.Gson)2 JSONObject (org.json.JSONObject)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Audio (ai.elimu.model.content.multimedia.Audio)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JSONArray (org.json.JSONArray)1