Search in sources :

Example 11 with Contributor

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

the class StoryBookParagraphDeleteController method handleRequest.

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(HttpSession session, @PathVariable Long id) {
    logger.info("handleRequest");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    logger.info("contributor.getRoles(): " + contributor.getRoles());
    if (!contributor.getRoles().contains(Role.EDITOR)) {
        // TODO: return HttpStatus.FORBIDDEN
        throw new IllegalAccessError("Missing role for access");
    }
    StoryBookParagraph storyBookParagraphToBeDeleted = storyBookParagraphDao.read(id);
    logger.info("storyBookParagraphToBeDeleted: " + storyBookParagraphToBeDeleted);
    logger.info("storyBookParagraphToBeDeleted.getSortOrder(): " + storyBookParagraphToBeDeleted.getSortOrder());
    String paragraphTextBeforeDeletion = storyBookParagraphToBeDeleted.getOriginalText();
    // Delete the paragraph's reference from corresponding audios (if any)
    List<Audio> paragraphAudios = audioDao.readAll(storyBookParagraphToBeDeleted);
    for (Audio paragraphAudio : paragraphAudios) {
        paragraphAudio.setStoryBookParagraph(null);
        audioDao.update(paragraphAudio);
    }
    // Delete the paragraph
    logger.info("Deleting StoryBookParagraph with ID " + storyBookParagraphToBeDeleted.getId());
    storyBookParagraphDao.delete(storyBookParagraphToBeDeleted);
    // Update the storybook's metadata
    StoryBook storyBook = storyBookParagraphToBeDeleted.getStoryBookChapter().getStoryBook();
    storyBook.setTimeLastUpdate(Calendar.getInstance());
    storyBook.setRevisionNumber(storyBook.getRevisionNumber() + 1);
    storyBook.setPeerReviewStatus(PeerReviewStatus.PENDING);
    storyBookDao.update(storyBook);
    // Store contribution event
    StoryBookContributionEvent storyBookContributionEvent = new StoryBookContributionEvent();
    storyBookContributionEvent.setContributor(contributor);
    storyBookContributionEvent.setTime(Calendar.getInstance());
    storyBookContributionEvent.setStoryBook(storyBook);
    storyBookContributionEvent.setRevisionNumber(storyBook.getRevisionNumber());
    storyBookContributionEvent.setComment("Deleted storybook paragraph in chapter " + (storyBookParagraphToBeDeleted.getStoryBookChapter().getSortOrder() + 1) + " (🤖 auto-generated comment)");
    storyBookContributionEvent.setParagraphTextBefore(paragraphTextBeforeDeletion);
    storyBookContributionEvent.setTimeSpentMs(0L);
    storyBookContributionEvent.setPlatform(Platform.WEBAPP);
    storyBookContributionEventDao.create(storyBookContributionEvent);
    String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/storybook/edit/" + storyBook.getId();
    String embedThumbnailUrl = null;
    if (storyBook.getCoverImage() != null) {
        embedThumbnailUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/image/" + storyBook.getCoverImage().getId() + "_r" + storyBook.getCoverImage().getRevisionNumber() + "." + storyBook.getCoverImage().getImageFormat().toString().toLowerCase();
    }
    DiscordHelper.sendChannelMessage("Storybook paragraph deleted: " + contentUrl, "\"" + storyBookContributionEvent.getStoryBook().getTitle() + "\"", "Comment: \"" + storyBookContributionEvent.getComment() + "\"", null, embedThumbnailUrl);
    // Update the sorting order of the remaining paragraphs
    List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookParagraphToBeDeleted.getStoryBookChapter());
    logger.info("storyBookParagraphs.size(): " + storyBookParagraphs.size());
    for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
        logger.info("storyBookParagraph.getId(): " + storyBookParagraph.getId() + ", storyBookParagraph.getSortOrder(): " + storyBookParagraph.getSortOrder());
        if (storyBookParagraph.getSortOrder() > storyBookParagraphToBeDeleted.getSortOrder()) {
            // Reduce sort order by 1
            storyBookParagraph.setSortOrder(storyBookParagraph.getSortOrder() - 1);
            storyBookParagraphDao.update(storyBookParagraph);
            logger.info("storyBookParagraph.getSortOrder() (after update): " + storyBookParagraph.getSortOrder());
        }
    }
    // Refresh the REST API cache
    storyBooksJsonService.refreshStoryBooksJSONArray();
    return "redirect:/content/storybook/edit/" + storyBook.getId() + "#ch-id-" + storyBookParagraphToBeDeleted.getStoryBookChapter().getId();
}
Also used : StoryBookContributionEvent(ai.elimu.model.contributor.StoryBookContributionEvent) StoryBook(ai.elimu.model.content.StoryBook) Contributor(ai.elimu.model.contributor.Contributor) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) Audio(ai.elimu.model.content.multimedia.Audio) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with Contributor

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

