Query based on Public CM Properties
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentRepository.java
index a40168a..c48c79e 100755 (executable)
@@ -1,7 +1,8 @@
-/*-\r
+/*\r
  * ============LICENSE_START=======================================================\r
- *  Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
- *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.\r
+ * Copyright (C) 2020-2021 Nordix Foundation.\r
+ * Modifications Copyright (C) 2020-2021 Bell Canada.\r
+ * Modifications Copyright (C) 2020-2021 Pantheon.tech.\r
  * ================================================================================\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
@@ -37,27 +38,35 @@ import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;\r
 \r
 @Repository\r
-public interface FragmentRepository extends JpaRepository<FragmentEntity, Long> {\r
+public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>, FragmentRepositoryCpsPathQuery {\r
 \r
     Optional<FragmentEntity> findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
-        @NonNull AnchorEntity anchorEntity, @NonNull String xpath);\r
+                                                              @NonNull AnchorEntity anchorEntity,\r
+                                                              @NonNull String xpath);\r
 \r
     default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
-        @NonNull AnchorEntity anchorEntity, @NonNull String xpath) {\r
+                                                           @NonNull AnchorEntity anchorEntity,\r
+                                                           @NonNull String xpath) {\r
         return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath)\r
             .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath));\r
     }\r
 \r
+    @Query(\r
+        value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND dataspace_id = :dataspace AND parent_id is NULL",\r
+        nativeQuery = true)\r
+    List<FragmentEntity> findRootsByDataspaceAndAnchor(@Param("dataspace") int dataspaceId,\r
+                                                       @Param("anchor") int anchorId);\r
+\r
+    default FragmentEntity findFirstRootByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity,\r
+                                                             @NonNull AnchorEntity anchorEntity) {\r
+        return findRootsByDataspaceAndAnchor(dataspaceEntity.getId(), anchorEntity.getId()).stream().findFirst()\r
+            .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName()));\r
+    }\r
+\r
+    List<FragmentEntity> findAllByAnchorAndXpathIn(@NonNull AnchorEntity anchorEntity,\r
+                                                   @NonNull Collection<String> xpath);\r
+\r
     @Modifying\r
     @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)")\r
     void deleteByAnchorIn(@NotNull @Param("anchors") Collection<AnchorEntity> anchorEntities);\r
-\r
-    @Query(value =\r
-        "SELECT * FROM FRAGMENT WHERE (anchor_id = :anchor) AND (xpath = (:xpath) OR xpath LIKE "\r
-            + "CONCAT(:xpath,'\\[@%]')) AND attributes @> jsonb_build_object(:leafName , :leafValue)",\r
-                nativeQuery = true)\r
-    // Above query will match an xpath with or without the index for a list [@key=value]\r
-    // and match anchor id, leaf name and leaf value\r
-    List<FragmentEntity> getByAnchorAndXpathAndLeafAttributes(@Param("anchor") int anchorId, @Param("xpath")\r
-        String xpathPrefix, @Param("leafName") String leafName, @Param("leafValue") Object leafValue);\r
-}
\ No newline at end of file
+}\r