use of ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException in project irida by phac-nml.
the class ProjectSampleMetadataController method saveProjectSampleMetadata.
/**
* Save uploaded metadata to the
*
* @param locale
* {@link Locale} of the current user.
* @param session
* {@link HttpSession}
* @param projectId
* {@link Long} identifier for the current project
*
* @return {@link Map} of potential errors.
*/
@RequestMapping(value = "/upload/save", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> saveProjectSampleMetadata(Locale locale, HttpSession session, @PathVariable long projectId) {
Map<String, Object> errors = new HashMap<>();
Project project = projectService.read(projectId);
SampleMetadataStorage stored = (SampleMetadataStorage) session.getAttribute("pm-" + projectId);
if (stored == null) {
errors.put("stored-error", true);
}
List<Sample> samplesToUpdate = new ArrayList<>();
List<Map<String, String>> found = stored.getFound();
if (found != null) {
// Lets try to get a sample
String sampleNameColumn = stored.getSampleNameColumn();
List<String> errorList = new ArrayList<>();
try {
for (Map<String, String> row : found) {
String name = row.get(sampleNameColumn);
Sample sample = sampleService.getSampleBySampleName(project, name);
row.remove(sampleNameColumn);
Map<MetadataTemplateField, MetadataEntry> newData = new HashMap<>();
// Need to overwrite duplicate keys
for (Entry<String, String> entry : row.entrySet()) {
MetadataTemplateField key = metadataTemplateService.readMetadataFieldByLabel(entry.getKey());
if (key == null) {
key = metadataTemplateService.saveMetadataField(new MetadataTemplateField(entry.getKey(), "text"));
}
newData.put(key, new MetadataEntry(entry.getValue(), "text"));
}
sample.mergeMetadata(newData);
// Save metadata back to the sample
samplesToUpdate.add(sample);
}
sampleService.updateMultiple(samplesToUpdate);
} catch (EntityNotFoundException e) {
// This really should not happen, but hey, you never know!
errorList.add(messageSource.getMessage("metadata.results.save.sample-not-found", new Object[] { e.getMessage() }, locale));
}
if (errorList.size() > 0) {
errors.put("save-errors", errorList);
}
} else {
errors.put("found-error", messageSource.getMessage("metadata.results.save.found-error", new Object[] {}, locale));
}
if (errors.size() == 0) {
return ImmutableMap.of("success", messageSource.getMessage("metadata.results.save.success", new Object[] { found.size() }, locale));
}
return errors;
}
use of ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException in project irida by phac-nml.
the class ReferenceFileController method deleteReferenceFile.
/**
* Delete a reference file. This will remove it from the project.
*
* @param fileId
* The id of the file to remove.
* @param projectId
* the project to delete the reference file for.
* @param response
* {@link HttpServletResponse} required for returning an error
* state.
* @param locale
* the locale specified by the browser.
*
* @return Success or error based on the result of deleting the file.
*/
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> deleteReferenceFile(@RequestParam Long fileId, @RequestParam Long projectId, HttpServletResponse response, Locale locale) {
Map<String, Object> result = new HashMap<>();
Project project = projectService.read(projectId);
ReferenceFile file = referenceFileService.read(fileId);
try {
logger.info("Removing file with id of : " + fileId);
projectService.removeReferenceFileFromProject(project, file);
result.put("result", "success");
result.put("msg", messageSource.getMessage("projects.meta.reference-file.delete-success", new Object[] { file.getLabel(), project.getName() }, locale));
} catch (EntityNotFoundException e) {
// This is required else the client does not know that an error was thrown!
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
logger.error("Failed to upload reference file, reason unknown.", e);
result.put("result", "error");
result.put("msg", messageSource.getMessage("projects.meta.reference-file.delete-error", new Object[] { file.getLabel(), project.getName() }, locale));
}
return result;
}
use of ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException in project irida by phac-nml.
the class CRUDServiceImplTest method testValidDelete.
@Test
public void testValidDelete() {
IdentifiableTestEntity i = new IdentifiableTestEntity();
i.setId(new Long(1));
when(crudService.exists(i.getId())).thenReturn(Boolean.TRUE);
try {
crudService.delete(i.getId());
} catch (EntityNotFoundException e) {
fail();
}
}
use of ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException in project irida by phac-nml.
the class UserServiceImplTest method testBadUsername.
@Test(expected = EntityNotFoundException.class)
public // should throw the exception to the caller instead of swallowing it.
void testBadUsername() {
String username = "superwrongusername";
when(userRepository.loadUserByUsername(username)).thenThrow(new EntityNotFoundException("not found"));
userService.getUserByUsername(username);
}
use of ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException in project irida by phac-nml.
the class RESTSampleSequenceFilesController method readSequenceFileForSequencingObject.
/**
* Read a single {@link SequenceFile} for a given {@link Sample} and
* {@link SequencingObject}
*
* @param sampleId
* ID of the {@link Sample}
* @param objectType
* type of {@link SequencingObject}
* @param objectId
* id of the {@link SequencingObject}
* @param fileId
* ID of the {@link SequenceFile} to read
* @return a {@link SequenceFile}
*/
@RequestMapping(value = "/api/samples/{sampleId}/{objectType}/{objectId}/files/{fileId}", method = RequestMethod.GET)
public ModelMap readSequenceFileForSequencingObject(@PathVariable Long sampleId, @PathVariable String objectType, @PathVariable Long objectId, @PathVariable Long fileId) {
ModelMap modelMap = new ModelMap();
Sample sample = sampleService.read(sampleId);
SequencingObject readSequenceFilePairForSample = sequencingObjectService.readSequencingObjectForSample(sample, objectId);
Optional<SequenceFile> findFirst = readSequenceFilePairForSample.getFiles().stream().filter(f -> f.getId().equals(fileId)).findFirst();
if (!findFirst.isPresent()) {
throw new EntityNotFoundException("File with id " + fileId + " is not associated with this sequencing object");
}
SequenceFile file = findFirst.get();
file.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).getSampleSequenceFiles(sampleId)).withRel(REL_SAMPLE_SEQUENCE_FILES));
file.add(linkTo(methodOn(RESTProjectSamplesController.class).getSample(sampleId)).withRel(REL_SAMPLE));
file.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).readSequencingObject(sampleId, objectType, objectId)).withRel(REL_SEQ_OBJECT));
file.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).readQCForSequenceFile(sampleId, objectType, objectId, fileId)).withRel(REL_SEQ_QC));
file.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).readSequenceFileForSequencingObject(sampleId, objectType, objectId, fileId)).withSelfRel());
modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, file);
return modelMap;
}
Aggregations