Fix for recursive SQL returning extra level of descendants
[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  * Modifications Copyright (C) 2023 TechMahindra Ltd.\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  *\r
20  * SPDX-License-Identifier: Apache-2.0\r
21  * ============LICENSE_END=========================================================\r
22  */\r
23 \r
24 package org.onap.cps.spi.repository;\r
25 \r
26 import java.util.Collection;\r
27 import java.util.List;\r
28 import java.util.Optional;\r
29 import org.onap.cps.spi.entities.AnchorEntity;\r
30 import org.onap.cps.spi.entities.DataspaceEntity;\r
31 import org.onap.cps.spi.entities.FragmentEntity;\r
32 import org.onap.cps.spi.entities.FragmentExtract;\r
33 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;\r
34 import org.springframework.data.jpa.repository.JpaRepository;\r
35 import org.springframework.data.jpa.repository.Modifying;\r
36 import org.springframework.data.jpa.repository.Query;\r
37 import org.springframework.data.repository.query.Param;\r
38 import org.springframework.stereotype.Repository;\r
39 \r
40 @Repository\r
41 public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>, FragmentRepositoryCpsPathQuery {\r
42 \r
43     Optional<FragmentEntity> findByAnchorAndXpath(AnchorEntity anchorEntity, String xpath);\r
44 \r
45     default FragmentEntity getByAnchorAndXpath(final AnchorEntity anchorEntity, final String xpath) {\r
46         return findByAnchorAndXpath(anchorEntity, xpath).orElseThrow(() ->\r
47             new DataNodeNotFoundException(anchorEntity.getDataspace().getName(), anchorEntity.getName(), xpath));\r
48     }\r
49 \r
50     List<FragmentEntity> findByAnchorIdAndXpathIn(long anchorId, String[] xpaths);\r
51 \r
52     default List<FragmentEntity> findByAnchorAndXpathIn(final AnchorEntity anchorEntity,\r
53                                                         final Collection<String> xpaths) {\r
54         return findByAnchorIdAndXpathIn(anchorEntity.getId(), xpaths.toArray(new String[0]));\r
55     }\r
56 \r
57     @Query(value = "SELECT fragment.* FROM fragment JOIN anchor ON anchor.id = fragment.anchor_id "\r
58         + "WHERE dataspace_id = :dataspaceId AND xpath = ANY (:xpaths)", nativeQuery = true)\r
59     List<FragmentEntity> findByDataspaceIdAndXpathIn(@Param("dataspaceId") int dataspaceId,\r
60                                                      @Param("xpaths") String[] xpaths);\r
61 \r
62     default List<FragmentEntity> findByDataspaceAndXpathIn(final DataspaceEntity dataspaceEntity,\r
63                                                            final Collection<String> xpaths) {\r
64         return findByDataspaceIdAndXpathIn(dataspaceEntity.getId(), xpaths.toArray(new String[0]));\r
65     }\r
66 \r
67     boolean existsByAnchorId(long anchorId);\r
68 \r
69     @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor")\r
70     List<FragmentExtract> findAllExtractsByAnchor(@Param("anchor") AnchorEntity anchorEntity);\r
71 \r
72     @Modifying\r
73     @Query(value = "DELETE FROM fragment WHERE anchor_id = ANY (:anchorIds)", nativeQuery = true)\r
74     void deleteByAnchorIdIn(@Param("anchorIds") long[] anchorIds);\r
75 \r
76     default void deleteByAnchorIn(final Collection<AnchorEntity> anchorEntities) {\r
77         deleteByAnchorIdIn(anchorEntities.stream().map(AnchorEntity::getId).mapToLong(id -> id).toArray());\r
78     }\r
79 \r
80     @Modifying\r
81     @Query(value = "DELETE FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)", nativeQuery = true)\r
82     void deleteByAnchorIdAndXpaths(@Param("anchorId") long anchorId, @Param("xpaths") String[] xpaths);\r
83 \r
84     default void deleteByAnchorIdAndXpaths(final long anchorId, final Collection<String> xpaths) {\r
85         deleteByAnchorIdAndXpaths(anchorId, xpaths.toArray(new String[0]));\r
86     }\r
87 \r
88     @Modifying\r
89     @Query(value = "DELETE FROM fragment f WHERE anchor_id = :anchorId AND xpath LIKE ANY (:xpathPatterns)",\r
90         nativeQuery = true)\r
91     void deleteByAnchorIdAndXpathLikeAny(@Param("anchorId") long anchorId,\r
92                                          @Param("xpathPatterns") String[] xpathPatterns);\r
93 \r
94     default void deleteListsByAnchorIdAndXpaths(long anchorId, Collection<String> xpaths) {\r
95         final String[] listXpathPatterns = xpaths.stream().map(xpath -> xpath + "[%").toArray(String[]::new);\r
96         deleteByAnchorIdAndXpathLikeAny(anchorId, listXpathPatterns);\r
97     }\r
98 \r
99     @Query(value = "SELECT xpath FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)",\r
100         nativeQuery = true)\r
101     List<String> findAllXpathByAnchorIdAndXpathIn(@Param("anchorId") long anchorId,\r
102                                                   @Param("xpaths") String[] xpaths);\r
103 \r
104     default List<String> findAllXpathByAnchorAndXpathIn(final AnchorEntity anchorEntity,\r
105                                                         final Collection<String> xpaths) {\r
106         return findAllXpathByAnchorIdAndXpathIn(anchorEntity.getId(), xpaths.toArray(new String[0]));\r
107     }\r
108 \r
109     boolean existsByAnchorAndXpathStartsWith(AnchorEntity anchorEntity, String xpath);\r
110 \r
111     @Query("SELECT xpath FROM FragmentEntity WHERE anchor = :anchor AND parentId IS NULL")\r
112     List<String> findAllXpathByAnchorAndParentIdIsNull(@Param("anchor") AnchorEntity anchorEntity);\r
113 \r
114     @Query(value\r
115         = "WITH RECURSIVE parent_search AS ("\r
116         + "  SELECT id, 0 AS depth "\r
117         + "    FROM fragment "\r
118         + "   WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths) "\r
119         + "   UNION "\r
120         + "  SELECT c.id, depth + 1 "\r
121         + "    FROM fragment c INNER JOIN parent_search p ON c.parent_id = p.id"\r
122         + "   WHERE depth < (SELECT CASE WHEN :maxDepth = -1 THEN " + Integer.MAX_VALUE + " ELSE :maxDepth END) "\r
123         + ") "\r
124         + "SELECT f.id, anchor_id AS anchorId, xpath, f.parent_id AS parentId, CAST(attributes AS TEXT) AS attributes "\r
125         + "FROM fragment f INNER JOIN parent_search p ON f.id = p.id",\r
126         nativeQuery = true\r
127     )\r
128     List<FragmentExtract> findExtractsWithDescendants(@Param("anchorId") long anchorId,\r
129                                                       @Param("xpaths") String[] xpaths,\r
130                                                       @Param("maxDepth") int maxDepth);\r
131 \r
132     default List<FragmentExtract> findExtractsWithDescendants(final long anchorId, final Collection<String> xpaths,\r
133                                                               final int maxDepth) {\r
134         return findExtractsWithDescendants(anchorId, xpaths.toArray(new String[0]), maxDepth);\r
135     }\r
136 \r
137     @Query(value\r
138         = "WITH RECURSIVE parent_search AS ("\r
139         + "  SELECT id, 0 AS depth "\r
140         + "    FROM fragment "\r
141         + "   WHERE id = ANY (:ids) "\r
142         + "   UNION "\r
143         + "  SELECT c.id, depth + 1 "\r
144         + "    FROM fragment c INNER JOIN parent_search p ON c.parent_id = p.id"\r
145         + "   WHERE depth < (SELECT CASE WHEN :maxDepth = -1 THEN " + Integer.MAX_VALUE + " ELSE :maxDepth END) "\r
146         + ") "\r
147         + "SELECT f.id, anchor_id AS anchorId, xpath, f.parent_id AS parentId, CAST(attributes AS TEXT) AS attributes "\r
148         + "FROM fragment f INNER JOIN parent_search p ON f.id = p.id",\r
149         nativeQuery = true\r
150     )\r
151     List<FragmentExtract> findExtractsWithDescendantsByIds(@Param("ids") long[] ids,\r
152                                                            @Param("maxDepth") int maxDepth);\r
153 \r
154     default List<FragmentExtract> findExtractsWithDescendantsByIds(final Collection<Long> ids, final int maxDepth) {\r
155         return findExtractsWithDescendantsByIds(ids.stream().mapToLong(id -> id).toArray(), maxDepth);\r
156     }\r
157 \r
158 }\r