Search in sources :

Example 31 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project drools-wb by kiegroup.

the class ScenarioCsvDownloadReportTest method generateAuditLogHeaderRULE.

@Test
public void generateAuditLogHeaderRULE() throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    CSVPrinter printer = getCSVPrinter(stringBuilder);
    scenarioCsvDownloadReport.generateAuditLogHeader(printer, RULE);
    String retrieved = stringBuilder.toString();
    commonCheckHeader(RULE_AUDIT_HEADER, retrieved);
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) Test(org.junit.Test)

Example 32 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project drools-wb by kiegroup.

the class ScenarioCsvDownloadReportTest method generateRulesCounterHeaderDMN.

@Test
public void generateRulesCounterHeaderDMN() throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    CSVPrinter printer = getCSVPrinter(stringBuilder);
    scenarioCsvDownloadReport.generateRulesCounterHeader(printer, DMN);
    String retrieved = stringBuilder.toString();
    commonCheckHeader(DMN_COUNTER_HEADER, retrieved);
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) Test(org.junit.Test)

Example 33 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project drools-wb by kiegroup.

the class ScenarioCsvDownloadReportTest method printAuditLogLine.

@Test
public void printAuditLogLine() throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    CSVPrinter printer = getCSVPrinter(stringBuilder);
    AuditLogLine auditLogLine = getAuditLogLine();
    scenarioCsvDownloadReport.printAuditLogLine(auditLogLine, printer);
    String retrieved = stringBuilder.toString();
    List<String> data = Arrays.asList(String.valueOf(auditLogLine.getScenarioIndex()), "\"" + auditLogLine.getScenario() + "\"", String.valueOf(auditLogLine.getExecutionIndex()), "\"" + auditLogLine.getDecisionOrRuleName() + "\"", auditLogLine.getResult(), auditLogLine.getMessage().orElse(""));
    commonCheckRetrievedString(retrieved, data);
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) AuditLogLine(org.drools.scenariosimulation.api.model.AuditLogLine) Test(org.junit.Test)

Example 34 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project webapp by elimu-ai.

the class LetterCsvExportController method handleRequest.

@RequestMapping(value = "/letters.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) throws IOException {
    logger.info("handleRequest");
    List<Letter> letters = letterDao.readAllOrderedByUsage();
    logger.info("letters.size(): " + letters.size());
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "text", "diacritic", "usage_count");
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    for (Letter letter : letters) {
        logger.info("letter.getText(): \"" + letter.getText() + "\"");
        csvPrinter.printRecord(letter.getId(), letter.getText(), letter.isDiacritic(), letter.getUsageCount());
        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 : Letter(ai.elimu.model.content.Letter) CSVPrinter(org.apache.commons.csv.CSVPrinter) StringWriter(java.io.StringWriter) CSVFormat(org.apache.commons.csv.CSVFormat) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project webapp by elimu-ai.

the class WordCsvExportController method handleRequest.

@RequestMapping(value = "/words.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) throws IOException {
    logger.info("handleRequest");
    List<Word> words = wordDao.readAllOrderedByUsage();
    logger.info("words.size(): " + words.size());
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "text", "letter_sound_correspondences", "usage_count", "word_type", "spelling_consistency", "root_word_id", "root_word_text");
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    for (Word word : words) {
        logger.info("word.getText(): \"" + word.getText() + "\"");
        JSONArray letterSoundCorrespondencesJsonArray = new JSONArray();
        int index = 0;
        for (LetterSoundCorrespondence letterSoundCorrespondence : word.getLetterSoundCorrespondences()) {
            JSONObject letterSoundCorrespondenceJsonObject = new JSONObject();
            letterSoundCorrespondenceJsonObject.put("id", letterSoundCorrespondence.getId());
            String[] lettersArray = new String[letterSoundCorrespondence.getLetters().size()];
            for (int i = 0; i < lettersArray.length; i++) {
                lettersArray[i] = letterSoundCorrespondence.getLetters().get(i).getText();
            }
            letterSoundCorrespondenceJsonObject.put("letters", lettersArray);
            String[] soundsArray = new String[letterSoundCorrespondence.getSounds().size()];
            for (int i = 0; i < soundsArray.length; i++) {
                soundsArray[i] = letterSoundCorrespondence.getSounds().get(i).getValueIpa();
            }
            letterSoundCorrespondenceJsonObject.put("sounds", soundsArray);
            letterSoundCorrespondenceJsonObject.put("usageCount", letterSoundCorrespondence.getUsageCount());
            letterSoundCorrespondencesJsonArray.put(index, letterSoundCorrespondenceJsonObject);
            index++;
        }
        Long rootWordId = null;
        String rootWordText = null;
        if (word.getRootWord() != null) {
            rootWordId = word.getRootWord().getId();
            rootWordText = word.getRootWord().getText();
        }
        csvPrinter.printRecord(word.getId(), word.getText(), letterSoundCorrespondencesJsonArray, word.getUsageCount(), word.getWordType(), word.getSpellingConsistency(), rootWordId, rootWordText);
        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 : Word(ai.elimu.model.content.Word) JSONArray(org.json.JSONArray) IOException(java.io.IOException) CSVPrinter(org.apache.commons.csv.CSVPrinter) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) CSVFormat(org.apache.commons.csv.CSVFormat) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

CSVPrinter (org.apache.commons.csv.CSVPrinter)75 IOException (java.io.IOException)26 CSVFormat (org.apache.commons.csv.CSVFormat)21 OutputStreamWriter (java.io.OutputStreamWriter)18 StringWriter (java.io.StringWriter)15 ArrayList (java.util.ArrayList)15 Writer (java.io.Writer)13 FileOutputStream (java.io.FileOutputStream)12 Test (org.junit.Test)11 FileWriter (java.io.FileWriter)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 BufferedWriter (java.io.BufferedWriter)7 PrintWriter (java.io.PrintWriter)5 JSONArray (org.json.JSONArray)5 File (java.io.File)4 Map (java.util.Map)4 Word (ai.elimu.model.content.Word)3 Path (java.nio.file.Path)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 Letter (ai.elimu.model.content.Letter)2