Search in sources :

Example 1 with InternalAttachmentRegistry

use of com.b2international.snowowl.core.attachments.InternalAttachmentRegistry in project snow-owl by b2ihealthcare.

the class SnomedExportApiTest method executeMultipleExportsAtTheSameTime.

@Test
public void executeMultipleExportsAtTheSameTime() throws Exception {
    Promise<Attachment> first = SnomedRequests.rf2().prepareExport().setReleaseType(Rf2ReleaseType.FULL).setCountryNamespaceElement("INT").setRefSetExportLayout(Rf2RefSetExportLayout.COMBINED).setLocales(LOCALES).build(branchPath.getPath()).execute(getBus());
    Promise<Attachment> second = SnomedRequests.rf2().prepareExport().setCountryNamespaceElement("INT").setRefSetExportLayout(Rf2RefSetExportLayout.COMBINED).setReleaseType(Rf2ReleaseType.SNAPSHOT).setLocales(LOCALES).build(branchPath.getPath()).execute(getBus());
    String message = Promise.all(first, second).then(input -> {
        Attachment firstResult = (Attachment) input.get(0);
        Attachment secondResult = (Attachment) input.get(1);
        InternalAttachmentRegistry fileRegistry = (InternalAttachmentRegistry) ApplicationContext.getServiceForClass(AttachmentRegistry.class);
        File firstArchive = fileRegistry.getAttachment(firstResult.getAttachmentId());
        File secondArchive = fileRegistry.getAttachment(secondResult.getAttachmentId());
        final Map<String, Boolean> firstArchiveMap = ImmutableMap.<String, Boolean>builder().put("sct2_Concept_Full", true).build();
        final Map<String, Boolean> secondArchiveMap = ImmutableMap.<String, Boolean>builder().put("sct2_Concept_Snapshot", true).build();
        try {
            assertArchiveContainsFiles(firstArchive, firstArchiveMap);
            assertArchiveContainsFiles(secondArchive, secondArchiveMap);
        } catch (Exception e) {
            return e.getMessage();
        }
        fileRegistry.delete(firstResult.getAttachmentId());
        fileRegistry.delete(secondResult.getAttachmentId());
        return null;
    }).fail(input -> input.getMessage()).getSync(2, TimeUnit.MINUTES);
    assertNull(message, message);
}
Also used : InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) SnomedExportRestRequests.export(com.b2international.snowowl.snomed.core.rest.SnomedExportRestRequests.export) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) RestExtensions.assertCreated(com.b2international.snowowl.test.commons.rest.RestExtensions.assertCreated) RestExtensions(com.b2international.snowowl.test.commons.rest.RestExtensions) Json(com.b2international.commons.json.Json) SnomedRestFixtures(com.b2international.snowowl.snomed.core.rest.SnomedRestFixtures) java.nio.file(java.nio.file) Pair(com.b2international.commons.Pair) SnomedComponentType(com.b2international.snowowl.snomed.core.rest.SnomedComponentType) SnomedComponentRestRequests.getComponent(com.b2international.snowowl.snomed.core.rest.SnomedComponentRestRequests.getComponent) Map(java.util.Map) RevisionIndex(com.b2international.index.revision.RevisionIndex) SnomedComponentRestRequests.updateComponent(com.b2international.snowowl.snomed.core.rest.SnomedComponentRestRequests.updateComponent) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) MODULE_SCT_CORE(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts.MODULE_SCT_CORE) UK_ACCEPTABLE_MAP(com.b2international.snowowl.snomed.core.rest.SnomedApiTestConstants.UK_ACCEPTABLE_MAP) ResourceURI(com.b2international.snowowl.core.ResourceURI) CodeSystemRestRequests(com.b2international.snowowl.test.commons.codesystem.CodeSystemRestRequests) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) ROOT_CONCEPT(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts.ROOT_CONCEPT) Sets(com.google.common.collect.Sets) List(java.util.List) SnomedRefSetRestRequests.updateRefSetMemberEffectiveTime(com.b2international.snowowl.snomed.core.rest.SnomedRefSetRestRequests.updateRefSetMemberEffectiveTime) Entry(java.util.Map.Entry) AbstractSnomedApiTest(com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest) Joiner(com.google.common.base.Joiner) CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem) EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) CodeSystemVersionRestRequests.createVersion(com.b2international.snowowl.test.commons.codesystem.CodeSystemVersionRestRequests.createVersion) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) SnomedRefSetRestRequests.updateRefSetComponent(com.b2international.snowowl.snomed.core.rest.SnomedRefSetRestRequests.updateRefSetComponent) Promise(com.b2international.snowowl.core.events.util.Promise) com.b2international.snowowl.snomed.core.domain(com.b2international.snowowl.snomed.core.domain) Multimap(com.google.common.collect.Multimap) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) Attachment(com.b2international.snowowl.core.attachments.Attachment) SnomedComponentRestRequests.createComponent(com.b2international.snowowl.snomed.core.rest.SnomedComponentRestRequests.createComponent) SnomedReferenceSetMembers(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers) UK_PREFERRED_MAP(com.b2international.snowowl.snomed.core.rest.SnomedApiTestConstants.UK_PREFERRED_MAP) BranchPathUtils(com.b2international.snowowl.core.branch.BranchPathUtils) SnomedApiTestConstants(com.b2international.snowowl.snomed.core.rest.SnomedApiTestConstants) DateFormats(com.b2international.snowowl.core.date.DateFormats) CommitResult(com.b2international.snowowl.core.request.CommitResult) SnomedExportRestRequests.doExport(com.b2international.snowowl.snomed.core.rest.SnomedExportRestRequests.doExport) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) File(java.io.File) CodeSystemRestRequests.createCodeSystem(com.b2international.snowowl.test.commons.codesystem.CodeSystemRestRequests.createCodeSystem) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) IBranchPath(com.b2international.snowowl.core.api.IBranchPath) Assert.assertEquals(org.junit.Assert.assertEquals) ApplicationContext(com.b2international.snowowl.core.ApplicationContext) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) Attachment(com.b2international.snowowl.core.attachments.Attachment) File(java.io.File) IOException(java.io.IOException) AbstractSnomedApiTest(com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest) Test(org.junit.Test)

