Search in sources :

Example 6 with TerminologyResource

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

the class ComponentEffectiveTimeRestoreChangeProcessor method getAvailableVersionPaths.

private List<String> getAvailableVersionPaths(RepositoryContext context, String branchPath) {
    final List<ResourceURI> codeSystemsToCheck = Lists.newArrayList();
    TerminologyResource relativeCodeSystem = context.service(TerminologyResource.class);
    // always check the direct extensionOf (aka parent) CodeSystem
    if (relativeCodeSystem.getExtensionOf() != null) {
        if (relativeCodeSystem.getExtensionOf().isHead()) {
            // in case of regular CodeSystem check the latest available version if available, if not, then skip
            getLatestCodeSystemVersion(context, relativeCodeSystem.getExtensionOf().withoutPath()).ifPresent(latestVersion -> {
                codeSystemsToCheck.add(relativeCodeSystem.getExtensionOf().asLatest());
            });
        } else {
            codeSystemsToCheck.add(relativeCodeSystem.getExtensionOf());
        }
    }
    // in case of an upgrade CodeSystem check the original CodeSystem as well
    if (relativeCodeSystem.getUpgradeOf() != null) {
        // TODO, it would be great to know that sync point between the Upgrade and the UpdradeOf and use that timestamp as reference, for now, fall back to the HEAD
        codeSystemsToCheck.add(relativeCodeSystem.getUpgradeOf());
    } else {
        // in case of regular CodeSystem check the latest available version if available, if not, then skip
        getLatestCodeSystemVersion(context, relativeCodeSystem.getResourceURI().withoutPath()).ifPresent(latestVersion -> {
            codeSystemsToCheck.add(latestVersion.getVersionResourceURI());
        });
    }
    return context.service(ResourceURIPathResolver.class).resolve(context, codeSystemsToCheck);
}
Also used : ResourceURI(com.b2international.snowowl.core.ResourceURI) ResourceURIPathResolver(com.b2international.snowowl.core.uri.ResourceURIPathResolver) TerminologyResource(com.b2international.snowowl.core.TerminologyResource)

Example 7 with TerminologyResource

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

the class DefaultResourceURIPathResolver method resolveWithVersion.

@Override
public PathWithVersion resolveWithVersion(ServiceProvider context, ResourceURI uriToResolve, Resource resource) {
    if (resource instanceof TerminologyResource) {
        TerminologyResource terminologyResource = (TerminologyResource) resource;
        if (uriToResolve.isHead()) {
            // use code system working branch directly when HEAD is specified
            final String workingBranchPath = terminologyResource.getBranchPath() + uriToResolve.getTimestampPart();
            return new PathWithVersion(workingBranchPath);
        }
        // prevent running version search if path does not look like a versionId (single path segment)
        final String relativeBranchPath = terminologyResource.getRelativeBranchPath(uriToResolve.getPath());
        if (uriToResolve.getPath().contains(Branch.SEPARATOR)) {
            final String absoluteBranchPath = relativeBranchPath + uriToResolve.getTimestampPart();
            return new PathWithVersion(absoluteBranchPath);
        }
        VersionSearchRequestBuilder versionSearch = ResourceRequests.prepareSearchVersion().one().filterByResource(terminologyResource.getResourceURI());
        if (uriToResolve.isLatest()) {
            // fetch the latest resource version if LATEST is specified in the URI
            versionSearch.sortBy(SearchResourceRequest.Sort.fieldDesc(VersionDocument.Fields.EFFECTIVE_TIME));
        } else {
            // try to fetch the path as exact version if not the special LATEST is specified in the URI
            versionSearch.filterByVersionId(uriToResolve.getPath());
        }
        // determine the final branch path, if based on the version search we find a version, then use that, otherwise use the defined path as relative branch of the code system working branch
        Versions versions = versionSearch.buildAsync().getRequest().execute(context);
        return versions.first().map(v -> {
            final String versionBranchPath = v.getBranchPath() + uriToResolve.getTimestampPart();
            final ResourceURI versionResourceURI = v.getVersionResourceURI().withTimestampPart(uriToResolve.getTimestampPart());
            return new PathWithVersion(versionBranchPath, versionResourceURI);
        }).orElseGet(() -> {
            if (uriToResolve.isLatest() || !allowBranches) {
                throw new BadRequestException("No Resource version is present in '%s'. Explicit '%s' can be used to retrieve the latest work in progress version of the Resource.", terminologyResource.getId(), terminologyResource.getId());
            }
            return new PathWithVersion(relativeBranchPath);
        });
    }
    return new PathWithVersion("");
}
Also used : VersionSearchRequestBuilder(com.b2international.snowowl.core.request.version.VersionSearchRequestBuilder) BadRequestException(com.b2international.commons.exceptions.BadRequestException) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) ResourceRequests(com.b2international.snowowl.core.request.ResourceRequests) VersionSearchRequestBuilder(com.b2international.snowowl.core.request.version.VersionSearchRequestBuilder) Set(java.util.Set) Collectors(java.util.stream.Collectors) Branch(com.b2international.snowowl.core.branch.Branch) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest) Resource(com.b2international.snowowl.core.Resource) List(java.util.List) VersionDocument(com.b2international.snowowl.core.version.VersionDocument) Map(java.util.Map) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) CompareUtils(com.b2international.commons.CompareUtils) Collections(java.util.Collections) Versions(com.b2international.snowowl.core.version.Versions) ResourceURI(com.b2international.snowowl.core.ResourceURI) ResourceURI(com.b2international.snowowl.core.ResourceURI) Versions(com.b2international.snowowl.core.version.Versions) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 8 with TerminologyResource

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

