Example usage for org.apache.commons.csv CSVRecord get

List of usage examples for org.apache.commons.csv CSVRecord get

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVRecord get.

Prototype

public String get(final String name) 

Source Link

Document

Returns a value by name.

Usage

From source file:org.sonar.server.db.migrations.v51.FeedFileSourcesBinaryData.java

private byte[] toBinary(Long fileSourceId, @Nullable String data) {
    FileSourceDb.Data.Builder dataBuilder = FileSourceDb.Data.newBuilder();
    CSVParser parser = null;/*from www .  jav  a 2 s  .  c o  m*/
    try {
        if (data != null) {
            parser = CSVParser.parse(data, CSVFormat.DEFAULT);
            Iterator<CSVRecord> rows = parser.iterator();
            int line = 1;
            while (rows.hasNext()) {
                CSVRecord row = rows.next();
                if (row.size() == 16) {

                    FileSourceDb.Line.Builder lineBuilder = dataBuilder.addLinesBuilder();
                    lineBuilder.setLine(line);
                    String s = row.get(0);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setScmRevision(s);
                    }
                    s = row.get(1);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setScmAuthor(s);
                    }
                    s = row.get(2);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setScmDate(DateUtils.parseDateTimeQuietly(s).getTime());
                    }
                    s = row.get(3);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setUtLineHits(Integer.parseInt(s));
                    }
                    s = row.get(4);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setUtConditions(Integer.parseInt(s));
                    }
                    s = row.get(5);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setUtCoveredConditions(Integer.parseInt(s));
                    }
                    s = row.get(6);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setItLineHits(Integer.parseInt(s));
                    }
                    s = row.get(7);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setItConditions(Integer.parseInt(s));
                    }
                    s = row.get(8);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setItCoveredConditions(Integer.parseInt(s));
                    }
                    s = row.get(9);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setOverallLineHits(Integer.parseInt(s));
                    }
                    s = row.get(10);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setOverallConditions(Integer.parseInt(s));
                    }
                    s = row.get(11);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setOverallCoveredConditions(Integer.parseInt(s));
                    }
                    s = row.get(12);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setHighlighting(s);
                    }
                    s = row.get(13);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setSymbols(s);
                    }
                    s = row.get(14);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.addAllDuplication(splitIntegers(s));
                    }
                    s = row.get(15);
                    if (s != null) {
                        lineBuilder.setSource(s);
                    }
                }
                line++;
            }
        }
        return FileSourceDto.encodeSourceData(dataBuilder.build());
    } catch (Exception e) {
        throw new IllegalStateException(
                "Invalid FILE_SOURCES.DATA on row with ID " + fileSourceId + ": " + data, e);
    } finally {
        IOUtils.closeQuietly(parser);
    }
}

From source file:org.sonar.server.source.index.SourceLineResultSetIterator.java

