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());
}
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();
}
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();
}
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();
}
}
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);
}
Aggregations