Search in sources :

Example 11 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project pwm by pwm-project.

the class LocalDBUtility method exportLocalDB.

public void exportLocalDB(final OutputStream outputStream, final Appendable debugOutput, final boolean showLineCount) throws PwmOperationalException, IOException {
    if (outputStream == null) {
        throw new PwmOperationalException(PwmError.ERROR_UNKNOWN, "outputFileStream for exportLocalDB cannot be null");
    }
    final int totalLines;
    if (showLineCount) {
        writeStringToOut(debugOutput, "counting records in LocalDB...");
        exportLineCounter = 0;
        for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
            if (loopDB.isBackup()) {
                exportLineCounter += localDB.size(loopDB);
            }
        }
        totalLines = exportLineCounter;
        writeStringToOut(debugOutput, " total lines: " + totalLines);
    } else {
        totalLines = 0;
    }
    exportLineCounter = 0;
    writeStringToOut(debugOutput, "export beginning");
    final long startTime = System.currentTimeMillis();
    final Timer statTimer = new Timer(true);
    statTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            if (showLineCount) {
                final float percentComplete = (float) exportLineCounter / (float) totalLines;
                final String percentStr = DecimalFormat.getPercentInstance().format(percentComplete);
                writeStringToOut(debugOutput, "exported " + exportLineCounter + " records, " + percentStr + " complete");
            } else {
                writeStringToOut(debugOutput, "exported " + exportLineCounter + " records");
            }
        }
    }, 30 * 1000, 30 * 1000);
    try (CSVPrinter csvPrinter = JavaHelper.makeCsvPrinter(new GZIPOutputStream(outputStream, GZIP_BUFFER_SIZE))) {
        csvPrinter.printComment(PwmConstants.PWM_APP_NAME + " " + PwmConstants.SERVLET_VERSION + " LocalDB export on " + JavaHelper.toIsoDate(new Date()));
        for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
            if (loopDB.isBackup()) {
                csvPrinter.printComment("Export of " + loopDB.toString());
                final LocalDB.LocalDBIterator<String> localDBIterator = localDB.iterator(loopDB);
                try {
                    while (localDBIterator.hasNext()) {
                        final String key = localDBIterator.next();
                        final String value = localDB.get(loopDB, key);
                        csvPrinter.printRecord(loopDB.toString(), key, value);
                        exportLineCounter++;
                    }
                } finally {
                    localDBIterator.close();
                }
                csvPrinter.flush();
            }
        }
        csvPrinter.printComment("export completed at " + JavaHelper.toIsoDate(new Date()));
    } catch (IOException e) {
        writeStringToOut(debugOutput, "IO error during localDB export: " + e.getMessage());
    } finally {
        statTimer.cancel();
    }
    writeStringToOut(debugOutput, "export complete, exported " + exportLineCounter + " records in " + TimeDuration.fromCurrent(startTime).asLongString());
}
Also used : IOException(java.io.IOException) Date(java.util.Date) PwmOperationalException(password.pwm.error.PwmOperationalException) CSVPrinter(org.apache.commons.csv.CSVPrinter) Timer(java.util.Timer) TimerTask(java.util.TimerTask) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Example 12 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project pwm by pwm-project.

the class DebugItemGenerator method outputZipDebugFile.

static void outputZipDebugFile(final PwmRequest pwmRequest, final ZipOutputStream zipOutput, final String pathPrefix) throws IOException, PwmUnrecoverableException {
    final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
    final String debugFileName = "zipDebugGeneration.csv";
    final ByteArrayOutputStream debugGeneratorLogBaos = new ByteArrayOutputStream();
    final CSVPrinter debugGeneratorLogFile = JavaHelper.makeCsvPrinter(debugGeneratorLogBaos);
    for (final Class<? extends DebugItemGenerator.Generator> serviceClass : DEBUG_ZIP_ITEM_GENERATORS) {
        try {
            final Instant startTime = Instant.now();
            LOGGER.trace(pwmRequest, "beginning output of item " + serviceClass.getSimpleName());
            final Object newInstance = serviceClass.newInstance();
            final DebugItemGenerator.Generator newGeneratorItem = (DebugItemGenerator.Generator) newInstance;
            zipOutput.putNextEntry(new ZipEntry(pathPrefix + newGeneratorItem.getFilename()));
            newGeneratorItem.outputItem(pwmApplication, pwmRequest, zipOutput);
            zipOutput.closeEntry();
            zipOutput.flush();
            final String finishMsg = "completed output of " + newGeneratorItem.getFilename() + " in " + TimeDuration.fromCurrent(startTime).asCompactString();
            LOGGER.trace(pwmRequest, finishMsg);
            debugGeneratorLogFile.printRecord(JavaHelper.toIsoDate(Instant.now()), finishMsg);
        } catch (Throwable e) {
            final String errorMsg = "unexpected error executing debug item output class '" + serviceClass.getName() + "', error: " + e.toString();
            LOGGER.error(pwmRequest, errorMsg);
            debugGeneratorLogFile.printRecord(JavaHelper.toIsoDate(Instant.now()), errorMsg);
            final Writer stackTraceOutput = new StringWriter();
            e.printStackTrace(new PrintWriter(stackTraceOutput));
            debugGeneratorLogFile.printRecord(stackTraceOutput);
        }
    }
    try {
        zipOutput.putNextEntry(new ZipEntry(pathPrefix + debugFileName));
        debugGeneratorLogFile.flush();
        zipOutput.write(debugGeneratorLogBaos.toByteArray());
        zipOutput.closeEntry();
    } catch (Exception e) {
        LOGGER.error("error generating " + debugFileName + ": " + e.getMessage());
    }
    zipOutput.flush();
}
Also used : PwmApplication(password.pwm.PwmApplication) Instant(java.time.Instant) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) IOException(java.io.IOException) CSVPrinter(org.apache.commons.csv.CSVPrinter) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) LdapDebugDataGenerator(password.pwm.ldap.LdapDebugDataGenerator) PrintWriter(java.io.PrintWriter)

