Revert "Migrate CPS to Spring-boot 3.0"
[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.exceptions.DataNodeNotFoundException;\r
33 import org.onap.cps.spi.utils.EscapeUtils;\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         FragmentPrefetchRepository {\r
43 \r
44     Optional<FragmentEntity> findByAnchorAndXpath(AnchorEntity anchorEntity, String xpath);\r
45 \r
46     default FragmentEntity getByAnchorAndXpath(final AnchorEntity anchorEntity, final String xpath) {\r
47         return findByAnchorAndXpath(anchorEntity, xpath).orElseThrow(() ->\r
48             new DataNodeNotFoundException(anchorEntity.getDataspace().getName(), anchorEntity.getName(), xpath));\r
49     }\r
50 \r
51     @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)",\r
52             nativeQuery = true)\r
53     List<FragmentEntity> findByAnchorIdAndXpathIn(@Param("anchorId") long anchorId,\r
54                                                   @Param("xpaths") String[] xpaths);\r
55 \r
56     default List<FragmentEntity> findByAnchorAndXpathIn(final AnchorEntity anchorEntity,\r
57                                                         final Collection<String> xpaths) {\r
58         return findByAnchorIdAndXpathIn(anchorEntity.getId(), xpaths.toArray(new String[0]));\r
59     }\r
60 \r
61     @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId \n"\r
62             + "AND xpath LIKE :escapedXpath||'[@%]' AND xpath NOT LIKE :escapedXpath||'[@%]/%[@%]'",\r
63             nativeQuery = true)\r
64     List<FragmentEntity> findListByAnchorIdAndEscapedXpath(@Param("anchorId") long anchorId,\r
65                                                            @Param("escapedXpath") String escapedXpath);\r
66 \r
67     default List<FragmentEntity> findListByAnchorAndXpath(final AnchorEntity anchorEntity, final String xpath) {\r
68         final String escapedXpath = EscapeUtils.escapeForSqlLike(xpath);\r
69         return findListByAnchorIdAndEscapedXpath(anchorEntity.getId(), escapedXpath);\r
70     }\r
71 \r
72     @Query(value = "SELECT fragment.* FROM fragment JOIN anchor ON anchor.id = fragment.anchor_id "\r
73         + "WHERE dataspace_id = :dataspaceId AND xpath = ANY (:xpaths)", nativeQuery = true)\r
74     List<FragmentEntity> findByDataspaceIdAndXpathIn(@Param("dataspaceId") int dataspaceId,\r
75                                                      @Param("xpaths") String[] xpaths);\r
76 \r
77     default List<FragmentEntity> findByDataspaceAndXpathIn(final DataspaceEntity dataspaceEntity,\r
78                                                            final Collection<String> xpaths) {\r
79         return findByDataspaceIdAndXpathIn(dataspaceEntity.getId(), xpaths.toArray(new String[0]));\r
80     }\r
81 \r
82     @Query(value = "SELECT * FROM fragment WHERE anchor_id IN (:anchorIds)"\r
83             + " AND xpath = ANY (:xpaths)", nativeQuery = true)\r
84     List<FragmentEntity> findByAnchorIdsAndXpathIn(@Param("anchorIds") Long[] anchorIds,\r
85                                                    @Param("xpaths") String[] xpaths);\r
86 \r
87     @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId LIMIT 1", nativeQuery = true)\r
88     Optional<FragmentEntity> findOneByAnchorId(@Param("anchorId") long anchorId);\r
89 \r
90     @Modifying\r
91     @Query(value = "DELETE FROM fragment WHERE anchor_id = ANY (:anchorIds)", nativeQuery = true)\r
92     void deleteByAnchorIdIn(@Param("anchorIds") long[] anchorIds);\r
93 \r
94     default void deleteByAnchorIn(final Collection<AnchorEntity> anchorEntities) {\r
95         deleteByAnchorIdIn(anchorEntities.stream().map(AnchorEntity::getId).mapToLong(id -> id).toArray());\r
96     }\r
97 \r
98     @Modifying\r
99     @Query(value = "DELETE FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)", nativeQuery = true)\r
100     void deleteByAnchorIdAndXpaths(@Param("anchorId") long anchorId, @Param("xpaths") String[] xpaths);\r
101 \r
102     default void deleteByAnchorIdAndXpaths(final long anchorId, final Collection<String> xpaths) {\r
103         deleteByAnchorIdAndXpaths(anchorId, xpaths.toArray(new String[0]));\r
104     }\r
105 \r
106     @Modifying\r
107     @Query(value = "DELETE FROM fragment f WHERE anchor_id = :anchorId AND xpath LIKE ANY (:xpathPatterns)",\r
108         nativeQuery = true)\r
109     void deleteByAnchorIdAndXpathLikeAny(@Param("anchorId") long anchorId,\r
110                                          @Param("xpathPatterns") String[] xpathPatterns);\r
111 \r
112     default void deleteListsByAnchorIdAndXpaths(long anchorId, Collection<String> xpaths) {\r
113         deleteByAnchorIdAndXpathLikeAny(anchorId,\r
114                 xpaths.stream().map(xpath -> EscapeUtils.escapeForSqlLike(xpath) + "[@%").toArray(String[]::new));\r
115     }\r
116 \r
117     @Query(value = "SELECT xpath FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)",\r
118         nativeQuery = true)\r
119     List<String> findAllXpathByAnchorIdAndXpathIn(@Param("anchorId") long anchorId,\r
120                                                   @Param("xpaths") String[] xpaths);\r
121 \r
122     default List<String> findAllXpathByAnchorAndXpathIn(final AnchorEntity anchorEntity,\r
123                                                         final Collection<String> xpaths) {\r
124         return findAllXpathByAnchorIdAndXpathIn(anchorEntity.getId(), xpaths.toArray(new String[0]));\r
125     }\r
126 \r
127     boolean existsByAnchorAndXpathStartsWith(AnchorEntity anchorEntity, String xpath);\r
128 \r
129     @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId AND parent_id IS NULL", nativeQuery = true)\r
130     List<FragmentEntity> findRootsByAnchorId(@Param("anchorId") long anchorId);\r
131 \r
132 }\r