use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.
the class ProjectSamplesController method downloadSamples.
/**
* Download a set of sequence files from selected samples within a project
*
* @param projectId Id for a {@link Project}
* @param ids List of ids ofr {@link Sample} within the project
* @param response {@link HttpServletResponse}
* @throws IOException if we fail to read a file from the filesystem.
*/
@RequestMapping(value = "/projects/{projectId}/download/files")
public void downloadSamples(@PathVariable Long projectId, @RequestParam(value = "ids[]") List<Long> ids, HttpServletResponse response) throws IOException {
Project project = projectService.read(projectId);
List<Sample> samples = (List<Sample>) sampleService.readMultiple(ids);
// Add the appropriate headers
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=\"" + project.getName() + ".zip\"");
response.setHeader("Transfer-Encoding", "chunked");
// storing used file names to ensure we don't have a conflict
Set<String> usedFileNames = new HashSet<>();
try (ZipOutputStream outputStream = new ZipOutputStream(response.getOutputStream())) {
for (Sample sample : samples) {
Collection<SampleSequencingObjectJoin> sequencingObjectsForSample = sequencingObjectService.getSequencingObjectsForSample(sample);
for (SampleSequencingObjectJoin join : sequencingObjectsForSample) {
for (SequenceFile file : join.getObject().getFiles()) {
Path path = file.getFile();
String fileName = project.getName() + "/" + sample.getSampleName() + "/" + path.getFileName().toString();
if (usedFileNames.contains(fileName)) {
fileName = handleDuplicate(fileName, usedFileNames);
}
final ZipEntry entry = new ZipEntry(fileName);
// set the file creation time on the zip entry to be
// whatever the creation time is on the filesystem
final BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
entry.setCreationTime(attr.creationTime());
entry.setLastModifiedTime(attr.creationTime());
outputStream.putNextEntry(entry);
usedFileNames.add(fileName);
Files.copy(path, outputStream);
outputStream.closeEntry();
}
}
}
outputStream.finish();
} catch (IOException e) {
// this generally means that the user has cancelled the download
// from their web browser; we can safely ignore this
logger.debug("This *probably* means that the user cancelled the download, " + "but it might be something else, see the stack trace below for more information.", e);
} catch (Exception e) {
logger.error("Download failed...", e);
} finally {
// close the response outputStream so that we're not leaking
// streams.
response.getOutputStream().close();
}
}
use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.
the class ProjectSamplesController method getProjectsAvailableToCopySamples.
/**
* Search for projects available for a user to copy samples to. If the user is an admin it will show all projects.
*
* @param projectId identifier for the current {@link Project}
* @param term A search term
* @param pageSize The size of the page requests
* @param page The page number (0 based)
* @return a {@code Map<String,Object>} containing: total: total number of elements results: A {@code
* Map<Long,String>} of project IDs and project names.
*/
@RequestMapping(value = "/projects/{projectId}/ajax/samples/available_projects")
@ResponseBody
public Map<String, Object> getProjectsAvailableToCopySamples(@PathVariable final Long projectId, @RequestParam String term, @RequestParam(required = false, defaultValue = "10") int pageSize, @RequestParam int page) {
final Project projectToExclude = projectService.read(projectId);
List<Map<String, String>> projectMap = new ArrayList<>();
Map<String, Object> response = new HashMap<>();
final Page<Project> projects = projectService.getUnassociatedProjects(projectToExclude, term, page, pageSize, Direction.ASC, PROJECT_NAME_PROPERTY);
for (Project p : projects) {
Map<String, String> map = new HashMap<>();
map.put("id", p.getId().toString());
map.put("text", p.getName());
projectMap.add(map);
}
response.put("total", projects.getTotalElements());
response.put("projects", projectMap);
return response;
}
use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.
the class ProjectSamplesController method ajaxSamplesMerge.
/**
* Merges a list of samples into either the first sample in the list with a new name if provided, or into the
* selected sample based on the id.
*
* @param projectId
* The id for the current {@link Project}
* @param mergeSampleId
* An id for a {@link Sample} to merge the other samples into.
* @param sampleIds
* A list of ids for {@link Sample} to merge together.
* @param sampleName
* An optional new name for the {@link Sample}.
* @param locale
* The {@link Locale} of the current user.
*
* @return a map of {@link Sample} properties representing the merged sample.
*/
@RequestMapping(value = "/projects/{projectId}/ajax/samples/merge", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, Object> ajaxSamplesMerge(@PathVariable Long projectId, @RequestParam Long mergeSampleId, @RequestParam(value = "sampleIds[]") List<Long> sampleIds, @RequestParam String sampleName, Locale locale) {
Map<String, Object> result = new HashMap<>();
int samplesMergeCount = sampleIds.size();
Project project = projectService.read(projectId);
// Determine which sample to merge into
Sample mergeIntoSample = sampleService.read(mergeSampleId);
sampleIds.remove(mergeSampleId);
if (!Strings.isNullOrEmpty(sampleName)) {
try {
mergeIntoSample.setSampleName(sampleName);
mergeIntoSample = sampleService.update(mergeIntoSample);
} catch (ConstraintViolationException e) {
logger.error(e.getLocalizedMessage());
result.put("result", "error");
result.put("warnings", getErrorsFromViolationException(e));
return result;
}
}
// Create an update map
List<Sample> mergeSamples = sampleIds.stream().map(sampleService::read).collect(Collectors.toList());
// Merge the samples
sampleService.mergeSamples(project, mergeIntoSample, mergeSamples);
result.put("result", "success");
result.put("message", messageSource.getMessage("project.samples.combine-success", new Object[] { samplesMergeCount, mergeIntoSample.getSampleName() }, locale));
return result;
}
use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.
the class ProjectSamplesController method getLinkerCommandModal.
/**
* Generate a modal for displaying the ngs-linker command
*
* @param ids List of {@link Sample} identifiers
* @param projectId identtifier for the current {@link Project}
* @param model UI Model
* @return Path to template
*/
@RequestMapping(value = "/projects/{projectId}/templates/linker-modal", produces = MediaType.TEXT_HTML_VALUE)
public String getLinkerCommandModal(@RequestParam(name = "sampleIds[]", defaultValue = "") List<Long> ids, @PathVariable Long projectId, Model model) {
Project project = projectService.read(projectId);
int totalSamples = sampleService.getSamplesForProject(project).size();
String cmd = LINKER_SCRIPT + " -p " + projectId;
if (ids.size() != 0 && ids.size() != totalSamples) {
cmd += " -s " + StringUtils.join(ids, " -s ");
}
model.addAttribute("command", cmd);
return PROJECT_TEMPLATE_DIR + "linker-modal.tmpl";
}
use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.
the class ProjectSamplesController method getShareSamplesModal.
/**
* Create a modal dialogue for moving or sharing {@link Sample} to another {@link Project}
*
* @param ids {@link List} of identifiers for {@link Sample}s to copy or move.
* @param projectId Identifier for the current {@link Project}
* @param model UI Model
* @param move Whether or not to display share or move wording.
* @return Path to share or move modal template.
*/
@RequestMapping(value = "/projects/{projectId}/templates/copy-move-modal", produces = MediaType.TEXT_HTML_VALUE)
public String getShareSamplesModal(@RequestParam(name = "sampleIds[]") List<Long> ids, @PathVariable Long projectId, Model model, @RequestParam(required = false) boolean move) {
Project project = projectService.read(projectId);
model.addAllAttributes(generateShareMoveSamplesContent(project, ids));
model.addAttribute("projectId", projectId);
model.addAttribute("type", move ? "move" : "copy");
model.addAttribute("isRemoteProject", project.isRemote());
return PROJECT_TEMPLATE_DIR + "copy-move-modal.tmpl";
}
Aggregations