Example 2 with InternalAttachmentRegistry

use of com.b2international.snowowl.core.attachments.InternalAttachmentRegistry in project snow-owl by b2ihealthcare.

the class SnomedRf2ExportRestService method export.

@Operation(summary = "Export SNOMED CT content to RF2", description = "Exports SNOMED CT content from the given branch to RF2.")
@ApiResponses({ @ApiResponse(responseCode = "200", description = "OK") })
@GetMapping
@ResponseBody
public ResponseEntity<?> export(@Parameter(description = "The branch path", required = true) @PathVariable(value = "path") final String branch, @ParameterObject final SnomedRf2ExportConfiguration params, @Parameter(description = "Accepted language tags, in order of preference", example = "en-US;q=0.8,en-GB;q=0.6") @RequestHeader(value = HttpHeaders.ACCEPT_LANGUAGE, defaultValue = "en-US;q=0.8,en-GB;q=0.6", required = false) final String acceptLanguage) {
    final Attachment exportedFile = SnomedRequests.rf2().prepareExport().setReleaseType(params.getType() == null ? null : Rf2ReleaseType.getByNameIgnoreCase(params.getType())).setExtensionOnly(params.isExtensionOnly()).setLocales(acceptLanguage).setIncludePreReleaseContent(params.isIncludeUnpublished()).setModules(params.getModuleIds()).setRefSets(params.getRefSetIds()).setCountryNamespaceElement(params.getNamespaceId()).setMaintainerType(Strings.isNullOrEmpty(params.getMaintainerType()) ? null : Rf2MaintainerType.getByNameIgnoreCase(params.getMaintainerType())).setNrcCountryCode(params.getNrcCountryCode()).setTransientEffectiveTime(params.getTransientEffectiveTime()).setStartEffectiveTime(params.getStartEffectiveTime()).setEndEffectiveTime(params.getEndEffectiveTime()).setRefSetExportLayout(params.getRefSetLayout() == null ? null : Rf2RefSetExportLayout.getByNameIgnoreCase(params.getRefSetLayout())).setComponentTypes(params.getComponentTypes()).build(branch).execute(getBus()).getSync();
    final File file = ((InternalAttachmentRegistry) attachments).getAttachment(exportedFile.getAttachmentId());
    final Resource exportZipResource = new FileSystemResource(file);
    final HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    httpHeaders.setContentDispositionFormData("attachment", exportedFile.getFileName());
    // TODO figure out a smart way to cache export results, probably it could be tied to commitTimestamps/versions/etc.
    file.deleteOnExit();
    return new ResponseEntity<>(exportZipResource, httpHeaders, HttpStatus.OK);
}
Also used : InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) Attachment(com.b2international.snowowl.core.attachments.Attachment) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses)

