Search in sources :

Example 11 with Branch

use of com.b2international.snowowl.core.branch.Branch in project snow-owl by b2ihealthcare.

the class BaseTerminologyResourceUpdateRequest method updateBranchPath.

private boolean updateBranchPath(final TransactionContext context, final ResourceDocument.Builder resource, final String currentBranchPath, final String toolingId) {
    // if extensionOf is set, branch path changes are already handled in updateExtensionOf
    if (extensionOf == null && branchPath != null && !currentBranchPath.equals(branchPath)) {
        try {
            final Branch branch = RepositoryRequests.branching().prepareGet(branchPath).build(toolingId).getRequest().execute(context);
            if (branch.isDeleted()) {
                throw new BadRequestException("Branch with identifier '%s' is deleted.", branchPath);
            }
        } catch (NotFoundException e) {
            throw e.toBadRequestException();
        }
        // TODO: check if update branch path coincides with a version working path
        // and update extensionOf accordingly?
        resource.extensionOf(null);
        resource.branchPath(branchPath);
        return true;
    }
    return false;
}
Also used : Branch(com.b2international.snowowl.core.branch.Branch) BadRequestException(com.b2international.commons.exceptions.BadRequestException) NotFoundException(com.b2international.commons.exceptions.NotFoundException)

Example 12 with Branch

use of com.b2international.snowowl.core.branch.Branch 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());
}
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) ResourceURI(com.b2international.snowowl.core.ResourceURI) Version(com.b2international.snowowl.core.version.Version) Branches(com.b2international.snowowl.core.branch.Branches) Branch(com.b2international.snowowl.core.branch.Branch) CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem)

Example 13 with Branch

use of com.b2international.snowowl.core.branch.Branch in project snow-owl by b2ihealthcare.

the class BranchesCommand method run.

@Override
public void run(CommandLineStream out) {
    if (isValidRepositoryName(repositoryId, out)) {
        if (!branchPath.startsWith(Branch.MAIN_PATH)) {
            out.println("Specify branch with full path. i.e. MAIN/PROJECT/TASK1. Got: '%s'", branchPath);
            return;
        }
        try {
            Branch parentBranch = RepositoryRequests.branching().prepareGet(branchPath).setExpand("children(direct:false)").build(repositoryId).execute(getBus()).getSync(1, TimeUnit.MINUTES);
            out.println("Branch hierarchy for '%s' in repository '%s':", branchPath, repositoryId);
            print(parentBranch, getDepthOfBranch(parentBranch), parentBranch.getChildren().getItems(), out);
        } catch (NotFoundException e) {
            out.println("Unable to find branch '%s'", branchPath);
            return;
        }
    }
}
Also used : Branch(com.b2international.snowowl.core.branch.Branch) NotFoundException(com.b2international.commons.exceptions.NotFoundException)

Example 14 with Branch

use of com.b2international.snowowl.core.branch.Branch in project snow-owl by b2ihealthcare.

the class SaveJobRequest method persistChanges.

