Search in sources :

Example 1 with RepositoryContext

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());
}
Also used : TerminologyResource(com.b2international.snowowl.core.TerminologyResource) Expressions(com.b2international.index.revision.Commit.Expressions) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) ResourceRequests(com.b2international.snowowl.core.request.ResourceRequests) Hits(com.b2international.index.Hits) DEFAULT_MAXIMUM_BRANCH_NAME_LENGTH(com.b2international.index.revision.RevisionBranch.DEFAULT_MAXIMUM_BRANCH_NAME_LENGTH) Collection(java.util.Collection) Set(java.util.Set) Commit(com.b2international.index.revision.Commit) SetView(com.google.common.collect.Sets.SetView) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) Expressions.regexp(com.b2international.index.query.Expressions.regexp) List(java.util.List) SearchIndexResourceRequest(com.b2international.snowowl.core.request.SearchIndexResourceRequest) Expressions(com.b2international.index.query.Expressions) Permission.isWildCardResource(com.b2international.snowowl.core.identity.Permission.isWildCardResource) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Expression(com.b2international.index.query.Expression) Permission(com.b2international.snowowl.core.identity.Permission) User(com.b2international.snowowl.core.identity.User) BRANCH(com.b2international.index.revision.Commit.Fields.BRANCH) User(com.b2international.snowowl.core.identity.User) Permission(com.b2international.snowowl.core.identity.Permission) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 2 with RepositoryContext

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);
}
Also used : ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) Hits(com.b2international.index.Hits) ResourceTypeConverter(com.b2international.snowowl.core.ResourceTypeConverter) Order(com.b2international.index.query.SortBy.Order) Map(java.util.Map) Resources(com.b2international.snowowl.core.Resources) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Builder(com.b2international.index.query.SortBy.Builder) Registry(com.b2international.snowowl.core.ResourceTypeConverter.Registry) Collectors(java.util.stream.Collectors) ResourceTypeConverter(com.b2international.snowowl.core.ResourceTypeConverter) Registry(com.b2international.snowowl.core.ResourceTypeConverter.Registry)

Example 3 with RepositoryContext

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());
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) SnomedConceptSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedConceptSearchRequestBuilder) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) SnomedReferenceSets(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSets) RepositoryRequests(com.b2international.snowowl.core.repository.RepositoryRequests) Sets.newTreeSet(com.google.common.collect.Sets.newTreeSet) Collections.singleton(java.util.Collections.singleton) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) LocalTime(java.time.LocalTime) RevisionIndex(com.b2international.index.revision.RevisionIndex) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) Branches(com.b2international.snowowl.core.branch.Branches) Permission(com.b2international.snowowl.core.identity.Permission) CompareUtils(com.b2international.commons.CompareUtils) Path(java.nio.file.Path) com.google.common.collect(com.google.common.collect) Collectors.toSet(java.util.stream.Collectors.toSet) ResourceURI(com.b2international.snowowl.core.ResourceURI) com.b2international.snowowl.core.request(com.b2international.snowowl.core.request) Version(com.b2international.snowowl.core.version.Version) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Request(com.b2international.snowowl.core.events.Request) IEventBus(com.b2international.snowowl.eventbus.IEventBus) Instant(java.time.Instant) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Stream(java.util.stream.Stream) VersionDocument(com.b2international.snowowl.core.version.VersionDocument) LocalDate(java.time.LocalDate) Sort(com.b2international.snowowl.core.request.SearchResourceRequest.Sort) Entry(java.util.Map.Entry) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) SnomedDescriptionIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry) BranchContext(com.b2international.snowowl.core.domain.BranchContext) Builder(com.google.common.collect.ImmutableList.Builder) FileUtils(com.b2international.commons.FileUtils) CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem) EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) java.util(java.util) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) LocalDateTime(java.time.LocalDateTime) com.b2international.snowowl.snomed.datastore.request.rf2.exporter(com.b2international.snowowl.snomed.datastore.request.rf2.exporter) com.b2international.snowowl.snomed.core.domain(com.b2international.snowowl.snomed.core.domain) Branch(com.b2international.snowowl.core.branch.Branch) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) Strings(com.google.common.base.Strings) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Attachment(com.b2international.snowowl.core.attachments.Attachment) SnomedRefSetMemberSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedRefSetMemberSearchRequestBuilder) BadRequestException(com.b2international.commons.exceptions.BadRequestException) BranchPathUtils(com.b2international.snowowl.core.branch.BranchPathUtils) DateFormats(com.b2international.snowowl.core.date.DateFormats) Files(java.nio.file.Files) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SnomedTerminologyComponentConstants(com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) ChronoUnit(java.time.temporal.ChronoUnit) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) NotEmpty(org.hibernate.validator.constraints.NotEmpty) DateTimeFormatter(java.time.format.DateTimeFormatter) IComponent(com.b2international.snowowl.core.domain.IComponent) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) Versions(com.b2international.snowowl.core.version.Versions) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) BranchContext(com.b2international.snowowl.core.domain.BranchContext) SnomedReferenceSets(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSets) SnomedConceptSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedConceptSearchRequestBuilder)

