use of ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject 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 ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject in project irida by phac-nml.
the class ProjectsController method writeProjectsToExcelFile.
/**
* Write the projects as a Excel 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 OutputStream} for the response
*/
private void writeProjectsToExcelFile(List<String> headers, List<DTProject> projects, Locale locale, HttpServletResponse response) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
int rowCount = 0;
// Create the headers
Row headerRow = sheet.createRow(rowCount++);
for (int cellCount = 0; cellCount < headers.size(); cellCount++) {
Cell cell = headerRow.createCell(cellCount);
cell.setCellValue(headers.get(cellCount));
}
// Create the rest of the sheet
DateFormat dateFormat = new SimpleDateFormat(messageSource.getMessage("locale.date.long", null, locale));
for (DTProject p : projects) {
Row row = sheet.createRow(rowCount++);
int cellCount = 0;
row.createCell(cellCount++).setCellValue(String.valueOf(p.getId()));
row.createCell(cellCount++).setCellValue(p.getName());
row.createCell(cellCount++).setCellValue(p.getOrganism());
row.createCell(cellCount++).setCellValue(String.valueOf(p.getSamples()));
row.createCell(cellCount++).setCellValue(dateFormat.format(p.getCreatedDate()));
row.createCell(cellCount).setCellValue(dateFormat.format(p.getModifiedDate()));
}
// Write the file
try (OutputStream stream = response.getOutputStream()) {
workbook.write(stream);
stream.flush();
}
}
use of ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject in project irida by phac-nml.
the class ProjectsController method exportProjectsToFile.
/**
* Export Projects table as either an excel file or CSV
*
* @param type
* of file to export (csv or excel)
* @param isAdmin
* if the currently logged in user is an administrator
* @param response
* {@link HttpServletResponse}
* @param principal
* {@link Principal}
* @param locale
* {@link Locale}
*
* @throws IOException
* thrown if cannot open the {@link HttpServletResponse} {@link OutputStream}
*/
@RequestMapping("/projects/ajax/export")
public void exportProjectsToFile(@RequestParam(value = "dtf") String type, @RequestParam(required = false, defaultValue = "false", value = "admin") Boolean isAdmin, HttpServletResponse response, Principal principal, Locale locale) throws IOException {
// Let's make sure the export type is set properly
if (!(type.equalsIgnoreCase("xlsx") || type.equalsIgnoreCase("csv"))) {
throw new IllegalArgumentException("No file type sent for downloading all projects. Expecting parameter 'dtf=' xlsx or csv");
}
List<Project> projects;
// If viewing the admin projects page give the user all the projects.
if (isAdmin) {
projects = (List<Project>) projectService.findAll();
} else // If on the users projects page, give the user their projects.
{
User user = userService.getUserByUsername(principal.getName());
projects = projectService.getProjectsForUser(user).stream().map(Join::getSubject).collect(Collectors.toList());
}
List<DTProject> dtProjects = projects.stream().map(this::createDataTablesProject).collect(Collectors.toList());
List<String> headers = ImmutableList.of("id", "name", "organism", "samples", "created", "modified").stream().map(h -> messageSource.getMessage("projects.table." + h, new Object[] {}, locale)).collect(Collectors.toList());
// Create the filename
Date date = new Date();
DateFormat fileDateFormat = new SimpleDateFormat(messageSource.getMessage("date.iso-8601", null, locale));
String filename = "IRIDA_projects_" + fileDateFormat.format(date);
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "." + type + "\"");
if (type.equals("xlsx")) {
writeProjectsToExcelFile(headers, dtProjects, locale, response);
} else {
writeProjectsToCsvFile(headers, dtProjects, locale, response);
}
}
Aggregations