Search in sources :

Example 11 with StoryBookChapter

use of ai.elimu.model.content.StoryBookChapter 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 12 with StoryBookChapter

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

the class StoryBookChapterCreateController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(@PathVariable Long storyBookId, Model model) {
    logger.info("handleRequest");
    StoryBookChapter storyBookChapter = new StoryBookChapter();
    StoryBook storyBook = storyBookDao.read(storyBookId);
    storyBookChapter.setStoryBook(storyBook);
    List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
    storyBookChapter.setSortOrder(storyBookChapters.size());
    model.addAttribute("storyBookChapter", storyBookChapter);
    List<Image> images = imageDao.readAllOrdered();
    model.addAttribute("images", images);
    return "content/storybook/chapter/create";
}
Also used : StoryBookChapter(ai.elimu.model.content.StoryBookChapter) StoryBook(ai.elimu.model.content.StoryBook) Image(ai.elimu.model.content.multimedia.Image) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with StoryBookChapter

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

the class StoryBooksJsonService method getStoryBooksJSONArray.

@Cacheable("storyBooks")
public JSONArray getStoryBooksJSONArray() {
    logger.info("getStoryBooksJSONArray");
    Date dateStart = new Date();
    JSONArray storyBooksJsonArray = new JSONArray();
    for (StoryBook storyBook : storyBookDao.readAllOrdered()) {
        StoryBookGson storyBookGson = JpaToGsonConverter.getStoryBookGson(storyBook);
        // Add chapters
        List<StoryBookChapterGson> storyBookChapterGsons = new ArrayList<>();
        for (StoryBookChapter storyBookChapter : storyBookChapterDao.readAll(storyBook)) {
            StoryBookChapterGson storyBookChapterGson = JpaToGsonConverter.getStoryBookChapterGson(storyBookChapter);
            // Add paragraphs
            List<StoryBookParagraphGson> storyBookParagraphGsons = new ArrayList<>();
            for (StoryBookParagraph storyBookParagraph : storyBookParagraphDao.readAll(storyBookChapter)) {
                StoryBookParagraphGson storyBookParagraphGson = JpaToGsonConverter.getStoryBookParagraphGson(storyBookParagraph);
                storyBookParagraphGsons.add(storyBookParagraphGson);
            }
            storyBookChapterGson.setStoryBookParagraphs(storyBookParagraphGsons);
            storyBookChapterGsons.add(storyBookChapterGson);
        }
        storyBookGson.setStoryBookChapters(storyBookChapterGsons);
        String json = new Gson().toJson(storyBookGson);
        storyBooksJsonArray.put(new JSONObject(json));
    }
    Date dateEnd = new Date();
    logger.info("getStoryBooksJSONArray duration: " + (dateEnd.getTime() - dateStart.getTime()) + " ms");
    return storyBooksJsonArray;
}
Also used : StoryBookChapter(ai.elimu.model.content.StoryBookChapter) StoryBook(ai.elimu.model.content.StoryBook) StoryBookParagraphGson(ai.elimu.model.v2.gson.content.StoryBookParagraphGson) JSONArray(org.json.JSONArray) StoryBookGson(ai.elimu.model.v2.gson.content.StoryBookGson) ArrayList(java.util.ArrayList) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) StoryBookGson(ai.elimu.model.v2.gson.content.StoryBookGson) StoryBookParagraphGson(ai.elimu.model.v2.gson.content.StoryBookParagraphGson) Gson(com.google.gson.Gson) StoryBookChapterGson(ai.elimu.model.v2.gson.content.StoryBookChapterGson) Date(java.util.Date) JSONObject(org.json.JSONObject) StoryBookChapterGson(ai.elimu.model.v2.gson.content.StoryBookChapterGson) Cacheable(org.springframework.cache.annotation.Cacheable)

Aggregations

StoryBookChapter (ai.elimu.model.content.StoryBookChapter)13 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)11 StoryBook (ai.elimu.model.content.StoryBook)10 ArrayList (java.util.ArrayList)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 Word (ai.elimu.model.content.Word)5 Image (ai.elimu.model.content.multimedia.Image)5 Language (ai.elimu.model.v2.enums.Language)5 HashMap (java.util.HashMap)5 Letter (ai.elimu.model.content.Letter)4 StoryBookContributionEvent (ai.elimu.model.contributor.StoryBookContributionEvent)4 StoryBookChapterGson (ai.elimu.model.v2.gson.content.StoryBookChapterGson)3 StoryBookParagraphGson (ai.elimu.model.v2.gson.content.StoryBookParagraphGson)3 Scheduled (org.springframework.scheduling.annotation.Scheduled)3 Contributor (ai.elimu.model.contributor.Contributor)2 StoryBookGson (ai.elimu.model.v2.gson.content.StoryBookGson)2 Gson (com.google.gson.Gson)2 File (java.io.File)2 URL (java.net.URL)2 List (java.util.List)2