Search in sources :

Example 1 with ImageGson

use of ai.elimu.model.v2.gson.content.ImageGson in project webapp by elimu-ai.

the class JpaToGsonConverter method getStoryBookChapterGson.

public static StoryBookChapterGson getStoryBookChapterGson(StoryBookChapter storyBookChapter) {
    if (storyBookChapter == null) {
        return null;
    } else {
        StoryBookChapterGson storyBookChapterGson = new StoryBookChapterGson();
        // BaseEntity
        storyBookChapterGson.setId(storyBookChapter.getId());
        // StoryBookChapter
        storyBookChapterGson.setSortOrder(storyBookChapter.getSortOrder());
        if (storyBookChapter.getImage() != null) {
            ImageGson imageGson = new ImageGson();
            imageGson.setId(storyBookChapter.getImage().getId());
            storyBookChapterGson.setImage(imageGson);
        }
        return storyBookChapterGson;
    }
}
Also used : ImageGson(ai.elimu.model.v2.gson.content.ImageGson) StoryBookChapterGson(ai.elimu.model.v2.gson.content.StoryBookChapterGson)

Example 2 with ImageGson

use of ai.elimu.model.v2.gson.content.ImageGson in project webapp by elimu-ai.

the class JpaToGsonConverter method getStoryBookGson.

public static StoryBookGson getStoryBookGson(StoryBook storyBook) {
    if (storyBook == null) {
        return null;
    } else {
        StoryBookGson storyBookGson = new StoryBookGson();
        // BaseEntity
        storyBookGson.setId(storyBook.getId());
        // Content
        storyBookGson.setRevisionNumber(storyBook.getRevisionNumber());
        storyBookGson.setUsageCount(storyBook.getUsageCount());
        // StoryBook
        storyBookGson.setTitle(storyBook.getTitle());
        storyBookGson.setDescription(storyBook.getDescription());
        if (storyBook.getCoverImage() != null) {
            ImageGson imageGson = new ImageGson();
            imageGson.setId(storyBook.getCoverImage().getId());
            storyBookGson.setCoverImage(imageGson);
        }
        storyBookGson.setReadingLevel(storyBook.getReadingLevel());
        return storyBookGson;
    }
}
Also used : ImageGson(ai.elimu.model.v2.gson.content.ImageGson) StoryBookGson(ai.elimu.model.v2.gson.content.StoryBookGson)

Example 3 with ImageGson

use of ai.elimu.model.v2.gson.content.ImageGson in project webapp by elimu-ai.

the class StoryBookCsvExportController method handleRequest.

@RequestMapping(value = "/storybooks.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) throws IOException {
    logger.info("handleRequest");
    List<StoryBook> storyBooks = storyBookDao.readAllOrdered();
    logger.info("storyBooks.size(): " + storyBooks.size());
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "title", "description", "content_license", "attribution_url", "reading_level", "cover_image_id", "chapters");
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    for (StoryBook storyBook : storyBooks) {
        logger.info("storyBook.getTitle(): \"" + storyBook.getTitle() + "\"");
        Long coverImageId = null;
        if (storyBook.getCoverImage() != null) {
            coverImageId = storyBook.getCoverImage().getId();
        }
        // Store chapters as JSON objects
        JSONArray chaptersJsonArray = new JSONArray();
        List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
        logger.info("storyBookChapters.size(): " + storyBookChapters.size());
        for (StoryBookChapter storyBookChapter : storyBookChapters) {
            logger.info("storyBookChapter.getId(): " + storyBookChapter.getId());
            StoryBookChapterGson storyBookChapterGson = JpaToGsonConverter.getStoryBookChapterGson(storyBookChapter);
            // TODO: move this code block to JpaToGsonConverter?
            if (storyBookChapterGson.getImage() != null) {
                ImageGson imageGsonWithIdOnly = new ImageGson();
                imageGsonWithIdOnly.setId(storyBookChapterGson.getImage().getId());
                storyBookChapterGson.setImage(imageGsonWithIdOnly);
            }
            // Store paragraphs as JSON objects
            List<StoryBookParagraphGson> storyBookParagraphs = new ArrayList<>();
            logger.info("storyBookParagraphs.size(): " + storyBookParagraphs.size());
            for (StoryBookParagraph storyBookParagraph : storyBookParagraphDao.readAll(storyBookChapter)) {
                logger.info("storyBookParagraph.getId(): " + storyBookParagraph.getId());
                StoryBookParagraphGson storyBookParagraphGson = JpaToGsonConverter.getStoryBookParagraphGson(storyBookParagraph);
                storyBookParagraphGson.setWords(null);
                storyBookParagraphs.add(storyBookParagraphGson);
            }
            storyBookChapterGson.setStoryBookParagraphs(storyBookParagraphs);
            String json = new Gson().toJson(storyBookChapterGson);
            JSONObject jsonObject = new JSONObject(json);
            logger.info("jsonObject: " + jsonObject);
            chaptersJsonArray.put(jsonObject);
        }
        logger.info("chaptersJsonArray: " + chaptersJsonArray);
        csvPrinter.printRecord(storyBook.getId(), storyBook.getTitle(), storyBook.getDescription(), storyBook.getContentLicense(), storyBook.getAttributionUrl(), storyBook.getReadingLevel(), coverImageId, chaptersJsonArray);
        csvPrinter.flush();
    }
    String csvFileContent = stringWriter.toString();
    response.setContentType("text/csv");
    byte[] bytes = csvFileContent.getBytes();
    response.setContentLength(bytes.length);
    try {
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    } catch (IOException ex) {
        logger.error(ex);
    }
}
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) ArrayList(java.util.ArrayList) StoryBookParagraph(ai.elimu.model.content.StoryBookParagraph) ImageGson(ai.elimu.model.v2.gson.content.ImageGson) Gson(com.google.gson.Gson) StoryBookParagraphGson(ai.elimu.model.v2.gson.content.StoryBookParagraphGson) StoryBookChapterGson(ai.elimu.model.v2.gson.content.StoryBookChapterGson) IOException(java.io.IOException) CSVPrinter(org.apache.commons.csv.CSVPrinter) ImageGson(ai.elimu.model.v2.gson.content.ImageGson) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) CSVFormat(org.apache.commons.csv.CSVFormat) StoryBookChapterGson(ai.elimu.model.v2.gson.content.StoryBookChapterGson) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ImageGson