Example 13 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project pwm by pwm-project.

the class ReportCsvUtility method outputSummaryToCsv.

public void outputSummaryToCsv(final OutputStream outputStream, final Locale locale) throws IOException {
    final List<ReportSummaryData.PresentationRow> outputList = reportService.getSummaryData().asPresentableCollection(pwmApplication.getConfig(), locale);
    final CSVPrinter csvPrinter = JavaHelper.makeCsvPrinter(outputStream);
    for (final ReportSummaryData.PresentationRow presentationRow : outputList) {
        final List<String> headerRow = new ArrayList<>();
        headerRow.add(presentationRow.getLabel());
        headerRow.add(presentationRow.getCount());
        headerRow.add(presentationRow.getPct());
        csvPrinter.printRecord(headerRow);
    }
    csvPrinter.close();
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) ArrayList(java.util.ArrayList)

Example 14 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project irida by phac-nml.

the class ProjectsController method writeProjectsToCsvFile.

/**
 * Write the projects as a CSV file
 *
 * @param headers
 * 		{@link List} for {@link String} headers for the information.
 * @param projects
 * 		{@link List} of {@link DTProject} to export
 * @param locale
 * 		{@link Locale}
 * @param response
 * 		{@link HttpServletResponse}
 *
 * @throws IOException
 * 		Thrown if cannot get the {@link PrintWriter} for the response
 */
private void writeProjectsToCsvFile(List<String> headers, List<DTProject> projects, Locale locale, HttpServletResponse response) throws IOException {
    PrintWriter writer = response.getWriter();
    try (CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withRecordSeparator(System.lineSeparator()))) {
        printer.printRecord(headers);
        DateFormat dateFormat = new SimpleDateFormat(messageSource.getMessage("locale.date.long", null, locale));
        for (DTProject p : projects) {
            List<String> record = new ArrayList<>();
            record.add(String.valueOf(p.getId()));
            record.add(p.getName());
            record.add(p.getOrganism());
            record.add(String.valueOf(p.getSamples()));
            record.add(dateFormat.format(p.getCreatedDate()));
            record.add(dateFormat.format(p.getModifiedDate()));
            printer.printRecord(record);
        }
        printer.flush();
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) DTProject(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) PrintWriter(java.io.PrintWriter)

Example 15 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project solr-cmd-utils by tblsoft.

the class CSVWriter method document.

@Override
public void document(Document document) {
    if (firstDocument) {
        try {
            if (headers == null) {
                headers = getFieldNames(document);
            }
            // PrintWriter out = new PrintWriter(absoluteFilename);
            OutputStream out = IOUtils.getOutputStream(absoluteFilename);
            CSVFormat format = CSVFormat.RFC4180;
            if (withHeaders) {
                format = format.withDelimiter(delimiter.charAt(0)).withHeader(headers);
            } else {
                format = format.withDelimiter(delimiter.charAt(0));
            }
            Writer out1 = new BufferedWriter(new OutputStreamWriter(out));
            printer = new CSVPrinter(out1, format);
            firstDocument = false;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    try {
        List<List<String>> csvRows = new ArrayList<List<String>>();
        List<String> csvList = new ArrayList<String>();
        for (String headerField : headers) {
            Field field = document.getField(headerField);
            String value = null;
            if (field != null && field.getValues() != null) {
                value = Joiner.on(multiValueSeperator).skipNulls().join(field.getValues());
            }
            csvList.add(value);
        }
        csvRows.add(csvList);
        printer.printRecords(csvRows);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    super.document(document);
}
Also used : OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) CSVPrinter(org.apache.commons.csv.CSVPrinter) Field(de.tblsoft.solr.pipeline.bean.Field) CSVFormat(org.apache.commons.csv.CSVFormat) OutputStreamWriter(java.io.OutputStreamWriter) ArrayList(java.util.ArrayList) List(java.util.List) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

CSVPrinter (org.apache.commons.csv.CSVPrinter)75 IOException (java.io.IOException)26 CSVFormat (org.apache.commons.csv.CSVFormat)21 OutputStreamWriter (java.io.OutputStreamWriter)18 StringWriter (java.io.StringWriter)15 ArrayList (java.util.ArrayList)15 Writer (java.io.Writer)13 FileOutputStream (java.io.FileOutputStream)12 Test (org.junit.Test)11 FileWriter (java.io.FileWriter)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 BufferedWriter (java.io.BufferedWriter)7 PrintWriter (java.io.PrintWriter)5 JSONArray (org.json.JSONArray)5 File (java.io.File)4 Map (java.util.Map)4 Word (ai.elimu.model.content.Word)3 Path (java.nio.file.Path)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 Letter (ai.elimu.model.content.Letter)2