the class SnomedRf2ExportRequest method collectExportableCodeSystemVersions.

private void collectExportableCodeSystemVersions(final RepositoryContext context, final Set<Version> versionsToExport, final TerminologyResource codeSystem, final String referenceBranch) {
    final List<Version> candidateVersions = newArrayList(getCodeSystemVersions(context, codeSystem.getResourceURI()));
    if (candidateVersions.isEmpty()) {
        return;
    }
    final Set<String> versionPaths = candidateVersions.stream().map(Version::getBranchPath).collect(Collectors.toSet());
    final Branches versionBranches = getBranches(context, versionPaths);
    final Map<String, Branch> versionBranchesByPath = Maps.uniqueIndex(versionBranches, Branch::path);
    // cutoff timestamp represents the timestamp on the current referenceBranch segments, cutting off any versions created after this timestamp
    final Branch cutoffBranch = getBranch(context, referenceBranch);
    final String latestVersionParentBranch = candidateVersions.stream().findFirst().map(v -> BranchPathUtils.createPath(v.getBranchPath()).getParentPath()).get();
    final long cutoffBaseTimestamp = getCutoffBaseTimestamp(context, cutoffBranch, latestVersionParentBranch);
    // Remove all code system versions which were created after the cut-off date, or don't have a corresponding branch
    candidateVersions.removeIf(v -> false || !versionBranchesByPath.containsKey(v.getBranchPath()) || versionBranchesByPath.get(v.getBranchPath()).baseTimestamp() > cutoffBaseTimestamp);
    versionsToExport.addAll(candidateVersions);
    // Exit early if only an extension code system should be exported, or we are already at the "base" code system
    final ResourceURI extensionOfUri = codeSystem.getExtensionOf();
    if (extensionOnly || extensionOfUri == null) {
        return;
    }
    // Otherwise, collect applicable versions using this code system's working path
    final CodeSystem extensionOfCodeSystem = CodeSystemRequests.prepareGetCodeSystem(extensionOfUri.getResourceId()).buildAsync().execute(context);
    collectExportableCodeSystemVersions(context, versionsToExport, extensionOfCodeSystem, codeSystem.getBranchPath());
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) SnomedConceptSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedConceptSearchRequestBuilder) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) SnomedReferenceSets(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSets) RepositoryRequests(com.b2international.snowowl.core.repository.RepositoryRequests) Sets.newTreeSet(com.google.common.collect.Sets.newTreeSet) Collections.singleton(java.util.Collections.singleton) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) LocalTime(java.time.LocalTime) RevisionIndex(com.b2international.index.revision.RevisionIndex) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) Branches(com.b2international.snowowl.core.branch.Branches) Permission(com.b2international.snowowl.core.identity.Permission) CompareUtils(com.b2international.commons.CompareUtils) Path(java.nio.file.Path) com.google.common.collect(com.google.common.collect) Collectors.toSet(java.util.stream.Collectors.toSet) ResourceURI(com.b2international.snowowl.core.ResourceURI) com.b2international.snowowl.core.request(com.b2international.snowowl.core.request) Version(com.b2international.snowowl.core.version.Version) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Request(com.b2international.snowowl.core.events.Request) IEventBus(com.b2international.snowowl.eventbus.IEventBus) Instant(java.time.Instant) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Stream(java.util.stream.Stream) VersionDocument(com.b2international.snowowl.core.version.VersionDocument) LocalDate(java.time.LocalDate) Sort(com.b2international.snowowl.core.request.SearchResourceRequest.Sort) Entry(java.util.Map.Entry) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) SnomedDescriptionIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry) BranchContext(com.b2international.snowowl.core.domain.BranchContext) Builder(com.google.common.collect.ImmutableList.Builder) FileUtils(com.b2international.commons.FileUtils) CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem) EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) java.util(java.util) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) LocalDateTime(java.time.LocalDateTime) com.b2international.snowowl.snomed.datastore.request.rf2.exporter(com.b2international.snowowl.snomed.datastore.request.rf2.exporter) com.b2international.snowowl.snomed.core.domain(com.b2international.snowowl.snomed.core.domain) Branch(com.b2international.snowowl.core.branch.Branch) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) Strings(com.google.common.base.Strings) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Attachment(com.b2international.snowowl.core.attachments.Attachment) SnomedRefSetMemberSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedRefSetMemberSearchRequestBuilder) BadRequestException(com.b2international.commons.exceptions.BadRequestException) BranchPathUtils(com.b2international.snowowl.core.branch.BranchPathUtils) DateFormats(com.b2international.snowowl.core.date.DateFormats) Files(java.nio.file.Files) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SnomedTerminologyComponentConstants(com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) ChronoUnit(java.time.temporal.ChronoUnit) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) NotEmpty(org.hibernate.validator.constraints.NotEmpty) DateTimeFormatter(java.time.format.DateTimeFormatter) IComponent(com.b2international.snowowl.core.domain.IComponent) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) Versions(com.b2international.snowowl.core.version.Versions) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) ResourceURI(com.b2international.snowowl.core.ResourceURI) Version(com.b2international.snowowl.core.version.Version) Branches(com.b2international.snowowl.core.branch.Branches) Branch(com.b2international.snowowl.core.branch.Branch) CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem)

