Search in sources :

Example 1 with ContentLicense

use of ai.elimu.model.enums.ContentLicense in project webapp by elimu-ai.

the class CsvContentExtractionHelper method getStoryBooksFromCsvBackup.

/**
 * For information on how the CSV files were generated, see {@link StoryBookCsvExportController#handleRequest}.
 * <p />
 * Also see {@link #getStoryBookChaptersFromCsvBackup}
 */
public static List<StoryBookGson> getStoryBooksFromCsvBackup(File csvFile) {
    logger.info("getStoryBooksFromCsvBackup");
    List<StoryBookGson> storyBookGsons = new ArrayList<>();
    Path csvFilePath = Paths.get(csvFile.toURI());
    logger.info("csvFilePath: " + csvFilePath);
    try {
        Reader reader = Files.newBufferedReader(csvFilePath);
        CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "title", "description", "content_license", "attribution_url", "reading_level", "cover_image_id", "chapters").withSkipHeaderRecord();
        CSVParser csvParser = new CSVParser(reader, csvFormat);
        for (CSVRecord csvRecord : csvParser) {
            logger.info("csvRecord: " + csvRecord);
            // Convert from CSV to GSON
            StoryBookGson storyBookGson = new StoryBookGson();
            String title = csvRecord.get("title");
            storyBookGson.setTitle(title);
            String description = csvRecord.get("description");
            storyBookGson.setDescription(description);
            if (StringUtils.isNotBlank(csvRecord.get("content_license"))) {
                ContentLicense contentLicense = ContentLicense.valueOf(csvRecord.get("content_license"));
            // storyBookGson.setContentLicense(contentLicense);
            }
            String attributionUrl = csvRecord.get("attribution_url");
            if (StringUtils.isNotBlank(csvRecord.get("reading_level"))) {
                ReadingLevel readingLevel = ReadingLevel.valueOf(csvRecord.get("reading_level"));
                storyBookGson.setReadingLevel(readingLevel);
            }
            if (StringUtils.isNotBlank(csvRecord.get("cover_image_id"))) {
                Long coverImageId = Long.valueOf(csvRecord.get("cover_image_id"));
            // storyBookGson.setCoverImage();
            }
            List<StoryBookChapterGson> storyBookChapterGsons = new ArrayList<>();
            JSONArray chaptersJsonArray = new JSONArray(csvRecord.get("chapters"));
            logger.info("chaptersJsonArray: " + chaptersJsonArray);
            for (int i = 0; i < chaptersJsonArray.length(); i++) {
                JSONObject chapterJsonObject = chaptersJsonArray.getJSONObject(i);
                logger.info("chapterJsonObject: " + chapterJsonObject);
                StoryBookChapterGson storyBookChapterGson = new StoryBookChapterGson();
                storyBookChapterGson.setSortOrder(chapterJsonObject.getInt("sortOrder"));
                List<StoryBookParagraphGson> storyBookParagraphGsons = new ArrayList<>();
                JSONArray paragraphsJsonArray = chapterJsonObject.getJSONArray("storyBookParagraphs");
                logger.info("paragraphsJsonArray: " + paragraphsJsonArray);
                for (int j = 0; j < paragraphsJsonArray.length(); j++) {
                    JSONObject paragraphJsonObject = paragraphsJsonArray.getJSONObject(j);
                    logger.info("paragraphJsonObject: " + paragraphJsonObject);
                    StoryBookParagraphGson storyBookParagraphGson = new StoryBookParagraphGson();
                    storyBookParagraphGson.setSortOrder(paragraphJsonObject.getInt("sortOrder"));
                    storyBookParagraphGson.setOriginalText(paragraphJsonObject.getString("originalText"));
                    // TODO: setWords
                    storyBookParagraphGsons.add(storyBookParagraphGson);
                }
                storyBookChapterGson.setStoryBookParagraphs(storyBookParagraphGsons);
                storyBookChapterGsons.add(storyBookChapterGson);
            }
            storyBookGson.setStoryBookChapters(storyBookChapterGsons);
            storyBookGsons.add(storyBookGson);
        }
    } catch (IOException ex) {
        logger.error(ex);
    }
    return storyBookGsons;
}
Also used : Path(java.nio.file.Path) ReadingLevel(ai.elimu.model.v2.enums.ReadingLevel) StoryBookParagraphGson(ai.elimu.model.v2.gson.content.StoryBookParagraphGson) StoryBookGson(ai.elimu.model.v2.gson.content.StoryBookGson) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Reader(java.io.Reader) IOException(java.io.IOException) ContentLicense(ai.elimu.model.enums.ContentLicense) JSONObject(org.json.JSONObject) CSVParser(org.apache.commons.csv.CSVParser) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) StoryBookChapterGson(ai.elimu.model.v2.gson.content.StoryBookChapterGson)

Aggregations

ContentLicense (ai.elimu.model.enums.ContentLicense)1 ReadingLevel (ai.elimu.model.v2.enums.ReadingLevel)1 StoryBookChapterGson (ai.elimu.model.v2.gson.content.StoryBookChapterGson)1 StoryBookGson (ai.elimu.model.v2.gson.content.StoryBookGson)1 StoryBookParagraphGson (ai.elimu.model.v2.gson.content.StoryBookParagraphGson)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 CSVParser (org.apache.commons.csv.CSVParser)1 CSVRecord (org.apache.commons.csv.CSVRecord)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1