use of ai.elimu.model.v2.gson.crowdsource.AudioPeerReviewEventGson 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;
}
Aggregations