use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class ValidationIssueSearchRequest method prepareQuery.
@Override
protected Expression prepareQuery(ServiceProvider context) {
final ExpressionBuilder queryBuilder = Expressions.builder();
addIdFilter(queryBuilder, ids -> Expressions.matchAny(ValidationIssue.Fields.ID, ids));
if (containsKey(OptionKey.RESOURCE_URI)) {
queryBuilder.filter(Expressions.matchAny(ValidationIssue.Fields.RESOURCE_URI, getCollection(OptionKey.RESOURCE_URI, String.class)));
}
Set<String> filterByRuleIds = null;
if (containsKey(OptionKey.TOOLING_ID)) {
final Collection<String> toolingIds = getCollection(OptionKey.TOOLING_ID, String.class);
final Set<String> ruleIds = ValidationRequests.rules().prepareSearch().all().filterByToolings(toolingIds).setFields(ValidationRule.Fields.ID).build().execute(context).stream().map(ValidationRule::getId).collect(Collectors.toSet());
if (ruleIds.isEmpty()) {
queryBuilder.filter(Expressions.matchNone());
} else {
filterByRuleIds = newHashSet(ruleIds);
}
}
if (containsKey(OptionKey.RULE_ID)) {
Set<String> ruleFilter = ImmutableSet.copyOf(getCollection(OptionKey.RULE_ID, String.class));
if (filterByRuleIds != null) {
SetView<String> diff = Sets.difference(ruleFilter, filterByRuleIds);
if (!diff.isEmpty()) {
throw new BadRequestException("Some of the ruleId filter values '%s' belong to a non-specified toolingId.", diff);
}
filterByRuleIds = ruleFilter;
} else {
filterByRuleIds = newHashSet(ruleFilter);
}
}
if (filterByRuleIds != null) {
queryBuilder.filter(Expressions.matchAny(ValidationIssue.Fields.RULE_ID, filterByRuleIds));
}
if (containsKey(OptionKey.AFFECTED_COMPONENT_ID)) {
Collection<String> affectedComponentIds = getCollection(OptionKey.AFFECTED_COMPONENT_ID, String.class);
queryBuilder.filter(Expressions.matchAny(ValidationIssue.Fields.AFFECTED_COMPONENT_ID, affectedComponentIds));
}
if (containsKey(OptionKey.AFFECTED_COMPONENT_LABEL)) {
final String searchTerm = getString(OptionKey.AFFECTED_COMPONENT_LABEL);
if (containsKey(OptionKey.AFFECTED_COMPONENT_ID)) {
queryBuilder.must(Expressions.matchTextPhrase(ValidationIssue.Fields.AFFECTED_COMPONENT_LABELS_TEXT, searchTerm));
} else {
queryBuilder.must(Expressions.builder().should(Expressions.dismaxWithScoreCategories(Expressions.matchTextPhrase(ValidationIssue.Fields.AFFECTED_COMPONENT_LABELS_TEXT, searchTerm), Expressions.matchTextAll(ValidationIssue.Fields.AFFECTED_COMPONENT_LABELS_PREFIX, searchTerm))).should(Expressions.boost(Expressions.exactMatch(ValidationIssue.Fields.AFFECTED_COMPONENT_ID, searchTerm), 1000f)).build());
}
}
if (containsKey(OptionKey.WHITELISTED)) {
boolean whitelisted = getBoolean(OptionKey.WHITELISTED);
queryBuilder.filter(Expressions.match(ValidationIssue.Fields.WHITELISTED, whitelisted));
}
if (containsKey(OptionKey.DETAILS)) {
if (!containsKey(OptionKey.TOOLING_ID)) {
throw new BadRequestException("At least one toolingId is required to be able to filter issues by details.");
}
final Collection<String> toolingIds = getCollection(OptionKey.TOOLING_ID, String.class);
final Options options = getOptions(OptionKey.DETAILS);
final ExpressionBuilder toolingQuery = Expressions.builder();
final Collection<ValidationIssueDetailExtension> extensions = context.service(ValidationIssueDetailExtensionProvider.class).getExtensions();
for (String toolingId : toolingIds) {
extensions.stream().filter(ext -> toolingId.equals(ext.getToolingId())).findFirst().ifPresent(extension -> {
final ExpressionBuilder extensionQuery = Expressions.builder();
extension.prepareQuery(extensionQuery, options);
toolingQuery.should(extensionQuery.build());
});
}
// at least one tooling should match
toolingQuery.setMinimumNumberShouldMatch(1);
queryBuilder.filter(toolingQuery.build());
}
return queryBuilder.build();
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class ComponentURI method of.
public static ComponentURI of(String uri) {
if (Strings.isNullOrEmpty(uri)) {
return ComponentURI.UNSPECIFIED;
}
final List<String> parts = SLASH_SPLITTER.splitToList(uri);
if (parts.size() < 4) {
throw new BadRequestException("A component uri consists of at least four parts (resourceType/resourceId/componentType/componentId). Arg was: %s", uri);
}
int terminologyComponentTypeIndex = parts.size() - 2;
int componentIdIndex = parts.size() - 1;
ResourceURI resourceURI = new ResourceURI(SLASH_JOINER.join(parts.subList(0, terminologyComponentTypeIndex)));
String componentType = parts.get(terminologyComponentTypeIndex);
String componentId = parts.get(componentIdIndex);
return new ComponentURI(resourceURI, componentType, componentId);
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class SnomedEclRefinementEvaluator method evalRefinement.
/**
* Evaluates an {@link AttributeConstraint} refinement on the given focusConceptId set on the given {@link BranchContext}.
* Grouped parameter can
*/
private Promise<Collection<Property>> evalRefinement(final BranchContext context, final AttributeConstraint refinement, final boolean grouped, final Set<String> focusConceptIds) {
final Comparison comparison = refinement.getComparison();
final Collection<String> typeConceptFilter = evalToConceptIds(context, refinement.getAttribute(), expressionForm).getSync(1, TimeUnit.MINUTES);
if (comparison instanceof AttributeComparison) {
// resolve non-* focusConcept ECLs to IDs, so we can filter relationships by source/destination
// filterByType and filterByDestination accepts ECL expressions as well, so serialize them into ECL and pass as String when required
// if reversed refinement, then we are interested in the destinationIds otherwise we need the sourceIds
final Collection<String> destinationConceptFilter = evalToConceptIds(context, ((AttributeComparison) comparison).getValue(), expressionForm).getSync(1, TimeUnit.MINUTES);
final Collection<String> focusConceptFilter = refinement.isReversed() ? destinationConceptFilter : focusConceptIds;
final Collection<String> valueConceptFilter = refinement.isReversed() ? focusConceptIds : destinationConceptFilter;
return evalStatements(context, focusConceptFilter, typeConceptFilter, valueConceptFilter, grouped, expressionForm);
} else if (comparison instanceof DataTypeComparison) {
if (refinement.isReversed()) {
throw new BadRequestException("Reversed flag is not supported in data type based comparison (string/numeric)");
} else {
final Promise<Collection<Property>> statementsWithValue = evalStatementsWithValue(context, focusConceptIds, typeConceptFilter, (DataTypeComparison) comparison);
final Promise<Collection<Property>> members = evalMembers(context, focusConceptIds, typeConceptFilter, (DataTypeComparison) comparison);
return Promise.all(statementsWithValue, members).then(results -> {
final Collection<Property> s = (Collection<Property>) results.get(0);
final Collection<Property> m = (Collection<Property>) results.get(1);
return FluentIterable.concat(s, m).toSet();
});
}
} else {
return SnomedEclEvaluationRequest.throwUnsupported(comparison);
}
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class SnomedDescriptionUtils method getLanguageRefSetIds.
/**
* Extracts the language reference set identifier from the specified list of {@link ExtendedLocale}s.
* <p>
* The identifiers may come from the value itself, if it includes a reference set ID (eg. {@code en-x-12345678901}),
* or from the language tag part, if it is well known (eg. {@code en-US}).
* <p>
* If no element from the input list can be converted, an {@link IllegalArgumentException} is thrown; no exception occurs
* if only some of the {@code ExtendedLocale}s could not be transformed into a language reference set identifier, however.
*
* @param context - the context to use to retrieve language map configuration
* @param locales the extended locale list to process (may not be {@code null})
* @return the converted language reference set identifiers or an empty {@link List}, never <code>null</code>
*/
public static List<String> getLanguageRefSetIds(final BranchContext context, final List<ExtendedLocale> locales) {
if (CompareUtils.isEmpty(locales)) {
return Collections.emptyList();
}
final ListMultimap<String, String> languageMap = getLanguageMapping(context);
final List<String> languageRefSetIds = newArrayList();
final List<ExtendedLocale> unconvertableLocales = new ArrayList<ExtendedLocale>();
for (final ExtendedLocale extendedLocale : locales) {
Collection<String> mappedRefSetIds;
if (!extendedLocale.getLanguageRefSetId().isEmpty()) {
mappedRefSetIds = Collections.singleton(extendedLocale.getLanguageRefSetId());
} else {
mappedRefSetIds = languageMap.get(extendedLocale.getLanguageTag());
}
if (mappedRefSetIds.isEmpty()) {
unconvertableLocales.add(extendedLocale);
} else {
mappedRefSetIds.forEach(mappedRefSetId -> {
if (!languageRefSetIds.contains(mappedRefSetId)) {
languageRefSetIds.add(mappedRefSetId);
}
});
}
}
if (languageRefSetIds.isEmpty() && !unconvertableLocales.isEmpty()) {
throw new BadRequestException("Don't know how to convert extended locale %s to a language reference set identifier.", Iterables.toString(unconvertableLocales));
}
return languageRefSetIds;
}
use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.
the class RelationshipValue method fromLiteral.
/**
* @param literal
* @return
*/
public static final RelationshipValue fromLiteral(final String literal) {
if (literal == null) {
return null;
}
if (literal.startsWith("#")) {
// Remove prefix, parse number
final String numericLiteral = literal.substring(1);
if (numericLiteral.contains(".")) {
return new RelationshipValue(new BigDecimal(numericLiteral));
} else {
return new RelationshipValue(Integer.valueOf(numericLiteral));
}
}
if (literal.startsWith("\"") && literal.endsWith("\"")) {
// Remove prefix and suffix, unescape quotes
final String quotedLiteral = literal.substring(1, literal.length() - 1);
final String unescapedLiteral = quotedLiteral.replace("\\\"", "\"");
return new RelationshipValue(unescapedLiteral);
}
throw new BadRequestException("Couldn't convert literal <" + literal + "> to a concrete value.");
}
Aggregations