Search in sources :

Example 1 with Csv

use of com.day.text.csv.Csv in project acs-aem-commons by Adobe-Consulting-Services.

the class UsersExportServlet method doGet.

/**
 * Generates a CSV file representing the User Data.
 *
 * @param request  the Sling HTTP Request object
 * @param response the Sling HTTP Response object
 * @throws IOException
 * @throws ServletException
 */
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, ServletException {
    response.setContentType("text/csv");
    response.setCharacterEncoding("UTF-8");
    final Parameters parameters = new Parameters(request);
    log.debug("Users to CSV Export Parameters: {}", parameters.toString());
    final Csv csv = new Csv();
    final Writer writer = response.getWriter();
    csv.writeInit(writer);
    final Iterator<Resource> resources = request.getResourceResolver().findResources(QUERY, Query.JCR_SQL2);
    // Using a HashMap to satisfy issue with duplicate results in AEM 6.1 GA
    HashMap<String, CsvUser> csvUsers = new LinkedHashMap<String, CsvUser>();
    while (resources.hasNext()) {
        try {
            Resource resource = resources.next();
            CsvUser csvUser = new CsvUser(resource);
            if (!csvUsers.containsKey(csvUser.getPath()) && checkGroups(parameters.getGroups(), parameters.getGroupFilter(), csvUser)) {
                csvUsers.put(csvUser.getPath(), csvUser);
            }
        } catch (RepositoryException e) {
            log.error("Unable to extract a user from resource.", e);
        }
    }
    List<String> columns = new ArrayList<String>();
    columns.add("Path");
    columns.add("User ID");
    columns.add("First Name");
    columns.add("Last Name");
    columns.add("E-mail Address");
    columns.add("Created Date");
    columns.add("Last Modified Date");
    for (String customProperty : parameters.getCustomProperties()) {
        columns.add(customProperty);
    }
    columns.add("All Groups");
    columns.add("Direct Groups");
    columns.add("Indirect Groups");
    csv.writeRow(columns.toArray(new String[columns.size()]));
    for (final CsvUser csvUser : csvUsers.values()) {
        List<String> values = new ArrayList<String>();
        try {
            values.add(csvUser.getPath());
            values.add(csvUser.getID());
            values.add(csvUser.getFirstName());
            values.add(csvUser.getLastName());
            values.add(csvUser.getEmail());
            values.add(csvUser.getCreatedDate());
            values.add(csvUser.getLastModifiedDate());
            for (String customProperty : parameters.getCustomProperties()) {
                values.add(csvUser.getCustomProperty(customProperty));
            }
            values.add(StringUtils.join(csvUser.getAllGroups(), GROUP_DELIMITER));
            values.add(StringUtils.join(csvUser.getDeclaredGroups(), GROUP_DELIMITER));
            values.add(StringUtils.join(csvUser.getTransitiveGroups(), GROUP_DELIMITER));
            csv.writeRow(values.toArray(new String[values.size()]));
        } catch (RepositoryException e) {
            log.error("Unable to export user to CSV report", e);
        }
    }
    csv.close();
}
Also used : Csv(com.day.text.csv.Csv) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Writer(java.io.Writer) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Csv

use of com.day.text.csv.Csv in project acs-aem-commons by Adobe-Consulting-Services.

the class ReportCSVExportServlet method doGet.

protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
    log.trace("doGet");
    // set response parameters
    response.setContentType("text/csv");
    response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(request.getResource().getValueMap().get(JcrConstants.JCR_TITLE, "report"), "UTF-8") + ".csv");
    Writer writer = null;
    try {
        writer = response.getWriter();
        // initialize the csv
        final Csv csv = new Csv();
        csv.writeInit(writer);
        // write the headers
        List<ReportCellCSVExporter> exporters = writeHeaders(request, csv);
        Resource configCtr = request.getResource().getChild("config");
        if (configCtr != null && configCtr.listChildren().hasNext()) {
            Iterator<Resource> children = configCtr.listChildren();
            while (children.hasNext()) {
                Resource config = children.next();
                if (config != null) {
                    updateCSV(config, request, exporters, csv, writer);
                    log.debug("Successfully export report with configuration: {}", config);
                    break;
                } else {
                    log.warn("Unable to export report for configuration: {}", config);
                }
            }
            csv.close();
        } else {
            throw new IOException("No configurations found for " + request.getResource());
        }
    } catch (ReportException e) {
        log.error("Exception extracting report to CSV", e);
        throw new ServletException("Exception extracting report to CSV", e);
    } finally {
        IOUtils.closeQuietly(writer);
    }
}
Also used : ServletException(javax.servlet.ServletException) Csv(com.day.text.csv.Csv) Resource(org.apache.sling.api.resource.Resource) ReportException(com.adobe.acs.commons.reports.api.ReportException) IOException(java.io.IOException) ReportCellCSVExporter(com.adobe.acs.commons.reports.api.ReportCellCSVExporter) Writer(java.io.Writer)

Aggregations

Csv (com.day.text.csv.Csv)2 Writer (java.io.Writer)2 Resource (org.apache.sling.api.resource.Resource)2 ReportCellCSVExporter (com.adobe.acs.commons.reports.api.ReportCellCSVExporter)1 ReportException (com.adobe.acs.commons.reports.api.ReportException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 RepositoryException (javax.jcr.RepositoryException)1 ServletException (javax.servlet.ServletException)1