Example 3 with InternalAttachmentRegistry

use of com.b2international.snowowl.core.attachments.InternalAttachmentRegistry in project snow-owl by b2ihealthcare.

the class ResourcesImportRequest method execute.

@Override
public ImportResponse execute(ServiceProvider context) {
    final InternalAttachmentRegistry attachmentRegistry = (InternalAttachmentRegistry) context.service(AttachmentRegistry.class);
    final File file = attachmentRegistry.getAttachment(sourceFile.getAttachmentId());
    try {
        final List<R> sourceFileContent = loadSourceFileContent(context, file);
        final Map<String, R> existingResources = newHashMap(loadExistingResources(context, sourceFileContent));
        final ImportDefectAcceptor defectAcceptor = new ImportDefectAcceptor(sourceFile.getFileName());
        validateSourceFileContent(context, sourceFileContent, existingResources, defectAcceptor);
        List<ImportDefect> defects = defectAcceptor.getDefects();
        // Content with validation errors can not be imported
        ImportResponse validationResponse = ImportResponse.defects(defects);
        if (!validationResponse.getErrors().isEmpty()) {
            return validationResponse;
        }
        final Set<ComponentURI> visitedComponents = Sets.newHashSet();
        // Import each resource present in the source file, along with its content
        for (final R resource : sourceFileContent) {
            final String id = resource.getId();
            ComponentURI importedResource = importResource(context, resource, existingResources.get(id));
            if (importedResource != null) {
                visitedComponents.add(importedResource);
            }
            visitedComponents.addAll(importContent(context, resource, existingResources.get(id)));
        }
        return ImportResponse.success(visitedComponents, defects);
    } catch (final ApiException e) {
        throw e;
    } catch (final Exception e) {
        String error = "Unexpected error happened during the import of the source file: " + sourceFile.getFileName();
        context.log().error(error, e);
        return ImportResponse.error(error);
    } finally {
        if (sourceFile != null && attachmentRegistry != null) {
            attachmentRegistry.delete(sourceFile.getAttachmentId());
        }
    }
}
Also used : InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) ComponentURI(com.b2international.snowowl.core.uri.ComponentURI) ApiException(com.b2international.commons.exceptions.ApiException) File(java.io.File) ApiException(com.b2international.commons.exceptions.ApiException)

Example 4 with InternalAttachmentRegistry

use of com.b2international.snowowl.core.attachments.InternalAttachmentRegistry in project snow-owl by b2ihealthcare.

the class SnomedRf2ImportRequest method execute.

