Search in sources :

Example 46 with CSVPrinter

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;
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) JaqyResultSetMetaData(com.teradata.jaqy.connection.JaqyResultSetMetaData) JaqyHelper(com.teradata.jaqy.interfaces.JaqyHelper) CSVExportInfo(com.teradata.jaqy.utils.CSVExportInfo) TypeHandler(com.teradata.jaqy.typehandler.TypeHandler)

Example 47 with CSVPrinter

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;
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) Instant(java.time.Instant) ArrayList(java.util.ArrayList)

Example 48 with CSVPrinter

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();
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) ArrayList(java.util.ArrayList)

Example 49 with CSVPrinter

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);
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) ErrorInformation(password.pwm.error.ErrorInformation) StoredConfigurationImpl(password.pwm.config.stored.StoredConfigurationImpl) LDAPPermissionCalculator(password.pwm.util.LDAPPermissionCalculator) ServletException(javax.servlet.ServletException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException)

Example 50 with 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);
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) SerializationException(org.apache.kafka.common.errors.SerializationException) StringWriter(java.io.StringWriter) SerializationException(org.apache.kafka.common.errors.SerializationException)

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