use of ai.elimu.model.content.StoryBookParagraph in project webapp by elimu-ai.
the class AudioCreateController method handleRequest.
@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model, @RequestParam(required = false) Long wordId, @RequestParam(required = false) Long storyBookParagraphId, @RequestParam(required = false) String autoFillTitle, @RequestParam(required = false) String autoFillTranscription) {
logger.info("handleRequest");
Audio audio = new Audio();
// Pre-select the Audio's corresponding Word
if (wordId != null) {
Word word = wordDao.read(wordId);
audio.setWord(word);
}
// Pre-select the Audio's corresponding StoryBookParagraph
if (storyBookParagraphId != null) {
StoryBookParagraph storyBookParagraph = storyBookParagraphDao.read(storyBookParagraphId);
audio.setStoryBookParagraph(storyBookParagraph);
}
// Pre-fill the Audio's title
if (StringUtils.isNotBlank(autoFillTitle)) {
audio.setTitle(autoFillTitle);
}
// Pre-fill the Audio's transcription
if (StringUtils.isNotBlank(autoFillTranscription)) {
audio.setTranscription(autoFillTranscription);
}
model.addAttribute("audio", audio);
model.addAttribute("words", wordDao.readAllOrdered());
model.addAttribute("storyBookParagraphs", storyBookParagraphDao.readAll());
model.addAttribute("contentLicenses", ContentLicense.values());
model.addAttribute("literacySkills", LiteracySkill.values());
model.addAttribute("numeracySkills", NumeracySkill.values());
model.addAttribute("timeStart", System.currentTimeMillis());
model.addAttribute("emojisByWordId", getEmojisByWordId());
return "content/multimedia/audio/create";
}
use of ai.elimu.model.content.StoryBookParagraph in project webapp by elimu-ai.
the class LetterUsageCountScheduler 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 Letters");
Map<String, Integer> letterFrequencyMap = new HashMap<>();
Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
List<StoryBook> storyBooks = storyBookDao.readAllOrdered();
logger.info("storyBooks.size(): " + storyBooks.size());
for (StoryBook storyBook : storyBooks) {
logger.info("storyBook.getTitle(): " + storyBook.getTitle());
List<String> paragraphs = new ArrayList<>();
List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
for (StoryBookChapter storyBookChapter : storyBookChapters) {
List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookChapter);
for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
paragraphs.add(storyBookParagraph.getOriginalText());
}
}
Map<String, Integer> letterFrequencyMapForBook = LetterFrequencyHelper.getLetterFrequency(paragraphs, language);
letterFrequencyMapForBook.keySet().forEach(letterText -> letterFrequencyMap.put(letterText, letterFrequencyMap.getOrDefault(letterText, 0) + letterFrequencyMapForBook.get(letterText)));
}
logger.info("letterFrequencyMap: " + letterFrequencyMap);
for (String letterText : letterFrequencyMap.keySet()) {
Letter existingLetter = letterDao.readByText(letterText);
if (existingLetter != null) {
existingLetter.setUsageCount(letterFrequencyMap.get(letterText));
letterDao.update(existingLetter);
}
}
logger.info("execute complete");
}
use of ai.elimu.model.content.StoryBookParagraph in project webapp by elimu-ai.
the class WordUsageCountScheduler method execute.
// At 06:00 every day
@Scheduled(cron = "00 00 06 * * *")
public synchronized void execute() {
logger.info("execute");
logger.info("Calculating usage count for Words");
Map<String, Integer> wordFrequencyMap = new HashMap<>();
Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
List<StoryBook> storyBooks = storyBookDao.readAllOrdered();
logger.info("storyBooks.size(): " + storyBooks.size());
for (StoryBook storyBook : storyBooks) {
logger.info("storyBook.getTitle(): " + storyBook.getTitle());
List<String> paragraphs = new ArrayList<>();
List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
for (StoryBookChapter storyBookChapter : storyBookChapters) {
List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookChapter);
for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
paragraphs.add(storyBookParagraph.getOriginalText());
}
}
Map<String, Integer> wordFrequencyMapForBook = WordFrequencyHelper.getWordFrequency(paragraphs, language);
wordFrequencyMapForBook.keySet().forEach(word -> wordFrequencyMap.put(word, wordFrequencyMap.getOrDefault(word, 0) + wordFrequencyMapForBook.get(word)));
}
for (String key : wordFrequencyMap.keySet()) {
String wordLowerCase = key.toLowerCase();
logger.info("wordLowerCase: \"" + wordLowerCase + "\"");
Word word = wordDao.readByText(wordLowerCase);
if (word != null) {
word.setUsageCount(wordFrequencyMap.get(wordLowerCase));
wordDao.update(word);
}
}
logger.info("execute complete");
}
use of ai.elimu.model.content.StoryBookParagraph in project webapp by elimu-ai.
the class StoryBookEditController method handleRequest.
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(Model model, @PathVariable Long id) {
logger.info("handleRequest");
StoryBook storyBook = storyBookDao.read(id);
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";
}
use of ai.elimu.model.content.StoryBookParagraph 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();
}
Aggregations