Search in sources :

Example 16 with Image

use of ai.elimu.model.content.multimedia.Image 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)

Example 17 with Image

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

the class BaseDaoTest method testRead.

@Test
public void testRead() {
    Image image1 = new Image();
    image1.setTitle("Title1");
    imageDao.create(image1);
    assertNotNull(image1.getId());
    assertNotNull(imageDao.read(image1.getId()));
}
Also used : Image(ai.elimu.model.content.multimedia.Image) Test(org.junit.Test)

Example 18 with Image

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

the class ModuleActivityController method handleRequest.

@RequestMapping(value = "/{activityId}", method = RequestMethod.GET)
public String handleRequest(Model model, @PathVariable Long activityId) {
    logger.info("handleRequest");
    Image imageLion = imageDao.read("Lion", Locale.EN);
    Image imageTiger = imageDao.read("Tiger", Locale.EN);
    Image imageCat = imageDao.read("Cat", Locale.EN);
    int random = (int) (Math.random() * 3);
    if (random == 0) {
        model.addAttribute("image1", imageLion);
        model.addAttribute("image2", imageTiger);
        model.addAttribute("image3", imageCat);
    } else if (random == 1) {
        model.addAttribute("image1", imageTiger);
        model.addAttribute("image2", imageCat);
        model.addAttribute("image3", imageLion);
    } else if (random == 2) {
        model.addAttribute("image1", imageCat);
        model.addAttribute("image2", imageTiger);
        model.addAttribute("image3", imageLion);
    }
    return "content/module/activity";
}
Also used : Image(ai.elimu.model.content.multimedia.Image) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 19 with Image

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

