Search in sources :

Example 1 with AttachmentRegistry

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

the class SnomedRf2ExportRequest method execute.

@Override
public Attachment execute(final BranchContext context) {
    final String referenceBranch = context.path();
    if (referenceBranch.contains(RevisionIndex.AT_CHAR) && !Rf2ReleaseType.SNAPSHOT.equals(releaseType)) {
        throw new BadRequestException("Only snapshot export is allowed for point-in-time branch path '%s'.", referenceBranch);
    }
    if (referenceBranch.contains(RevisionIndex.REV_RANGE)) {
        if (!Rf2ReleaseType.DELTA.equals(releaseType) || needsVersionBranchesForDeltaExport()) {
            throw new BadRequestException("Only unpublished delta export is allowed for branch path range '%s'.", referenceBranch);
        }
    }
    // register export start time for later use
    final long exportStartTime = Instant.now().toEpochMilli();
    // Step 1: check if the export reference branch is a working branch path descendant
    final CodeSystem referenceCodeSystem = (CodeSystem) context.service(TerminologyResource.class);
    if (!CompareUtils.isEmpty(referenceCodeSystem.getSettings())) {
        if (Strings.isNullOrEmpty(countryNamespaceElement)) {
            if (maintainerType == null) {
                String maintainerType = (String) referenceCodeSystem.getSettings().get(SnomedTerminologyComponentConstants.CODESYSTEM_MAINTAINER_TYPE_CONFIG_KEY);
                String nrcCountryCode = (String) referenceCodeSystem.getSettings().get(SnomedTerminologyComponentConstants.CODESYSTEM_NRC_COUNTRY_CODE_CONFIG_KEY);
                if (!Strings.isNullOrEmpty(maintainerType)) {
                    String customCountryNamespaceElement = getCountryNamespaceElement(context, referenceCodeSystem, Rf2MaintainerType.getByNameIgnoreCase(maintainerType), Strings.nullToEmpty(nrcCountryCode));
                    countryNamespaceElement = customCountryNamespaceElement;
                }
            } else {
                countryNamespaceElement = getCountryNamespaceElement(context, referenceCodeSystem, maintainerType, Strings.nullToEmpty(nrcCountryCode));
            }
        }
        if (refSetExportLayout == null && referenceCodeSystem.getSettings().containsKey(SnomedTerminologyComponentConstants.CODESYSTEM_RF2_EXPORT_LAYOUT_CONFIG_KEY)) {
            String refSetLayout = (String) referenceCodeSystem.getSettings().get(SnomedTerminologyComponentConstants.CODESYSTEM_RF2_EXPORT_LAYOUT_CONFIG_KEY);
            Rf2RefSetExportLayout rf2RefSetExportLayout = Rf2RefSetExportLayout.getByNameIgnoreCase(refSetLayout);
            refSetExportLayout = rf2RefSetExportLayout;
        }
    }
    if (Strings.isNullOrEmpty(countryNamespaceElement)) {
        countryNamespaceElement = getCountryNamespaceElement(context, referenceCodeSystem, DEFAULT_MAINTAINER_TYPE, "");
    }
    if (refSetExportLayout == null) {
        refSetExportLayout = DEFAULT_RF2_EXPORT_LAYOUT;
    }
    // Step 2: retrieve code system versions that are visible from the reference branch
    final TreeSet<Version> versionsToExport = getAllExportableCodeSystemVersions(context, referenceCodeSystem);
    // Step 3: compute branches to export
    final List<String> branchesToExport = computeBranchesToExport(referenceBranch, versionsToExport);
    // Step 4: compute possible language codes
    Multimap<String, String> availableLanguageCodes = getLanguageCodes(context, branchesToExport);
    Path exportDirectory = null;
    try {
        final UUID exportId = UUID.randomUUID();
        // create temporary export directory
        exportDirectory = createExportDirectory(exportId);
        // get archive effective time based on latest version effective / transient effective time / current date
        final LocalDateTime archiveEffectiveDate = getArchiveEffectiveTime(context, versionsToExport);
        final String archiveEffectiveDateShort = EffectiveTimes.format(archiveEffectiveDate.toLocalDate(), DateFormats.SHORT);
        // create main folder including release status and archive effective date
        final Path releaseDirectory = createReleaseDirectory(exportDirectory, archiveEffectiveDate);
        final Set<String> visitedComponentEffectiveTimes = newHashSet();
        final long effectiveTimeStart = startEffectiveTime != null ? EffectiveTimes.getEffectiveTime(startEffectiveTime) : 0;
        final long effectiveTimeEnd = endEffectiveTime != null ? EffectiveTimes.getEffectiveTime(endEffectiveTime) : Long.MAX_VALUE;
        // export content from the pre-computed version branches
        for (String branch : branchesToExport) {
            exportBranch(releaseDirectory, context, branch, archiveEffectiveDateShort, effectiveTimeStart, effectiveTimeEnd, visitedComponentEffectiveTimes, availableLanguageCodes.get(branch));
        }
        // export content from reference branch
        if (includePreReleaseContent) {
            // If a special branch path was given, use it directly
            final String referenceBranchToExport = containsSpecialCharacter(referenceBranch) ? referenceBranch : RevisionIndex.toBranchAtPath(referenceBranch, exportStartTime);
            exportBranch(releaseDirectory, context, referenceBranchToExport, archiveEffectiveDateShort, EffectiveTimes.UNSET_EFFECTIVE_TIME, EffectiveTimes.UNSET_EFFECTIVE_TIME, visitedComponentEffectiveTimes, availableLanguageCodes.get(referenceBranch));
        }
        // Step 6: compress to archive and upload to the file registry
        final AttachmentRegistry fileRegistry = context.service(AttachmentRegistry.class);
        registerResult(fileRegistry, exportId, exportDirectory);
        final String fileName = releaseDirectory.getFileName() + ".zip";
        return new Attachment(exportId, fileName);
    } catch (final Exception e) {
        throw new SnowowlRuntimeException("Failed to export terminology content to RF2.", e);
    } finally {
        if (exportDirectory != null) {
            FileUtils.deleteDirectory(exportDirectory.toFile());
        }
    }
}
Also used : Path(java.nio.file.Path) LocalDateTime(java.time.LocalDateTime) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) Attachment(com.b2international.snowowl.core.attachments.Attachment) CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) IOException(java.io.IOException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Version(com.b2international.snowowl.core.version.Version) BadRequestException(com.b2international.commons.exceptions.BadRequestException) TerminologyResource(com.b2international.snowowl.core.TerminologyResource)

Example 2 with AttachmentRegistry

use of com.b2international.snowowl.core.attachments.AttachmentRegistry 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 3 with AttachmentRegistry

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

the class OntologyExportRequest method execute.

@Override
public String execute(final BranchContext context) {
    final RevisionSearcher revisionSearcher = context.service(RevisionSearcher.class);
    TerminologyResource resource = context.service(TerminologyResource.class);
    @SuppressWarnings("unchecked") final Set<String> reasonerExcludedModuleIds = Collections3.toImmutableSet((Iterable) resource.getSettings().getOrDefault(REASONER_EXCLUDE_MODULE_IDS, Collections.emptySet()));
    final SnomedCoreConfiguration configuration = context.service(SnomedCoreConfiguration.class);
    final boolean concreteDomainSupportEnabled = configuration.isConcreteDomainSupported();
    final ReasonerTaxonomyBuilder taxonomyBuilder = new ReasonerTaxonomyBuilder(reasonerExcludedModuleIds);
    taxonomyBuilder.addActiveConceptIds(revisionSearcher);
    taxonomyBuilder.finishConcepts();
    taxonomyBuilder.addFullySpecifiedNames(revisionSearcher);
    taxonomyBuilder.addConceptFlags(revisionSearcher);
    taxonomyBuilder.addActiveStatedEdges(revisionSearcher);
    taxonomyBuilder.addActiveStatedRelationships(revisionSearcher);
    taxonomyBuilder.addNeverGroupedTypeIds(revisionSearcher);
    taxonomyBuilder.addActiveAxioms(revisionSearcher);
    if (concreteDomainSupportEnabled) {
        taxonomyBuilder.addActiveConcreteDomainMembers(revisionSearcher);
    }
    final ReasonerTaxonomy taxonomy = taxonomyBuilder.build();
    final OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
    ontologyManager.getOntologyFactories().add(new DelegateOntologyFactory(taxonomy));
    final IRI ontologyIRI = IRI.create(DelegateOntology.NAMESPACE_SCTM + ontologyModuleId);
    try {
        final OWLOntology ontology = ontologyManager.createOntology(ontologyIRI);
        OWLOntology ontologyToExport = ontologyManager.createOntology();
        ontology.getAxioms().forEach(axiom -> {
            ontologyManager.addAxiom(ontologyToExport, axiom);
        });
        final OWLDocumentFormat documentFormat = getOWLDocumentFormat();
        final AttachmentRegistry fileRegistry = context.service(AttachmentRegistry.class);
        final UUID id = UUID.randomUUID();
        final PipedOutputStream os = new PipedOutputStream();
        final PipedInputStream is = new PipedInputStream(os, PIPE_SIZE);
        final ForkJoinTask<?> uploadTask = ForkJoinTask.adapt(() -> fileRegistry.upload(id, is));
        final ForkJoinTask<?> saveTask = ForkJoinTask.adapt(() -> {
            try {
                ontologyManager.saveOntology(ontologyToExport, documentFormat, os);
            } catch (final OWLOntologyStorageException e) {
                throw createExportFailedException(context, e);
            } finally {
                try {
                    os.close();
                } catch (final IOException e) {
                    throw createExportFailedException(context, e);
                }
            }
        });
        ForkJoinTask.invokeAll(saveTask, uploadTask);
        return id.toString();
    } catch (final OWLOntologyCreationException e) {
        throw createExportFailedException(context, e);
    } catch (final IOException e) {
        throw createExportFailedException(context, e);
    } finally {
        // invalidate static cache entries :'(
        ontologyManager.getOntologies().forEach(o -> {
            ontologyManager.applyChange(new SetOntologyID(o, new OWLOntologyID()));
        });
    }
}
Also used : ReasonerTaxonomy(com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomy) ReasonerTaxonomyBuilder(com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomyBuilder) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) SnomedCoreConfiguration(com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration) DelegateOntologyFactory(com.b2international.snowowl.snomed.reasoner.ontology.DelegateOntologyFactory) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) UUID(java.util.UUID) RevisionSearcher(com.b2international.index.revision.RevisionSearcher)

Aggregations

AttachmentRegistry (com.b2international.snowowl.core.attachments.AttachmentRegistry)3 TerminologyResource (com.b2international.snowowl.core.TerminologyResource)2 IOException (java.io.IOException)2 ApiException (com.b2international.commons.exceptions.ApiException)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)1 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)1 Attachment (com.b2international.snowowl.core.attachments.Attachment)1 InternalAttachmentRegistry (com.b2international.snowowl.core.attachments.InternalAttachmentRegistry)1 CodeSystem (com.b2international.snowowl.core.codesystem.CodeSystem)1 ComponentURI (com.b2international.snowowl.core.uri.ComponentURI)1 Version (com.b2international.snowowl.core.version.Version)1 SnomedCoreConfiguration (com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration)1 ReasonerTaxonomy (com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomy)1 ReasonerTaxonomyBuilder (com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomyBuilder)1 DelegateOntologyFactory (com.b2international.snowowl.snomed.reasoner.ontology.DelegateOntologyFactory)1 File (java.io.File)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 Path (java.nio.file.Path)1