the class StoryBookParagraphEditController method handleSubmit.

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(HttpServletRequest request, HttpSession session, @Valid StoryBookParagraph storyBookParagraph, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    if (result.hasErrors()) {
        model.addAttribute("storyBookParagraph", storyBookParagraph);
        model.addAttribute("timeStart", System.currentTimeMillis());
        return "content/storybook/paragraph/edit";
    } else {
        // Fetch previously stored paragraph to make it possible to check if the text was modified or not when
        // storing the StoryBookContributionEvent below.
        StoryBookParagraph storyBookParagraphBeforeEdit = storyBookParagraphDao.read(storyBookParagraph.getId());
        storyBookParagraphDao.update(storyBookParagraph);
        // Update the storybook's metadata
        StoryBook storyBook = storyBookParagraph.getStoryBookChapter().getStoryBook();
        storyBook.setTimeLastUpdate(Calendar.getInstance());
        storyBook.setRevisionNumber(storyBook.getRevisionNumber() + 1);
        storyBook.setPeerReviewStatus(PeerReviewStatus.PENDING);
        storyBookDao.update(storyBook);
        // Store contribution event
        StoryBookContributionEvent storyBookContributionEvent = new StoryBookContributionEvent();
        storyBookContributionEvent.setContributor(contributor);
        storyBookContributionEvent.setTime(Calendar.getInstance());
        storyBookContributionEvent.setStoryBook(storyBook);
        storyBookContributionEvent.setRevisionNumber(storyBook.getRevisionNumber());
        storyBookContributionEvent.setComment("Edited storybook paragraph in chapter " + (storyBookParagraph.getStoryBookChapter().getSortOrder() + 1) + " (🤖 auto-generated comment)");
        if (!storyBookParagraphBeforeEdit.getOriginalText().equals(storyBookParagraph.getOriginalText())) {
            storyBookContributionEvent.setParagraphTextBefore(StringUtils.abbreviate(storyBookParagraphBeforeEdit.getOriginalText(), 1000));
            storyBookContributionEvent.setParagraphTextAfter(StringUtils.abbreviate(storyBookParagraph.getOriginalText(), 1000));
        }
        storyBookContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
        storyBookContributionEvent.setPlatform(Platform.WEBAPP);
        storyBookContributionEventDao.create(storyBookContributionEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/storybook/edit/" + storyBook.getId();
        String embedThumbnailUrl = null;
        if (storyBook.getCoverImage() != null) {
            embedThumbnailUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/image/" + storyBook.getCoverImage().getId() + "_r" + storyBook.getCoverImage().getRevisionNumber() + "." + storyBook.getCoverImage().getImageFormat().toString().toLowerCase();
        }
        DiscordHelper.sendChannelMessage("Storybook paragraph edited: " + contentUrl, "\"" + storyBookContributionEvent.getStoryBook().getTitle() + "\"", "Comment: \"" + storyBookContributionEvent.getComment() + "\"", null, embedThumbnailUrl);
        // Refresh the REST API cache
        storyBooksJsonService.refreshStoryBooksJSONArray();
        return "redirect:/content/storybook/edit/" + storyBookParagraph.getStoryBookChapter().getStoryBook().getId() + "#ch-id-" + storyBookParagraph.getStoryBookChapter().getId();
    }
}
Also used : StoryBookContributionEvent(ai.elimu.model.contributor.StoryBookContributionEvent) StoryBook(ai.elimu.model.content.StoryBook) Contributor(ai.elimu.model.contributor.Contributor) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with Contributor

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

the class WordPeerReviewsController method handleGetRequest.

/**
 * Get {@link WordContributionEvent}s pending a {@link WordPeerReviewEvent} for the current {@link Contributor}.
 */
@RequestMapping(method = RequestMethod.GET)
public String handleGetRequest(HttpSession session, Model model) {
    logger.info("handleGetRequest");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    logger.info("contributor: " + contributor);
    // Get the most recent WordContributionEvent for each Word, including those made by the current Contributor
    List<WordContributionEvent> mostRecentWordContributionEvents = wordContributionEventDao.readMostRecentPerWord();
    logger.info("mostRecentWordContributionEvents.size(): " + mostRecentWordContributionEvents.size());
    // For each WordContributionEvent, check if the Contributor has already performed a peer-review.
    // If not, add it to the list of pending peer reviews.
    List<WordContributionEvent> wordContributionEventsPendingPeerReview = new ArrayList<>();
    for (WordContributionEvent mostRecentWordContributionEvent : mostRecentWordContributionEvents) {
        // Ignore WordContributionEvents made by the current Contributor
        if (mostRecentWordContributionEvent.getContributor().getId().equals(contributor.getId())) {
            continue;
        }
        // Check if the current Contributor has already peer-reviewed this Word contribution
        List<WordPeerReviewEvent> wordPeerReviewEvents = wordPeerReviewEventDao.readAll(mostRecentWordContributionEvent, contributor);
        if (wordPeerReviewEvents.isEmpty()) {
            wordContributionEventsPendingPeerReview.add(mostRecentWordContributionEvent);
        }
    }
    logger.info("wordContributionEventsPendingPeerReview.size(): " + wordContributionEventsPendingPeerReview.size());
    model.addAttribute("wordContributionEventsPendingPeerReview", wordContributionEventsPendingPeerReview);
    model.addAttribute("emojisByWordId", getEmojisByWordId());
    return "content/word/peer-reviews/pending";
}
Also used : WordPeerReviewEvent(ai.elimu.model.contributor.WordPeerReviewEvent) ArrayList(java.util.ArrayList) Contributor(ai.elimu.model.contributor.Contributor) WordContributionEvent(ai.elimu.model.contributor.WordContributionEvent) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with Contributor

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

the class MostRecentContributionsController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model) {
    logger.info("handleRequest");
    List<StoryBookContributionEvent> storyBookContributionEvents = storyBookContributionEventDao.readMostRecent(9);
    logger.info("storyBookContributionEvents.size(): " + storyBookContributionEvents.size());
    model.addAttribute("storyBookContributionEvents", storyBookContributionEvents);
    List<AudioContributionEvent> audioContributionEvents = audioContributionEventDao.readMostRecent(10);
    logger.info("audioContributionEvents.size(): " + audioContributionEvents.size());
    model.addAttribute("audioContributionEvents", audioContributionEvents);
    List<WordContributionEvent> wordContributionEvents = wordContributionEventDao.readMostRecent(10);
    logger.info("wordContributionEvents.size(): " + wordContributionEvents.size());
    model.addAttribute("wordContributionEvents", wordContributionEvents);
    List<NumberContributionEvent> numberContributionEvents = numberContributionEventDao.readMostRecent(10);
    logger.info("numberContributionEvents.size(): " + numberContributionEvents.size());
    model.addAttribute("numberContributionEvents", numberContributionEvents);
    List<Contributor> contributorsWithStoryBookContributions = contributorDao.readAllWithStoryBookContributions();
    logger.info("contributorsWithStoryBookContributions.size(): " + contributorsWithStoryBookContributions.size());
    model.addAttribute("contributorsWithStoryBookContributions", contributorsWithStoryBookContributions);
    Map<Long, Long> storyBookContributionsCountMap = new HashMap<>();
    for (Contributor contributor : contributorsWithStoryBookContributions) {
        storyBookContributionsCountMap.put(contributor.getId(), storyBookContributionEventDao.readCount(contributor));
    }
    model.addAttribute("storyBookContributionsCountMap", storyBookContributionsCountMap);
    List<Contributor> contributorsWithAudioContributions = contributorDao.readAllWithAudioContributions();
    logger.info("contributorsWithAudioContributions.size(): " + contributorsWithAudioContributions.size());
    model.addAttribute("contributorsWithAudioContributions", contributorsWithAudioContributions);
    Map<Long, Long> audioContributionsCountMap = new HashMap<>();
    for (Contributor contributor : contributorsWithAudioContributions) {
        audioContributionsCountMap.put(contributor.getId(), audioContributionEventDao.readCount(contributor));
    }
    model.addAttribute("audioContributionsCountMap", audioContributionsCountMap);
    List<Contributor> contributorsWithWordContributions = contributorDao.readAllWithWordContributions();
    logger.info("contributorsWithWordContributions.size(): " + contributorsWithWordContributions.size());
    model.addAttribute("contributorsWithWordContributions", contributorsWithWordContributions);
    Map<Long, Long> wordContributionsCountMap = new HashMap<>();
    for (Contributor contributor : contributorsWithWordContributions) {
        wordContributionsCountMap.put(contributor.getId(), wordContributionEventDao.readCount(contributor));
    }
    model.addAttribute("wordContributionsCountMap", wordContributionsCountMap);
    List<Contributor> contributorsWithNumberContributions = contributorDao.readAllWithNumberContributions();
    logger.info("contributorsWithNumberContributions.size(): " + contributorsWithNumberContributions.size());
    model.addAttribute("contributorsWithNumberContributions", contributorsWithNumberContributions);
    Map<Long, Long> numberContributionsCountMap = new HashMap<>();
    for (Contributor contributor : contributorsWithNumberContributions) {
        numberContributionsCountMap.put(contributor.getId(), numberContributionEventDao.readCount(contributor));
    }
    model.addAttribute("numberContributionsCountMap", numberContributionsCountMap);
    return "contributions/most-recent";
}
Also used : HashMap(java.util.HashMap) Contributor(ai.elimu.model.contributor.Contributor) StoryBookContributionEvent(ai.elimu.model.contributor.StoryBookContributionEvent) NumberContributionEvent(ai.elimu.model.contributor.NumberContributionEvent) AudioContributionEvent(ai.elimu.model.contributor.AudioContributionEvent) WordContributionEvent(ai.elimu.model.contributor.WordContributionEvent) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with Contributor

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

