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