use of ai.elimu.model.v2.gson.content.ImageGson in project webapp by elimu-ai.

the class ImagesRestController method handleGetRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleGetRequest(HttpServletRequest request) {
    logger.info("handleGetRequest");
    JSONArray imagesJsonArray = new JSONArray();
    for (Image image : imageDao.readAllOrdered()) {
        ImageGson imageGson = JpaToGsonConverter.getImageGson(image);
        String json = new Gson().toJson(imageGson);
        imagesJsonArray.put(new JSONObject(json));
    }
    String jsonResponse = imagesJsonArray.toString();
    logger.info("jsonResponse: " + jsonResponse);
    return jsonResponse;
}
Also used : ImageGson(ai.elimu.model.v2.gson.content.ImageGson) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ImageGson(ai.elimu.model.v2.gson.content.ImageGson) Gson(com.google.gson.Gson) Image(ai.elimu.model.content.multimedia.Image) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with ImageGson

use of ai.elimu.model.v2.gson.content.ImageGson in project webapp by elimu-ai.

the class JpaToGsonConverter method getImageGson.

public static ImageGson getImageGson(Image image) {
    if (image == null) {
        return null;
    } else {
        ImageGson imageGson = new ImageGson();
        // BaseEntity
        imageGson.setId(image.getId());
        // Content
        imageGson.setRevisionNumber(image.getRevisionNumber());
        imageGson.setUsageCount(image.getUsageCount());
        // Image
        imageGson.setTitle(image.getTitle());
        imageGson.setImageFormat(image.getImageFormat());
        imageGson.setBytesUrl("/image/" + image.getId() + "_r" + image.getRevisionNumber() + "." + image.getImageFormat().toString().toLowerCase());
        imageGson.setBytesSize(image.getBytes().length / 1024);
        Set<WordGson> wordGsons = new HashSet<>();
        for (Word word : image.getWords()) {
            WordGson wordGson = new WordGson();
            wordGson.setId(word.getId());
            wordGsons.add(wordGson);
        }
        imageGson.setWords(wordGsons);
        return imageGson;
    }
}
Also used : WordGson(ai.elimu.model.v2.gson.content.WordGson) ImageGson(ai.elimu.model.v2.gson.content.ImageGson) Word(ai.elimu.model.content.Word) HashSet(java.util.HashSet)

Aggregations

ImageGson (ai.elimu.model.v2.gson.content.ImageGson)5 StoryBookChapterGson (ai.elimu.model.v2.gson.content.StoryBookChapterGson)2 Gson (com.google.gson.Gson)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 StoryBook (ai.elimu.model.content.StoryBook)1 StoryBookChapter (ai.elimu.model.content.StoryBookChapter)1 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)1 Word (ai.elimu.model.content.Word)1 Image (ai.elimu.model.content.multimedia.Image)1 StoryBookGson (ai.elimu.model.v2.gson.content.StoryBookGson)1 StoryBookParagraphGson (ai.elimu.model.v2.gson.content.StoryBookParagraphGson)1 WordGson (ai.elimu.model.v2.gson.content.WordGson)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 CSVPrinter (org.apache.commons.csv.CSVPrinter)1