Persistence layer - Query Datanodes using cpsPath that contains contains a leaf name...
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentRepository.java
index ba83f15..a40168a 100755 (executable)
@@ -1,6 +1,7 @@
 /*-\r
  * ============LICENSE_START=======================================================\r
  *  Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.\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
 \r
 package org.onap.cps.spi.repository;\r
 \r
-import org.onap.cps.spi.entities.Fragment;\r
+import java.util.Collection;\r
+import java.util.List;\r
+import java.util.Optional;\r
+import javax.validation.constraints.NotNull;\r
+import org.checkerframework.checker.nullness.qual.NonNull;\r
+import org.onap.cps.spi.entities.AnchorEntity;\r
+import org.onap.cps.spi.entities.DataspaceEntity;\r
+import org.onap.cps.spi.entities.FragmentEntity;\r
+import org.onap.cps.spi.exceptions.DataNodeNotFoundException;\r
 import org.springframework.data.jpa.repository.JpaRepository;\r
+import org.springframework.data.jpa.repository.Modifying;\r
+import org.springframework.data.jpa.repository.Query;\r
+import org.springframework.data.repository.query.Param;\r
 import org.springframework.stereotype.Repository;\r
 \r
 @Repository\r
-public interface FragmentRepository extends JpaRepository<Fragment, Integer> {\r
+public interface FragmentRepository extends JpaRepository<FragmentEntity, Long> {\r
+\r
+    Optional<FragmentEntity> findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
+        @NonNull AnchorEntity anchorEntity, @NonNull String xpath);\r
+\r
+    default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
+        @NonNull AnchorEntity anchorEntity, @NonNull String xpath) {\r
+        return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath)\r
+            .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath));\r
+    }\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