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);
}
}
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("");
}
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);
}
}
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;
}
Aggregations