Search in sources :

Example 41 with Contributor

use of ai.elimu.model.Contributor 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 42 with Contributor

use of ai.elimu.model.Contributor 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)

Example 43 with Contributor

use of ai.elimu.model.Contributor in project webapp by elimu-ai.

the class ImageListController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model, HttpSession session) {
    logger.info("handleRequest");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    // To ease development/testing, auto-generate Images
    List<Image> imagesGenerated = generateImages(contributor.getLocale());
    for (Image image : imagesGenerated) {
        Image existingImage = imageDao.read(image.getTitle(), image.getLocale());
        if (existingImage == null) {
            ImageFormat imageFormat = ImageFormat.PNG;
            image.setContentType("image/png");
            String fileName = image.getTitle() + "." + imageFormat.toString().toLowerCase();
            logger.info("Looking up file \"" + image.getTitle() + "." + imageFormat.toString().toLowerCase() + "\"...");
            URL url = getClass().getResource(fileName);
            logger.info("url.getPath(): " + url.getPath());
            try {
                byte[] imageBytes = IOUtils.toByteArray(url);
                image.setBytes(imageBytes);
                image.setImageFormat(imageFormat);
                int[] dominantColor = ImageColorHelper.getDominantColor(image.getBytes());
                image.setDominantColor("rgb(" + dominantColor[0] + "," + dominantColor[1] + "," + dominantColor[2] + ")");
                imageDao.create(image);
            } catch (IOException ex) {
                logger.error(null, ex);
            }
        }
    }
    List<Image> images = imageDao.readAllOrdered(contributor.getLocale());
    model.addAttribute("images", images);
    return "content/multimedia/image/list";
}
Also used : Contributor(ai.elimu.model.Contributor) IOException(java.io.IOException) Image(ai.elimu.model.content.multimedia.Image) URL(java.net.URL) ImageFormat(ai.elimu.model.enums.content.ImageFormat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 44 with Contributor

use of ai.elimu.model.Contributor in project webapp by elimu-ai.

the class VideoCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpSession session, /*@Valid*/
Video video, @RequestParam("bytes") MultipartFile multipartFile, @RequestParam("thumbnail") MultipartFile multipartFileThumbnail, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    if (StringUtils.isBlank(video.getTitle())) {
        result.rejectValue("title", "NotNull");
    } else {
        Video existingVideo = videoDao.read(video.getTitle(), video.getLocale());
        if (existingVideo != 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(".m4v")) {
                video.setVideoFormat(VideoFormat.M4V);
            } else if (originalFileName.toLowerCase().endsWith(".mp4")) {
                video.setVideoFormat(VideoFormat.MP4);
            } else {
                result.rejectValue("bytes", "typeMismatch");
            }
            if (video.getVideoFormat() != null) {
                String contentType = multipartFile.getContentType();
                logger.info("contentType: " + contentType);
                video.setContentType(contentType);
                video.setBytes(bytes);
            // TODO: convert to a default video format?
            }
        }
        byte[] bytesThumbnail = multipartFileThumbnail.getBytes();
        if (multipartFileThumbnail.isEmpty() || (bytesThumbnail == null) || (bytesThumbnail.length == 0)) {
            result.rejectValue("thumbnail", "NotNull");
        } else {
            String originalFileName = multipartFileThumbnail.getOriginalFilename();
            logger.info("originalFileName: " + originalFileName);
            if (!originalFileName.toLowerCase().endsWith(".png")) {
                result.rejectValue("thumbnail", "typeMismatch");
            } else {
                video.setThumbnail(bytesThumbnail);
                int width = ImageHelper.getWidth(bytesThumbnail);
                logger.info("width: " + width + "px");
                if (width < ImageHelper.MINIMUM_WIDTH) {
                    result.rejectValue("thumbnail", "image.too.small");
                    video.setBytes(null);
                } else {
                    if (width > ImageHelper.MINIMUM_WIDTH) {
                        bytesThumbnail = ImageHelper.scaleImage(bytesThumbnail, ImageHelper.MINIMUM_WIDTH);
                        video.setBytes(bytesThumbnail);
                    }
                }
            }
        }
    } 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/video/create";
    } else {
        video.setTitle(video.getTitle().toLowerCase());
        video.setTimeLastUpdate(Calendar.getInstance());
        videoDao.create(video);
        VideoRevisionEvent videoRevisionEvent = new VideoRevisionEvent();
        videoRevisionEvent.setContributor(contributor);
        videoRevisionEvent.setCalendar(Calendar.getInstance());
        videoRevisionEvent.setVideo(video);
        videoRevisionEvent.setTitle(video.getTitle());
        videoRevisionEventDao.create(videoRevisionEvent);
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String text = URLEncoder.encode(contributor.getFirstName() + " just added a new Video:\n" + "• Language: \"" + video.getLocale().getLanguage() + "\"\n" + "• Title: \"" + video.getTitle() + "\"\n" + "See ") + "http://elimu.ai/content/multimedia/video/edit/" + video.getId();
            String iconUrl = contributor.getImageUrl();
            String imageUrl = "http://elimu.ai/video/" + video.getId() + "/thumbnail.png";
            SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.CONTENT_CREATION), text, iconUrl, imageUrl);
        }
        return "redirect:/content/multimedia/video/list#" + video.getId();
    }
}
Also used : VideoRevisionEvent(ai.elimu.model.contributor.VideoRevisionEvent) Video(ai.elimu.model.content.multimedia.Video) Contributor(ai.elimu.model.Contributor) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 45 with Contributor

