Search in sources :

Example 6 with EntityNotFoundException

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;
}
Also used : Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) Project(ca.corefacility.bioinformatics.irida.model.project.Project) SampleMetadataStorage(ca.corefacility.bioinformatics.irida.ria.utilities.SampleMetadataStorage) MetadataEntry(ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry) MetadataTemplateField(ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with EntityNotFoundException

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;
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) ReferenceFile(ca.corefacility.bioinformatics.irida.model.project.ReferenceFile) HashMap(java.util.HashMap) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with EntityNotFoundException

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();
    }
}
Also used : IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) Test(org.junit.Test)

Example 9 with EntityNotFoundException

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);
}
Also used : EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) Test(org.junit.Test)

Example 10 with EntityNotFoundException

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;
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) RootResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.RootResource) LoggerFactory(org.slf4j.LoggerFactory) AnalysisFastQC(ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisFastQC) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) ControllerLinkBuilder.methodOn(org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn) Controller(org.springframework.stereotype.Controller) RequestPart(org.springframework.web.bind.annotation.RequestPart) RESTAnalysisSubmissionController(ca.corefacility.bioinformatics.irida.web.controller.api.RESTAnalysisSubmissionController) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) ModelMap(org.springframework.ui.ModelMap) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SequencingRun(ca.corefacility.bioinformatics.irida.model.run.SequencingRun) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) ControllerLinkBuilder.linkTo(org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo) HttpHeaders(com.google.common.net.HttpHeaders) Objects(com.google.common.base.Objects) Path(java.nio.file.Path) ResourceCollection(ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection) BiMap(com.google.common.collect.BiMap) Link(org.springframework.hateoas.Link) Logger(org.slf4j.Logger) AnalysisSubmission(ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission) SequencingObjectService(ca.corefacility.bioinformatics.irida.service.SequencingObjectService) Files(java.nio.file.Files) RESTProjectSamplesController(ca.corefacility.bioinformatics.irida.web.controller.api.projects.RESTProjectSamplesController) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) HttpServletResponse(javax.servlet.http.HttpServletResponse) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) SequencingRunService(ca.corefacility.bioinformatics.irida.service.SequencingRunService) RESTGenericController(ca.corefacility.bioinformatics.irida.web.controller.api.RESTGenericController) HttpStatus(org.springframework.http.HttpStatus) SequenceFileResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.sequencefile.SequenceFileResource) Optional(java.util.Optional) MultipartFile(org.springframework.web.multipart.MultipartFile) SampleService(ca.corefacility.bioinformatics.irida.service.sample.SampleService) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) AnalysisService(ca.corefacility.bioinformatics.irida.service.AnalysisService) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)45 Test (org.junit.Test)12 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)10 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)8 Project (ca.corefacility.bioinformatics.irida.model.project.Project)7 Transactional (org.springframework.transaction.annotation.Transactional)7 User (ca.corefacility.bioinformatics.irida.model.user.User)6 RemoteAPIToken (ca.corefacility.bioinformatics.irida.model.RemoteAPIToken)5 AnalysisType (ca.corefacility.bioinformatics.irida.model.enums.AnalysisType)5 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)5 AnalysisFastQC (ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisFastQC)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 IridaWorkflowNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.IridaWorkflowNotFoundException)4 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)4 SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)4 IridaWorkflow (ca.corefacility.bioinformatics.irida.model.workflow.IridaWorkflow)4 Analysis (ca.corefacility.bioinformatics.irida.model.workflow.analysis.Analysis)4 Path (java.nio.file.Path)4