Search in sources :

Example 11 with AnalysisFile

use of com.odysseusinc.arachne.portal.model.AnalysisFile in project ArachneCentralAPI by OHDSI.

the class BaseAnalysisServiceImpl method saveFile.

@Override
public AnalysisFile saveFile(String link, IUser user, A analysis, String label, Boolean isExecutable) throws IOException {
    throwAccessDeniedExceptionIfLocked(analysis);
    Study study = analysis.getStudy();
    String fileNameLowerCase = UUID.randomUUID().toString();
    try {
        if (link == null) {
            throw new IORuntimeException("wrong url");
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_OCTET_STREAM));
        HttpEntity<String> entity = new HttpEntity<>(headers);
        URL url = new URL(link);
        String fileName = FilenameUtils.getName(url.getPath());
        ResponseEntity<byte[]> response = restTemplate.exchange(link, HttpMethod.GET, entity, byte[].class);
        if (response.getStatusCode() == HttpStatus.OK) {
            final String contentType = response.getHeaders().getContentType().toString();
            Path pathToAnalysis = getAnalysisPath(analysis);
            Files.copy(new ByteArrayInputStream(response.getBody()), pathToAnalysis, REPLACE_EXISTING);
            AnalysisFile analysisFile = new AnalysisFile();
            analysisFile.setUuid(fileNameLowerCase);
            analysisFile.setAnalysis(analysis);
            analysisFile.setContentType(contentType);
            analysisFile.setLabel(label);
            analysisFile.setAuthor(user);
            analysisFile.setExecutable(Boolean.TRUE.equals(isExecutable));
            analysisFile.setRealName(fileName);
            analysisFile.setEntryPoint(fileName);
            Date created = new Date();
            analysisFile.setCreated(created);
            analysisFile.setUpdated(created);
            analysisFile.setVersion(1);
            return analysisFileRepository.save(analysisFile);
        }
    } catch (IOException | RuntimeException ex) {
        String message = "error save file to disk, filename=" + fileNameLowerCase + " ex=" + ex.toString();
        LOGGER.error(message, ex);
        throw new IOException(message);
    }
    return null;
}
Also used : Path(java.nio.file.Path) Study(com.odysseusinc.arachne.portal.model.Study) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) IOException(java.io.IOException) URL(java.net.URL) Date(java.util.Date) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 12 with AnalysisFile

use of com.odysseusinc.arachne.portal.model.AnalysisFile in project ArachneCentralAPI by OHDSI.

the class BaseAnalysisServiceImpl method fullDelete.

@Override
public void fullDelete(List<A> analyses) {
    for (A analysis : analyses) {
        List<AnalysisFile> files = analysis.getFiles();
        analysisFileRepository.delete(files);
        for (AnalysisFile file : files) {
            deleteAnalysisFile(analysis, file);
        }
    }
    analysisRepository.delete(analyses);
}
Also used : AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile)

Example 13 with AnalysisFile

use of com.odysseusinc.arachne.portal.model.AnalysisFile in project ArachneCentralAPI by OHDSI.

the class BaseAnalysisServiceImpl method writeToFile.

@Override
@PreAuthorize("hasPermission(#analysisFile, " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).DELETE_ANALYSIS_FILES)")
public void writeToFile(AnalysisFile analysisFile, FileDTO fileContentDTO, IUser updatedBy) throws IOException {
    Analysis analysis = analysisFile.getAnalysis();
    throwAccessDeniedExceptionIfLocked(analysis);
    Study study = analysis.getStudy();
    try {
        Path analysisFolder = analysisHelper.getAnalysisFolder(analysis);
        if (Files.notExists(analysisFolder)) {
            Files.createDirectories(analysisFolder);
        }
        Path targetPath = analysisFolder.resolve(analysisFile.getUuid());
        byte[] content = fileContentDTO.getContent().getBytes(StandardCharsets.UTF_8);
        try (final InputStream stream = new ByteArrayInputStream(content)) {
            Files.copy(stream, targetPath, REPLACE_EXISTING);
        }
        analysisFile.setUpdated(new Date());
        analysisFile.setEntryPoint(analysisFile.getEntryPoint());
        analysisFile.setUpdatedBy(updatedBy);
        analysisFile.setContentType(CommonFileUtils.getContentType(analysisFile.getRealName(), targetPath.toString()));
        analysisFile.incrementVersion();
        analysisFile.setAntivirusStatus(AntivirusStatus.SCANNING);
        analysisFile.setAntivirusDescription(null);
        final AnalysisFile saved = analysisFileRepository.save(analysisFile);
        eventPublisher.publishEvent(new AntivirusJobEvent(this, new AntivirusJob(saved.getId(), saved.getRealName(), new FileInputStream(targetPath.toString()), AntivirusJobFileType.ANALYSIS_FILE)));
    } catch (IOException | RuntimeException ex) {
        String message = "error save file to disk, filename=" + analysisFile.getUuid() + " ex=" + ex.toString();
        LOGGER.error(message, ex);
        throw new IOException(message);
    }
}
Also used : Path(java.nio.file.Path) Study(com.odysseusinc.arachne.portal.model.Study) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) IOException(java.io.IOException) Date(java.util.Date) FileInputStream(java.io.FileInputStream) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) ByteArrayInputStream(java.io.ByteArrayInputStream) Analysis(com.odysseusinc.arachne.portal.model.Analysis) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 14 with AnalysisFile

