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;
}
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";
}
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;
}
Aggregations