Search in sources :

Example 46 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project jgnash by ccavanaugh.

the class IEXParser method retrieveSplitEvents.

private Set<SecurityHistoryEvent> retrieveSplitEvents(final SecurityNode securityNode, final LocalDate endDate) throws IOException {
    final String range = IEXChartPeriod.getPath((int) DAYS.between(endDate, LocalDate.now()));
    final String restURL = baseURL + "/stock/" + securityNode.getSymbol() + "/splits/" + range + "?token=" + tokenSupplier.get() + "&format=csv";
    final Set<SecurityHistoryEvent> historyEvents = new HashSet<>();
    final URL url = new URL(restURL);
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(ConnectionFactory.getConnectionTimeout() * 1000);
    // return an empty list if the response is bad
    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
        connection.disconnect();
        return historyEvents;
    }
    try (final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
        reader.mark(READ_AHEAD_LIMIT);
        if (isDataOkay(reader.readLine())) {
            reader.reset();
            try (final CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader())) {
                LocalDate startDate = LocalDate.now().minusDays(1);
                for (final CSVRecord record : csvParser) {
                    final LocalDate date = LocalDate.parse(record.get("exDate"), DateTimeFormatter.ISO_DATE);
                    if (DateUtils.between(date, endDate, startDate)) {
                        final BigDecimal toFactor = new BigDecimal(record.get("toFactor"));
                        final BigDecimal fromFactor = new BigDecimal(record.get("fromFactor"));
                        final BigDecimal ratio = fromFactor.divide(toFactor, MathConstants.mathContext);
                        final SecurityHistoryEvent event = new SecurityHistoryEvent(SecurityHistoryEventType.SPLIT, date, ratio);
                        historyEvents.add(event);
                    }
                }
            } catch (final IllegalArgumentException iae) {
                LogUtil.logSevere(IEXParser.class, iae);
            }
        }
    }
    return historyEvents;
}
Also used : InputStreamReader(java.io.InputStreamReader) LocalDate(java.time.LocalDate) URL(java.net.URL) BigDecimal(java.math.BigDecimal) SecurityHistoryEvent(jgnash.engine.SecurityHistoryEvent) HttpURLConnection(java.net.HttpURLConnection) CSVParser(org.apache.commons.csv.CSVParser) BufferedReader(java.io.BufferedReader) CSVRecord(org.apache.commons.csv.CSVRecord) HashSet(java.util.HashSet)

Example 47 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project Asqatasun by Asqatasun.

the class ExtractCsvAndCopy method copy.