Example 4 with RepositoryContext

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;
    }
}
Also used : MergeConflict(com.b2international.snowowl.core.merge.MergeConflict) BadRequestException(com.b2international.commons.exceptions.BadRequestException) BranchRequest(com.b2international.snowowl.core.request.BranchRequest) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ConflictException(com.b2international.commons.exceptions.ConflictException) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) MergeConflict(com.b2international.snowowl.core.merge.MergeConflict) Collection(java.util.Collection) Request(com.b2international.snowowl.core.events.Request) ResourceURIPathResolver(com.b2international.snowowl.core.uri.ResourceURIPathResolver) Merge(com.b2international.snowowl.core.merge.Merge) NotNull(javax.validation.constraints.NotNull) Branch(com.b2international.snowowl.core.branch.Branch) RepositoryRequests(com.b2international.snowowl.core.repository.RepositoryRequests) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) List(java.util.List) Map(java.util.Map) ApiError(com.b2international.commons.exceptions.ApiError) ResourceRepositoryCommitRequestBuilder(com.b2international.snowowl.core.context.ResourceRepositoryCommitRequestBuilder) User(com.b2international.snowowl.core.identity.User) ResourceURI(com.b2international.snowowl.core.ResourceURI) User(com.b2international.snowowl.core.identity.User) ConflictException(com.b2international.commons.exceptions.ConflictException) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) Merge(com.b2international.snowowl.core.merge.Merge) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ApiError(com.b2international.commons.exceptions.ApiError) ResourceRepositoryCommitRequestBuilder(com.b2international.snowowl.core.context.ResourceRepositoryCommitRequestBuilder) BranchRequest(com.b2international.snowowl.core.request.BranchRequest)

Example 5 with RepositoryContext

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());
}
Also used : RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) ImmutableSet(com.google.common.collect.ImmutableSet) Status(com.b2international.snowowl.core.merge.Merge.Status) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Merge(com.b2international.snowowl.core.merge.Merge) RemoteJobEntry(com.b2international.snowowl.core.jobs.RemoteJobEntry) Collectors(java.util.stream.Collectors) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest) List(java.util.List) Merges(com.b2international.snowowl.core.merge.Merges) Expressions(com.b2international.index.query.Expressions) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) Map(java.util.Map) ApiError(com.b2international.commons.exceptions.ApiError) RemoteJobs(com.b2international.snowowl.core.jobs.RemoteJobs) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Permission(com.b2international.snowowl.core.identity.Permission) Builder(com.b2international.snowowl.core.merge.Merge.Builder) NotImplementedException(com.b2international.commons.exceptions.NotImplementedException) RemoteJobTracker(com.b2international.snowowl.core.jobs.RemoteJobTracker) RemoteJobs(com.b2international.snowowl.core.jobs.RemoteJobs) Merges(com.b2international.snowowl.core.merge.Merges) RemoteJobTracker(com.b2international.snowowl.core.jobs.RemoteJobTracker) Merge(com.b2international.snowowl.core.merge.Merge) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)18 Request (com.b2international.snowowl.core.events.Request)8 Collectors (java.util.stream.Collectors)8 CompareUtils (com.b2international.commons.CompareUtils)5 BadRequestException (com.b2international.commons.exceptions.BadRequestException)5 Branch (com.b2international.snowowl.core.branch.Branch)5 EffectiveTimes (com.b2international.snowowl.core.date.EffectiveTimes)5 Permission (com.b2international.snowowl.core.identity.Permission)5 IOException (java.io.IOException)5 ResourceURI (com.b2international.snowowl.core.ResourceURI)4 TerminologyResource (com.b2international.snowowl.core.TerminologyResource)4 AccessControl (com.b2international.snowowl.core.authorization.AccessControl)4 RepositoryRequests (com.b2international.snowowl.core.repository.RepositoryRequests)4 SnomedRf2Headers (com.b2international.snowowl.snomed.common.SnomedRf2Headers)4 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)4 LocalDate (java.time.LocalDate)4 java.util (java.util)4 List (java.util.List)4 Stream (java.util.stream.Stream)4 NotNull (javax.validation.constraints.NotNull)4