Search in sources :

Example 66 with CSVPrinter

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();
        }
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) FileOutputStream(java.io.FileOutputStream) TableItem(org.eclipse.swt.widgets.TableItem) OutputStreamWriter(java.io.OutputStreamWriter) TableColumn(org.eclipse.swt.widgets.TableColumn) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) SharesLabelProvider(name.abuchen.portfolio.ui.util.viewers.SharesLabelProvider) CellLabelProvider(org.eclipse.jface.viewers.CellLabelProvider)

Example 67 with CSVPrinter

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();
        }
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 68 with CSVPrinter

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();
        }
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Security(name.abuchen.portfolio.model.Security) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 69 with CSVPrinter

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);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Security(name.abuchen.portfolio.model.Security) LocalDate(java.time.LocalDate) CSVPrinter(org.apache.commons.csv.CSVPrinter) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 70 with CSVPrinter

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);
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) StringWriter(java.io.StringWriter) SystemException(com.evolveum.midpoint.util.exception.SystemException) CSVFormat(org.apache.commons.csv.CSVFormat) IOException(java.io.IOException)

Aggregations

CSVPrinter (org.apache.commons.csv.CSVPrinter)71 IOException (java.io.IOException)25 CSVFormat (org.apache.commons.csv.CSVFormat)20 OutputStreamWriter (java.io.OutputStreamWriter)17 StringWriter (java.io.StringWriter)14 Writer (java.io.Writer)13 ArrayList (java.util.ArrayList)12 FileOutputStream (java.io.FileOutputStream)11 Test (org.junit.Test)11 FileWriter (java.io.FileWriter)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 BufferedWriter (java.io.BufferedWriter)6 File (java.io.File)5 PrintWriter (java.io.PrintWriter)5 JSONArray (org.json.JSONArray)5 Map (java.util.Map)4 Word (ai.elimu.model.content.Word)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Letter (ai.elimu.model.content.Letter)2 LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)2