Merge "Fetch CM handles by collection of xpaths"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentRepository.java
1 /*\r
2  * ============LICENSE_START=======================================================\r
3  * Copyright (C) 2021-2022 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 javax.validation.constraints.NotNull;\r
29 import org.checkerframework.checker.nullness.qual.NonNull;\r
30 import org.onap.cps.spi.entities.AnchorEntity;\r
31 import org.onap.cps.spi.entities.DataspaceEntity;\r
32 import org.onap.cps.spi.entities.FragmentEntity;\r
33 import org.onap.cps.spi.entities.FragmentExtract;\r
34 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;\r
35 import org.springframework.data.jpa.repository.JpaRepository;\r
36 import org.springframework.data.jpa.repository.Modifying;\r
37 import org.springframework.data.jpa.repository.Query;\r
38 import org.springframework.data.repository.query.Param;\r
39 import org.springframework.stereotype.Repository;\r
40 \r
41 @Repository\r
42 public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>, FragmentRepositoryCpsPathQuery,\r
43         FragmentRepositoryMultiPathQuery {\r
44 \r
45     Optional<FragmentEntity> findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
46                                                               @NonNull AnchorEntity anchorEntity,\r
47                                                               @NonNull String xpath);\r
48 \r
49     default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
50                                                            @NonNull AnchorEntity anchorEntity,\r
51                                                            @NonNull String xpath) {\r
52         return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath)\r
53             .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath));\r
54     }\r
55 \r
56     @Query(\r
57         value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND dataspace_id = :dataspace AND parent_id is NULL",\r
58         nativeQuery = true)\r
59     List<FragmentEntity> findRootsByDataspaceAndAnchor(@Param("dataspace") int dataspaceId,\r
60                                                        @Param("anchor") int anchorId);\r
61 \r
62     @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
63             + " CAST(attributes AS TEXT) AS attributes"\r
64             + " FROM FRAGMENT WHERE anchor_id = :anchorId",\r
65             nativeQuery = true)\r
66     List<FragmentExtract> findRootsByAnchorId(@Param("anchorId") int anchorId);\r
67 \r
68     /**\r
69      * find top level fragment by anchor.\r
70      *\r
71      * @param dataspaceEntity dataspace entity\r
72      * @param anchorEntity anchor entity\r
73      * @return FragmentEntity fragment entity\r
74      */\r
75     default List<FragmentExtract> getTopLevelFragments(DataspaceEntity dataspaceEntity,\r
76                                                        AnchorEntity anchorEntity) {\r
77         final List<FragmentExtract> fragmentExtracts = findRootsByAnchorId(anchorEntity.getId());\r
78         if (fragmentExtracts.isEmpty()) {\r
79             throw new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName());\r
80         }\r
81         return fragmentExtracts;\r
82     }\r
83 \r
84     @Modifying\r
85     @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)")\r
86     void deleteByAnchorIn(@NotNull @Param("anchors") Collection<AnchorEntity> anchorEntities);\r
87 \r
88     @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
89         + " CAST(attributes AS TEXT) AS attributes"\r
90         + " FROM FRAGMENT WHERE anchor_id = :anchorId"\r
91         + " AND ( xpath = :parentXpath OR xpath LIKE CONCAT(:parentXpath,'/%') )",\r
92            nativeQuery = true)\r
93     List<FragmentExtract> findByAnchorIdAndParentXpath(@Param("anchorId") int anchorId,\r
94                                                        @Param("parentXpath") String parentXpath);\r
95 \r
96     @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
97         + " CAST(attributes AS TEXT) AS attributes"\r
98         + " FROM FRAGMENT WHERE anchor_id = :anchorId"\r
99         + " AND xpath ~ :xpathRegex",\r
100         nativeQuery = true)\r
101     List<FragmentExtract> quickFindWithDescendants(@Param("anchorId") int anchorId,\r
102                                                    @Param("xpathRegex") String xpathRegex);\r
103 }\r