use of com.b2international.snowowl.core.domain.RepositoryContext in project snow-owl by b2ihealthcare.
the class CommitInfoSearchRequest method addSecurityFilter.
private void addSecurityFilter(final ExpressionBuilder builder, RepositoryContext context) {
final User user = context.service(User.class);
if (user.isAdministrator() || user.hasPermission(Permission.requireAll(Permission.OPERATION_BROWSE, Permission.ALL))) {
return;
}
final List<Permission> readPermissions = user.getPermissions().stream().filter(p -> Permission.ALL.equals(p.getOperation()) || Permission.OPERATION_BROWSE.equals(p.getOperation())).collect(Collectors.toList());
final Set<String> exactResourceIds = readPermissions.stream().flatMap(p -> p.getResources().stream()).filter(resource -> !resource.endsWith("*")).collect(Collectors.toSet());
final Set<String> resourceIdPrefixes = readPermissions.stream().flatMap(p -> p.getResources().stream()).filter(resource -> isWildCardResource(resource)).map(resource -> resource.substring(0, resource.length() - 1)).collect(Collectors.toSet());
SetView<String> resourceIds = Sets.union(exactResourceIds, resourceIdPrefixes);
ExpressionBuilder branchFilter = Expressions.builder();
ResourceRequests.prepareSearch().filterByIds(resourceIds).setLimit(resourceIds.size()).setFields(ResourceDocument.Fields.ID, ResourceDocument.Fields.BRANCH_PATH, ResourceDocument.Fields.RESOURCE_TYPE).buildAsync().getRequest().execute(context).stream().filter(TerminologyResource.class::isInstance).map(TerminologyResource.class::cast).forEach(r -> {
if (resourceIdPrefixes.contains(r.getId())) {
final String branchPattern = String.format("%s(/[a-zA-Z0-9.~_\\-]{1,%d})?", r.getBranchPath(), DEFAULT_MAXIMUM_BRANCH_NAME_LENGTH);
branchFilter.should(regexp(BRANCH, branchPattern));
}
});
builder.filter(branchFilter.build());
}
use of com.b2international.snowowl.core.domain.RepositoryContext in project snow-owl by b2ihealthcare.
the class ResourceSearchRequest method toQuerySortBy.
@Override
protected void toQuerySortBy(RepositoryContext context, Builder sortBuilder, Sort sort) {
if (sort instanceof SortField) {
SortField sortField = (SortField) sort;
if (ResourceSearchRequestBuilder.TYPE_RANK.equals(sortField.getField())) {
Registry registry = context.service(ResourceTypeConverter.Registry.class);
Map<String, Integer> orderMap = registry.getResourceTypeConverters().values().stream().collect(Collectors.toMap(typeDef -> typeDef.getResourceType(), typeDef -> typeDef.getRank()));
sortBuilder.sortByScriptNumeric(ResourceSearchRequestBuilder.TYPE_RANK, Map.of("ranks", orderMap), sort.isAscending() ? Order.ASC : Order.DESC);
return;
}
}
super.toQuerySortBy(context, sortBuilder, sort);
}
use of com.b2international.snowowl.core.domain.RepositoryContext in project snow-owl by b2ihealthcare.
the class SnomedRf2ExportRequest method getIdentifierConcepts.
private List<SnomedConcept> getIdentifierConcepts(final RepositoryContext context, final String currentVersion) {
final Collection<String> refSetsToLoad;
if (refSets == null) {
// Retrieve all reference sets if refSets is null
final Request<BranchContext, SnomedReferenceSets> refSetRequest = SnomedRequests.prepareSearchRefSet().all().build();
final SnomedReferenceSets allReferenceSets = execute(context, currentVersion, refSetRequest);
refSetsToLoad = allReferenceSets.stream().map(r -> r.getId()).collect(Collectors.toSet());
} else {
refSetsToLoad = refSets;
}
final SnomedConceptSearchRequestBuilder refSetRequestBuilder = SnomedRequests.prepareSearchConcept().all().filterByIds(refSetsToLoad).setExpand("pt(),referenceSet()").setLocales(locales());
final Request<BranchContext, SnomedConcepts> request = refSetRequestBuilder.build();
final SnomedConcepts referenceSets = execute(context, currentVersion, request);
// Return only the identifier concepts which have an existing reference set on this branch
return referenceSets.stream().filter(c -> c.getReferenceSet() != null).collect(Collectors.toList());
}
use of com.b2international.snowowl.core.domain.RepositoryContext in project snow-owl by b2ihealthcare.
the class CodeSystemUpgradeSynchronizationRequest method execute.
@Override
public Boolean execute(RepositoryContext context) {
final String message = String.format("Merge %s into %s", source, codeSystemId);
CodeSystem codeSystem = CodeSystemRequests.prepareGetCodeSystem(codeSystemId.getResourceId()).build().execute(context);
if (codeSystem.getUpgradeOf() == null) {
throw new BadRequestException("Code System '%s' is not an Upgrade Code System. It cannot be synchronized with '%s'.", codeSystemId, source);
}
final String sourceBranchPath = context.service(ResourceURIPathResolver.class).resolve(context, List.of(source)).stream().findFirst().get();
// merge all changes from the source to the current upgrade of branch
final Merge merge = RepositoryRequests.merging().prepareCreate().setSource(sourceBranchPath).setTarget(codeSystem.getBranchPath()).setUserId(context.service(User.class).getUsername()).setCommitComment(message).setSquash(false).build(codeSystem.getToolingId()).getRequest().execute(context);
if (merge.getStatus() != Merge.Status.COMPLETED) {
// report conflicts
ApiError apiError = merge.getApiError();
Collection<MergeConflict> conflicts = merge.getConflicts();
context.log().error("Failed to sync source CodeSystem content to upgrade CodeSystem. Error: {}. Conflicts: {}", apiError.getMessage(), conflicts);
throw new ConflictException("Upgrade code system synchronization can not be performed due to conflicting content errors.").withAdditionalInfo(Map.of("conflicts", conflicts, "mergeError", apiError.getMessage()));
}
if (!codeSystem.getUpgradeOf().equals(source)) {
return new BranchRequest<>(Branch.MAIN_PATH, new ResourceRepositoryCommitRequestBuilder().setBody((tx) -> {
ResourceDocument entry = tx.lookup(codeSystemId.getResourceId(), ResourceDocument.class);
tx.add(ResourceDocument.builder(entry).upgradeOf(source).build());
tx.commit(String.format("Update upgradeOf from '%s' to '%s'", codeSystem.getUpgradeOf(), source));
return Boolean.TRUE;
}).setCommitComment(String.format("Complete upgrade of %s to %s", codeSystem.getUpgradeOf().getResourceId(), codeSystem.getExtensionOf())).build()).execute(context).getResultAs(Boolean.class);
} else {
return Boolean.TRUE;
}
}
use of com.b2international.snowowl.core.domain.RepositoryContext in project snow-owl by b2ihealthcare.
the class SearchMergeRequest method doExecute.
@Override
public Merges doExecute(RepositoryContext context) {
final ExpressionBuilder queryBuilder = Expressions.builder();
// add mergerequest type filter
queryBuilder.filter(RemoteJobEntry.Expressions.matchRequestType(ImmutableSet.of(BranchRebaseRequest.class.getSimpleName(), BranchMergeRequest.class.getSimpleName())));
if (containsKey(OptionKey.SOURCE)) {
queryBuilder.filter(RemoteJobEntry.Expressions.matchParameter(SOURCE_FIELD, getCollection(OptionKey.SOURCE, String.class)));
}
if (containsKey(OptionKey.TARGET)) {
queryBuilder.filter(RemoteJobEntry.Expressions.matchParameter(TARGET_FIELD, getCollection(OptionKey.TARGET, String.class)));
}
if (containsKey(OptionKey.STATUS)) {
queryBuilder.filter(RemoteJobEntry.Expressions.matchParameter(STATUS_FIELD, getCollection(OptionKey.STATUS, String.class)));
}
final RemoteJobs jobs = context.service(RemoteJobTracker.class).search(queryBuilder.build(), Integer.MAX_VALUE);
final ObjectMapper mapper = context.service(ObjectMapper.class);
final List<Merge> items = jobs.stream().map(job -> createMergefromJobEntry(job, mapper)).collect(Collectors.toList());
return new Merges(items, jobs.getSearchAfter(), jobs.getLimit(), jobs.getTotal());
}
Aggregations