Merge "Fetch fragment entities using recursive SQL query"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentRepository.java
1 /*\r
2  * ============LICENSE_START=======================================================\r
3  * Copyright (C) 2021-2023 Nordix Foundation.\r
4  * Modifications Copyright (C) 2020-2021 Bell Canada.\r
5  * Modifications Copyright (C) 2020-2021 Pantheon.tech.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  * SPDX-License-Identifier: Apache-2.0\r
20  * ============LICENSE_END=========================================================\r
21  */\r
22 \r
23 package org.onap.cps.spi.repository;\r
24 \r
25 import java.util.Collection;\r
26 import java.util.List;\r
27 import java.util.Optional;\r
28 import org.onap.cps.spi.entities.AnchorEntity;\r
29 import org.onap.cps.spi.entities.FragmentEntity;\r
30 import org.onap.cps.spi.entities.FragmentExtract;\r
31 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;\r
32 import org.springframework.data.jpa.repository.JpaRepository;\r
33 import org.springframework.data.jpa.repository.Modifying;\r
34 import org.springframework.data.jpa.repository.Query;\r
35 import org.springframework.data.repository.query.Param;\r
36 import org.springframework.stereotype.Repository;\r
37 \r
38 @Repository\r
39 public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>, FragmentRepositoryCpsPathQuery,\r
40         FragmentNativeRepository {\r
41 \r
42     Optional<FragmentEntity> findByAnchorAndXpath(AnchorEntity anchorEntity, String xpath);\r
43 \r
44     default FragmentEntity getByAnchorAndXpath(final AnchorEntity anchorEntity, final String xpath) {\r
45         return findByAnchorAndXpath(anchorEntity, xpath).orElseThrow(() ->\r
46             new DataNodeNotFoundException(anchorEntity.getDataspace().getName(), anchorEntity.getName(), xpath));\r
47     }\r
48 \r
49     boolean existsByAnchorId(int anchorId);\r
50 \r
51     @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor")\r
52     List<FragmentExtract> findAllExtractsByAnchor(@Param("anchor") AnchorEntity anchorEntity);\r
53 \r
54     @Modifying\r
55     @Query("DELETE FROM FragmentEntity WHERE anchor IN (:anchors)")\r
56     void deleteByAnchorIn(@Param("anchors") Collection<AnchorEntity> anchorEntities);\r
57 \r
58     @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor"\r
59         + " AND (xpath = :parentXpath OR xpath LIKE CONCAT(:parentXpath,'/%'))")\r
60     List<FragmentExtract> findByAnchorAndParentXpath(@Param("anchor") AnchorEntity anchorEntity,\r
61                                                      @Param("parentXpath") String parentXpath);\r
62 \r
63     @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
64         + " CAST(attributes AS TEXT) AS attributes"\r
65         + " FROM FRAGMENT WHERE anchor_id = :anchorId"\r
66         + " AND xpath ~ :xpathRegex",\r
67         nativeQuery = true)\r
68     List<FragmentExtract> quickFindWithDescendants(@Param("anchorId") int anchorId,\r
69                                                    @Param("xpathRegex") String xpathRegex);\r
70 \r
71     @Query("SELECT xpath FROM FragmentEntity WHERE anchor = :anchor AND xpath IN :xpaths")\r
72     List<String> findAllXpathByAnchorAndXpathIn(@Param("anchor") AnchorEntity anchorEntity,\r
73                                                 @Param("xpaths") Collection<String> xpaths);\r
74 \r
75     boolean existsByAnchorAndXpathStartsWith(AnchorEntity anchorEntity, String xpath);\r
76 \r
77     @Query("SELECT xpath FROM FragmentEntity WHERE anchor = :anchor AND parentId IS NULL")\r
78     List<String> findAllXpathByAnchorAndParentIdIsNull(@Param("anchor") AnchorEntity anchorEntity);\r
79 \r
80     @Query(value\r
81         = "WITH RECURSIVE parent_search AS ("\r
82         + "  SELECT id, 0 AS depth "\r
83         + "    FROM fragment "\r
84         + "   WHERE anchor_id = :anchorId AND xpath IN :xpaths "\r
85         + "   UNION "\r
86         + "  SELECT c.id, depth + 1 "\r
87         + "    FROM fragment c INNER JOIN parent_search p ON c.parent_id = p.id"\r
88         + "   WHERE depth <= (SELECT CASE WHEN :maxDepth = -1 THEN " + Integer.MAX_VALUE + " ELSE :maxDepth END) "\r
89         + ") "\r
90         + "SELECT f.id, anchor_id AS anchorId, xpath, f.parent_id AS parentId, CAST(attributes AS TEXT) AS attributes "\r
91         + "FROM fragment f INNER JOIN parent_search p ON f.id = p.id",\r
92         nativeQuery = true\r
93     )\r
94     List<FragmentExtract> findExtractsWithDescendants(@Param("anchorId") int anchorId,\r
95                                                       @Param("xpaths") Collection<String> xpaths,\r
96                                                       @Param("maxDepth") int maxDepth);\r
97 }\r