[CPS] RI: Code Refactoring
[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.FragmentEntity;\r
31 import org.onap.cps.spi.entities.FragmentExtract;\r
32 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;\r
33 import org.springframework.data.jpa.repository.JpaRepository;\r
34 import org.springframework.data.jpa.repository.Modifying;\r
35 import org.springframework.data.jpa.repository.Query;\r
36 import org.springframework.data.repository.query.Param;\r
37 import org.springframework.stereotype.Repository;\r
38 \r
39 @Repository\r
40 public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>, FragmentRepositoryCpsPathQuery {\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     @Query(value = "SELECT * FROM fragment WHERE xpath = ANY (:xpaths)", nativeQuery = true)\r
55     List<FragmentEntity> findAllByXpathIn(@Param("xpaths") String[] xpath);\r
56 \r
57     default List<FragmentEntity> findAllByXpathIn(final Collection<String> xpaths) {\r
58         return findAllByXpathIn(xpaths.toArray(new String[0]));\r
59     }\r
60 \r
61     @Modifying\r
62     @Query(value = "DELETE FROM fragment WHERE anchor_id = ANY (:anchorIds)", nativeQuery = true)\r
63     void deleteByAnchorIdIn(@Param("anchorIds") int[] anchorIds);\r
64 \r
65     default void deleteByAnchorIn(final Collection<AnchorEntity> anchorEntities) {\r
66         deleteByAnchorIdIn(anchorEntities.stream().map(AnchorEntity::getId).mapToInt(id -> id).toArray());\r
67     }\r
68 \r
69     @Modifying\r
70     @Query(value = "DELETE FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)", nativeQuery = true)\r
71     void deleteByAnchorIdAndXpaths(@Param("anchorId") int anchorId, @Param("xpaths") String[] xpaths);\r
72 \r
73     default void deleteByAnchorIdAndXpaths(final int anchorId, final Collection<String> xpaths) {\r
74         deleteByAnchorIdAndXpaths(anchorId, xpaths.toArray(new String[0]));\r
75     }\r
76 \r
77     @Modifying\r
78     @Query(value = "DELETE FROM fragment f WHERE anchor_id = :anchorId AND xpath LIKE ANY (:xpathPatterns)",\r
79         nativeQuery = true)\r
80     void deleteByAnchorIdAndXpathLikeAny(@Param("anchorId") int anchorId,\r
81                                          @Param("xpathPatterns") String[] xpathPatterns);\r
82 \r
83     default void deleteListsByAnchorIdAndXpaths(int anchorId, Collection<String> xpaths) {\r
84         final String[] listXpathPatterns = xpaths.stream().map(xpath -> xpath + "[%").toArray(String[]::new);\r
85         deleteByAnchorIdAndXpathLikeAny(anchorId, listXpathPatterns);\r
86     }\r
87 \r
88     @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor"\r
89         + " AND (xpath = :parentXpath OR xpath LIKE CONCAT(:parentXpath,'/%'))")\r
90     List<FragmentExtract> findByAnchorAndParentXpath(@Param("anchor") AnchorEntity anchorEntity,\r
91                                                      @Param("parentXpath") String parentXpath);\r
92 \r
93     @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
94         + " CAST(attributes AS TEXT) AS attributes"\r
95         + " FROM FRAGMENT WHERE anchor_id = :anchorId"\r
96         + " AND xpath ~ :xpathRegex",\r
97         nativeQuery = true)\r
98     List<FragmentExtract> quickFindWithDescendants(@Param("anchorId") int anchorId,\r
99                                                    @Param("xpathRegex") String xpathRegex);\r
100 \r
101     @Query(value = "SELECT xpath FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)",\r
102         nativeQuery = true)\r
103     List<String> findAllXpathByAnchorIdAndXpathIn(@Param("anchorId") int anchorId,\r
104                                                   @Param("xpaths") String[] xpaths);\r
105 \r
106     default List<String> findAllXpathByAnchorAndXpathIn(final AnchorEntity anchorEntity,\r
107                                                         final Collection<String> xpaths) {\r
108         return findAllXpathByAnchorIdAndXpathIn(anchorEntity.getId(), xpaths.toArray(new String[0]));\r
109     }\r
110 \r
111     boolean existsByAnchorAndXpathStartsWith(AnchorEntity anchorEntity, String xpath);\r
112 \r
113     @Query("SELECT xpath FROM FragmentEntity WHERE anchor = :anchor AND parentId IS NULL")\r
114     List<String> findAllXpathByAnchorAndParentIdIsNull(@Param("anchor") AnchorEntity anchorEntity);\r
115 \r
116     @Query(value\r
117         = "WITH RECURSIVE parent_search AS ("\r
118         + "  SELECT id, 0 AS depth "\r
119         + "    FROM fragment "\r
120         + "   WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths) "\r
121         + "   UNION "\r
122         + "  SELECT c.id, depth + 1 "\r
123         + "    FROM fragment c INNER JOIN parent_search p ON c.parent_id = p.id"\r
124         + "   WHERE depth <= (SELECT CASE WHEN :maxDepth = -1 THEN " + Integer.MAX_VALUE + " ELSE :maxDepth END) "\r
125         + ") "\r
126         + "SELECT f.id, anchor_id AS anchorId, xpath, f.parent_id AS parentId, CAST(attributes AS TEXT) AS attributes "\r
127         + "FROM fragment f INNER JOIN parent_search p ON f.id = p.id",\r
128         nativeQuery = true\r
129     )\r
130     List<FragmentExtract> findExtractsWithDescendants(@Param("anchorId") int anchorId,\r
131                                                       @Param("xpaths") String[] xpaths,\r
132                                                       @Param("maxDepth") int maxDepth);\r
133 \r
134     default List<FragmentExtract> findExtractsWithDescendants(final int anchorId, final Collection<String> xpaths,\r
135                                                               final int maxDepth) {\r
136         return findExtractsWithDescendants(anchorId, xpaths.toArray(new String[0]), maxDepth);\r
137     }\r
138 \r
139     @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
140             + " CAST(attributes AS TEXT) AS attributes"\r
141             + " FROM FRAGMENT WHERE xpath ~ :xpathRegex",\r
142             nativeQuery = true)\r
143     List<FragmentExtract> quickFindWithDescendantsAcrossAnchor(@Param("xpathRegex") String xpathRegex);\r
144 }\r