Search in sources :

Example 21 with ResourceURI

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

the class TerminologyResourceContentRequest method execute.

@Override
public R execute(TerminologyResourceContext context) {
    final ResourceURI resourceURI = context.resourceURI();
    final TerminologyResource resource = context.resource();
    final PathWithVersion branchPathWithVersion = context.service(ResourceURIPathResolver.class).resolveWithVersion(context, resourceURI, resource);
    final String path = branchPathWithVersion.getPath();
    final ResourceURI versionResourceURI = branchPathWithVersion.getVersionResourceURI();
    if (versionResourceURI != null) {
        context = context.inject().bind(ResourceURI.class, versionResourceURI).bind(PathWithVersion.class, branchPathWithVersion).build();
    }
    return new RepositoryRequest<R>(resource.getToolingId(), new BranchRequest<R>(path, next())).execute(context);
}
Also used : ResourceURI(com.b2international.snowowl.core.ResourceURI) ResourceURIPathResolver(com.b2international.snowowl.core.uri.ResourceURIPathResolver) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) PathWithVersion(com.b2international.snowowl.core.uri.ResourceURIPathResolver.PathWithVersion) BranchRequest(com.b2international.snowowl.core.request.BranchRequest)

Example 22 with ResourceURI

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

the class CodeSystemConverter method expandUpgradeOfInfo.

private void expandUpgradeOfInfo(List<CodeSystem> results) {
    if (!expand().containsKey(CodeSystem.Expand.UPGRADE_INFO)) {
        return;
    }
    final List<ResourceURI> upgradeOfURIs = results.stream().filter(codeSystem -> codeSystem.getUpgradeOf() != null).map(codeSystem -> codeSystem.getUpgradeOf().withoutPath()).collect(Collectors.toList());
    // nothing to expand, quit early
    if (upgradeOfURIs.isEmpty()) {
        return;
    }
    final List<String> upgradeOfBranches = context().service(ResourceURIPathResolver.class).resolve(context(), upgradeOfURIs);
    final Map<ResourceURI, String> branchesByUpgradeOf = Maps.newHashMap();
    Iterator<ResourceURI> uriIterator = upgradeOfURIs.iterator();
    Iterator<String> branchIterator = upgradeOfBranches.iterator();
    while (uriIterator.hasNext() && branchIterator.hasNext()) {
        ResourceURI uri = uriIterator.next();
        String branch = branchIterator.next();
        branchesByUpgradeOf.put(uri, branch);
    }
    for (CodeSystem result : results) {
        BaseRevisionBranching branching = context().service(RepositoryManager.class).get(result.getToolingId()).service(BaseRevisionBranching.class);
        String upgradeOfCodeSystemBranchPath = branchesByUpgradeOf.get(result.getUpgradeOf().withoutPath());
        if (!Strings.isNullOrEmpty(upgradeOfCodeSystemBranchPath)) {
            RevisionBranch branch = branching.getBranch(result.getBranchPath());
            BranchState branchState = branching.getBranchState(result.getBranchPath(), upgradeOfCodeSystemBranchPath);
            BranchInfo mainInfo = new BranchInfo(branch.getPath(), branchState, branch.getBaseTimestamp(), branch.getHeadTimestamp());
            List<ResourceURI> availableVersions = Lists.newArrayList();
            List<BranchInfo> versionBranchInfo = Lists.newArrayList();
            if (!result.getUpgradeOf().isHead()) {
                long startTimestamp;
                final String upgradeOfVersionBranch = context().service(ResourceURIPathResolver.class).resolve(context(), List.of(result.getUpgradeOf())).stream().findFirst().orElse("");
                if (!Strings.isNullOrEmpty(upgradeOfVersionBranch)) {
                    startTimestamp = branching.getBranch(upgradeOfVersionBranch).getBaseTimestamp() + 1;
                } else {
                    startTimestamp = Long.MIN_VALUE;
                }
                ResourceRequests.prepareSearchVersion().all().filterByResource(result.getUpgradeOf().withoutPath()).filterByResourceBranchPath(upgradeOfCodeSystemBranchPath).build().execute(context()).stream().filter(csv -> !csv.getVersionResourceURI().isHead()).forEach(csv -> {
                    RevisionBranch versionBranch = branching.getBranch(csv.getBranchPath());
                    if (versionBranch.getBaseTimestamp() > startTimestamp) {
                        BranchState versionBranchState = branching.getBranchState(result.getBranchPath(), versionBranch.getPath());
                        if (versionBranchState == BranchState.BEHIND || versionBranchState == BranchState.DIVERGED) {
                            availableVersions.add(csv.getVersionResourceURI());
                        }
                        versionBranchInfo.add(new BranchInfo(branch.getPath(), versionBranchState, versionBranch.getBaseTimestamp(), versionBranch.getHeadTimestamp()));
                    }
                });
            }
            result.setUpgradeInfo(new UpgradeInfo(mainInfo, versionBranchInfo, availableVersions));
        }
    }
}
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) RevisionBranch(com.b2international.index.revision.RevisionBranch) BranchInfo(com.b2international.snowowl.core.branch.BranchInfo) BranchState(com.b2international.index.revision.RevisionBranch.BranchState) BaseRevisionBranching(com.b2international.index.revision.BaseRevisionBranching) ResourceURIPathResolver(com.b2international.snowowl.core.uri.ResourceURIPathResolver)

Example 23 with ResourceURI

use of com.b2international.snowowl.core.ResourceURI 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 24 with ResourceURI

use of com.b2international.snowowl.core.ResourceURI 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 25 with ResourceURI

use of com.b2international.snowowl.core.ResourceURI 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)

Aggregations

ResourceURI (com.b2international.snowowl.core.ResourceURI)49 Test (org.junit.Test)29 CodeSystem (com.b2international.snowowl.core.codesystem.CodeSystem)21 LocalDate (java.time.LocalDate)19 Collectors (java.util.stream.Collectors)11 TerminologyResource (com.b2international.snowowl.core.TerminologyResource)7 List (java.util.List)7 Set (java.util.Set)7 BadRequestException (com.b2international.commons.exceptions.BadRequestException)6 Options (com.b2international.commons.options.Options)6 CodeSystemRequests (com.b2international.snowowl.core.codesystem.CodeSystemRequests)6 IEventBus (com.b2international.snowowl.eventbus.IEventBus)6 IOException (java.io.IOException)6 Map (java.util.Map)6 TimeUnit (java.util.concurrent.TimeUnit)6 RepositoryManager (com.b2international.snowowl.core.RepositoryManager)5 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)5 CodeSystems (com.b2international.snowowl.core.codesystem.CodeSystems)5 Version (com.b2international.snowowl.core.version.Version)5 Strings (com.google.common.base.Strings)5