@Override
protected SourceFile read(ResultSet rs) throws SQLException {
    String projectUuid = rs.getString(1);
    String fileUuid = rs.getString(2);
    Long updatedAt = SqlUtil.getLong(rs, 3);
    if (updatedAt == null) {
        updatedAt = System.currentTimeMillis();
    }//ww  w.j a v a2  s  .  c  om
    Date updatedDate = new Date(updatedAt);
    SourceFile result = new SourceFile(fileUuid, updatedAt);

    Reader csv = rs.getCharacterStream(4);
    if (csv == null) {
        return result;
    }

    int line = 1;
    CSVParser csvParser = null;
    try {
        csvParser = new CSVParser(csv, CSVFormat.DEFAULT);

        for (CSVRecord csvRecord : csvParser) {
            SourceLineDoc doc = new SourceLineDoc(Maps.<String, Object>newHashMap());

            doc.setProjectUuid(projectUuid);
            doc.setFileUuid(fileUuid);
            doc.setLine(line);
            doc.setUpdateDate(updatedDate);
            doc.setScmRevision(csvRecord.get(0));
            doc.setScmAuthor(csvRecord.get(1));
            doc.setScmDate(DateUtils.parseDateTimeQuietly(csvRecord.get(2)));
            // UT
            doc.setUtLineHits(parseIntegerFromRecord(csvRecord.get(3)));
            doc.setUtConditions(parseIntegerFromRecord(csvRecord.get(4)));
            doc.setUtCoveredConditions(parseIntegerFromRecord(csvRecord.get(5)));
            // IT
            doc.setItLineHits(parseIntegerFromRecord(csvRecord.get(6)));
            doc.setItConditions(parseIntegerFromRecord(csvRecord.get(7)));
            doc.setItCoveredConditions(parseIntegerFromRecord(csvRecord.get(8)));
            // OVERALL
            doc.setOverallLineHits(parseIntegerFromRecord(csvRecord.get(9)));
            doc.setOverallConditions(parseIntegerFromRecord(csvRecord.get(10)));
            doc.setOverallCoveredConditions(parseIntegerFromRecord(csvRecord.get(11)));
            doc.setHighlighting(csvRecord.get(12));
            doc.setSymbols(csvRecord.get(13));

            doc.setDuplications(parseDuplications(csvRecord.get(14)));
            doc.setSource(csvRecord.get(csvRecord.size() - 1));

            result.addLine(doc);

            line++;
        }
    } catch (IOException ioError) {
        throw new IllegalStateException(
                "Impossible to open stream for file_sources.data with file_uuid " + fileUuid, ioError);
    } catch (ArrayIndexOutOfBoundsException lineError) {
        throw new IllegalStateException(
                String.format("Impossible to parse source line data, stuck at line %d", line), lineError);
    } finally {
        IOUtils.closeQuietly(csv);
        IOUtils.closeQuietly(csvParser);
    }

    return result;
}

From source file:org.tanaguru.rules.doc.utils.updateAw22toRgaa30.CopyFiles.java

public void copyFilesAvailable() throws IOException {
    ResourceBundle resourceBundle = ResourceBundle.getBundle("parameters");
    for (CSVRecord record : getCsv(resourceBundle)) {
        String oldTest = record.get("originalReference");
        String newTest = record.get("targetReference");
        String state = record.get("can_copy");
        if (state.equalsIgnoreCase("1")) {
            copyRuleClassWithValidState(oldTest, newTest, resourceBundle);
            copyTestCasesWithValidState(oldTest, newTest, resourceBundle);
            copyRuleUnitTestWithValidState(oldTest, newTest, resourceBundle, false);
        }//  www  .  j a va2  s  .co m
        if (state.equalsIgnoreCase("2")) {
            copyRuleClassWithValidState(oldTest, newTest, resourceBundle);
            copyRuleUnitTestWithValidState(oldTest, newTest, resourceBundle, true);
            copyTestCasesWithValidState(oldTest, newTest, resourceBundle);
        }
    }
}

From source file:org.thegalactic.context.io.ContextSerializerCsv.java

/**
 * Read a context from a csv file.//from w  w w.j a v  a2s.co  m
 *
 * The following format is respected:
 *
 * The first line contains the attribute names, the other lines contains the
 * observations identifier followed by boolean values
 *
 * ~~~
 * "",a,b,c,d,e
 * 1,1,0,1,0,0
 * 2,1,1,0,0,0
 * 3,0,1,0,1,1
 * 4,0,0,1,0,1
 * ~~~
 *
 * If the first attribute is the empty string, the first column corresponds
 * to the individual identifiers. In the other case, the individual
 * identifiers will be generated by successive integers.
 *
 * ~~~
 * a,b,c,d,e
 * 1,0,1,0,0
 * 1,1,0,0,0
 * 0,1,0,1,1
 * 0,0,1,0,1
 * ~~~
 *
 * @param context a context to read
 * @param file    a file
 *
 * @throws IOException When an IOException occurs
 */
