Search in sources :

Example 11 with StoryBookContributionEvent

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

the class StoryBookEditController method handleSubmit.

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(@Valid StoryBook storyBook, BindingResult result, Model model, HttpServletRequest request, HttpSession session) {
    logger.info("handleSubmit");
    StoryBook existingStoryBook = storyBookDao.readByTitle(storyBook.getTitle());
    if ((existingStoryBook != null) && !existingStoryBook.getId().equals(storyBook.getId())) {
        result.rejectValue("title", "NonUnique");
    }
    if (result.hasErrors()) {
        model.addAttribute("storyBook", storyBook);
        model.addAttribute("timeStart", System.currentTimeMillis());
        model.addAttribute("contentLicenses", ContentLicense.values());
        List<Image> coverImages = imageDao.readAllOrdered();
        model.addAttribute("coverImages", coverImages);
        model.addAttribute("readingLevels", ReadingLevel.values());
        List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
        model.addAttribute("storyBookChapters", storyBookChapters);
        // Map<StoryBookChapter.id, List<StoryBookParagraph>>
        Map<Long, List<StoryBookParagraph>> paragraphsPerStoryBookChapterMap = new HashMap<>();
        for (StoryBookChapter storyBookChapter : storyBookChapters) {
            paragraphsPerStoryBookChapterMap.put(storyBookChapter.getId(), storyBookParagraphDao.readAll(storyBookChapter));
        }
        model.addAttribute("paragraphsPerStoryBookChapterMap", paragraphsPerStoryBookChapterMap);
        List<String> paragraphs = new ArrayList<>();
        for (StoryBookChapter storyBookChapter : storyBookChapters) {
            List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookChapter);
            for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
                paragraphs.add(storyBookParagraph.getOriginalText());
            }
        }
        model.addAttribute("storyBookContributionEvents", storyBookContributionEventDao.readAll(storyBook));
        model.addAttribute("storyBookPeerReviewEvents", storyBookPeerReviewEventDao.readAll(storyBook));
        Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
        Map<String, Integer> wordFrequencyMap = WordFrequencyHelper.getWordFrequency(paragraphs, language);
        model.addAttribute("wordFrequencyMap", wordFrequencyMap);
        Map<String, Word> wordMap = new HashMap<>();
        for (Word word : wordDao.readAllOrdered()) {
            wordMap.put(word.getText(), word);
        }
        model.addAttribute("wordMap", wordMap);
        model.addAttribute("emojisByWordId", getEmojisByWordId());
        Map<String, Integer> letterFrequencyMap = LetterFrequencyHelper.getLetterFrequency(paragraphs, language);
        model.addAttribute("letterFrequencyMap", letterFrequencyMap);
        Map<String, Letter> letterMap = new HashMap<>();
        for (Letter letter : letterDao.readAllOrdered()) {
            letterMap.put(letter.getText(), letter);
        }
        model.addAttribute("letterMap", letterMap);
        return "content/storybook/edit";
    } else {
        storyBook.setTimeLastUpdate(Calendar.getInstance());
        storyBook.setRevisionNumber(storyBook.getRevisionNumber() + 1);
        storyBookDao.update(storyBook);
        StoryBookContributionEvent storyBookContributionEvent = new StoryBookContributionEvent();
        storyBookContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
        storyBookContributionEvent.setTime(Calendar.getInstance());
        storyBookContributionEvent.setStoryBook(storyBook);
        storyBookContributionEvent.setRevisionNumber(storyBook.getRevisionNumber());
        storyBookContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 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 edited: " + contentUrl, "\"" + storyBookContributionEvent.getStoryBook().getTitle() + "\"", "Comment: \"" + storyBookContributionEvent.getComment() + "\"", null, embedThumbnailUrl);
        // Refresh REST API cache
        storyBooksJsonService.refreshStoryBooksJSONArray();
        return "redirect:/content/storybook/list#" + storyBook.getId();
    }
}
Also used : StoryBookChapter(ai.elimu.model.content.StoryBookChapter) StoryBook(ai.elimu.model.content.StoryBook) Word(ai.elimu.model.content.Word) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) Image(ai.elimu.model.content.multimedia.Image) Letter(ai.elimu.model.content.Letter) StoryBookContributionEvent(ai.elimu.model.contributor.StoryBookContributionEvent) Language(ai.elimu.model.v2.enums.Language) ArrayList(java.util.ArrayList) List(java.util.List) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with StoryBookContributionEvent

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