public void copy(Iterable<CSVRecord> records) {
    for (CSVRecord record : records) {
        String targetReference = record.get(TARGET_REFERENTIAL);
        String originalReference = record.get(ORIGINAL_REFERENTIAL);
        String canCopy = record.get(CAN_COPY);
        if (canCopy.equals("1")) {
            String testCode = targetReference.split("-")[1].replace(".", "-");
            String oldTestCode = originalReference.split("-")[1].replace(".", "-");
            try {
                FileUtils.copyFile(getFile(PATH_TO_OLD_HTML_FILE + "Rule-" + oldTestCode + ".html"), getFile(PATH_TO_CREATE_HTML_FILE + "Rule-" + testCode + ".html"));
            } catch (FileNotFoundException ex) {
                System.out.println("Rule missing : " + oldTestCode);
            } catch (IOException ex) {
            }
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) CSVRecord(org.apache.commons.csv.CSVRecord) IOException(java.io.IOException)

Example 48 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project Asqatasun by Asqatasun.

the class CodeGeneratorMojo method generate.

/**
 * @param ve
 * @param records
 * @throws java.io.IOException
 */
public void generate(VelocityEngine ve, Iterable<CSVRecord> records) throws IOException, ResourceNotFoundException, ParseErrorException, Exception {
    // Getting the Template
    Template ruleTemplate = ve.getTemplate(templateRule.getPath());
    Template pomTemplate = ve.getTemplate(pom.getPath());
    Template webappBeansTemplate = ve.getTemplate(templateBeansWebapp.getPath());
    Template webappBeansExpressionTemplate = ve.getTemplate(templateBeansExpression.getPath());
    Template auditResultConsoleTemplate = ve.getTemplate(templateAuditResultConsole.getPath());
    Template auditSetUpFormTemplate = ve.getTemplate(templateAuditSetUpForm.getPath());
    Template testCaseTemplate = ve.getTemplate(templateTestCase.getPath());
    Template descriptorTemplate = ve.getTemplate(templateDescriptor.getPath());
    Template installTemplate = ve.getTemplate(templateInstallSh.getPath());
    Template unitTestTemplate = ve.getTemplate(templateUnitTest.getPath());
    Template ruleImplementationTestCaseTemplate = ve.getTemplate(templateRuleImplementationTestCase.getPath());
    // Create a context and add data to the templateRule placeholder
    VelocityContext context = new VelocityContext();
    // Fetch templateRule into a StringWriter
    FileGenerator fg = new FileGenerator(referential, referentialLabel, destinationFolder, refDescriptor, isCriterionPresent);
    fg.createI18NFiles(langSet);
    // using the i18n keys.
    for (CSVRecord record : records) {
        String testLabelDefault = record.get(TEST_LABEL_COLUMN_NAME + langSet.first());
        String test = record.get(TEST_CODE_COLUMN_NAME);
        for (String lang : langSet) {
            writeToI18NFile(fg, record, lang);
        }
        IS_I18N_REFERENTIAL_CREATED = true;
        if (!isCriterionPresent) {
            test = test.concat("-1");
        }
        context = fg.getContextRuleClassFile(referential, PACKAGE_NAME, test, testLabelDefault, context);
        fg.writeFileCodeGenerate(context, ruleTemplate, getClassNameFromCsvColumn(record));
        fg.writeUnitTestGenerate(context, unitTestTemplate, testLabelDefault, getClassNameFromCsvColumn(record));
        String[] testsCasesState = { "Passed", "Failed", "NMI", "NA" };
        for (int i = 0; i < testsCasesState.length; i++) {
            context.put("state", testsCasesState[i]);
            fg.writeTestCaseGenerate(context, testCaseTemplate, getClassNameFromCsvColumn(record), String.valueOf(i + 1));
        }
        addLevelAndScopeToList(record);
    }
    fg.createSqlReference();
    fg.createSqlTheme();
    fg.createSqlCritere();
    fg.createSqlTest(levelList, scopeList);
    fg.createSqlParameters();
    fg.writeAuditSetUpFormBeanGenerate(context, auditSetUpFormTemplate);
    fg.writeAuditResultConsoleBeanGenerate(context, auditResultConsoleTemplate);
    fg.writeWebappBeansGenerate(context, webappBeansTemplate);
    fg.writeWebappBeansExpressionGenerate(context, webappBeansExpressionTemplate);
    fg.writeInstallGenerate(context, installTemplate);
    fg.writeDescriptorGenerate(context, descriptorTemplate);
    fg.writeRuleImplementationTestCaseGenerate(context, ruleImplementationTestCaseTemplate);
    fg.adaptPom(context, pomTemplate);
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) CSVRecord(org.apache.commons.csv.CSVRecord) Template(org.apache.velocity.Template)

Example 49 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project midpoint by Evolveum.

the class ImportController method parseColumnsAsVariablesFromFile.

public List<VariablesMap> parseColumnsAsVariablesFromFile(ReportDataType reportData) throws IOException {
    List<String> headers = new ArrayList<>();
    Reader reader = Files.newBufferedReader(Paths.get(reportData.getFilePath()));
    CSVFormat csvFormat = support.createCsvFormat();
    if (compiledCollection != null) {
        Class<ObjectType> type = compiledCollection.getTargetClass(reportService.getPrismContext());
        if (type == null) {
            throw new IllegalArgumentException("Couldn't define type of imported objects");
        }
        PrismObjectDefinition<?> def = reportService.getPrismContext().getSchemaRegistry().findItemDefinitionByCompileTimeClass(type, PrismObjectDefinition.class);
        for (GuiObjectColumnType column : columns) {
            Validate.notNull(column.getName(), "Name of column is null");
            String label = GenericSupport.getLabel(column, def, localizationService);
            headers.add(label);
        }
    } else {
        csvFormat = csvFormat.withFirstRecordAsHeader();
    }
    if (support.isHeader()) {
        if (!headers.isEmpty()) {
            String[] arrayHeader = new String[headers.size()];
            arrayHeader = headers.toArray(arrayHeader);
            csvFormat = csvFormat.withHeader(arrayHeader);
        }
        csvFormat = csvFormat.withSkipHeaderRecord(true);
    } else {
        if (headers.isEmpty()) {
            throw new IllegalArgumentException("Couldn't find headers please " + "define them via view element or write them to csv file and set " + "header element in file format configuration to true.");
        }
        csvFormat = csvFormat.withSkipHeaderRecord(false);
    }
    CSVParser csvParser = new CSVParser(reader, csvFormat);
    if (headers.isEmpty()) {
        headers = csvParser.getHeaderNames();
    }
    List<VariablesMap> variablesMaps = new ArrayList<>();
    for (CSVRecord csvRecord : csvParser) {
        VariablesMap variables = new VariablesMap();
        for (String name : headers) {
            String value;
            if (support.isHeader()) {
                value = csvRecord.get(name);
            } else {
                value = csvRecord.get(headers.indexOf(name));
            }
            if (value != null && value.isEmpty()) {
                value = null;
            }
            if (value != null && value.contains(support.getMultivalueDelimiter())) {
                String[] realValues = value.split(support.getMultivalueDelimiter());
                variables.put(name, Arrays.asList(realValues), String.class);
            } else {
                variables.put(name, value, String.class);
            }
        }
        variablesMaps.add(variables);
    }
    return variablesMaps;
}
Also used : Reader(java.io.Reader) CSVParser(org.apache.commons.csv.CSVParser) CSVFormat(org.apache.commons.csv.CSVFormat) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) CSVRecord(org.apache.commons.csv.CSVRecord)

Example 50 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project Anserini by castorini.

the class DprNqTopicReader method read.

@Override
public SortedMap<Integer, Map<String, String>> read(BufferedReader reader) throws IOException {
    SortedMap<Integer, Map<String, String>> map = new TreeMap<>();
    CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withDelimiter('\t').withTrim());
    Integer topicID = 0;
    for (CSVRecord csvRecord : csvParser) {
        Map<String, String> fields = new HashMap<>();
        fields.put("title", csvRecord.get(0).toString());
        fields.put("answers", csvRecord.get(1).toString());
        map.put(topicID, fields);
        topicID += 1;
    }
    csvParser.close();
    return map;
}
Also used : HashMap(java.util.HashMap) CSVParser(org.apache.commons.csv.CSVParser) CSVRecord(org.apache.commons.csv.CSVRecord) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap) SortedMap(java.util.SortedMap)

Aggregations

CSVRecord (org.apache.commons.csv.CSVRecord)127 CSVParser (org.apache.commons.csv.CSVParser)71 IOException (java.io.IOException)40 CSVFormat (org.apache.commons.csv.CSVFormat)40 ArrayList (java.util.ArrayList)36 Reader (java.io.Reader)24 StringReader (java.io.StringReader)22 InputStreamReader (java.io.InputStreamReader)18 FileReader (java.io.FileReader)16 Test (org.junit.Test)14 Path (java.nio.file.Path)13 HashMap (java.util.HashMap)11 File (java.io.File)10 PreparedStatement (java.sql.PreparedStatement)10 InputStream (java.io.InputStream)9 ResultSet (java.sql.ResultSet)9 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)9 CSVCommonsLoader (org.apache.phoenix.util.CSVCommonsLoader)9 BufferedReader (java.io.BufferedReader)8 Map (java.util.Map)7