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