Example 9 with TerminologyResource

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

the class TerminologyResourceRequest method initialize.

private void initialize(ServiceProvider context) {
    if (resourcePath.startsWith(Branch.MAIN_PATH)) {
        context.log().warn("Reflective access of terminology resources ('{}/{}') is not the recommended way of accessing resources. Consider using Resource IDs and relative branch path expressions.", toolingId, resourcePath);
        this.resource = context.service(PathTerminologyResourceResolver.class).resolve(context, toolingId, resourcePath);
        this.resourceUri = resource.getResourceURI(resourcePath);
        this.branchPath = resourcePath;
    } else {
        // resourcePaths are just ID/PATH style expressions to reference content in a terminology repository
        final ResourceURI referenceResourceUri = ResourceURI.of("any", resourcePath);
        Resource resource = ResourceRequests.prepareGet(referenceResourceUri).buildAsync().getRequest().execute(context);
        if (!(resource instanceof TerminologyResource)) {
            throw new NotFoundException("Terminology Resource", referenceResourceUri.getResourceId());
        }
        this.resource = (TerminologyResource) resource;
        this.resourceUri = this.resource.getResourceURI().withPath(referenceResourceUri.getPath()).withTimestampPart(referenceResourceUri.getTimestampPart());
        this.branchPath = context.service(ResourceURIPathResolver.class).resolve(context, referenceResourceUri, resource);
    }
}
Also used : ResourceURI(com.b2international.snowowl.core.ResourceURI) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) Resource(com.b2international.snowowl.core.Resource) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) NotFoundException(com.b2international.commons.exceptions.NotFoundException)

Aggregations

TerminologyResource (com.b2international.snowowl.core.TerminologyResource)9 ResourceURI (com.b2international.snowowl.core.ResourceURI)6 CompareUtils (com.b2international.commons.CompareUtils)3 BadRequestException (com.b2international.commons.exceptions.BadRequestException)2 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)2 Resource (com.b2international.snowowl.core.Resource)2 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)2 AttachmentRegistry (com.b2international.snowowl.core.attachments.AttachmentRegistry)2 AccessControl (com.b2international.snowowl.core.authorization.AccessControl)2 Branch (com.b2international.snowowl.core.branch.Branch)2 BranchContext (com.b2international.snowowl.core.domain.BranchContext)2 Request (com.b2international.snowowl.core.events.Request)2 Permission (com.b2international.snowowl.core.identity.Permission)2 ResourceURIPathResolver (com.b2international.snowowl.core.uri.ResourceURIPathResolver)2 VersionDocument (com.b2international.snowowl.core.version.VersionDocument)2 Versions (com.b2international.snowowl.core.version.Versions)2 SnomedCoreConfiguration (com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration)2 ReasonerTaxonomy (com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomy)2 DelegateOntologyFactory (com.b2international.snowowl.snomed.reasoner.ontology.DelegateOntologyFactory)2 IOException (java.io.IOException)2