Search in sources :

Example 16 with LetterSoundCorrespondence

use of ai.elimu.model.content.LetterSoundCorrespondence in project webapp by elimu-ai.

the class LetterSoundCorrespondenceEditController method handleRequest.

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(Model model, @PathVariable Long id) {
    logger.info("handleRequest");
    LetterSoundCorrespondence letterSoundCorrespondence = letterSoundCorrespondenceDao.read(id);
    model.addAttribute("letterSoundCorrespondence", letterSoundCorrespondence);
    model.addAttribute("timeStart", System.currentTimeMillis());
    List<Letter> letters = letterDao.readAllOrdered();
    model.addAttribute("letters", letters);
    List<Sound> sounds = soundDao.readAllOrdered();
    model.addAttribute("sounds", sounds);
    model.addAttribute("letterSoundCorrespondenceContributionEvents", letterSoundCorrespondenceContributionEventDao.readAll(letterSoundCorrespondence));
    model.addAttribute("letterSoundCorrespondencePeerReviewEvents", letterSoundCorrespondencePeerReviewEventDao.readAll(letterSoundCorrespondence));
    List<Word> words = wordDao.readAllOrderedByUsage();
    model.addAttribute("words", words);
    return "content/letter-sound-correspondence/edit";
}
Also used : Letter(ai.elimu.model.content.Letter) Word(ai.elimu.model.content.Word) Sound(ai.elimu.model.content.Sound) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 17 with LetterSoundCorrespondence

use of ai.elimu.model.content.LetterSoundCorrespondence in project webapp by elimu-ai.

the class LetterSoundCorrespondencePeerReviewEventCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(@RequestParam Long letterSoundCorrespondenceContributionEventId, @RequestParam Boolean approved, @RequestParam(required = false) String comment, HttpSession session) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    logger.info("letterSoundCorrespondenceContributionEventId: " + letterSoundCorrespondenceContributionEventId);
    LetterSoundCorrespondenceContributionEvent letterSoundCorrespondenceContributionEvent = letterSoundCorrespondenceContributionEventDao.read(letterSoundCorrespondenceContributionEventId);
    logger.info("letterSoundCorrespondenceContributionEvent: " + letterSoundCorrespondenceContributionEvent);
    // Store the peer review event
    LetterSoundCorrespondencePeerReviewEvent letterSoundCorrespondencePeerReviewEvent = new LetterSoundCorrespondencePeerReviewEvent();
    letterSoundCorrespondencePeerReviewEvent.setContributor(contributor);
    letterSoundCorrespondencePeerReviewEvent.setLetterSoundCorrespondenceContributionEvent(letterSoundCorrespondenceContributionEvent);
    letterSoundCorrespondencePeerReviewEvent.setApproved(approved);
    letterSoundCorrespondencePeerReviewEvent.setComment(StringUtils.abbreviate(comment, 1000));
    letterSoundCorrespondencePeerReviewEvent.setTime(Calendar.getInstance());
    letterSoundCorrespondencePeerReviewEvent.setPlatform(Platform.WEBAPP);
    letterSoundCorrespondencePeerReviewEventDao.create(letterSoundCorrespondencePeerReviewEvent);
    String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/letterSoundCorrespondence/edit/" + letterSoundCorrespondenceContributionEvent.getLetterSoundCorrespondence().getId();
    DiscordHelper.sendChannelMessage("LetterSoundCorrespondence peer-reviewed: " + contentUrl, "\"" + letterSoundCorrespondenceContributionEvent.getLetterSoundCorrespondence().getLetters().stream().map(Letter::getText).collect(Collectors.joining()) + "\"", "Comment: \"" + letterSoundCorrespondencePeerReviewEvent.getComment() + "\"", letterSoundCorrespondencePeerReviewEvent.isApproved(), null);
    // Update the letterSoundCorrespondence's peer review status
    int approvedCount = 0;
    int notApprovedCount = 0;
    for (LetterSoundCorrespondencePeerReviewEvent peerReviewEvent : letterSoundCorrespondencePeerReviewEventDao.readAll(letterSoundCorrespondenceContributionEvent)) {
        if (peerReviewEvent.isApproved()) {
            approvedCount++;
        } else {
            notApprovedCount++;
        }
    }
    logger.info("approvedCount: " + approvedCount);
    logger.info("notApprovedCount: " + notApprovedCount);
    LetterSoundCorrespondence letterSoundCorrespondence = letterSoundCorrespondenceContributionEvent.getLetterSoundCorrespondence();
    if (approvedCount >= notApprovedCount) {
        letterSoundCorrespondence.setPeerReviewStatus(PeerReviewStatus.APPROVED);
    } else {
        letterSoundCorrespondence.setPeerReviewStatus(PeerReviewStatus.NOT_APPROVED);
    }
    letterSoundCorrespondenceDao.update(letterSoundCorrespondence);
    return "redirect:/content/letter-sound-correspondence/edit/" + letterSoundCorrespondenceContributionEvent.getLetterSoundCorrespondence().getId() + "#contribution-events";
}
Also used : Letter(ai.elimu.model.content.Letter) LetterSoundCorrespondenceContributionEvent(ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent) Contributor(ai.elimu.model.contributor.Contributor) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) LetterSoundCorrespondencePeerReviewEvent(ai.elimu.model.contributor.LetterSoundCorrespondencePeerReviewEvent) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 18 with LetterSoundCorrespondence

