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