use of org.apache.commons.csv.CSVPrinter in project portfolio by buchen.
the class TableViewerCSVExporter method writeToFile.
@Override
protected void writeToFile(File file) throws IOException {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
CSVPrinter printer = new CSVPrinter(writer);
printer.setStrategy(STRATEGY);
int columnCount = viewer.getTable().getColumnCount();
// write header
for (TableColumn column : viewer.getTable().getColumns()) printer.print(column.getText());
printer.println();
// check for "special" label provider
SharesLabelProvider[] labelProvider = new SharesLabelProvider[columnCount];
for (int ii = 0; ii < columnCount; ii++) {
CellLabelProvider p = viewer.getLabelProvider(ii);
if (p instanceof SharesLabelProvider)
labelProvider[ii] = (SharesLabelProvider) p;
}
// write body
for (TableItem item : viewer.getTable().getItems()) {
for (int ii = 0; ii < columnCount; ii++) {
if (labelProvider[ii] != null) {
Long value = labelProvider[ii].getValue(item.getData());
// $NON-NLS-1$
printer.print(value != null ? Values.Share.format(value) : "");
} else
printer.print(item.getText(ii));
}
printer.println();
}
}
}
use of org.apache.commons.csv.CSVPrinter in project portfolio by buchen.
the class CSVExporter method exportAccountTransactions.
public void exportAccountTransactions(File file, Account account) throws IOException {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
CSVPrinter printer = new CSVPrinter(writer);
printer.setStrategy(STRATEGY);
printer.println(new String[] { //
Messages.CSVColumn_Date, //
Messages.CSVColumn_Type, //
Messages.CSVColumn_Value, //
Messages.CSVColumn_TransactionCurrency, //
Messages.CSVColumn_Taxes, //
Messages.CSVColumn_Shares, //
Messages.CSVColumn_ISIN, //
Messages.CSVColumn_WKN, //
Messages.CSVColumn_TickerSymbol, //
Messages.CSVColumn_SecurityName, Messages.CSVColumn_Note });
for (AccountTransaction t : account.getTransactions()) {
printer.print(t.getDateTime().toString());
printer.print(t.getType().toString());
printer.print(Values.Amount.format(t.getType().isDebit() ? -t.getAmount() : t.getAmount()));
printer.print(t.getCurrencyCode());
printer.print(t.getType() == AccountTransaction.Type.DIVIDENDS ? Values.Amount.format(t.getUnitSum(Unit.Type.TAX).getAmount()) : // $NON-NLS-1$
"");
// $NON-NLS-1$
printer.print(t.getShares() != 0 ? Values.Share.format(t.getShares()) : "");
printSecurityInfo(printer, t);
printer.print(escapeNull(t.getNote()));
printer.println();
}
}
}
use of org.apache.commons.csv.CSVPrinter in project portfolio by buchen.
the class CSVExporter method exportSecurityMasterData.
public void exportSecurityMasterData(File file, List<Security> securities) throws IOException {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
CSVPrinter printer = new CSVPrinter(writer);
printer.setStrategy(STRATEGY);
printer.println(new String[] { //
Messages.CSVColumn_ISIN, //
Messages.CSVColumn_WKN, //
Messages.CSVColumn_TickerSymbol, //
Messages.CSVColumn_SecurityName, Messages.CSVColumn_Currency, Messages.CSVColumn_Note });
for (Security s : securities) {
printer.print(escapeNull(s.getIsin()));
printer.print(escapeNull(s.getWkn()));
printer.print(escapeNull(s.getTickerSymbol()));
printer.print(escapeNull(s.getName()));
printer.print(escapeNull(s.getCurrencyCode()));
printer.print(escapeNull(s.getNote()));
printer.println();
}
}
}
use of org.apache.commons.csv.CSVPrinter in project portfolio by buchen.
the class CSVExporter method exportMergedSecurityPrices.
public void exportMergedSecurityPrices(File file, List<Security> securities) throws IOException {
// prepare: (a) find earliest date (b) ignore securities w/o quotes
LocalDate earliestDate = null;
List<Security> export = new ArrayList<Security>(securities.size());
for (Security s : securities) {
List<SecurityPrice> prices = s.getPrices();
if (!prices.isEmpty()) {
export.add(s);
LocalDate quoteDate = prices.get(0).getDate();
if (earliestDate == null)
earliestDate = quoteDate;
else
earliestDate = earliestDate.isAfter(quoteDate) ? quoteDate : earliestDate;
}
}
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
CSVPrinter printer = new CSVPrinter(writer);
printer.setStrategy(STRATEGY);
// write header
printer.print(Messages.CSVColumn_Date);
for (Security security : export) printer.print(security.getExternalIdentifier());
printer.println();
// stop if no securities exist
if (earliestDate == null)
return;
// write quotes
LocalDate pointer = earliestDate;
LocalDate today = LocalDate.now();
while (pointer.compareTo(today) <= 0) {
// check if any quotes exist for that day at all
int[] indices = new int[export.size()];
int ii = 0;
for (Security security : export) {
SecurityPrice p = new SecurityPrice(pointer, 0);
indices[ii] = Collections.binarySearch(security.getPrices(), p);
ii++;
}
boolean hasValues = false;
for (ii = 0; ii < indices.length && !hasValues; ii++) hasValues = indices[ii] >= 0;
if (hasValues) {
printer.print(pointer.toString());
for (ii = 0; ii < indices.length; ii++) {
if (indices[ii] < 0)
// $NON-NLS-1$
printer.print("");
else
printer.print(Values.Quote.format(export.get(ii).getPrices().get(indices[ii]).getValue()));
}
printer.println();
}
pointer = pointer.plusDays(1);
}
}
}
use of org.apache.commons.csv.CSVPrinter in project midpoint by Evolveum.
the class CsvReportDataWriter method getStringData.
@Override
public String getStringData() {
try {
StringWriter stringWriter = new StringWriter();
CSVFormat csvFormat = createCsvFormat();
CSVPrinter printer = new CSVPrinter(stringWriter, csvFormat);
for (ExportedReportDataRow row : getDataRows()) {
printer.printRecord(createPhysicalColumnsList(row));
}
printer.flush();
return stringWriter.toString();
} catch (IOException e) {
throw new SystemException("Unexpected IOException: " + e.getMessage(), e);
}
}
Aggregations