use of ai.elimu.model.Contributor in project webapp by elimu-ai.

the class VideoEditController method handleSubmit.

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(HttpSession session, Video video, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model) {
    logger.info("handleSubmit");
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    if (StringUtils.isBlank(video.getTitle())) {
        result.rejectValue("title", "NotNull");
    } else {
        Video existingVideo = videoDao.read(video.getTitle(), video.getLocale());
        if ((existingVideo != null) && !existingVideo.getId().equals(video.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(".m4v")) {
                video.setVideoFormat(VideoFormat.M4V);
            } else if (originalFileName.toLowerCase().endsWith(".mp4")) {
                video.setVideoFormat(VideoFormat.MP4);
            } else {
                result.rejectValue("bytes", "typeMismatch");
            }
            if (video.getVideoFormat() != null) {
                String contentType = multipartFile.getContentType();
                logger.info("contentType: " + contentType);
                video.setContentType(contentType);
                video.setBytes(bytes);
            // TODO: convert to a default video format?
            }
        }
    } catch (IOException e) {
        logger.error(e);
    }
    if (result.hasErrors()) {
        model.addAttribute("video", video);
        model.addAttribute("contentLicenses", ContentLicense.values());
        model.addAttribute("literacySkills", LiteracySkill.values());
        model.addAttribute("numeracySkills", NumeracySkill.values());
        model.addAttribute("videoRevisionEvents", videoRevisionEventDao.readAll(video));
        model.addAttribute("letters", letterDao.readAllOrdered(contributor.getLocale()));
        model.addAttribute("numbers", numberDao.readAllOrdered(contributor.getLocale()));
        model.addAttribute("words", wordDao.readAllOrdered(contributor.getLocale()));
        return "content/multimedia/video/edit";
    } else {
        video.setTitle(video.getTitle().toLowerCase());
        video.setTimeLastUpdate(Calendar.getInstance());
        video.setRevisionNumber(video.getRevisionNumber() + 1);
        videoDao.update(video);
        VideoRevisionEvent videoRevisionEvent = new VideoRevisionEvent();
        videoRevisionEvent.setContributor(contributor);
        videoRevisionEvent.setCalendar(Calendar.getInstance());
        videoRevisionEvent.setVideo(video);
        videoRevisionEvent.setTitle(video.getTitle());
        videoRevisionEventDao.create(videoRevisionEvent);
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String text = URLEncoder.encode(contributor.getFirstName() + " just updated a Video:\n" + "• Language: \"" + video.getLocale().getLanguage() + "\"\n" + "• Title: \"" + video.getTitle() + "\"\n" + "• Revision number: #" + video.getRevisionNumber() + "\n" + "See ") + "http://elimu.ai/content/multimedia/video/edit/" + video.getId();
            String iconUrl = contributor.getImageUrl();
            String imageUrl = "http://elimu.ai/video/" + video.getId() + "/thumbnail.png";
            SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.CONTENT_CREATION), text, iconUrl, imageUrl);
        }
        return "redirect:/content/multimedia/video/list#" + video.getId();
    }
}
Also used : VideoRevisionEvent(ai.elimu.model.contributor.VideoRevisionEvent) Video(ai.elimu.model.content.multimedia.Video) Contributor(ai.elimu.model.Contributor) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Contributor (ai.elimu.model.Contributor)57 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)51 Project (ai.elimu.model.project.Project)12 IOException (java.io.IOException)10 Allophone (ai.elimu.model.content.Allophone)9 Word (ai.elimu.model.content.Word)8 Image (ai.elimu.model.content.multimedia.Image)8 Audio (ai.elimu.model.content.multimedia.Audio)6 Letter (ai.elimu.model.content.Letter)5 Number (ai.elimu.model.content.Number)5 AppCategory (ai.elimu.model.project.AppCategory)5 StoryBook (ai.elimu.model.content.StoryBook)4 Video (ai.elimu.model.content.multimedia.Video)4 Scheduled (org.springframework.scheduling.annotation.Scheduled)4 AppCollection (ai.elimu.model.project.AppCollection)3 AppGroup (ai.elimu.model.project.AppGroup)3 Locale (java.util.Locale)3 Application (ai.elimu.model.admin.Application)2 ApplicationVersion (ai.elimu.model.admin.ApplicationVersion)2 Syllable (ai.elimu.model.content.Syllable)2