the class ImageCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpSession session, /*@Valid*/
Image image, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    if (StringUtils.isBlank(image.getTitle())) {
        result.rejectValue("title", "NotNull");
    } else {
        Image existingImage = imageDao.read(image.getTitle(), image.getLocale());
        if (existingImage != null) {
            result.rejectValue("title", "NonUnique");
        }
    }
    try {
        byte[] bytes = multipartFile.getBytes();
        if (multipartFile.isEmpty() || (bytes == null) || (bytes.length == 0)) {
            result.rejectValue("bytes", "NotNull");
        } else {
            String originalFileName = multipartFile.getOriginalFilename();
            logger.info("originalFileName: " + originalFileName);
            if (originalFileName.toLowerCase().endsWith(".png")) {
                image.setImageFormat(ImageFormat.PNG);
            } else if (originalFileName.toLowerCase().endsWith(".jpg") || originalFileName.toLowerCase().endsWith(".jpeg")) {
                image.setImageFormat(ImageFormat.JPG);
            } else if (originalFileName.toLowerCase().endsWith(".gif")) {
                image.setImageFormat(ImageFormat.GIF);
            } else {
                result.rejectValue("bytes", "typeMismatch");
            }
            if (image.getImageFormat() != null) {
                String contentType = multipartFile.getContentType();
                logger.info("contentType: " + contentType);
                image.setContentType(contentType);
                image.setBytes(bytes);
                if (image.getImageFormat() != ImageFormat.GIF) {
                    int width = ImageHelper.getWidth(bytes);
                    logger.info("width: " + width + "px");
                    if (width < ImageHelper.MINIMUM_WIDTH) {
                        result.rejectValue("bytes", "image.too.small");
                        image.setBytes(null);
                    } else {
                        if (width > ImageHelper.MINIMUM_WIDTH) {
                            bytes = ImageHelper.scaleImage(bytes, ImageHelper.MINIMUM_WIDTH);
                            image.setBytes(bytes);
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        logger.error(e);
    }
    if (result.hasErrors()) {
        model.addAttribute("contentLicenses", ContentLicense.values());
        model.addAttribute("literacySkills", LiteracySkill.values());
        model.addAttribute("numeracySkills", NumeracySkill.values());
        return "content/multimedia/image/create";
    } else {
        image.setTitle(image.getTitle().toLowerCase());
        int[] dominantColor = ImageColorHelper.getDominantColor(image.getBytes());
        image.setDominantColor("rgb(" + dominantColor[0] + "," + dominantColor[1] + "," + dominantColor[2] + ")");
        image.setTimeLastUpdate(Calendar.getInstance());
        imageDao.create(image);
        // TODO: store RevisionEvent
        // Label Image with Word of matching title
        Word matchingWord = wordDao.readByText(contributor.getLocale(), image.getTitle());
        if (matchingWord != null) {
            Set<Word> labeledWords = new HashSet<>();
            if (!labeledWords.contains(matchingWord)) {
                labeledWords.add(matchingWord);
                image.setWords(labeledWords);
                imageDao.update(image);
            }
        }
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String text = URLEncoder.encode(contributor.getFirstName() + " just added a new Image:\n" + "• Language: \"" + image.getLocale().getLanguage() + "\"\n" + "• Title: \"" + image.getTitle() + "\"\n" + "See ") + "http://elimu.ai/content/multimedia/image/edit/" + image.getId();
            String iconUrl = contributor.getImageUrl();
            String imageUrl = "http://elimu.ai/image/" + image.getId() + "." + image.getImageFormat().toString().toLowerCase();
            SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.CONTENT_CREATION), text, iconUrl, imageUrl);
        }
        return "redirect:/content/multimedia/image/list#" + image.getId();
    }
}
Also used : Word(ai.elimu.model.content.Word) Contributor(ai.elimu.model.Contributor) IOException(java.io.IOException) Image(ai.elimu.model.content.multimedia.Image) HashSet(java.util.HashSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 20 with Image

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

the class ImageEditController method handleSubmit.

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(HttpSession session, Image image, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    if (StringUtils.isBlank(image.getTitle())) {
        result.rejectValue("title", "NotNull");
    } else {
        Image existingImage = imageDao.read(image.getTitle(), image.getLocale());
        if ((existingImage != null) && !existingImage.getId().equals(image.getId())) {
            result.rejectValue("title", "NonUnique");
        }
    }
    try {
        byte[] bytes = multipartFile.getBytes();
        if (multipartFile.isEmpty() || (bytes == null) || (bytes.length == 0)) {
            result.rejectValue("bytes", "NotNull");
        } else {
            String originalFileName = multipartFile.getOriginalFilename();
            logger.info("originalFileName: " + originalFileName);
            if (originalFileName.toLowerCase().endsWith(".png")) {
                image.setImageFormat(ImageFormat.PNG);
            } else if (originalFileName.toLowerCase().endsWith(".jpg") || originalFileName.toLowerCase().endsWith(".jpeg")) {
                image.setImageFormat(ImageFormat.JPG);
            } else if (originalFileName.toLowerCase().endsWith(".gif")) {
                image.setImageFormat(ImageFormat.GIF);
            } else {
                result.rejectValue("bytes", "typeMismatch");
            }
            if (image.getImageFormat() != null) {
                String contentType = multipartFile.getContentType();
                logger.info("contentType: " + contentType);
                image.setContentType(contentType);
                image.setBytes(bytes);
                if (image.getImageFormat() != ImageFormat.GIF) {
                    int width = ImageHelper.getWidth(bytes);
                    logger.info("width: " + width + "px");
                    if (width < ImageHelper.MINIMUM_WIDTH) {
                        result.rejectValue("bytes", "image.too.small");
                        image.setBytes(null);
                    } else {
                        if (width > ImageHelper.MINIMUM_WIDTH) {
                            bytes = ImageHelper.scaleImage(bytes, ImageHelper.MINIMUM_WIDTH);
                            image.setBytes(bytes);
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        logger.error(e);
    }
    if (result.hasErrors()) {
        model.addAttribute("image", image);
        model.addAttribute("contentLicenses", ContentLicense.values());
        model.addAttribute("literacySkills", LiteracySkill.values());
        model.addAttribute("numeracySkills", NumeracySkill.values());
        model.addAttribute("letters", letterDao.readAllOrdered(contributor.getLocale()));
        model.addAttribute("numbers", numberDao.readAllOrdered(contributor.getLocale()));
        model.addAttribute("words", wordDao.readAllOrdered(contributor.getLocale()));
        Audio audio = audioDao.read(image.getTitle(), contributor.getLocale());
        model.addAttribute("audio", audio);
        return "content/multimedia/image/edit";
    } else {
        image.setTitle(image.getTitle().toLowerCase());
        image.setTimeLastUpdate(Calendar.getInstance());
        image.setRevisionNumber(image.getRevisionNumber() + 1);
        imageDao.update(image);
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String text = URLEncoder.encode(contributor.getFirstName() + " just edited an Image:\n" + "• Language: \"" + image.getLocale().getLanguage() + "\"\n" + "• Title: \"" + image.getTitle() + "\"\n" + "• Revision number: #" + image.getRevisionNumber() + "\n" + "See ") + "http://elimu.ai/content/multimedia/image/edit/" + image.getId();
            String iconUrl = contributor.getImageUrl();
            String imageUrl = "http://elimu.ai/image/" + image.getId() + "." + image.getImageFormat().toString().toLowerCase();
            SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.CONTENT_CREATION), text, iconUrl, imageUrl);
        }
        return "redirect:/content/multimedia/image/list#" + image.getId();
    }
}
Also used : Contributor(ai.elimu.model.Contributor) IOException(java.io.IOException) Image(ai.elimu.model.content.multimedia.Image) Audio(ai.elimu.model.content.multimedia.Audio) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Image (ai.elimu.model.content.multimedia.Image)38 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)31 StoryBook (ai.elimu.model.content.StoryBook)11 Word (ai.elimu.model.content.Word)11 Contributor (ai.elimu.model.Contributor)8 IOException (java.io.IOException)8 Audio (ai.elimu.model.content.multimedia.Audio)7 StoryBookChapter (ai.elimu.model.content.StoryBookChapter)5 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)5 StoryBookContributionEvent (ai.elimu.model.contributor.StoryBookContributionEvent)5 ArrayList (java.util.ArrayList)5 Letter (ai.elimu.model.content.Letter)4 Language (ai.elimu.model.v2.enums.Language)4 Test (org.junit.Test)4 Syllable (ai.elimu.model.content.Syllable)3 ImageContributionEvent (ai.elimu.model.contributor.ImageContributionEvent)3 HashSet (java.util.HashSet)3 Emoji (ai.elimu.model.content.Emoji)2 Number (ai.elimu.model.content.Number)2 AudioContributionEvent (ai.elimu.model.contributor.AudioContributionEvent)2