2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.pnfsimulator.template.search.handler;
23 import com.google.common.collect.Lists;
24 import com.google.gson.JsonPrimitive;
25 import org.springframework.data.mongodb.core.query.Criteria;
27 import java.util.List;
28 import java.util.regex.Pattern;
31 * This class is a helper class for constructing apropriate criteria for query send to mongodb based on type of value.
32 * Query is build to search mongodb for templates that contains key-value pairs that satisfy given criteria.
33 * Value is oftype JsonPrimitive, based on its primitive java type following criteria are build to get proper document:
34 * -for string - there is a regex expression that ignores every meta character inside passed argument and searches for exact literal match ignoring case;
35 * -for number - all numbers are treated as double (mongodb number type equivalent)
36 * -for boolean - exact match, used string representation of boolean in search
39 public class PrimitiveValueCriteriaBuilder {
41 private final List<ValueTypeHandler> typeHandlers;
43 public PrimitiveValueCriteriaBuilder() {
44 typeHandlers = Lists.newArrayList(new StringValueHandler(), new NumberValueHandler(), new BoolValueHandler());
47 public Criteria applyValueCriteriaBasedOnPrimitiveType(Criteria baseCriteria, JsonPrimitive jsonPrimitive) {
48 ValueTypeHandler typeHandler = typeHandlers.stream()
49 .filter(el -> el.isProperTypeHandler(jsonPrimitive))
52 new IllegalArgumentException(String.format(
53 "Expected json primitive, but given value: %s is of type: %s and could not be decoded",
54 jsonPrimitive, jsonPrimitive.getClass().toString())));
55 return typeHandler.chainCriteriaForValue(baseCriteria, jsonPrimitive);
58 private interface ValueTypeHandler {
59 boolean isProperTypeHandler(JsonPrimitive value);
61 Criteria chainCriteriaForValue(Criteria criteria, JsonPrimitive value);
64 private class BoolValueHandler implements ValueTypeHandler {
65 public boolean isProperTypeHandler(JsonPrimitive value) {
66 return value.isBoolean();
69 public Criteria chainCriteriaForValue(Criteria criteria, JsonPrimitive value) {
70 return criteria.is(value.getAsString());
75 private class NumberValueHandler implements ValueTypeHandler {
76 public boolean isProperTypeHandler(JsonPrimitive value) {
77 return value.isNumber();
80 public Criteria chainCriteriaForValue(Criteria baseCriteria, JsonPrimitive value) {
81 return baseCriteria.is(value.getAsDouble());
85 private class StringValueHandler implements ValueTypeHandler {
86 public boolean isProperTypeHandler(JsonPrimitive value) {
87 return value.isString();
90 public Criteria chainCriteriaForValue(Criteria baseCriteria, JsonPrimitive value) {
91 return baseCriteria.regex(makeRegexCaseInsensitive(value.getAsString()));
94 private Pattern makeRegexCaseInsensitive(String base) {
95 String metaCharEscaped = convertToIgnoreMetaChars(base);
96 return Pattern.compile("^" + metaCharEscaped + "$", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
99 private String convertToIgnoreMetaChars(String valueWithMetaChars) {
100 return Pattern.quote(valueWithMetaChars);