private Boolean persistChanges(final BranchContext context, final IProgressMonitor monitor) {
    // Repeat the same checks as in ClassificationSaveRequest, now within the lock
    final ClassificationTask classification = ClassificationRequests.prepareGetClassification(classificationId).build().execute(context);
    final String branchPath = classification.getBranch();
    final Branch branch = RepositoryRequests.branching().prepareGet(branchPath).build().execute(context);
    if (!ClassificationSaveRequest.SAVEABLE_STATUSES.contains(classification.getStatus())) {
        throw new BadRequestException("Classification '%s' is not in the expected state to start saving changes.", classificationId);
    }
    if (classification.getTimestamp() < branch.headTimestamp()) {
        throw new BadRequestException("Classification '%s' on branch '%s' is stale (classification timestamp: %s, head timestamp: %s).", classificationId, branchPath, classification.getTimestamp(), branch.headTimestamp());
    }
    final ClassificationTracker classificationTracker = context.service(ClassificationTracker.class);
    // Signal the state change
    classificationTracker.classificationSaving(classificationId);
    final SubMonitor subMonitor = SubMonitor.convert(monitor, "Persisting changes", 6);
    final BulkRequestBuilder<TransactionContext> bulkRequestBuilder = BulkRequest.create();
    applyChanges(subMonitor, context, bulkRequestBuilder);
    long resultTimeStamp = Commit.NO_COMMIT_TIMESTAMP;
    for (List<Request<TransactionContext, ?>> partition : Iterables.partition(bulkRequestBuilder.build().getRequests(), getCommitLimit(context))) {
        final BulkRequestBuilder<TransactionContext> batchRequest = BulkRequest.create();
        partition.forEach(request -> batchRequest.add(request));
        final Request<BranchContext, CommitResult> commitRequest = SnomedRequests.prepareCommit().setBody(batchRequest.build()).setCommitComment(commitComment).setParentContextDescription(DatastoreLockContextDescriptions.SAVE_CLASSIFICATION_RESULTS).setAuthor(userId).build();
        final CommitResult commitResult = new IdRequest<>(commitRequest).execute(context);
        resultTimeStamp = commitResult.getCommitTimestamp();
    }
    if (Commit.NO_COMMIT_TIMESTAMP == resultTimeStamp) {
        classificationTracker.classificationSaveFailed(classificationId);
        return Boolean.FALSE;
    } else {
        classificationTracker.classificationSaved(classificationId, resultTimeStamp);
        return Boolean.TRUE;
    }
}
Also used : CommitResult(com.b2international.snowowl.core.request.CommitResult) SubMonitor(org.eclipse.core.runtime.SubMonitor) Request(com.b2international.snowowl.core.events.Request) BulkRequest(com.b2international.snowowl.core.events.bulk.BulkRequest) Branch(com.b2international.snowowl.core.branch.Branch) TransactionContext(com.b2international.snowowl.core.domain.TransactionContext) BranchContext(com.b2international.snowowl.core.domain.BranchContext) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ClassificationTracker(com.b2international.snowowl.snomed.reasoner.classification.ClassificationTracker)

Example 15 with Branch

use of com.b2international.snowowl.core.branch.Branch 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("");
}
Also used : VersionSearchRequestBuilder(com.b2international.snowowl.core.request.version.VersionSearchRequestBuilder) BadRequestException(com.b2international.commons.exceptions.BadRequestException) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) ResourceRequests(com.b2international.snowowl.core.request.ResourceRequests) VersionSearchRequestBuilder(com.b2international.snowowl.core.request.version.VersionSearchRequestBuilder) Set(java.util.Set) Collectors(java.util.stream.Collectors) Branch(com.b2international.snowowl.core.branch.Branch) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest) Resource(com.b2international.snowowl.core.Resource) List(java.util.List) VersionDocument(com.b2international.snowowl.core.version.VersionDocument) Map(java.util.Map) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) CompareUtils(com.b2international.commons.CompareUtils) Collections(java.util.Collections) Versions(com.b2international.snowowl.core.version.Versions) ResourceURI(com.b2international.snowowl.core.ResourceURI) ResourceURI(com.b2international.snowowl.core.ResourceURI) Versions(com.b2international.snowowl.core.version.Versions) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Aggregations

Branch (com.b2international.snowowl.core.branch.Branch)15 BadRequestException (com.b2international.commons.exceptions.BadRequestException)8 Request (com.b2international.snowowl.core.events.Request)7 RepositoryRequests (com.b2international.snowowl.core.repository.RepositoryRequests)6 RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)5 RevisionBranch (com.b2international.index.revision.RevisionBranch)4 ResourceURI (com.b2international.snowowl.core.ResourceURI)4 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)4 Merge (com.b2international.snowowl.core.merge.Merge)4 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)4 NotNull (javax.validation.constraints.NotNull)4 CompareUtils (com.b2international.commons.CompareUtils)3 NotFoundException (com.b2international.commons.exceptions.NotFoundException)3 BaseRevisionBranching (com.b2international.index.revision.BaseRevisionBranching)3 BranchPathUtils (com.b2international.snowowl.core.branch.BranchPathUtils)3 Branching (com.b2international.snowowl.core.branch.Branching)3 Merging (com.b2international.snowowl.core.branch.Merging)3 User (com.b2international.snowowl.core.identity.User)3 RemoteJobEntry (com.b2international.snowowl.core.jobs.RemoteJobEntry)3 CommitResult (com.b2international.snowowl.core.request.CommitResult)3