Search in sources :

Example 1 with AchillesFile

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

the class AchillesImportServiceImpl method importData.

@Override
@Async(value = "importAchillesReportsExecutor")
@Transactional
public void importData(IDataSource dataSource, File archivedData) throws IOException {
    Characterization characterization = new Characterization();
    characterization.setDataSource(dataSource);
    final Long dataSourceId = dataSource.getId();
    final String dataSourceName = dataSource.getName();
    final DataNode dataNode = dataSource.getDataNode();
    final Long dataNodeId = dataNode.getId();
    final String dataNodeName = dataNode.getName();
    LOGGER.info(IMPORT_ACHILLES_RESULT_LOG, "Started", dataSourceId, dataSourceName, dataNodeId, dataNodeName);
    Timestamp now = new Timestamp(new Date().getTime());
    characterization.setDate(now);
    Path tempDir = Files.createTempDirectory("achilles_");
    entityManager.setFlushMode(FlushModeType.COMMIT);
    batch.set(new ArrayList<>());
    try {
        unzipData(archivedData, tempDir);
        List<AchillesFile> files = new LinkedList<>();
        JsonParser parser = new JsonParser();
        final Characterization result = characterizationRepository.save(characterization);
        Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                AchillesFile achillesFile = new AchillesFile();
                achillesFile.setFilePath(tempDir.relativize(file).toString());
                try (JsonReader reader = new JsonReader(new InputStreamReader(Files.newInputStream(file, READ)))) {
                    JsonObject jsonObject = parser.parse(reader).getAsJsonObject();
                    achillesFile.setData(jsonObject);
                }
                achillesFile.setCharacterization(result);
                saveAsBatch(achillesFile);
                return CONTINUE;
            }
        });
        flush();
        LOGGER.info(IMPORT_ACHILLES_RESULT_LOG, "Finished", dataSourceId, dataSourceName, dataNodeId, dataNodeName);
    } finally {
        FileUtils.deleteQuietly(tempDir.toFile());
    }
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Timestamp(java.sql.Timestamp) Date(java.util.Date) LinkedList(java.util.LinkedList) AchillesFile(com.odysseusinc.arachne.portal.model.achilles.AchillesFile) Characterization(com.odysseusinc.arachne.portal.model.achilles.Characterization) DataNode(com.odysseusinc.arachne.portal.model.DataNode) JsonReader(com.google.gson.stream.JsonReader) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) JsonParser(com.google.gson.JsonParser) Async(org.springframework.scheduling.annotation.Async) Transactional(javax.transaction.Transactional)

Example 2 with AchillesFile

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

the class AchillesControllerTest method getFile.

@WithUserDetails(value = "admin@odysseusinc.com")
@DatabaseSetup(value = { "/data/achilles/datanode.xml", "/data/achilles/users.xml", "/data/achilles/studies.xml", "/data/achilles/datasources.xml", "/data/achilles/reports.xml", "/data/achilles/characterizations.xml", "/data/achilles/permissions.xml", "/data/achilles/study-participants.xml" })
public void getFile() throws Exception {
    Bootstrap bootstrap = new Bootstrap();
    AchillesFile observationFile = achillesFileRepository.findOne(11L);
    assertThat(observationFile, is(notNullValue()));
    observationFile.setData(bootstrap.observationJson());
    AchillesFile dashboardFile = achillesFileRepository.findOne(4L);
    assertThat(dashboardFile, is(notNullValue()));
    dashboardFile.setData(bootstrap.dashboardJson());
    achillesFileRepository.save(Arrays.asList(observationFile, dashboardFile));
    mvc.perform(get(String.format(API_FILES, PUBLIC_DS, "dashboard.json")).accept(MediaType.APPLICATION_JSON_UTF8)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$.result.SUMMARY.ATTRIBUTE_NAME[0]", is("Source name")));
    mvc.perform(get(String.format(API_FILES, PUBLIC_DS, "observations/observation_3051227.json")).accept(MediaType.APPLICATION_JSON_UTF8)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$.result.OBSERVATIONS_BY_TYPE.CONCEPT_NAME", is("Lab observation numeric result")));
}
Also used : AchillesFile(com.odysseusinc.arachne.portal.model.achilles.AchillesFile) WithUserDetails(org.springframework.security.test.context.support.WithUserDetails) DatabaseSetup(com.github.springtestdbunit.annotation.DatabaseSetup)

Example 3 with AchillesFile

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

the class BaseAchillesController method getFile.

@ApiOperation("Get file contents")
@RequestMapping(value = { "datasource/{id}/files/{filename:.*}", "datasource/{id}/files/{filepath:.*}/{filename:.*}" }, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public JsonResult<JsonNode> getFile(@PathVariable("id") Long datasourceId, @RequestParam(name = "char", required = false) Long characterizationId, @PathVariable(value = "filepath", required = false) String path, @PathVariable("filename") String filename) throws NotExistException, IOException {
    final String filepath = StringUtils.isBlank(path) ? filename : path + File.separator + filename;
    DS dataSource = checkDataSource(datasourceId);
    if (characterizationId == null) {
        characterizationId = achillesService.getLatestCharacterizationId(dataSource);
    }
    AchillesFile file = achillesService.getAchillesFile(characterizationId, filepath).orElseThrow(() -> new NotExistException(String.format("File %s not found", filepath), AchillesFile.class));
    JsonObject jsonObject = file.getData();
    JsonNode node = objectMapper.readTree(new Gson().toJson(jsonObject));
    return new JsonResult<>(NO_ERROR, node);
}
Also used : JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) NotExistException(com.odysseusinc.arachne.portal.exception.NotExistException) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonResult(com.odysseusinc.arachne.commons.api.v1.dto.util.JsonResult) AchillesFile(com.odysseusinc.arachne.portal.model.achilles.AchillesFile) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

AchillesFile (com.odysseusinc.arachne.portal.model.achilles.AchillesFile)3 JsonObject (com.google.gson.JsonObject)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)1 Gson (com.google.gson.Gson)1 JsonParser (com.google.gson.JsonParser)1 JsonReader (com.google.gson.stream.JsonReader)1 JsonResult (com.odysseusinc.arachne.commons.api.v1.dto.util.JsonResult)1 NotExistException (com.odysseusinc.arachne.portal.exception.NotExistException)1 DataNode (com.odysseusinc.arachne.portal.model.DataNode)1 Characterization (com.odysseusinc.arachne.portal.model.achilles.Characterization)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1