@Override
public ImportResponse execute(BranchContext context) {
    log = LoggerFactory.getLogger("import");
    context = context.inject().bind(Logger.class, log).build();
    Rf2ImportConfiguration importConfig = new Rf2ImportConfiguration(releaseType, createVersions);
    validate(context, importConfig);
    final InternalAttachmentRegistry fileReg = (InternalAttachmentRegistry) context.service(AttachmentRegistry.class);
    final File rf2Archive = fileReg.getAttachment(this.rf2Archive.getAttachmentId());
    try (Locks locks = Locks.on(context).lock(DatastoreLockContextDescriptions.IMPORT)) {
        return doImport(context, rf2Archive, importConfig);
    } catch (Exception e) {
        if (e instanceof ApiException) {
            throw (ApiException) e;
        }
        throw SnowowlRuntimeException.wrap(e);
    }
}
Also used : InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) Locks(com.b2international.snowowl.core.locks.Locks) ZipFile(java.util.zip.ZipFile) File(java.io.File) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) ApiException(com.b2international.commons.exceptions.ApiException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ApiException(com.b2international.commons.exceptions.ApiException)

Example 5 with InternalAttachmentRegistry

use of com.b2international.snowowl.core.attachments.InternalAttachmentRegistry in project snow-owl by b2ihealthcare.

the class ImportRequest method doExecute.

@Override
public final ImportResponse doExecute(TransactionContext context) {
    context.log().info("Importing components from source file '{}'.", this.attachment.getFileName());
    InternalAttachmentRegistry iar = null;
    try {
        iar = (InternalAttachmentRegistry) context.service(AttachmentRegistry.class);
        File attachment = iar.getAttachment(this.attachment.getAttachmentId());
        ImportDefectAcceptor defectsAcceptor = new ImportDefectAcceptor(this.attachment.getFileName());
        doValidate(context, attachment, defectsAcceptor, context.service(IProgressMonitor.class));
        final ImportResponse validationResponse = ImportResponse.defects(defectsAcceptor.getDefects());
        if (!validationResponse.getErrors().isEmpty()) {
            return validationResponse;
        } else {
            final Set<ComponentURI> visitedComponents = Sets.newHashSet();
            doImport(context, attachment, visitedComponents::add, context.service(IProgressMonitor.class));
            context.log().info("Finished importing components from source file '{}'.", this.attachment.getFileName());
            return ImportResponse.success(visitedComponents, validationResponse.getDefects());
        }
    } catch (ApiException e) {
        throw e;
    } catch (Exception e) {
        String error = "Unexpected error happened during the import of the source file: " + attachment.getFileName();
        context.log().error(error, e);
        return ImportResponse.error(error);
    } finally {
        if (attachment != null && iar != null) {
            iar.delete(attachment.getAttachmentId());
        }
    }
}
Also used : InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ComponentURI(com.b2international.snowowl.core.uri.ComponentURI) File(java.io.File) ApiException(com.b2international.commons.exceptions.ApiException) ApiException(com.b2international.commons.exceptions.ApiException)

Aggregations

InternalAttachmentRegistry (com.b2international.snowowl.core.attachments.InternalAttachmentRegistry)5 File (java.io.File)5 ApiException (com.b2international.commons.exceptions.ApiException)3 AttachmentRegistry (com.b2international.snowowl.core.attachments.AttachmentRegistry)3 Attachment (com.b2international.snowowl.core.attachments.Attachment)2 ComponentURI (com.b2international.snowowl.core.uri.ComponentURI)2 Pair (com.b2international.commons.Pair)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)1 Json (com.b2international.commons.json.Json)1 RevisionIndex (com.b2international.index.revision.RevisionIndex)1 ApplicationContext (com.b2international.snowowl.core.ApplicationContext)1 ResourceURI (com.b2international.snowowl.core.ResourceURI)1 IBranchPath (com.b2international.snowowl.core.api.IBranchPath)1 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)1 BranchPathUtils (com.b2international.snowowl.core.branch.BranchPathUtils)1 CodeSystem (com.b2international.snowowl.core.codesystem.CodeSystem)1 DateFormats (com.b2international.snowowl.core.date.DateFormats)1 EffectiveTimes (com.b2international.snowowl.core.date.EffectiveTimes)1 Promise (com.b2international.snowowl.core.events.util.Promise)1