use of com.odysseusinc.arachne.portal.model.AnalysisFile in project ArachneCentralAPI by OHDSI.

the class BaseAnalysisServiceImpl method setIsExecutable.

/**
 * Updates isExecutable flag in a file with the given uuid and sets entry point
 *
 * @param uuid analysis file uuid
 */
@Override
public void setIsExecutable(String uuid) {
    AnalysisFile analysisFile = analysisFileRepository.findByUuid(uuid);
    Analysis analysis = analysisFile.getAnalysis();
    throwAccessDeniedExceptionIfLocked(analysis);
    setExecutableFileInAnalysis(analysis, uuid);
}
Also used : Analysis(com.odysseusinc.arachne.portal.model.Analysis) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile)

Example 15 with AnalysisFile

use of com.odysseusinc.arachne.portal.model.AnalysisFile in project ArachneCentralAPI by OHDSI.

the class BaseAnalysisController method deleteFile.

@ApiOperation("Delete Analysis file")
@RequestMapping(value = "/api/v1/analysis-management/analyses/{analysisId}/code-files/{fileUuid}", method = DELETE)
public JsonResult<Boolean> deleteFile(@PathVariable("analysisId") Long analysisId, @PathVariable("fileUuid") String uuid) throws PermissionDeniedException, NotExistException {
    final JsonResult<Boolean> result;
    AnalysisFile analysisFile = analysisService.getAnalysisFile(analysisId, uuid);
    T analysis = (T) analysisFile.getAnalysis();
    Boolean deleteAnalysisFile = analysisService.deleteAnalysisFile(analysis, analysisFile);
    result = new JsonResult<>(NO_ERROR);
    result.setResult(deleteAnalysisFile);
    return result;
}
Also used : PUT(org.springframework.web.bind.annotation.RequestMethod.PUT) GET(org.springframework.web.bind.annotation.RequestMethod.GET) POST(org.springframework.web.bind.annotation.RequestMethod.POST) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

AnalysisFile (com.odysseusinc.arachne.portal.model.AnalysisFile)21 Path (java.nio.file.Path)8 ApiOperation (io.swagger.annotations.ApiOperation)6 IOException (java.io.IOException)6 Date (java.util.Date)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 Analysis (com.odysseusinc.arachne.portal.model.Analysis)5 JsonResult (com.odysseusinc.arachne.commons.api.v1.dto.util.JsonResult)4 AnalysisFileDTO (com.odysseusinc.arachne.portal.api.v1.dto.AnalysisFileDTO)4 IORuntimeException (com.odysseusinc.arachne.portal.exception.IORuntimeException)4 Study (com.odysseusinc.arachne.portal.model.Study)4 IUser (com.odysseusinc.arachne.portal.model.IUser)3 LinkedList (java.util.LinkedList)3 SubmissionFile (com.odysseusinc.arachne.portal.model.SubmissionFile)2 SubmissionGroup (com.odysseusinc.arachne.portal.model.SubmissionGroup)2 ArachnePermission (com.odysseusinc.arachne.portal.security.ArachnePermission)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 HashSet (java.util.HashSet)2 List (java.util.List)2