Search in sources :

Example 1 with Resource

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

the class ResourceConverter method expandCommits.

private void expandCommits(List<Resource> results) {
    if (expand().containsKey(TerminologyResource.Expand.COMMITS)) {
        Options expandOptions = expand().getOptions(TerminologyResource.Expand.COMMITS);
        // commit searches must be performed individually on each resource to provide correct results
        var commitSearchRequests = results.stream().filter(TerminologyResource.class::isInstance).map(TerminologyResource.class::cast).map(res -> {
            return RepositoryRequests.commitInfos().prepareSearchCommitInfo().filterByBranch(res.getBranchPath()).setLimit(getLimit(expandOptions)).setFields(expandOptions.containsKey("fields") ? expandOptions.getList("fields", String.class) : CommitInfo.Fields.DEAFULT_FIELD_SELECTION).sortBy(expandOptions.containsKey("sort") ? expandOptions.getString("sort") : null).setLocales(locales()).build(res.getToolingId()).execute(context().service(IEventBus.class)).then(commits -> {
                res.setCommits(commits);
                return commits;
            });
        }).collect(Collectors.toList());
        // wait until all search requests resolve, or timeout of 3 minutes reached
        Promise.all(commitSearchRequests).getSync(3, TimeUnit.MINUTES);
    }
}
Also used : TerminologyResource(com.b2international.snowowl.core.TerminologyResource) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) ResourceTypeConverter(com.b2international.snowowl.core.ResourceTypeConverter) Promise(com.b2international.snowowl.core.events.util.Promise) CommitInfo(com.b2international.snowowl.core.commit.CommitInfo) Set(java.util.Set) IEventBus(com.b2international.snowowl.eventbus.IEventBus) Collectors(java.util.stream.Collectors) Branch(com.b2international.snowowl.core.branch.Branch) RepositoryRequests(com.b2international.snowowl.core.repository.RepositoryRequests) TimeUnit(java.util.concurrent.TimeUnit) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) Resource(com.b2international.snowowl.core.Resource) List(java.util.List) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) Options(com.b2international.commons.options.Options) Map(java.util.Map) Resources(com.b2international.snowowl.core.Resources) IComponent(com.b2international.snowowl.core.domain.IComponent) Versions(com.b2international.snowowl.core.version.Versions) Options(com.b2international.commons.options.Options) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) IEventBus(com.b2international.snowowl.eventbus.IEventBus)

Example 2 with Resource

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

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

Example 4 with Resource

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

the class BaseResourceUpdateRequest method updateBundle.

private boolean updateBundle(TransactionContext context, String resourceId, String oldBundleId, Builder updated) {
    if (bundleId == null || bundleId.equals(oldBundleId)) {
        return false;
    }
    final List<String> bundleAncestorIds = getBundleAncestorIds(context, resourceId);
    updated.bundleId(bundleId);
    updated.bundleAncestorIds(bundleAncestorIds);
    // Update bundle ancestor IDs on all descendants of the resource (their bundle ID does not change)
    final Iterator<Resource> descendants = ResourceRequests.prepareSearch().filterByBundleAncestorId(resourceId).setLimit(5_000).stream(context).flatMap(Resources::stream).iterator();
    final Multimap<String, Resource> resourcesByParentId = Multimaps.index(descendants, Resource::getBundleId);
    // Calculate new ancestor ID list for direct children of this resource first
    final ImmutableList.Builder<String> ancestorIdsOfParent = ImmutableList.<String>builder().addAll(bundleAncestorIds);
    if (!IComponent.ROOT_ID.equals(bundleId)) {
        ancestorIdsOfParent.add(bundleId);
    }
    final Map<String, List<String>> newAncestorIdsByParentId = newHashMap(Map.of(resourceId, ancestorIdsOfParent.build()));
    // Start processing ancestor ID lists with the direct children of the resource
    final Deque<Map.Entry<String, Collection<Resource>>> toProcess = new ArrayDeque<>();
    toProcess.addLast(new AbstractMap.SimpleImmutableEntry<>(resourceId, resourcesByParentId.get(resourceId)));
    while (!toProcess.isEmpty()) {
        final Entry<String, Collection<Resource>> entry = toProcess.removeFirst();
        final String parentId = entry.getKey();
        final Collection<Resource> resources = entry.getValue();
        /*
			 * XXX: We will use the same ancestor ID list for all sibling resources. The
			 * call removes the entry from the map as it will never be read again after this
			 * iteration.
			 */
        final List<String> newAncestorIds = newAncestorIdsByParentId.remove(parentId);
        for (final Resource current : resources) {
            final String id = current.getId();
            final ResourceDocument resource = context.lookup(id, ResourceDocument.class);
            final ResourceDocument.Builder resourceBuilder = ResourceDocument.builder(resource);
            if (!Objects.equals(resource.getBundleAncestorIds(), newAncestorIds)) {
                resourceBuilder.bundleAncestorIds(newAncestorIds);
                context.update(resource, resourceBuilder.build());
            }
            final Collection<Resource> next = resourcesByParentId.get(id);
            if (!next.isEmpty()) {
                /*
					 * If the current resource has any children, make a note that we have to update
					 * bundleAncestorIds for them as well. The bundleId for these resources remains
					 * "id".
					 */
                final List<String> nextAncestorIds = ImmutableList.<String>builder().addAll(newAncestorIds).add(parentId).build();
                newAncestorIdsByParentId.put(id, nextAncestorIds);
                toProcess.add(new AbstractMap.SimpleImmutableEntry<>(id, next));
            }
        }
    }
    return true;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Resource(com.b2international.snowowl.core.Resource) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) Builder(com.b2international.snowowl.core.internal.ResourceDocument.Builder) Entry(java.util.Map.Entry) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

Resource (com.b2international.snowowl.core.Resource)4 TerminologyResource (com.b2international.snowowl.core.TerminologyResource)3 ResourceURI (com.b2international.snowowl.core.ResourceURI)2 Branch (com.b2international.snowowl.core.branch.Branch)2 ResourceDocument (com.b2international.snowowl.core.internal.ResourceDocument)2 Versions (com.b2international.snowowl.core.version.Versions)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 CompareUtils (com.b2international.commons.CompareUtils)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 NotFoundException (com.b2international.commons.exceptions.NotFoundException)1 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)1 Options (com.b2international.commons.options.Options)1 ResourceTypeConverter (com.b2international.snowowl.core.ResourceTypeConverter)1 Resources (com.b2international.snowowl.core.Resources)1 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)1 CommitInfo (com.b2international.snowowl.core.commit.CommitInfo)1 IComponent (com.b2international.snowowl.core.domain.IComponent)1