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());
}
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;
}
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;
}
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;
}
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;
}
Aggregations