the class StoryBookChapterDeleteController method handleRequest.

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(HttpSession session, @PathVariable Long storyBookId, @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");
    }
    StoryBookChapter storyBookChapterToBeDeleted = storyBookChapterDao.read(id);
    logger.info("storyBookChapterToBeDeleted: " + storyBookChapterToBeDeleted);
    logger.info("storyBookChapterToBeDeleted.getSortOrder(): " + storyBookChapterToBeDeleted.getSortOrder());
    // Delete the chapter's paragraphs
    List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookChapterToBeDeleted);
    logger.info("storyBookParagraphs.size(): " + storyBookParagraphs.size());
    for (StoryBookParagraph storyBookParagraphToBeDeleted : storyBookParagraphs) {
        // 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);
        }
        logger.info("Deleting StoryBookParagraph with ID " + storyBookParagraphToBeDeleted.getId());
        storyBookParagraphDao.delete(storyBookParagraphToBeDeleted);
    }
    // Delete the chapter
    logger.info("Deleting StoryBookChapter with ID " + storyBookChapterToBeDeleted.getId());
    storyBookChapterDao.delete(storyBookChapterToBeDeleted);
    // Delete the chapter's image (if any)
    Image chapterImage = storyBookChapterToBeDeleted.getImage();
    logger.info("chapterImage: " + chapterImage);
    if (chapterImage != null) {
        // Remove content labels
        chapterImage.setLiteracySkills(null);
        chapterImage.setNumeracySkills(null);
        chapterImage.setLetters(null);
        chapterImage.setNumbers(null);
        chapterImage.setWords(null);
        imageDao.update(chapterImage);
        // Remove contribution events
        for (ImageContributionEvent imageContributionEvent : imageContributionEventDao.readAll(chapterImage)) {
            logger.warn("Deleting ImageContributionEvent from the database");
            imageContributionEventDao.delete(imageContributionEvent);
        }
        logger.warn("Deleting the chapter image from the database");
        imageDao.delete(chapterImage);
    }
    // Update the StoryBook's metadata
    StoryBook storyBook = storyBookChapterToBeDeleted.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 chapter " + (storyBookChapterToBeDeleted.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 deleted: " + contentUrl, "\"" + storyBookContributionEvent.getStoryBook().getTitle() + "\"", "Comment: \"" + storyBookContributionEvent.getComment() + "\"", null, embedThumbnailUrl);
    // Update the sorting order of the remaining chapters
    List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
    logger.info("storyBookChapters.size(): " + storyBookChapters.size());
    for (StoryBookChapter storyBookChapter : storyBookChapters) {
        logger.info("storyBookChapter.getId(): " + storyBookChapter.getId() + ", storyBookChapter.getSortOrder(): " + storyBookChapter.getSortOrder());
        if (storyBookChapter.getSortOrder() > storyBookChapterToBeDeleted.getSortOrder()) {
            // Reduce sort order by 1
            storyBookChapter.setSortOrder(storyBookChapter.getSortOrder() - 1);
            storyBookChapterDao.update(storyBookChapter);
            logger.info("storyBookChapter.getSortOrder() (after update): " + storyBookChapter.getSortOrder());
        }
    }
    // Refresh the REST API cache
    storyBooksJsonService.refreshStoryBooksJSONArray();
    return "redirect:/content/storybook/edit/" + storyBookId;
}
Also used : StoryBookChapter(ai.elimu.model.content.StoryBookChapter) StoryBook(ai.elimu.model.content.StoryBook) ImageContributionEvent(ai.elimu.model.contributor.ImageContributionEvent) Contributor(ai.elimu.model.contributor.Contributor) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) Image(ai.elimu.model.content.multimedia.Image) StoryBookContributionEvent(ai.elimu.model.contributor.StoryBookContributionEvent) Audio(ai.elimu.model.content.multimedia.Audio) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with StoryBookContributionEvent

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

the class StoryBookParagraphCreateController method handleSubmit.

@RequestMapping(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("audios", audioDao.readAllOrderedByTitle());
        model.addAttribute("timeStart", request.getParameter("timeStart"));
        return "content/storybook/paragraph/create";
    } else {
        storyBookParagraphDao.create(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("Created storybook paragraph in chapter " + (storyBookParagraph.getStoryBookChapter().getSortOrder() + 1) + " (🤖 auto-generated comment)");
        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 created: " + 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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

StoryBookContributionEvent (ai.elimu.model.contributor.StoryBookContributionEvent)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 Contributor (ai.elimu.model.contributor.Contributor)10 StoryBook (ai.elimu.model.content.StoryBook)9 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)6 Image (ai.elimu.model.content.multimedia.Image)5 StoryBookChapter (ai.elimu.model.content.StoryBookChapter)4 ArrayList (java.util.ArrayList)4 NumberContributionEvent (ai.elimu.model.contributor.NumberContributionEvent)3 WordContributionEvent (ai.elimu.model.contributor.WordContributionEvent)3 HashMap (java.util.HashMap)3 Letter (ai.elimu.model.content.Letter)2 Word (ai.elimu.model.content.Word)2 Audio (ai.elimu.model.content.multimedia.Audio)2 StoryBookPeerReviewEvent (ai.elimu.model.contributor.StoryBookPeerReviewEvent)2 File (java.io.File)2 URL (java.net.URL)2 List (java.util.List)2 StoryBookLearningEvent (ai.elimu.model.analytics.StoryBookLearningEvent)1 Emoji (ai.elimu.model.content.Emoji)1