public void read(Context context, BufferedReader file) throws IOException {
    // Parse the file
    CSVParser parser = CSVFormat.RFC4180.parse(file);

    // Get the records and record size
    List<CSVRecord> records = parser.getRecords();
    int length = records.size();

    // Verify length
    if (length == 0) {
        throw new IOException("CSV cannot be empty");
    }

    // Get the attributes and the attribute size
    CSVRecord attributes = records.get(0);
    int size = attributes.size();

    // Detect invalid attribute size
    if (size == 1 && attributes.get(0).equals("")) {
        throw new IOException("Attribute size cannot be 0");
    }

    // Index of the first attribute
    int first = 0;
    if (attributes.get(0).equals("")) {
        first = 1;
    }

    // Get the attributes
    for (int i = first; i < size; i++) {
        String attribute = attributes.get(i);

        // Detect duplicated attribute
        if (!context.addToAttributes(attribute)) {
            throw new IOException("Duplicated attribute");
        }

        // Detect empty attribute
        if ("".equals(attribute)) {
            throw new IOException("Empty attribute");
        }
    }

    // Get the data
    for (int j = 1; j < length; j++) {
        // Get the current record
        CSVRecord record = records.get(j);

        // Detect incorrect size
        if (record.size() != size) {
            throw new IOException("Line does not have the correct number of attributes");
        }

        // Get the observation identifier
        String identifier;
        if (first == 1) {
            identifier = record.get(0);
        } else {
            identifier = String.valueOf(j);
        }

        // Detect duplicated identifier
        if (!context.addToObservations(identifier)) {
            throw new IOException("Duplicated identifier");
        }

        // Add the extent/intent for the current identifier and current attribute
        for (int i = first; i < size; i++) {
            if (record.get(i).equals("1")) {
                context.addExtentIntent(identifier, attributes.get(i));
            }
        }
    }

    // Close the parser
    parser.close();
    context.setBitSets();
}

From source file:org.thingsboard.server.service.install.cql.CassandraDbHelper.java

private static void setColumnValue(TableMetadata tableMetadata, String column, CSVRecord record,
        BoundStatement boundStatement) {
    String value = record.get(column);
    DataType type = tableMetadata.getColumn(column).getType();
    if (value == null) {
        boundStatement.setToNull(column);
    } else if (type == DataType.cdouble()) {
        boundStatement.setDouble(column, Double.valueOf(value));
    } else if (type == DataType.cint()) {
        boundStatement.setInt(column, Integer.valueOf(value));
    } else if (type == DataType.bigint()) {
        boundStatement.setLong(column, Long.valueOf(value));
    } else if (type == DataType.uuid()) {
        boundStatement.setUUID(column, UUID.fromString(value));
    } else if (type == DataType.timeuuid()) {
        boundStatement.setUUID(column, UUID.fromString(value));
    } else if (type == DataType.cfloat()) {
        boundStatement.setFloat(column, Float.valueOf(value));
    } else if (type == DataType.timestamp()) {
        boundStatement.setTimestamp(column, new Date(Long.valueOf(value)));
    } else {//from ww w  .j  ava2  s  . co  m
        boundStatement.setString(column, value);
    }
}

From source file:org.thingsboard.server.service.install.sql.SqlDbHelper.java

private static void setColumnValue(int index, String column, CSVRecord record,
        PreparedStatement preparedStatement) throws SQLException {
    String value = record.get(column);
    int type = preparedStatement.getParameterMetaData().getParameterType(index + 1);
    preparedStatement.setObject(index + 1, value, type);
}

From source file:org.totschnig.myexpenses.task.CsvImportTask.java

private String saveGetFromRecord(CSVRecord record, int index) {
    return record.size() > index ? record.get(index).trim() : "";
}

From source file:org.transitime.custom.sfmta.delayTimes.Intersection.java

private static String getValue(CSVRecord record, String name) {
    if (!record.isSet(name)) {
        logger.error("Column {} not defined", name);
        return null;
    }/*ww  w  .  java  2s .c  o m*/

    // Get the value. First trim whitespace so that
    // value will be consistent. 
    String value = record.get(name).trim();
    return value;
}

From source file:org.transitime.gtfs.readers.GtfsFrequenciesReader.java

@Override
public GtfsFrequency handleRecord(CSVRecord record, boolean supplemental) throws ParseException {
    if (GtfsData.tripNotFiltered(record.get("trip_id")))
        return new GtfsFrequency(record, supplemental, getFileName());
    else/*w  w  w.  j ava 2s.  c o m*/
        return null;
}

From source file:org.transitime.gtfs.readers.GtfsRoutesReader.java

@Override
public GtfsRoute handleRecord(CSVRecord record, boolean supplemental) throws ParseException {
    if (GtfsData.routeNotFiltered(record.get("route_id")))
        return new GtfsRoute(record, supplemental, getFileName());
    else//from ww  w.  j  av  a  2  s  . c  o m
        return null;
}