the class StoryBookChapterCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpSession session, @PathVariable Long storyBookId, @Valid StoryBookChapter storyBookChapter, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    if (result.hasErrors()) {
        model.addAttribute("storyBookChapter", storyBookChapter);
        List<Image> images = imageDao.readAllOrdered();
        model.addAttribute("images", images);
        return "content/storybook/chapter/create";
    } else {
        storyBookChapterDao.create(storyBookChapter);
        // Update the storybook's metadata
        StoryBook storyBook = storyBookChapter.getStoryBook();
        storyBook.setTimeLastUpdate(Calendar.getInstance());
        storyBook.setRevisionNumber(storyBook.getRevisionNumber() + 1);
        storyBook.setPeerReviewStatus(PeerReviewStatus.PENDING);
        storyBookDao.update(storyBook);
        // Store contribution event
        StoryBookContributionEvent storyBookContributionEvent = new StoryBookContributionEvent();
        storyBookContributionEvent.setContributor(contributor);
        storyBookContributionEvent.setTime(Calendar.getInstance());
        storyBookContributionEvent.setStoryBook(storyBook);
        storyBookContributionEvent.setRevisionNumber(storyBook.getRevisionNumber());
        storyBookContributionEvent.setComment("Created storybook chapter " + (storyBookChapter.getSortOrder() + 1) + " (🤖 auto-generated comment)");
        storyBookContributionEvent.setTimeSpentMs(0L);
        storyBookContributionEvent.setPlatform(Platform.WEBAPP);
        storyBookContributionEventDao.create(storyBookContributionEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/storybook/edit/" + storyBook.getId();
        String embedThumbnailUrl = null;
        if (storyBook.getCoverImage() != null) {
            embedThumbnailUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/image/" + storyBook.getCoverImage().getId() + "_r" + storyBook.getCoverImage().getRevisionNumber() + "." + storyBook.getCoverImage().getImageFormat().toString().toLowerCase();
        }
        DiscordHelper.sendChannelMessage("Storybook chapter created: " + contentUrl, "\"" + storyBookContributionEvent.getStoryBook().getTitle() + "\"", "Comment: \"" + storyBookContributionEvent.getComment() + "\"", null, embedThumbnailUrl);
        return "redirect:/content/storybook/edit/" + storyBookId + "#ch-id-" + storyBookChapter.getId();
    }
}
Also used : StoryBookContributionEvent(ai.elimu.model.contributor.StoryBookContributionEvent) StoryBook(ai.elimu.model.content.StoryBook) Contributor(ai.elimu.model.contributor.Contributor) Image(ai.elimu.model.content.multimedia.Image) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Contributor (ai.elimu.model.contributor.Contributor)38 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)32 StoryBookContributionEvent (ai.elimu.model.contributor.StoryBookContributionEvent)10 WordContributionEvent (ai.elimu.model.contributor.WordContributionEvent)8 ArrayList (java.util.ArrayList)8 JSONObject (org.json.JSONObject)8 StoryBook (ai.elimu.model.content.StoryBook)7 Word (ai.elimu.model.content.Word)7 Audio (ai.elimu.model.content.multimedia.Audio)6 AudioContributionEvent (ai.elimu.model.contributor.AudioContributionEvent)6 NumberContributionEvent (ai.elimu.model.contributor.NumberContributionEvent)5 HashMap (java.util.HashMap)5 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)4 Gson (com.google.gson.Gson)4 LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)3 AudioPeerReviewEvent (ai.elimu.model.contributor.AudioPeerReviewEvent)3 LetterSoundCorrespondenceContributionEvent (ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 Letter (ai.elimu.model.content.Letter)2