Search in sources :

Example 1 with AudioPeerReviewEvent

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

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

the class AudioPeerReviewEventCreateController method handleSubmit.

/**
 * Note: The logic in this method is similar to the one used at {@link AudioPeerReviewsRestController#uploadAudioPeerReview}
 */
@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(@RequestParam Long audioContributionEventId, @RequestParam Boolean approved, @RequestParam(required = false) String comment, HttpSession session) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    logger.info("audioContributionEventId: " + audioContributionEventId);
    AudioContributionEvent audioContributionEvent = audioContributionEventDao.read(audioContributionEventId);
    logger.info("audioContributionEvent: " + audioContributionEvent);
    // Store the peer review event
    AudioPeerReviewEvent audioPeerReviewEvent = new AudioPeerReviewEvent();
    audioPeerReviewEvent.setContributor(contributor);
    audioPeerReviewEvent.setAudioContributionEvent(audioContributionEvent);
    audioPeerReviewEvent.setApproved(approved);
    audioPeerReviewEvent.setComment(StringUtils.abbreviate(comment, 1000));
    audioPeerReviewEvent.setTime(Calendar.getInstance());
    audioPeerReviewEvent.setPlatform(Platform.WEBAPP);
    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().getTitle() + "\"", "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);
    return "redirect:/content/multimedia/audio/edit/" + audioContributionEvent.getAudio().getId() + "#contribution-events";
}
Also used : Contributor(ai.elimu.model.contributor.Contributor) 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 AudioPeerReviewEvent

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

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