use of com.b2international.snowowl.core.events.Request in project snow-owl by b2ihealthcare.
the class VersionCreateRequest method execute.
@Override
public Boolean execute(RepositoryContext context) {
final String user = context.service(User.class).getUsername();
if (!resource.isHead()) {
throw new BadRequestException("Version '%s' cannot be created on unassigned branch '%s'.", version, resource).withDeveloperMessage("Did you mean to version '%s'?", resource.withoutPath());
}
if (resourcesById == null) {
resourcesById = fetchResources(context);
}
if (!resourcesById.containsKey(resource)) {
context.log().warn("Resource cannot be found during versioning: " + resourcesById + ", uri: " + resource);
throw new NotFoundException("Resource", resource.getResourceId());
}
// validate new path
RevisionBranch.BranchNameValidator.DEFAULT.checkName(version);
TerminologyResource resourceToVersion = resourcesById.get(resource);
// TODO resurrect or eliminate tooling dependencies
final List<TerminologyResource> resourcesToVersion = List.of(resourcesById.get(resource));
// final List<CodeSystem> resourcesToVersion = codeSystem.getDependenciesAndSelf()
// .stream()
// .map(resourcesById::get)
// .collect(Collectors.toList());
resourcesToVersion.stream().filter(cs -> cs.getUpgradeOf() != null).findAny().ifPresent(cs -> {
throw new BadRequestException("Upgrade resource '%s' can not be versioned.", cs.getResourceURI());
});
for (TerminologyResource terminologyResource : resourcesToVersion) {
// check that the new versionId does not conflict with any other currently available branch
final String newVersionPath = String.join(Branch.SEPARATOR, terminologyResource.getBranchPath(), version);
final String repositoryId = terminologyResource.getToolingId();
if (!force) {
// branch needs checking in the non-force cases only
try {
Branch branch = RepositoryRequests.branching().prepareGet(newVersionPath).build(repositoryId).execute(context);
if (!branch.isDeleted()) {
throw new ConflictException("An existing version or branch with path '%s' conflicts with the specified version identifier.", newVersionPath);
}
} catch (NotFoundException e) {
// branch does not exist, ignore
}
} else {
// if there is no conflict, delete the branch (the request also ignores non-existent branches)
deleteBranch(context, newVersionPath, repositoryId);
}
}
acquireLocks(context, user, resourcesToVersion);
final IProgressMonitor monitor = SubMonitor.convert(context.service(IProgressMonitor.class), TASK_WORK_STEP);
try {
// resourcesToVersion.forEach(resourceToVersion -> {
// check that the specified effective time is valid in this code system
validateVersion(context, resourceToVersion);
// version components in the given repository
new RepositoryRequest<>(resourceToVersion.getToolingId(), new BranchRequest<>(resourceToVersion.getBranchPath(), new RevisionIndexReadRequest<CommitResult>(context.service(RepositoryManager.class).get(resourceToVersion.getToolingId()).service(VersioningRequestBuilder.class).build(new VersioningConfiguration(user, resourceToVersion.getResourceURI(), version, description, effectiveTime, force))))).execute(context);
// tag the repository
doTag(context, resourceToVersion, monitor);
// create a version for the resource
return new BranchRequest<>(Branch.MAIN_PATH, new ResourceRepositoryCommitRequestBuilder().setBody(tx -> {
tx.add(VersionDocument.builder().id(resource.withPath(version).withoutResourceType()).version(version).description(description).effectiveTime(EffectiveTimes.getEffectiveTime(effectiveTime)).resource(resource).branchPath(resourceToVersion.getRelativeBranchPath(version)).author(user).createdAt(Instant.now().toEpochMilli()).updatedAt(Instant.now().toEpochMilli()).toolingId(resourceToVersion.getToolingId()).url(buildVersionUrl(context, resourceToVersion)).build());
return Boolean.TRUE;
}).setCommitComment(CompareUtils.isEmpty(commitComment) ? String.format("Version '%s' as of '%s'", resource, version) : commitComment).build()).execute(context).getResultAs(Boolean.class);
} finally {
releaseLocks(context);
if (null != monitor) {
monitor.done();
}
}
}
use of com.b2international.snowowl.core.events.Request in project snow-owl by b2ihealthcare.
the class ConcreteDomainChangeConverter method expand.
@Override
public void expand(final List<ConcreteDomainChange> results) {
if (!expand().containsKey(ConcreteDomainChange.Expand.CONCRETE_DOMAIN_MEMBER)) {
return;
}
/*
* Depending on the CD member change search request, we might need to issue
* SNOMED CT searches against multiple branches; find out which ones we have.
*/
final Multimap<String, ConcreteDomainChange> itemsByBranch = getItemsByBranch(results);
// Check if we only need to load inferred CD members in their entirety
final Options expandOptions = expand().getOptions(ConcreteDomainChange.Expand.CONCRETE_DOMAIN_MEMBER);
final boolean inferredOnly = expandOptions.getBoolean("inferredOnly");
final Options cdMemberExpandOptions = expandOptions.getOptions("expand");
final Options referencedComponentOptions = cdMemberExpandOptions.getOptions(SnomedReferenceSetMember.Expand.REFERENCED_COMPONENT);
/*
* Remove this option from the member expand options map, so that member search
* does not expand the referenced component again
*/
final boolean needsReferencedComponent = cdMemberExpandOptions.keySet().remove(SnomedReferenceSetMember.Expand.REFERENCED_COMPONENT);
for (final String branch : itemsByBranch.keySet()) {
final Collection<ConcreteDomainChange> itemsForCurrentBranch = itemsByBranch.get(branch);
/*
* Expand referenced component on members via a separate search request, as they
* can be different from the referenced component on the "origin" member
*/
if (needsReferencedComponent) {
final List<ReasonerConcreteDomainMember> blankMembers = itemsForCurrentBranch.stream().filter(c -> !inferredOnly || ChangeNature.NEW.equals(c.getChangeNature())).map(ConcreteDomainChange::getConcreteDomainMember).collect(Collectors.toList());
final Multimap<String, ReasonerConcreteDomainMember> membersByReferencedComponent = Multimaps.index(blankMembers, ReasonerConcreteDomainMember::getReferencedComponentId);
final Multimap<ComponentCategory, String> referencedComponentsByCategory = Multimaps.index(membersByReferencedComponent.keySet(), SnomedIdentifiers::getComponentCategory);
for (final Entry<ComponentCategory, Collection<String>> categoryEntry : referencedComponentsByCategory.asMap().entrySet()) {
expandComponentCategory(branch, categoryEntry.getKey(), categoryEntry.getValue(), referencedComponentOptions, membersByReferencedComponent);
}
}
/*
* Then fetch all the required members (these will have a referenced component
* ID that should no longer be copied on inferred members). Note that the same "origin"
* member might be used for multiple eg. "new" counterparts.
*/
final Set<String> cdMemberUuids = itemsForCurrentBranch.stream().filter(c -> !inferredOnly || ChangeNature.NEW.equals(c.getChangeNature())).map(c -> c.getConcreteDomainMember().getOriginMemberId()).collect(Collectors.toSet());
final Request<BranchContext, SnomedReferenceSetMembers> cdMemberSearchRequest = SnomedRequests.prepareSearchMember().filterByIds(cdMemberUuids).setLimit(cdMemberUuids.size()).setExpand(cdMemberExpandOptions).setLocales(locales()).build();
final SnomedReferenceSetMembers cdMembers = new BranchRequest<>(branch, new RevisionIndexReadRequest<>(cdMemberSearchRequest)).execute(context());
final Map<String, SnomedReferenceSetMember> cdMembersByUuid = Maps.uniqueIndex(cdMembers, SnomedReferenceSetMember::getId);
/*
* Finally, set the member on the change item, but preserve the properties that
* were already set in "toResource"
*/
for (final ConcreteDomainChange item : itemsForCurrentBranch) {
final ReasonerConcreteDomainMember reasonerMember = item.getConcreteDomainMember();
final String memberUuid = reasonerMember.getOriginMemberId();
switch(item.getChangeNature()) {
case NEW:
{
final SnomedReferenceSetMember expandedMember = cdMembersByUuid.get(memberUuid);
final Map<String, Object> expandedProperties = expandedMember.getProperties();
// reasonerMember.setCharacteristicTypeId(...) is already set
// reasonerMember.setGroup(...) is already set
// reasonerMember.setReferencedComponent(...) is already set (or expanded)
reasonerMember.setReferenceSetId(expandedMember.getRefsetId());
// reasonerMember.setReleased(...) is already set
reasonerMember.setSerializedValue((String) expandedProperties.get(SnomedRf2Headers.FIELD_VALUE));
reasonerMember.setTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_TYPE_ID));
}
break;
case UPDATED:
if (!inferredOnly) {
final SnomedReferenceSetMember expandedMember = cdMembersByUuid.get(memberUuid);
final Map<String, Object> expandedProperties = expandedMember.getProperties();
reasonerMember.setCharacteristicTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID));
reasonerMember.setGroup((Integer) expandedProperties.get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP));
// reasonerMember.setReferencedComponent(...) is already set (or expanded)
reasonerMember.setReferenceSetId(expandedMember.getRefsetId());
// reasonerMember.setReleased(...) is already set
// reasonerMember.setSerializedValue(...) is already set
reasonerMember.setTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_TYPE_ID));
}
break;
case REDUNDANT:
if (!inferredOnly) {
final SnomedReferenceSetMember expandedMember = cdMembersByUuid.get(memberUuid);
final Map<String, Object> expandedProperties = expandedMember.getProperties();
reasonerMember.setCharacteristicTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID));
reasonerMember.setGroup((Integer) expandedProperties.get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP));
// reasonerMember.setReferencedComponent(...) is already set (or expanded)
reasonerMember.setReferenceSetId(expandedMember.getRefsetId());
// reasonerMember.setReleased(...) is already set
reasonerMember.setSerializedValue((String) expandedProperties.get(SnomedRf2Headers.FIELD_VALUE));
reasonerMember.setTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_TYPE_ID));
}
break;
default:
throw new IllegalStateException(String.format("Unexpected CD member change '%s' found with UUID '%s'.", item.getChangeNature(), item.getConcreteDomainMember().getOriginMemberId()));
}
}
}
}
use of com.b2international.snowowl.core.events.Request in project snow-owl by b2ihealthcare.
the class RelationshipChangeConverter method expand.
@Override
public void expand(final List<RelationshipChange> results) {
if (!expand().containsKey(RelationshipChange.Expand.RELATIONSHIP)) {
return;
}
/*
* Depending on the relationship change search request, we might need to issue
* SNOMED CT searches against multiple branches; find out which ones we have.
*/
final Multimap<String, RelationshipChange> itemsByBranch = getItemsByBranch(results);
// Check if we only need to load inferred relationships in their entirety
final Options expandOptions = expand().getOptions(RelationshipChange.Expand.RELATIONSHIP);
final boolean inferredOnly = expandOptions.getBoolean("inferredOnly");
final Options relationshipExpandOptions = expandOptions.getOptions("expand");
final Options sourceOptions = relationshipExpandOptions.getOptions(SnomedRelationship.Expand.SOURCE);
final Options typeOptions = relationshipExpandOptions.getOptions(SnomedRelationship.Expand.TYPE);
final Options destinationOptions = relationshipExpandOptions.getOptions(SnomedRelationship.Expand.DESTINATION);
final boolean needsSource = relationshipExpandOptions.keySet().contains(SnomedRelationship.Expand.SOURCE);
final boolean needsType = relationshipExpandOptions.keySet().contains(SnomedRelationship.Expand.TYPE);
final boolean needsDestination = relationshipExpandOptions.keySet().contains(SnomedRelationship.Expand.DESTINATION);
// Do not allow expansion of members
final boolean needsMembers = relationshipExpandOptions.keySet().contains(MEMBERS);
if (needsMembers) {
throw new BadRequestException("Members can not be expanded on reasoner relationship changes.");
}
for (final String branch : itemsByBranch.keySet()) {
final Collection<RelationshipChange> itemsForCurrentBranch = itemsByBranch.get(branch);
/*
* Expand concepts on the relationship currently set on each item first, as they
* might have changed when compared to the "origin" relationship.
*/
if (needsSource) {
expandConcepts(branch, itemsForCurrentBranch, sourceOptions, ReasonerRelationship::getSourceId, ReasonerRelationship::setSource);
}
if (needsType) {
expandConcepts(branch, itemsForCurrentBranch, typeOptions, ReasonerRelationship::getTypeId, ReasonerRelationship::setType);
}
if (needsDestination) {
expandConcepts(branch, itemsForCurrentBranch, destinationOptions, ReasonerRelationship::getDestinationId, ReasonerRelationship::setDestination);
}
// Now fetch the rest of the properties for the relationships (except IS As where no ID is recorded)
final Set<String> relationshipIds = itemsForCurrentBranch.stream().filter(c -> !inferredOnly || ChangeNature.NEW.equals(c.getChangeNature())).map(c -> c.getRelationship().getOriginId()).filter(id -> id != null).collect(Collectors.toSet());
final Request<BranchContext, SnomedRelationships> relationshipSearchRequest = SnomedRequests.prepareSearchRelationship().filterByIds(relationshipIds).setLimit(relationshipIds.size()).setExpand(relationshipExpandOptions).setLocales(locales()).build();
final SnomedRelationships relationships = new BranchRequest<>(branch, new RevisionIndexReadRequest<>(relationshipSearchRequest)).execute(context());
final Map<String, SnomedRelationship> relationshipsById = Maps.uniqueIndex(relationships, SnomedRelationship::getId);
for (final RelationshipChange item : itemsForCurrentBranch) {
final ReasonerRelationship reasonerRelationship = item.getRelationship();
final String originId = reasonerRelationship.getOriginId();
switch(item.getChangeNature()) {
case NEW:
if (originId == null) {
// reasonerRelationship.setCharacteristicType(...) is already set
// reasonerRelationship.setDestination(...) is already set
reasonerRelationship.setDestinationNegated(false);
// reasonerRelationship.setGroup(...) is already set
reasonerRelationship.setModifierId(Concepts.EXISTENTIAL_RESTRICTION_MODIFIER);
// reasonerRelationship.setReleased(...) is already set
// reasonerRelationship.setSource(...) is already set
// reasonerRelationship.setType(...) is already set
// reasonerRelationship.setUnionGroup(...) is already set
// reasonerRelationship.setValue(...) is already set
} else {
final SnomedRelationship expandedRelationship = relationshipsById.get(originId);
// reasonerRelationship.setCharacteristicType(...) is already set
reasonerRelationship.setDestination(expandedRelationship.getDestination());
reasonerRelationship.setDestinationNegated(expandedRelationship.isDestinationNegated());
// reasonerRelationship.setGroup(...) is already set
reasonerRelationship.setModifierId(expandedRelationship.getModifierId());
// reasonerRelationship.setReleased(...) is already set
// reasonerRelationship.setSource(...) is already set
reasonerRelationship.setType(expandedRelationship.getType());
// reasonerRelationship.setUnionGroup(...) is already set
reasonerRelationship.setValueAsObject(expandedRelationship.getValueAsObject());
}
break;
case UPDATED:
if (!inferredOnly) {
final SnomedRelationship expandedRelationship = relationshipsById.get(originId);
reasonerRelationship.setCharacteristicTypeId(expandedRelationship.getCharacteristicTypeId());
reasonerRelationship.setDestination(expandedRelationship.getDestination());
reasonerRelationship.setDestinationNegated(expandedRelationship.isDestinationNegated());
// reasonerRelationship.setGroup(...) is already set
reasonerRelationship.setModifierId(expandedRelationship.getModifierId());
// reasonerRelationship.setReleased(...) is already set
reasonerRelationship.setSource(expandedRelationship.getSource());
reasonerRelationship.setType(expandedRelationship.getType());
reasonerRelationship.setUnionGroup(expandedRelationship.getUnionGroup());
reasonerRelationship.setValueAsObject(expandedRelationship.getValueAsObject());
}
break;
case REDUNDANT:
if (!inferredOnly) {
final SnomedRelationship expandedRelationship = relationshipsById.get(originId);
reasonerRelationship.setCharacteristicTypeId(expandedRelationship.getCharacteristicTypeId());
reasonerRelationship.setDestination(expandedRelationship.getDestination());
reasonerRelationship.setDestinationNegated(expandedRelationship.isDestinationNegated());
reasonerRelationship.setGroup(expandedRelationship.getRelationshipGroup());
reasonerRelationship.setModifierId(expandedRelationship.getModifierId());
// reasonerRelationship.setReleased(...) is already set
reasonerRelationship.setSource(expandedRelationship.getSource());
reasonerRelationship.setType(expandedRelationship.getType());
reasonerRelationship.setUnionGroup(expandedRelationship.getUnionGroup());
reasonerRelationship.setValueAsObject(expandedRelationship.getValueAsObject());
}
break;
default:
throw new IllegalStateException(String.format("Unexpected relationship change '%s' found with SCTID '%s'.", item.getChangeNature(), item.getRelationship().getOriginId()));
}
}
}
}
use of com.b2international.snowowl.core.events.Request in project snow-owl by b2ihealthcare.
the class ConceptChangeConverter method expand.
@Override
public void expand(final List<ConceptChange> results) {
if (!expand().containsKey(ConceptChange.Expand.CONCEPT)) {
return;
}
/*
* Depending on the concept change search request, we might need to issue
* SNOMED CT searches against multiple branches; find out which ones we have.
*/
final Multimap<String, ConceptChange> itemsByBranch = getItemsByBranch(results);
// Check if we only need to load inferred concepts in their entirety
final Options expandOptions = expand().getOptions(ConceptChange.Expand.CONCEPT);
// pt() and fsn() are the only useful options here
final Options conceptExpandOptions = expandOptions.getOptions("expand");
conceptExpandOptions.keySet().retainAll(CONCEPT_EXPAND_OPTIONS);
for (final String branch : itemsByBranch.keySet()) {
final Collection<ConceptChange> itemsForCurrentBranch = itemsByBranch.get(branch);
final Set<String> conceptIds = itemsForCurrentBranch.stream().map(c -> c.getConcept().getOriginConceptId()).collect(Collectors.toSet());
final Request<BranchContext, SnomedConcepts> conceptSearchRequest = SnomedRequests.prepareSearchConcept().filterByIds(conceptIds).setLimit(conceptIds.size()).setExpand(conceptExpandOptions).setLocales(locales()).build();
final SnomedConcepts concepts = new BranchRequest<>(branch, new RevisionIndexReadRequest<>(conceptSearchRequest)).execute(context());
final Map<String, SnomedConcept> conceptsById = Maps.uniqueIndex(concepts, SnomedConcept::getId);
for (final ConceptChange item : itemsForCurrentBranch) {
final ReasonerConcept reasonerConcept = item.getConcept();
final String conceptId = reasonerConcept.getOriginConceptId();
final SnomedConcept expandedConcept = conceptsById.get(conceptId);
reasonerConcept.setDefinitionStatusId(expandedConcept.getDefinitionStatusId());
reasonerConcept.setPt(expandedConcept.getPt());
reasonerConcept.setFsn(expandedConcept.getFsn());
// reasonerConcept.setReleased(...) is already set
}
}
}
Aggregations