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