Example usage for org.apache.commons.csv CSVPrinter println

List of usage examples for org.apache.commons.csv CSVPrinter println

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVPrinter println.

Prototype

public void println() throws IOException 

Source Link

Document

Outputs the record separator.

Usage

From source file:com.streamsets.pipeline.lib.jdbc.JdbcLoadRecordWriter.java

@Override
public List<OnRecordErrorException> writeBatch(Iterator<Record> recordIterator) throws StageException {
    final List<OnRecordErrorException> errorRecords = new LinkedList<>();
    if (!recordIterator.hasNext()) {
        return errorRecords;
    }// w ww .ja  va  2  s. c  om

    // Assume all records have the same columns.
    final Record first = recordIterator.next();
    SortedMap<String, String> columnsToParameters = recordReader.getColumnsToParameters(first,
            OperationType.LOAD_CODE, getColumnsToParameters(), getColumnsToFields());
    if (columnsToParameters.isEmpty()) {
        throw new StageException(JdbcErrors.JDBC_22);
    }

    final Set<String> columnNames = columnsToParameters.keySet();
    final String loadSql = "LOAD DATA LOCAL INFILE '' " + duplicateKeyAction.getKeyword() + " INTO TABLE "
            + getTableName() + " (" + Joiner.on(", ").join(columnNames) + ")";
    try (Connection connection = getDataSource().getConnection()) {
        Connection conn = connection.unwrap(Connection.class);
        try (PreparedStatement statement = conn.prepareStatement(loadSql)) {
            PipedInputStream is = new PipedInputStream();
            PipedOutputStream os = new PipedOutputStream(is);
            statement.getClass().getMethod("setLocalInfileInputStream", InputStream.class).invoke(statement,
                    is);

            Future<?> future = loadOutputExecutor.submit(() -> {
                try (OutputStreamWriter writer = new OutputStreamWriter(os)) {
                    CSVPrinter printer = new CSVPrinter(writer, CSVFormat.MYSQL);
                    Record record = first;
                    while (record != null) {
                        int opCode = getOperationCode(record, errorRecords);
                        if (opCode == OperationType.LOAD_CODE) {
                            for (String column : columnNames) {
                                Field field = record.get(getColumnsToFields().get(column));
                                printer.print(field.getValue());
                            }
                            printer.println();
                        } else if (opCode > 0) {
                            LOG.debug("Sending record to error due to unsupported operation {}", opCode);
                            errorRecords.add(new OnRecordErrorException(record, JdbcErrors.JDBC_70, opCode));
                        } else {
                            // It should be added to the error records.
                        }
                        record = recordIterator.hasNext() ? recordIterator.next() : null;
                    }
                    ;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });

            if (LOG.isDebugEnabled()) {
                LOG.debug("Executing query: {}", statement.toString());
            }
            statement.execute();
            future.get();
        }
        connection.commit();
    } catch (SQLException e) {
        handleSqlException(e);
    } catch (Exception e) {
        throw new StageException(JdbcErrors.JDBC_14, e.getMessage(), e);
    }
    return errorRecords;
}

From source file:com.rcv.ResultsWriter.java

private void addHeaderRows(CSVPrinter csvPrinter, String precinct) throws IOException {
    csvPrinter.printRecord("Contest", config.getContestName());
    csvPrinter.printRecord("Jurisdiction", config.getContestJurisdiction());
    csvPrinter.printRecord("Office", config.getContestOffice());
    csvPrinter.printRecord("Date", config.getContestDate());
    csvPrinter.printRecord("Threshold", winningThreshold.toString());
    if (precinct != null && !precinct.isEmpty()) {
        csvPrinter.printRecord("Precinct", precinct);
    }/*w  w w. j a va  2  s.  com*/
    csvPrinter.println();
}

From source file:biz.webgate.dominoext.poi.component.kernel.csv.XPagesDataSourceExportProcessor.java

public void process(List<CSVColumn> lstColumns, UICSV csvDef, CSVPrinter csvPrinter, FacesContext context)
        throws POIException {
    DataSource ds = csvDef.getPageDataSource();

    if (ds != null) {
        try {//from ww w  .j  av a 2 s  . c  om
            TabularDataModel tdm = getTDM(ds, context);
            for (int nCount = 0; nCount < tdm.getRowCount(); nCount++) {
                nCount++;
                for (CSVColumn cl : lstColumns) {
                    String strTitle = cl.getColumnTitle();
                    Object objCurrent = null;
                    if (strTitle != null && !"".equals(strTitle)) {
                        objCurrent = getColumnValue(cl.getColumnTitle(), tdm, context);
                    } else {
                        objCurrent = cl.executeComputeValue(context, tdm.getRowData(), nCount, csvDef.getVar(),
                                csvDef.getIndex());
                    }
                    csvPrinter.print(objCurrent);
                }
                csvPrinter.println();
            }
        } catch (Exception e) {
            throw new POIException("Error in process", e);
        }
    } else {
        throw new POIException("No DataSource found", null);
    }

}

From source file:com.rodaxsoft.mailgun.CampaignManager.java

/**
 * Saves campaign events to a CSV file with the following format:
 * <code>&lt;campaign name&gt;_(&lt;campaign id&gt;)_&lt;timestamp&gt;.csv</code>
 * @param campaignId The campaign ID/*from w  ww.  j a  va2  s  .  c  om*/
 * @throws ContextedException if a processing error occurs
 * @throws IOException if an I/O error occurs
 */
void saveCampaignEventsToCSV(String campaignId) throws ContextedException, IOException {

    Campaign campaign = getCampaign(campaignId);

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    String dateTime = format.format(new Date());
    String fileName;

    if (campaign != null) {

        String name = StringUtils.replace(campaign.getName(), " ", "_");
        fileName = new StringBuilder(name).append("_(").append(campaignId).append(")_").append(dateTime)
                .append(".csv").toString();
    } else {
        fileName = campaignId + "_" + dateTime + ".csv";
    }

    CSVPrinter csvPrinter = null;
    PrintWriter pw = null;
    CSVFormat csvFormat = null;
    try {

        pw = new PrintWriter(fileName);
        final List<Map<String, Object>> events = getEvents(campaignId);

        for (Map<String, Object> map : events) {

            if (null == csvPrinter) {
                final Set<String> keySet = map.keySet();
                int size = keySet.size();
                String[] keys = keySet.toArray(new String[size]);
                csvFormat = CSVFormat.DEFAULT.withHeader(keys);
                csvPrinter = new CSVPrinter(pw, csvFormat);
            }
            //            city   domain   tags   timestamp   region   ip   country   recipient   event   user_vars

            String[] headers = csvFormat.getHeader();
            for (String key : headers) {
                csvPrinter.print(map.get(key));
            }

            csvPrinter.println();
        }

    } finally {

        if (csvPrinter != null) {
            csvPrinter.flush();
        }

        IOUtils.closeQuietly(csvPrinter);
    }

}

From source file:com.rcv.ResultsWriter.java

private void addActionRows(CSVPrinter csvPrinter) throws IOException {
    // print eliminated candidates in first action row
    csvPrinter.print("Eliminated");
    // for each round print any candidates who were eliminated
    for (int round = 1; round <= numRounds; round++) {
        // list of all candidates eliminated in this round
        List<String> eliminated = roundToEliminatedCandidates.get(round);
        if (eliminated != null && eliminated.size() > 0) {
            addActionRowCandidates(eliminated, csvPrinter);
        } else {/*from w w w . ja  va2s .c o  m*/
            csvPrinter.print("");
        }
    }
    csvPrinter.println();

    // print elected candidates in second action row
    csvPrinter.print("Elected");
    // for each round print any candidates who were elected
    for (int round = 1; round <= numRounds; round++) {
        // list of all candidates eliminated in this round
        List<String> winners = roundToWinningCandidates.get(round);
        if (winners != null && winners.size() > 0) {
            addActionRowCandidates(winners, csvPrinter);
        } else {
            csvPrinter.print("");
        }
    }
    csvPrinter.println();
}

From source file:net.sourceforge.ganttproject.io.GanttCSVExport.java

private void writeResourceHeaders(CSVPrinter writer) throws IOException {
    if (csvOptions.bExportResourceID) {
        writer.print(i18n("tableColID"));
    }//from w  ww.j av  a  2 s  .  c  o  m
    if (csvOptions.bExportResourceName) {
        writer.print(i18n("tableColResourceName"));
    }
    if (csvOptions.bExportResourceMail) {
        writer.print(i18n("tableColResourceEMail"));
    }
    if (csvOptions.bExportResourcePhone) {
        writer.print(i18n("tableColResourcePhone"));
    }
    if (csvOptions.bExportResourceRole) {
        writer.print(i18n("tableColResourceRole"));
    }
    List<CustomPropertyDefinition> customFieldDefs = myProject.getResourceCustomPropertyManager()
            .getDefinitions();
    for (int i = 0; i < customFieldDefs.size(); i++) {
        CustomPropertyDefinition nextDef = customFieldDefs.get(i);
        writer.print(nextDef.getName());
    }
    writer.println();
    writer.println();
}

From source file:com.gs.obevo.db.apps.reveng.CsvStaticDataWriter.java

private void writeTable(DbPlatform dbtype, PhysicalSchema schema, String tableName, File directory,
        MutableSet<String> updateTimeColumns, final CSVFormat csvFormat) {
    directory.mkdirs();/*from w  ww .j av  a2s . c  o m*/
    DaTable table = this.metadataManager.getTableInfo(schema.getPhysicalName(), tableName,
            new DaSchemaInfoLevel().setRetrieveTableColumns(true));
    if (table == null) {
        System.out.println("No data found for table " + tableName);
        return;
    }
    MutableList<String> columnNames = table.getColumns().collect(DaNamedObject.TO_NAME).toList();
    final String updateTimeColumnForTable = updateTimeColumns == null ? null
            : updateTimeColumns.detect(Predicates.in(columnNames));
    if (updateTimeColumnForTable != null) {
        columnNames.remove(updateTimeColumnForTable);
        System.out.println("Will mark " + updateTimeColumnForTable + " as an updateTimeColumn on this table");
    }

    final File tableFile = new File(directory, tableName + ".csv");
    final String selectSql = String.format("SELECT %s FROM %s%s", columnNames.makeString(", "),
            dbtype.getSchemaPrefix(schema), tableName);

    // using the jdbcTempate and ResultSetHandler to avoid sql-injection warnings in findbugs
    sqlExecutor.executeWithinContext(schema, new Procedure<Connection>() {
        @Override
        public void value(Connection conn) {
            sqlExecutor.getJdbcTemplate().query(conn, selectSql, new ResultSetHandler<Void>() {
                @Override
                public Void handle(ResultSet rs) throws SQLException {
                    CSVPrinter writer = null;
                    try {
                        FileWriter fw = new FileWriter(tableFile);
                        writer = new CSVPrinter(fw, csvFormat);

                        if (updateTimeColumnForTable != null) {
                            String metadataLine = String.format("//// METADATA %s=\"%s\"",
                                    TextMarkupDocumentReader.ATTR_UPDATE_TIME_COLUMN, updateTimeColumnForTable);
                            fw.write(metadataLine + "\n"); // writing using the FileWriter directly to avoid having the quotes
                            // delimited
                        }

                        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

                        int columnCount = rs.getMetaData().getColumnCount();

                        // print headers
                        for (int i = 1; i <= columnCount; ++i) {
                            writer.print(rs.getMetaData().getColumnName(i));
                        }
                        writer.println();

                        while (rs.next()) {
                            for (int i = 1; i <= columnCount; ++i) {
                                Object object = rs.getObject(i);
                                if (object != null) {
                                    switch (rs.getMetaData().getColumnType(i)) {
                                    case Types.DATE:
                                        object = dateFormat.format(object);
                                        break;
                                    case Types.TIMESTAMP:
                                        object = dateTimeFormat.format(object);
                                        break;
                                    case Types.LONGVARCHAR:
                                    case Types.VARCHAR:
                                    case Types.CHAR:
                                        // escape the string text if declared so that the input CSV can also handle the escapes
                                        if (csvFormat.getEscapeCharacter() != null
                                                && object instanceof String) {
                                            object = ((String) object).replace(
                                                    "" + csvFormat.getEscapeCharacter(),
                                                    "" + csvFormat.getEscapeCharacter()
                                                            + csvFormat.getEscapeCharacter());
                                        }
                                        break;
                                    }
                                }
                                writer.print(object);
                            }

                            writer.println();
                        }

                        writer.flush();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        IOUtils.closeQuietly(writer);
                    }

                    return null;
                }
            });
        }
    });

    int blankFileSize = updateTimeColumnForTable == null ? 1 : 2;

    if (!tableFile.canRead() || FileUtilsCobra.readLines(tableFile).size() <= blankFileSize) {
        System.out.println("No data found for table " + tableName + "; will clean up file");
        FileUtils.deleteQuietly(tableFile);
    }
}

From source file:com.rcv.ResultsWriter.java

private void generateSummarySpreadsheet(Map<Integer, Map<String, BigDecimal>> roundTallies, String precinct,
        String outputPath) throws IOException {
    String csvPath = outputPath + ".csv";
    Logger.log(Level.INFO, "Generating summary spreadsheets: %s...", csvPath);

    // Get all candidates sorted by their first round tally. This determines the display order.
    // container for firstRoundTally
    Map<String, BigDecimal> firstRoundTally = roundTallies.get(1);
    // candidates sorted by first round tally
    List<String> sortedCandidates = sortCandidatesByTally(firstRoundTally);

    // totalActiveVotesPerRound is a map of round to total votes cast in each round
    Map<Integer, BigDecimal> totalActiveVotesPerRound = new HashMap<>();
    // round indexes over all rounds plus final results round
    for (int round = 1; round <= numRounds; round++) {
        // tally is map of candidate to tally for the current round
        Map<String, BigDecimal> tallies = roundTallies.get(round);
        // total will contain total votes for all candidates in this round
        // this is used for calculating other derived data
        BigDecimal total = BigDecimal.ZERO;
        // tally indexes over all tallies for the current round
        for (BigDecimal tally : tallies.values()) {
            total = total.add(tally);/*from  w  ww.j a  va 2 s.  com*/
        }
        totalActiveVotesPerRound.put(round, total);
    }

    // csvPrinter will be used to write output to csv file
    CSVPrinter csvPrinter;
    try {
        BufferedWriter writer = Files.newBufferedWriter(Paths.get(csvPath));
        csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT);
    } catch (IOException exception) {
        Logger.log(Level.SEVERE, "Error creating CSV file: %s\n%s", csvPath, exception.toString());
        throw exception;
    }

    // print contest info
    addHeaderRows(csvPrinter, precinct);

    // add a row header for the round column labels
    csvPrinter.print("Rounds");
    // round indexes over all rounds
    for (int round = 1; round <= numRounds; round++) {
        // label string will have the actual text which goes in the cell
        String label = String.format("Round %d", round);
        // cell for round label
        csvPrinter.print(label);
    }
    csvPrinter.println();

    // actions don't make sense in individual precinct results
    if (precinct == null || precinct.isEmpty()) {
        addActionRows(csvPrinter);
    }

    final BigDecimal totalActiveVotesFirstRound = totalActiveVotesPerRound.get(1);

    // For each candidate: for each round: output total votes
    // candidate indexes over all candidates
    for (String candidate : sortedCandidates) {
        // show each candidate row with their totals for each round
        // text for the candidate name
        String candidateDisplayName = this.config.getNameForCandidateID(candidate);
        csvPrinter.print(candidateDisplayName);

        // round indexes over all rounds
        for (int round = 1; round <= numRounds; round++) {
            // vote tally this round
            BigDecimal thisRoundTally = roundTallies.get(round).get(candidate);
            // not all candidates may have a tally in every round
            if (thisRoundTally == null) {
                thisRoundTally = BigDecimal.ZERO;
            }
            // total votes cell
            csvPrinter.print(thisRoundTally.toString());
        }
        // advance to next line
        csvPrinter.println();
    }

    // row for the inactive CVR counts
    // inactive CVR header cell
    csvPrinter.print("Inactive ballots");

    // round indexes through all rounds
    for (int round = 1; round <= numRounds; round++) {
        // count of votes inactive this round
        BigDecimal thisRoundInactive = BigDecimal.ZERO;

        if (round > 1) {
            // Exhausted count is the difference between the total votes in round 1 and the total votes
            // in the current round.
            thisRoundInactive = totalActiveVotesFirstRound.subtract(totalActiveVotesPerRound.get(round))
                    .subtract(roundToResidualSurplus.get(round));
        }
        // total votes cell
        csvPrinter.print(thisRoundInactive.toString());
    }
    csvPrinter.println();

    // row for residual surplus (if needed)
    // We check if we accumulated any residual surplus over the course of the tabulation by testing
    // whether the value in the final round is positive.
    if (roundToResidualSurplus.get(numRounds).signum() == 1) {
        csvPrinter.print("Residual surplus");
        for (int round = 1; round <= numRounds; round++) {
            csvPrinter.print(roundToResidualSurplus.get(round).toString());
        }
        csvPrinter.println();
    }

    // write xls to disk
    try {
        // output stream is used to write data to disk
        csvPrinter.flush();
        csvPrinter.close();
    } catch (IOException exception) {
        Logger.log(Level.SEVERE, "Error saving file: %s\n%s", outputPath, exception.toString());
        throw exception;
    }
}

