Search in sources :

Example 1 with Versions

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

the class CodeSystemsCommand method getVersionsInfo.

private String getVersionsInfo(CodeSystem cs) {
    final ImmutableList.Builder<String> result = ImmutableList.builder();
    result.add("Versions:");
    final Versions versions = ResourceRequests.prepareSearchVersion().all().filterByResource(cs.getResourceURI()).sortBy(Sort.fieldAsc(VersionDocument.Fields.EFFECTIVE_TIME)).buildAsync().execute(getBus()).getSync(1, TimeUnit.MINUTES);
    if (versions.isEmpty()) {
        result.add("No versions have been created yet.");
    } else {
        result.addAll(versions.stream().map(v -> getVersionInformation(v, cs).concat("\n")).collect(ImmutableList.toImmutableList()));
    }
    return String.join(CODE_SYSTEM_SUBPROPERTY_DELIMITER, result.build());
}
Also used : Versions(com.b2international.snowowl.core.version.Versions) ImmutableList(com.google.common.collect.ImmutableList)

Example 2 with Versions

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

the class CodeSystemConverter method expandAvailableUpgrades.

private void expandAvailableUpgrades(List<CodeSystem> results) {
    if (!expand().containsKey(CodeSystem.Expand.AVAILABLE_UPGRADES)) {
        return;
    }
    final Set<ResourceURI> parentResources = results.stream().map(CodeSystem::getExtensionOf).filter(uri -> uri != null).collect(Collectors.toSet());
    final Versions parentVersions = ResourceRequests.prepareSearchVersion().all().filterByResources(parentResources.stream().map(ResourceURI::withoutPath).map(ResourceURI::toString).collect(Collectors.toSet())).build().execute(context());
    final TreeMultimap<ResourceURI, Version> versionsByResource = TreeMultimap.create(Comparator.naturalOrder(), Comparator.comparing(Version::getEffectiveTime));
    versionsByResource.putAll(Multimaps.index(parentVersions, Version::getResource));
    for (final CodeSystem result : results) {
        final ResourceURI extensionOf = result.getExtensionOf();
        // or the CodeSystem already has an upgrade
        if (extensionOf == null || result.getUpgradeOf() != null || hasUpgrade(result, results)) {
            // always set the field if user expands it
            result.setAvailableUpgrades(List.of());
            continue;
        }
        final ResourceURI resource = extensionOf.withoutPath();
        final String version = extensionOf.getPath();
        final NavigableSet<Version> candidates = versionsByResource.get(resource);
        final Optional<Version> currentExtensionVersion = candidates.stream().filter(v -> v.getVersion().equals(version)).findFirst();
        final Optional<List<ResourceURI>> upgradeUris = currentExtensionVersion.map(currentVersion -> {
            final SortedSet<Version> upgradeVersions = candidates.tailSet(currentVersion, false);
            return upgradeVersions.stream().map(upgradeVersion -> upgradeVersion.getVersionResourceURI()).collect(Collectors.toList());
        });
        result.setAvailableUpgrades(upgradeUris.orElseGet(List::of));
    }
}
Also used : BaseRevisionBranching(com.b2international.index.revision.BaseRevisionBranching) java.util(java.util) BranchState(com.b2international.index.revision.RevisionBranch.BranchState) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) ResourceRequests(com.b2international.snowowl.core.request.ResourceRequests) RepositoryManager(com.b2international.snowowl.core.RepositoryManager) RevisionBranch(com.b2international.index.revision.RevisionBranch) BranchInfo(com.b2international.snowowl.core.branch.BranchInfo) ResourceURIPathResolver(com.b2international.snowowl.core.uri.ResourceURIPathResolver) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) Multimaps(com.google.common.collect.Multimaps) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) Strings(com.google.common.base.Strings) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) Options(com.b2international.commons.options.Options) Lists(com.google.common.collect.Lists) TreeMultimap(com.google.common.collect.TreeMultimap) Versions(com.b2international.snowowl.core.version.Versions) ResourceURI(com.b2international.snowowl.core.ResourceURI) BaseResourceConverter(com.b2international.snowowl.core.request.BaseResourceConverter) Version(com.b2international.snowowl.core.version.Version) ResourceURI(com.b2international.snowowl.core.ResourceURI) Versions(com.b2international.snowowl.core.version.Versions) Version(com.b2international.snowowl.core.version.Version)

Example 3 with Versions

use of com.b2international.snowowl.core.version.Versions 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 4 with Versions

use of com.b2international.snowowl.core.version.Versions 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 5 with Versions

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

the class ResourceConverter method expandVersions.

private void expandVersions(List<Resource> results) {
    if (expand().containsKey(TerminologyResource.Expand.VERSIONS)) {
        Options expandOptions = expand().getOptions(TerminologyResource.Expand.VERSIONS);
        // version searches must be performed on individual terminology resources to provide correct results
        results.stream().filter(TerminologyResource.class::isInstance).map(TerminologyResource.class::cast).forEach(res -> {
            Versions versions = ResourceRequests.prepareSearchVersion().filterByResource(res.getResourceURI()).setLimit(getLimit(expandOptions)).setFields(expandOptions.containsKey("fields") ? expandOptions.getList("fields", String.class) : null).sortBy(expandOptions.containsKey("sort") ? expandOptions.getString("sort") : null).setLocales(locales()).build().execute(context());
            res.setVersions(versions);
        });
    }
}
Also used : Options(com.b2international.commons.options.Options) Versions(com.b2international.snowowl.core.version.Versions) TerminologyResource(com.b2international.snowowl.core.TerminologyResource)

Aggregations

Versions (com.b2international.snowowl.core.version.Versions)5 ResourceURI (com.b2international.snowowl.core.ResourceURI)3 TerminologyResource (com.b2international.snowowl.core.TerminologyResource)3 Collectors (java.util.stream.Collectors)3 CompareUtils (com.b2international.commons.CompareUtils)2 BadRequestException (com.b2international.commons.exceptions.BadRequestException)2 Options (com.b2international.commons.options.Options)2 Branch (com.b2international.snowowl.core.branch.Branch)2 RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)2 ResourceRequests (com.b2international.snowowl.core.request.ResourceRequests)2 Version (com.b2international.snowowl.core.version.Version)2 VersionDocument (com.b2international.snowowl.core.version.VersionDocument)2 Strings (com.google.common.base.Strings)2 java.util (java.util)2 FileUtils (com.b2international.commons.FileUtils)1 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)1 BaseRevisionBranching (com.b2international.index.revision.BaseRevisionBranching)1 RevisionBranch (com.b2international.index.revision.RevisionBranch)1 BranchState (com.b2international.index.revision.RevisionBranch.BranchState)1 RevisionIndex (com.b2international.index.revision.RevisionIndex)1