use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class SnomedDescriptionSearchRequest method prepareQuery.
@Override
protected Expression prepareQuery(BranchContext context) {
if (containsKey(OptionKey.TERM) && get(OptionKey.TERM, TermFilter.class).getTerm().length() < 2) {
throw new BadRequestException("Description term must be at least 2 characters long.");
}
final ExpressionBuilder queryBuilder = Expressions.builder();
// Add (presumably) most selective filters first
addActiveClause(queryBuilder);
addReleasedClause(queryBuilder);
addLanguageFilter(queryBuilder);
addNamespaceFilter(queryBuilder);
addNamespaceConceptIdFilter(context, queryBuilder);
addActiveMemberOfClause(context, queryBuilder);
addMemberOfClause(context, queryBuilder);
addEclFilter(context, queryBuilder, OptionKey.LANGUAGE_REFSET, ids -> {
return Expressions.builder().should(preferredIn(ids)).should(acceptableIn(ids)).build();
});
addEclFilter(context, queryBuilder, OptionKey.ACCEPTABLE_IN, ids -> acceptableIn(ids));
addEclFilter(context, queryBuilder, OptionKey.PREFERRED_IN, ids -> preferredIn(ids));
// apply locale based filters
addFilter(queryBuilder, OptionKey.LANGUAGE_REFSET_LOCALES, ExtendedLocale.class, locales -> {
final List<String> languageRefSetIds = SnomedDescriptionUtils.getLanguageRefSetIds(context, (List<ExtendedLocale>) locales);
return Expressions.builder().should(preferredIn(languageRefSetIds)).should(acceptableIn(languageRefSetIds)).build();
});
addFilter(queryBuilder, OptionKey.ACCEPTABLE_IN_LOCALES, ExtendedLocale.class, locales -> {
final List<String> languageRefSetIds = SnomedDescriptionUtils.getLanguageRefSetIds(context, (List<ExtendedLocale>) locales);
return acceptableIn(languageRefSetIds);
});
addFilter(queryBuilder, OptionKey.PREFERRED_IN_LOCALES, ExtendedLocale.class, locales -> {
final List<String> languageRefSetIds = SnomedDescriptionUtils.getLanguageRefSetIds(context, (List<ExtendedLocale>) locales);
return preferredIn(languageRefSetIds);
});
addEffectiveTimeClause(queryBuilder);
addIdFilter(queryBuilder, RevisionDocument.Expressions::ids);
addEclFilter(context, queryBuilder, SnomedSearchRequest.OptionKey.MODULE, SnomedDocument.Expressions::modules);
addEclFilter(context, queryBuilder, OptionKey.CONCEPT, SnomedDescriptionIndexEntry.Expressions::concepts);
addEclFilter(context, queryBuilder, OptionKey.TYPE, SnomedDescriptionIndexEntry.Expressions::types);
addEclFilter(context, queryBuilder, OptionKey.CASE_SIGNIFICANCE, SnomedDescriptionIndexEntry.Expressions::caseSignificances);
if (containsKey(OptionKey.SEMANTIC_TAG)) {
queryBuilder.filter(SnomedDescriptionIndexEntry.Expressions.semanticTags(getCollection(OptionKey.SEMANTIC_TAG, String.class)));
}
if (containsKey(OptionKey.SEMANTIC_TAG_REGEX)) {
queryBuilder.filter(SnomedDescriptionIndexEntry.Expressions.semanticTagRegex(getString(OptionKey.SEMANTIC_TAG_REGEX)));
}
if (containsKey(OptionKey.TERM_REGEX)) {
final String regex = getString(OptionKey.TERM_REGEX);
queryBuilder.filter(SnomedDescriptionIndexEntry.Expressions.matchTermRegex(regex));
}
if (containsKey(OptionKey.TERM)) {
final TermFilter termFilter = get(OptionKey.TERM, TermFilter.class);
queryBuilder.must(toDescriptionTermQuery(termFilter));
}
return queryBuilder.build();
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class SnomedRefSetMemberSearchRequest method prepareQuery.
@Override
protected Expression prepareQuery(BranchContext context) {
final Collection<String> referencedComponentIds = getCollection(OptionKey.REFERENCED_COMPONENT, String.class);
final Collection<SnomedRefSetType> refSetTypes = getCollection(OptionKey.REFSET_TYPE, SnomedRefSetType.class);
final Options propsFilter = getOptions(OptionKey.PROPS);
ExpressionBuilder queryBuilder = Expressions.builder();
addActiveClause(queryBuilder);
addReleasedClause(queryBuilder);
addEclFilter(context, queryBuilder, SnomedSearchRequest.OptionKey.MODULE, SnomedDocument.Expressions::modules);
addIdFilter(queryBuilder, RevisionDocument.Expressions::ids);
addEffectiveTimeClause(queryBuilder);
addEclFilter(context, queryBuilder, OptionKey.REFSET, SnomedRefSetMemberIndexEntry.Expressions::refsetIds);
addComponentClause(queryBuilder);
if (containsKey(OptionKey.REFERENCED_COMPONENT_TYPE)) {
queryBuilder.filter(referencedComponentTypes(getCollection(OptionKey.REFERENCED_COMPONENT_TYPE, String.class)));
}
if (!referencedComponentIds.isEmpty()) {
queryBuilder.filter(referencedComponentIds(referencedComponentIds));
}
if (!refSetTypes.isEmpty()) {
queryBuilder.filter(refSetTypes(refSetTypes));
}
if (!propsFilter.isEmpty()) {
final Set<String> propKeys = newHashSet(propsFilter.keySet());
if (propKeys.remove(SnomedRf2Headers.FIELD_ACCEPTABILITY_ID)) {
queryBuilder.filter(acceptabilityIds(propsFilter.getCollection(SnomedRf2Headers.FIELD_ACCEPTABILITY_ID, String.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP)) {
final String operatorKey = SearchResourceRequest.operator(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP);
SearchResourceRequest.Operator op;
if (propKeys.remove(operatorKey)) {
op = propsFilter.get(operatorKey, Operator.class);
} else {
op = SearchResourceRequest.Operator.EQUALS;
}
switch(op) {
case EQUALS:
queryBuilder.filter(relationshipGroup(propsFilter.get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP, Integer.class)));
break;
case NOT_EQUALS:
queryBuilder.mustNot(relationshipGroup(propsFilter.get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP, Integer.class)));
break;
default:
throw new NotImplementedException("Unsupported relationship group operator %s", op);
}
}
if (propKeys.remove(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::characteristicTypeIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_CORRELATION_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_CORRELATION_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::correlationIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_DESCRIPTION_FORMAT)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_DESCRIPTION_FORMAT, String.class), SnomedRefSetMemberIndexEntry.Expressions::descriptionFormats);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_CATEGORY_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_CATEGORY_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::mapCategoryIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::targetComponentIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_TARGET)) {
queryBuilder.filter(mapTargets(propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_TARGET, String.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_TARGET_DESCRIPTION)) {
queryBuilder.filter(mapTargetDescriptions(propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_TARGET_DESCRIPTION, String.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_GROUP)) {
queryBuilder.filter(mapGroups(propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_GROUP, Integer.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_PRIORITY)) {
queryBuilder.filter(mapPriority(propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_PRIORITY, Integer.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MAP_BLOCK)) {
queryBuilder.filter(mapBlock(propsFilter.getCollection(SnomedRf2Headers.FIELD_MAP_BLOCK, Integer.class)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_VALUE_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_VALUE_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::valueIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_TYPE_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_TYPE_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::typeIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_DOMAIN_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_MRCM_DOMAIN_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::domainIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_CONTENT_TYPE_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_MRCM_CONTENT_TYPE_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::contentTypeIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_RULE_STRENGTH_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_MRCM_RULE_STRENGTH_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::ruleStrengthIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_RULE_REFSET_ID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRf2Headers.FIELD_MRCM_RULE_REFSET_ID, String.class), SnomedRefSetMemberIndexEntry.Expressions::ruleRefSetIds);
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_GROUPED)) {
queryBuilder.filter(grouped(propsFilter.getBoolean(SnomedRf2Headers.FIELD_MRCM_GROUPED)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_MRCM_RANGE_CONSTRAINT)) {
queryBuilder.filter(rangeConstraint(propsFilter.getString(SnomedRf2Headers.FIELD_MRCM_RANGE_CONSTRAINT)));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_OWL_EXPRESSION)) {
queryBuilder.filter(Expressions.exactMatch(SnomedRf2Headers.FIELD_OWL_EXPRESSION, propsFilter.getString(SnomedRf2Headers.FIELD_OWL_EXPRESSION)));
}
if (propKeys.remove(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_CONCEPTID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_CONCEPTID, String.class), SnomedRefSetMemberIndexEntry.Expressions::owlExpressionConcept);
}
if (propKeys.remove(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_DESTINATIONID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_DESTINATIONID, String.class), SnomedRefSetMemberIndexEntry.Expressions::owlExpressionDestination);
}
if (propKeys.remove(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_TYPEID)) {
addEclFilter(context, queryBuilder, propsFilter.getCollection(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_TYPEID, String.class), SnomedRefSetMemberIndexEntry.Expressions::owlExpressionType);
}
if (propKeys.remove(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_GCI)) {
queryBuilder.filter(gciAxiom(propsFilter.getBoolean(SnomedRefSetMemberSearchRequestBuilder.OWL_EXPRESSION_GCI)));
}
final Collection<DataType> dataTypes = propsFilter.getCollection(SnomedRefSetMemberIndexEntry.Fields.DATA_TYPE, DataType.class);
if (propKeys.remove(SnomedRefSetMemberIndexEntry.Fields.DATA_TYPE)) {
queryBuilder.filter(dataTypes(dataTypes));
}
if (propKeys.remove(SnomedRf2Headers.FIELD_VALUE)) {
if (dataTypes.size() != 1) {
throw new BadRequestException("DataType filter must be specified if filtering by value");
}
final DataType dataType = Iterables.getOnlyElement(dataTypes);
final String operatorKey = SearchResourceRequest.operator(SnomedRf2Headers.FIELD_VALUE);
SearchResourceRequest.Operator op;
if (propKeys.remove(operatorKey)) {
op = propsFilter.get(operatorKey, Operator.class);
} else {
op = SearchResourceRequest.Operator.EQUALS;
}
final Collection<Object> attributeValues = propsFilter.getCollection(SnomedRf2Headers.FIELD_VALUE, Object.class);
switch(op) {
case EQUALS:
queryBuilder.filter(values(dataType, attributeValues));
break;
case NOT_EQUALS:
queryBuilder.mustNot(values(dataType, attributeValues));
break;
case LESS_THAN:
checkRangeValue(attributeValues);
queryBuilder.filter(valueRange(dataType, null, Iterables.getOnlyElement(attributeValues), false, false));
break;
case LESS_THAN_EQUALS:
checkRangeValue(attributeValues);
queryBuilder.filter(valueRange(dataType, null, Iterables.getOnlyElement(attributeValues), false, true));
break;
case GREATER_THAN:
checkRangeValue(attributeValues);
queryBuilder.filter(valueRange(dataType, Iterables.getOnlyElement(attributeValues), null, false, false));
break;
case GREATER_THAN_EQUALS:
checkRangeValue(attributeValues);
queryBuilder.filter(valueRange(dataType, Iterables.getOnlyElement(attributeValues), null, true, false));
break;
default:
throw new NotImplementedException("Unsupported concrete domain value operator %s", op);
}
}
if (!propKeys.isEmpty()) {
throw new IllegalQueryParameterException("Unsupported property filter(s), %s", propKeys);
}
}
return queryBuilder.build();
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class SnomedRefSetCreateRequest method execute.
@Override
public String execute(TransactionContext context) {
RefSetSupport.checkType(type, referencedComponentType);
final SnomedConceptDocument concept;
if (Strings.isNullOrEmpty(identifierId)) {
throw new BadRequestException("Reference set identifier ID may not be null or empty.");
} else {
try {
concept = context.lookup(identifierId, SnomedConceptDocument.class);
if (concept.getRefSetType() != null) {
throw new BadRequestException("Identifier concept %s has been already registered as refset", identifierId);
}
} catch (ComponentNotFoundException e) {
throw e.toBadRequestException();
}
}
final SnomedConceptDocument.Builder updatedConcept = SnomedConceptDocument.builder(concept);
final SnomedReferenceSet refSet = new SnomedReferenceSet();
refSet.setType(type);
refSet.setReferencedComponentType(referencedComponentType);
if (SnomedRefSetUtil.isMapping(type)) {
refSet.setMapTargetComponentType(mapTargetComponentType);
}
updatedConcept.refSet(refSet);
context.update(concept, updatedConcept.build());
return identifierId;
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class SnomedModuleDependencyMemberCreateDelegate method execute.
@Override
public String execute(SnomedReferenceSet refSet, TransactionContext context) {
checkRefSetType(refSet, SnomedRefSetType.MODULE_DEPENDENCY);
checkReferencedComponent(refSet);
checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_MODULE_ID, getModuleId());
checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_REFERENCED_COMPONENT_ID, getReferencedComponentId());
SnomedModuleDependencyReferenceSetMemberBuilder builder = SnomedComponents.newModuleDependencyMember().withId(getId()).withActive(isActive()).withReferencedComponent(getReferencedComponentId()).withModuleId(getModuleId()).withRefSet(getReferenceSetId());
try {
if (hasProperty(SnomedRf2Headers.FIELD_SOURCE_EFFECTIVE_TIME)) {
String sourceEffectiveTime = getProperty(SnomedRf2Headers.FIELD_SOURCE_EFFECTIVE_TIME);
builder.withSourceEffectiveTime(Strings.isNullOrEmpty(sourceEffectiveTime) ? null : EffectiveTimes.parse(sourceEffectiveTime, DateFormats.SHORT));
}
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("Error while parsing date")) {
throw new BadRequestException(e.getMessage());
}
}
try {
if (hasProperty(SnomedRf2Headers.FIELD_TARGET_EFFECTIVE_TIME)) {
String targetEffectiveTime = getProperty(SnomedRf2Headers.FIELD_TARGET_EFFECTIVE_TIME);
builder.withTargetEffectiveTime(Strings.isNullOrEmpty(targetEffectiveTime) ? null : EffectiveTimes.parse(targetEffectiveTime, DateFormats.SHORT));
}
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("Error while parsing date")) {
throw new BadRequestException(e.getMessage());
}
}
return builder.addTo(context).getId();
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class MapTypeRefSetUpdateRequest method execute.
@Override
public Boolean execute(TransactionContext context) {
// fail fast if refset identifier concept does not exist
SnomedConceptDocument conceptDocument = context.lookup(referenceSetId, SnomedConceptDocument.class);
SnomedRefSetType refSetType = conceptDocument.getRefSetType();
if (!SnomedRefSetUtil.isMapping(refSetType)) {
throw new BadRequestException("Map target codesystem can be set only for map-type reference sets, reference set: '%s' reference set type: '%s'.", referenceSetId, refSetType);
}
Builder conceptBuilder = SnomedConceptDocument.builder(conceptDocument);
boolean changed = false;
if (conceptDocument.getMapTargetComponentType() != null) {
String currentMapTargetComponent = conceptDocument.getMapTargetComponentType();
if (!mapTargetComponent.equals(currentMapTargetComponent)) {
conceptBuilder.mapTargetComponentType(mapTargetComponent).build();
changed = true;
}
} else {
conceptBuilder.mapTargetComponentType(mapTargetComponent).build();
changed = true;
}
if (changed) {
context.update(conceptDocument, conceptBuilder.build());
}
return changed;
}
Aggregations