From source file:net.sourceforge.ganttproject.io.GanttCSVExport.java

/** write the resources.
 * @throws IOException *///www  .  j  a  v  a  2s  .  c o  m
private void writeResources(CSVPrinter writer) throws IOException {
    writeResourceHeaders(writer);
    // parse all resources
    for (HumanResource p : myProject.getHumanResourceManager().getResources()) {
        // ID
        if (csvOptions.bExportResourceID) {
            writer.print(String.valueOf(p.getId()));
        }
        // Name
        if (csvOptions.bExportResourceName) {
            writer.print(p.getName());
        }
        // Mail
        if (csvOptions.bExportResourceMail) {
            writer.print(p.getMail());
        }
        // Phone
        if (csvOptions.bExportResourcePhone) {
            writer.print(p.getPhone());
        }
        // Role
        if (csvOptions.bExportResourceRole) {
            Role role = p.getRole();
            String sRoleID = role == null ? "0" : role.getPersistentID();
            writer.print(sRoleID);
        }
        List<CustomProperty> customProps = p.getCustomProperties();
        for (int j = 0; j < customProps.size(); j++) {
            CustomProperty nextProperty = customProps.get(j);
            writer.print(nextProperty.getValueAsString());
        }
        writer.println();
    }
}

From source file:net.sourceforge.ganttproject.io.GanttCSVExport.java

