Merge "Sensible equals and hashCode for FragmentEntity (CPS-1664 #1)"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentQueryBuilder.java
index ca6e18b..0134873 100644 (file)
@@ -31,7 +31,6 @@ import javax.persistence.PersistenceContext;
 import javax.persistence.Query;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.collections4.CollectionUtils;
 import org.onap.cps.cpspath.parser.CpsPathPrefixType;
 import org.onap.cps.cpspath.parser.CpsPathQuery;
 import org.onap.cps.spi.entities.FragmentEntity;
@@ -42,10 +41,10 @@ import org.springframework.stereotype.Component;
 @Slf4j
 @Component
 public class FragmentQueryBuilder {
-    private static final String REGEX_ABSOLUTE_PATH_PREFIX = ".*\\/";
-    private static final String REGEX_OPTIONAL_LIST_INDEX_POSTFIX = "(\\[@(?!.*\\[).*?])?";
-    private static final String REGEX_DESCENDANT_PATH_POSTFIX = "(\\/.*)?";
-    private static final String REGEX_END_OF_INPUT = "$";
+    private static final String REGEX_ABSOLUTE_PATH_PREFIX = "^";
+    private static final String REGEX_DESCENDANT_PATH_PREFIX = "^.*\\/";
+    private static final String REGEX_OPTIONAL_LIST_INDEX_POSTFIX = "(\\[@(?!.*\\[).*?])?$";
+    private static final String REGEX_FOR_QUICK_FIND_WITH_DESCENDANTS = "(\\[@.*?])?(\\/.*)?$";
 
     @PersistenceContext
     private EntityManager entityManager;
@@ -79,51 +78,66 @@ public class FragmentQueryBuilder {
         return getQuery(cpsPathQuery, sqlStringBuilder, queryParameters);
     }
 
+    /**
+     * Create a regular expression (string) for matching xpaths based on the given cps path query.
+     *
+     * @param cpsPathQuery the cps path query to determine the required regular expression
+     * @return a string representing the required regular expression
+     */
+    public static String getXpathSqlRegex(final CpsPathQuery cpsPathQuery) {
+        final StringBuilder xpathRegexBuilder = getRegexStringBuilderWithPrefix(cpsPathQuery);
+        xpathRegexBuilder.append(REGEX_OPTIONAL_LIST_INDEX_POSTFIX);
+        return xpathRegexBuilder.toString();
+    }
+
+    /**
+     * Create a regular expression (string) for matching xpaths with (all) descendants
+     * based on the given cps path query.
+     *
+     * @param cpsPathQuery the cps path query to determine the required regular expression
+     * @return a string representing the required regular expression
+     */
+    public static String getXpathSqlRegexForQuickFindWithDescendants(final CpsPathQuery cpsPathQuery) {
+        final StringBuilder xpathRegexBuilder = getRegexStringBuilderWithPrefix(cpsPathQuery);
+        xpathRegexBuilder.append(REGEX_FOR_QUICK_FIND_WITH_DESCENDANTS);
+        return xpathRegexBuilder.toString();
+    }
+
     private Query getQuery(final CpsPathQuery cpsPathQuery, final StringBuilder sqlStringBuilder,
                            final Map<String, Object> queryParameters) {
-        final String xpathRegex = getXpathSqlRegex(cpsPathQuery, false);
+        final String xpathRegex = getXpathSqlRegex(cpsPathQuery);
         queryParameters.put("xpathRegex", xpathRegex);
         final List<String> queryBooleanOperatorsType = cpsPathQuery.getBooleanOperatorsType();
         if (cpsPathQuery.hasLeafConditions()) {
             sqlStringBuilder.append(" AND (");
             final Queue<String> booleanOperatorsQueue = (queryBooleanOperatorsType == null) ? null : new LinkedList<>(
-                    queryBooleanOperatorsType);
+                queryBooleanOperatorsType);
             cpsPathQuery.getLeavesData().entrySet().forEach(entry -> {
                 sqlStringBuilder.append(" attributes @> ");
                 sqlStringBuilder.append("'" + jsonObjectMapper.asJsonString(entry) + "'");
-                if (!CollectionUtils.isEmpty(booleanOperatorsQueue)) {
+                if (!(booleanOperatorsQueue == null || booleanOperatorsQueue.isEmpty())) {
                     sqlStringBuilder.append(" " + booleanOperatorsQueue.poll() + " ");
                 }
             });
             sqlStringBuilder.append(")");
         }
         addTextFunctionCondition(cpsPathQuery, sqlStringBuilder, queryParameters);
+        addContainsFunctionCondition(cpsPathQuery, sqlStringBuilder, queryParameters);
         final Query query = entityManager.createNativeQuery(sqlStringBuilder.toString(), FragmentEntity.class);
         setQueryParameters(query, queryParameters);
         return query;
     }
 
-    /**
-     * Create a regular expression (string) for xpath based on the given cps path query.
-     *
-     * @param cpsPathQuery  the cps path query to determine the required regular expression
-     * @param includeDescendants include descendants yes or no
-     * @return a string representing the required regular expression
-     */
-    public static String getXpathSqlRegex(final CpsPathQuery cpsPathQuery, final boolean includeDescendants) {
+    private static StringBuilder getRegexStringBuilderWithPrefix(final CpsPathQuery cpsPathQuery) {
         final StringBuilder xpathRegexBuilder = new StringBuilder();
         if (CpsPathPrefixType.ABSOLUTE.equals(cpsPathQuery.getCpsPathPrefixType())) {
-            xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix()));
-        } else {
             xpathRegexBuilder.append(REGEX_ABSOLUTE_PATH_PREFIX);
-            xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getDescendantName()));
-        }
-        xpathRegexBuilder.append(REGEX_OPTIONAL_LIST_INDEX_POSTFIX);
-        if (includeDescendants) {
-            xpathRegexBuilder.append(REGEX_DESCENDANT_PATH_POSTFIX);
+            xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix()));
+            return xpathRegexBuilder;
         }
-        xpathRegexBuilder.append(REGEX_END_OF_INPUT);
-        return xpathRegexBuilder.toString();
+        xpathRegexBuilder.append(REGEX_DESCENDANT_PATH_PREFIX);
+        xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getDescendantName()));
+        return xpathRegexBuilder;
     }
 
     private static String escapeXpath(final String xpath) {
@@ -160,6 +174,16 @@ public class FragmentQueryBuilder {
         }
     }
 
+    private static void addContainsFunctionCondition(final CpsPathQuery cpsPathQuery,
+                                                     final StringBuilder sqlStringBuilder,
+                                                     final Map<String, Object> queryParameters) {
+        if (cpsPathQuery.hasContainsFunctionCondition()) {
+            sqlStringBuilder.append(" AND attributes ->> :containsLeafName LIKE CONCAT('%',:containsValue,'%') ");
+            queryParameters.put("containsLeafName", cpsPathQuery.getContainsFunctionConditionLeafName());
+            queryParameters.put("containsValue", cpsPathQuery.getContainsFunctionConditionValue());
+        }
+    }
+
     private static void setQueryParameters(final Query query, final Map<String, Object> queryParameters) {
         for (final Map.Entry<String, Object> queryParameter : queryParameters.entrySet()) {
             query.setParameter(queryParameter.getKey(), queryParameter.getValue());