Search in sources :

Example 1 with DTProject

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();
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) DTProject(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) PrintWriter(java.io.PrintWriter)

Example 2 with DTProject

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();
    }
}
Also used : DTProject(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) OutputStream(java.io.OutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with DTProject

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);
    }
}
Also used : ProjectRole(ca.corefacility.bioinformatics.irida.model.enums.ProjectRole) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ProjectService(ca.corefacility.bioinformatics.irida.service.ProjectService) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) TaxonomyService(ca.corefacility.bioinformatics.irida.service.TaxonomyService) Model(org.springframework.ui.Model) CSVFormat(org.apache.commons.csv.CSVFormat) DataTablesResponseModel(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.models.DataTablesResponseModel) Cell(org.apache.poi.ss.usermodel.Cell) DataTablesResponse(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesResponse) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) DTProject(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject) ConstraintViolation(javax.validation.ConstraintViolation) DateFormat(java.text.DateFormat) PrintWriter(java.io.PrintWriter) HttpSession(javax.servlet.http.HttpSession) ProjectRemoteService(ca.corefacility.bioinformatics.irida.service.remote.ProjectRemoteService) ImmutableMap(com.google.common.collect.ImmutableMap) ProjectWithoutOwnerException(ca.corefacility.bioinformatics.irida.exceptions.ProjectWithoutOwnerException) IridaOAuthException(ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) RemoteAPI(ca.corefacility.bioinformatics.irida.model.RemoteAPI) Page(org.springframework.data.domain.Page) ProjectSyncFrequency(ca.corefacility.bioinformatics.irida.model.project.ProjectSyncFrequency) Collectors(java.util.stream.Collectors) IridaRestApiWebConfig(ca.corefacility.bioinformatics.irida.config.web.IridaRestApiWebConfig) DateFormatter(org.springframework.format.datetime.DateFormatter) Principal(java.security.Principal) RemoteStatus(ca.corefacility.bioinformatics.irida.model.remote.RemoteStatus) Entry(java.util.Map.Entry) User(ca.corefacility.bioinformatics.irida.model.user.User) SampleService(ca.corefacility.bioinformatics.irida.service.sample.SampleService) Authentication(org.springframework.security.core.Authentication) CSVPrinter(org.apache.commons.csv.CSVPrinter) java.util(java.util) SimpleDateFormat(java.text.SimpleDateFormat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) Controller(org.springframework.stereotype.Controller) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) Scope(org.springframework.context.annotation.Scope) Value(org.springframework.beans.factory.annotation.Value) Strings(com.google.common.base.Strings) UpdateSamplePermission(ca.corefacility.bioinformatics.irida.security.permissions.sample.UpdateSamplePermission) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) ImmutableList(com.google.common.collect.ImmutableList) Formatter(org.springframework.format.Formatter) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) TreeNode(ca.corefacility.bioinformatics.irida.util.TreeNode) MessageSource(org.springframework.context.MessageSource) OutputStream(java.io.OutputStream) RemoteAPIService(ca.corefacility.bioinformatics.irida.service.RemoteAPIService) Logger(org.slf4j.Logger) AnalysisState(ca.corefacility.bioinformatics.irida.model.enums.AnalysisState) Role(ca.corefacility.bioinformatics.irida.model.user.Role) DataTablesRequest(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.config.DataTablesRequest) CartController(ca.corefacility.bioinformatics.irida.ria.web.analysis.CartController) IridaWorkflowsService(ca.corefacility.bioinformatics.irida.service.workflow.IridaWorkflowsService) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Project(ca.corefacility.bioinformatics.irida.model.project.Project) HttpStatus(org.springframework.http.HttpStatus) FileSizeConverter(ca.corefacility.bioinformatics.irida.ria.utilities.converters.FileSizeConverter) ConstraintViolationException(javax.validation.ConstraintViolationException) UserService(ca.corefacility.bioinformatics.irida.service.user.UserService) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) Row(org.apache.poi.ss.usermodel.Row) SyncStatus(ca.corefacility.bioinformatics.irida.model.remote.RemoteStatus.SyncStatus) ResponseEntity(org.springframework.http.ResponseEntity) DataTablesParams(ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesParams) User(ca.corefacility.bioinformatics.irida.model.user.User) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) DTProject(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject) DTProject(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject) Project(ca.corefacility.bioinformatics.irida.model.project.Project) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

DTProject (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject)3 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 OutputStream (java.io.OutputStream)2 Cell (org.apache.poi.ss.usermodel.Cell)2 Row (org.apache.poi.ss.usermodel.Row)2 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 IridaRestApiWebConfig (ca.corefacility.bioinformatics.irida.config.web.IridaRestApiWebConfig)1 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)1 IridaOAuthException (ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException)1 ProjectWithoutOwnerException (ca.corefacility.bioinformatics.irida.exceptions.ProjectWithoutOwnerException)1 RemoteAPI (ca.corefacility.bioinformatics.irida.model.RemoteAPI)1 AnalysisState (ca.corefacility.bioinformatics.irida.model.enums.AnalysisState)1 ProjectRole (ca.corefacility.bioinformatics.irida.model.enums.ProjectRole)1 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)1 Project (ca.corefacility.bioinformatics.irida.model.project.Project)1 ProjectSyncFrequency (ca.corefacility.bioinformatics.irida.model.project.ProjectSyncFrequency)1 RemoteStatus (ca.corefacility.bioinformatics.irida.model.remote.RemoteStatus)1 SyncStatus (ca.corefacility.bioinformatics.irida.model.remote.RemoteStatus.SyncStatus)1