private void writeTaskHeaders(CSVPrinter writer) throws IOException {
    if (csvOptions.bExportTaskID) {
        writer.print(TaskDefaultColumn.ID.getName());
    }//w w  w.j  a va  2 s  . c  o  m
    if (csvOptions.bExportTaskName) {
        writer.print(TaskDefaultColumn.NAME.getName());
    }
    if (csvOptions.bExportTaskStartDate) {
        writer.print(TaskDefaultColumn.BEGIN_DATE.getName());
    }
    if (csvOptions.bExportTaskEndDate) {
        writer.print(TaskDefaultColumn.END_DATE.getName());
    }
    if (csvOptions.bExportTaskDuration) {
        writer.print(TaskDefaultColumn.DURATION.getName());
    }
    if (csvOptions.bExportTaskPercent) {
        writer.print(TaskDefaultColumn.COMPLETION.getName());
    }
    if (csvOptions.bExportTaskWebLink) {
        writer.print(i18n("webLink"));
    }
    if (csvOptions.bExportTaskResources) {
        writer.print(i18n("resources"));
    }
    if (csvOptions.bExportTaskNotes) {
        writer.print(i18n("notes"));
    }
    writer.print(TaskDefaultColumn.PREDECESSORS.getName());
    for (CustomPropertyDefinition def : myProject.getTaskCustomColumnManager().getDefinitions()) {
        writer.print(def.getName());
    }
    writer.println();
    writer.println();
}