use of org.apache.commons.csv.CSVPrinter in project jaqy by Teradata.
the class CSVExporter method export.
@Override
public long export(JaqyResultSet rs, JaqyInterpreter interpreter) throws Exception {
PrintWriter pw = new PrintWriter(new BufferedWriter(m_out));
JaqyResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
TypeHandler[] handlers = new TypeHandler[columns];
JaqyHelper helper = rs.getHelper();
CSVPrinter printer = new CSVPrinter(pw, m_format);
for (int i = 0; i < columns; ++i) {
// print the header row
printer.print(metaData.getColumnLabel(i + 1));
CSVExportInfo fileInfo = m_fileInfoMap.get(i + 1);
if (fileInfo == null)
handlers[i] = helper.getTypeHandler(rs, i + 1);
else
handlers[i] = new FileHandler(m_file, fileInfo);
}
printer.println();
long count = 0;
while (rs.next()) {
++count;
for (int i = 0; i < columns; ++i) {
printer.print(handlers[i].getString(rs, i + 1, interpreter));
}
printer.println();
}
printer.close();
return count;
}
use of org.apache.commons.csv.CSVPrinter in project pwm by pwm-project.
the class StatisticsManager method outputStatsToCsv.
public int outputStatsToCsv(final OutputStream outputStream, final Locale locale, final boolean includeHeader) throws IOException {
LOGGER.trace("beginning output stats to csv process");
final Instant startTime = Instant.now();
final StatisticsManager statsManger = pwmApplication.getStatisticsManager();
final CSVPrinter csvPrinter = JavaHelper.makeCsvPrinter(outputStream);
if (includeHeader) {
final List<String> headers = new ArrayList<>();
headers.add("KEY");
headers.add("YEAR");
headers.add("DAY");
for (final Statistic stat : Statistic.values()) {
headers.add(stat.getLabel(locale));
}
csvPrinter.printRecord(headers);
}
int counter = 0;
final Map<StatisticsManager.DailyKey, String> keys = statsManger.getAvailableKeys(PwmConstants.DEFAULT_LOCALE);
for (final StatisticsManager.DailyKey loopKey : keys.keySet()) {
counter++;
final StatisticsBundle bundle = statsManger.getStatBundleForKey(loopKey.toString());
final List<String> lineOutput = new ArrayList<>();
lineOutput.add(loopKey.toString());
lineOutput.add(String.valueOf(loopKey.year));
lineOutput.add(String.valueOf(loopKey.day));
for (final Statistic stat : Statistic.values()) {
lineOutput.add(bundle.getStatistic(stat));
}
csvPrinter.printRecord(lineOutput);
}
csvPrinter.flush();
LOGGER.trace("completed output stats to csv process; output " + counter + " records in " + TimeDuration.fromCurrent(startTime).asCompactString());
return counter;
}
use of org.apache.commons.csv.CSVPrinter in project pwm by pwm-project.
the class ReportCsvUtility method outputToCsv.
public void outputToCsv(final OutputStream outputStream, final boolean includeHeader, final Locale locale, final Configuration config, final ReportColumnFilter columnFilter) throws IOException, ChaiUnavailableException, ChaiOperationException, PwmUnrecoverableException, PwmOperationalException {
final CSVPrinter csvPrinter = JavaHelper.makeCsvPrinter(outputStream);
final Class localeClass = password.pwm.i18n.Admin.class;
if (includeHeader) {
final List<String> headerRow = new ArrayList<>();
if (columnFilter.isUsernameVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_Username", config, localeClass));
}
if (columnFilter.isUserDnVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_UserDN", config, localeClass));
}
if (columnFilter.isLdapProfileVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_LDAP_Profile", config, localeClass));
}
if (columnFilter.isEmailVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_Email", config, localeClass));
}
if (columnFilter.isUserGuidVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_UserGuid", config, localeClass));
}
if (columnFilter.isAccountExpirationTimeVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_AccountExpireTime", config, localeClass));
}
if (columnFilter.isPasswordExpirationTimeVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_PwdExpireTime", config, localeClass));
}
if (columnFilter.isPasswordChangeTimeVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_PwdChangeTime", config, localeClass));
}
if (columnFilter.isResponseSetTimeVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_ResponseSaveTime", config, localeClass));
}
if (columnFilter.isLastLoginTimeVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_LastLogin", config, localeClass));
}
if (columnFilter.isHasResponsesVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_HasResponses", config, localeClass));
}
if (columnFilter.isHasHelpdeskResponsesVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_HasHelpdeskResponses", config, localeClass));
}
if (columnFilter.isResponseStorageMethodVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_ResponseStorageMethod", config, localeClass));
}
if (columnFilter.isResponseFormatTypeVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_ResponseFormatType", config, localeClass));
}
if (columnFilter.isPasswordStatusExpiredVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_PwdExpired", config, localeClass));
}
if (columnFilter.isPasswordStatusPreExpiredVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_PwdPreExpired", config, localeClass));
}
if (columnFilter.isPasswordStatusViolatesPolicyVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_PwdViolatesPolicy", config, localeClass));
}
if (columnFilter.isPasswordStatusWarnPeriodVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_PwdWarnPeriod", config, localeClass));
}
if (columnFilter.isRequiresPasswordUpdateVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_RequiresPasswordUpdate", config, localeClass));
}
if (columnFilter.isRequiresResponseUpdateVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_RequiresResponseUpdate", config, localeClass));
}
if (columnFilter.isRequiresProfileUpdateVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_RequiresProfileUpdate", config, localeClass));
}
if (columnFilter.isCacheTimestampVisible()) {
headerRow.add(LocaleHelper.getLocalizedMessage(locale, "Field_Report_RecordCacheTime", config, localeClass));
}
csvPrinter.printRecord(headerRow);
}
ClosableIterator<UserCacheRecord> cacheBeanIterator = null;
try {
cacheBeanIterator = iterator();
while (cacheBeanIterator.hasNext()) {
final UserCacheRecord userCacheRecord = cacheBeanIterator.next();
outputRecordRow(config, locale, userCacheRecord, csvPrinter, columnFilter);
}
} finally {
if (cacheBeanIterator != null) {
cacheBeanIterator.close();
}
}
csvPrinter.flush();
}
use of org.apache.commons.csv.CSVPrinter in project pwm by pwm-project.
the class ConfigManagerServlet method downloadPermissionReportCsv.
private void downloadPermissionReportCsv(final PwmRequest pwmRequest) throws PwmUnrecoverableException, IOException, ChaiUnavailableException, ServletException {
pwmRequest.getPwmResponse().markAsDownload(HttpContentType.csv, pwmRequest.getConfig().readAppProperty(AppProperty.DOWNLOAD_FILENAME_LDAP_PERMISSION_CSV));
final CSVPrinter csvPrinter = JavaHelper.makeCsvPrinter(pwmRequest.getPwmResponse().getOutputStream());
try {
final StoredConfigurationImpl storedConfiguration = readCurrentConfiguration(pwmRequest);
final LDAPPermissionCalculator ldapPermissionCalculator = new LDAPPermissionCalculator(storedConfiguration);
for (final LDAPPermissionCalculator.PermissionRecord permissionRecord : ldapPermissionCalculator.getPermissionRecords()) {
final String settingTxt = permissionRecord.getPwmSetting() == null ? LocaleHelper.getLocalizedMessage(Display.Value_NotApplicable, pwmRequest) : permissionRecord.getPwmSetting().toMenuLocationDebug(permissionRecord.getProfile(), pwmRequest.getLocale());
csvPrinter.printRecord(permissionRecord.getActor().getLabel(pwmRequest.getLocale(), pwmRequest.getConfig()), permissionRecord.getAttribute(), permissionRecord.getAccess().toString(), settingTxt);
}
} catch (Exception e) {
final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, e.getMessage());
LOGGER.error(pwmRequest, errorInformation);
pwmRequest.respondWithError(errorInformation);
} finally {
IOUtils.closeQuietly(csvPrinter);
}
}
use of org.apache.commons.csv.CSVPrinter in project ksql by confluentinc.
the class KsqlDelimitedSerializer method serialize.
@Override
public byte[] serialize(final String topic, final GenericRow genericRow) {
if (genericRow == null) {
return null;
}
try {
StringWriter stringWriter = new StringWriter();
CSVPrinter csvPrinter = new CSVPrinter(stringWriter, CSVFormat.DEFAULT);
csvPrinter.printRecord(genericRow.getColumns());
String result = stringWriter.toString();
return result.substring(0, result.length() - 2).getBytes(StandardCharsets.UTF_8);
} catch (Exception e) {
throw new SerializationException("Error serializing CSV message", e);
}
}
Aggregations