\r
package org.onap.cps.spi.repository;\r
\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<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] and match anchor id,\r
+ // 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
+\r
+ @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND xpath LIKE %:xpath", nativeQuery = true)\r
+ // Above query will match the end of an xpath and anchor id\r
+ List<FragmentEntity> getByAnchorAndEndsWithXpath(@Param("anchor") int anchorId, @Param("xpath") String xpath);\r
}
\ No newline at end of file