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