use of ai.elimu.model.content.LetterSoundCorrespondence in project webapp by elimu-ai.

the class LetterSoundCorrespondenceUsageCountScheduler method execute.

// At 06:15 every day
@Scheduled(cron = "00 15 06 * * *")
public synchronized void execute() {
    logger.info("execute");
    logger.info("Calculating usage count for LetterSoundCorrespondences");
    // <id, usageCount>
    Map<Long, Integer> letterSoundCorrespondenceFrequencyMap = new HashMap<>();
    List<Word> words = wordDao.readAll();
    logger.info("words.size(): " + words.size());
    for (Word word : words) {
        logger.info("word.getText(): " + word.getText());
        for (LetterSoundCorrespondence letterSoundCorrespondence : word.getLetterSoundCorrespondences()) {
            letterSoundCorrespondenceFrequencyMap.put(letterSoundCorrespondence.getId(), letterSoundCorrespondenceFrequencyMap.getOrDefault(letterSoundCorrespondence.getId(), 0) + word.getUsageCount());
        }
    }
    // Update the values previously stored in the database
    for (LetterSoundCorrespondence letterSoundCorrespondence : letterSoundCorrespondenceDao.readAll()) {
        logger.info("letterSoundCorrespondence.getId(): " + letterSoundCorrespondence.getId());
        logger.info("letterSoundCorrespondence Letters: \"" + letterSoundCorrespondence.getLetters().stream().map(Letter::getText).collect(Collectors.joining()) + "\"");
        logger.info("letterSoundCorrespondence Sounds: /" + letterSoundCorrespondence.getSounds().stream().map(Sound::getValueIpa).collect(Collectors.joining()) + "/");
        logger.info("letterSoundCorrespondence.getUsageCount() (before update): " + letterSoundCorrespondence.getUsageCount());
        int newUsageCount = 0;
        if (letterSoundCorrespondenceFrequencyMap.containsKey(letterSoundCorrespondence.getId())) {
            newUsageCount = letterSoundCorrespondenceFrequencyMap.get(letterSoundCorrespondence.getId());
        }
        logger.info("newUsageCount: " + newUsageCount);
        letterSoundCorrespondence.setUsageCount(newUsageCount);
        letterSoundCorrespondenceDao.update(letterSoundCorrespondence);
        logger.info("letterSoundCorrespondence.getUsageCount() (after update): " + letterSoundCorrespondence.getUsageCount());
    }
    logger.info("execute complete");
}
Also used : Letter(ai.elimu.model.content.Letter) Word(ai.elimu.model.content.Word) HashMap(java.util.HashMap) Sound(ai.elimu.model.content.Sound) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 19 with LetterSoundCorrespondence

use of ai.elimu.model.content.LetterSoundCorrespondence 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 LetterSoundCorrespondence

use of ai.elimu.model.content.LetterSoundCorrespondence in project webapp by elimu-ai.

the class StringToLetterSoundCorrespondenceConverter method convert.

/**
 * Convert LetterSoundCorrespondence id to LetterSoundCorrespondence entity
 */
public LetterSoundCorrespondence convert(String id) {
    if (StringUtils.isBlank(id)) {
        return null;
    } else {
        Long letterSoundCorrespondenceId = Long.parseLong(id);
        LetterSoundCorrespondence letterSoundCorrespondence = letterSoundCorrespondenceDao.read(letterSoundCorrespondenceId);
        return letterSoundCorrespondence;
    }
}
Also used : LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence)

Aggregations

LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)21 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Letter (ai.elimu.model.content.Letter)10 Sound (ai.elimu.model.content.Sound)10 Word (ai.elimu.model.content.Word)7 ArrayList (java.util.ArrayList)7 JSONArray (org.json.JSONArray)6 JSONObject (org.json.JSONObject)5 LetterSoundCorrespondenceContributionEvent (ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent)4 LetterSoundCorrespondenceGson (ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson)4 IOException (java.io.IOException)4 CSVFormat (org.apache.commons.csv.CSVFormat)4 Contributor (ai.elimu.model.contributor.Contributor)3 WordGson (ai.elimu.model.v2.gson.content.WordGson)3 Gson (com.google.gson.Gson)3 WordContributionEvent (ai.elimu.model.contributor.WordContributionEvent)2 WordContributionEventGson (ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson)2 Reader (java.io.Reader)2 StringWriter (java.io.StringWriter)2 Path (java.nio.file.Path)2