Editing of Nordix Licenses to ONAP guidelines
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentRepository.java
1 /*\r
2  * ============LICENSE_START=======================================================\r
3  * Copyright (C) 2020-201 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.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> {\r
42 \r
43     Optional<FragmentEntity> findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
44         @NonNull AnchorEntity anchorEntity, @NonNull String xpath);\r
45 \r
46     default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
47         @NonNull AnchorEntity anchorEntity, @NonNull String xpath) {\r
48         return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath)\r
49             .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath));\r
50     }\r
51 \r
52     @Query(\r
53         value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND dataspace_id = :dataspace AND parent_id is NULL",\r
54         nativeQuery = true)\r
55     List<FragmentEntity> findRootsByDataspaceAndAnchor(\r
56         @Param("dataspace") int dataspaceId, @Param("anchor") int anchorId);\r
57 \r
58     default FragmentEntity findFirstRootByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity,\r
59         @NonNull AnchorEntity anchorEntity) {\r
60         return findRootsByDataspaceAndAnchor(dataspaceEntity.getId(), anchorEntity.getId()).stream().findFirst()\r
61             .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName()));\r
62     }\r
63 \r
64     List<FragmentEntity> findAllByAnchorAndXpathIn(@NonNull AnchorEntity anchorEntity,\r
65         @NonNull Collection<String> xpath);\r
66 \r
67     @Modifying\r
68     @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)")\r
69     void deleteByAnchorIn(@NotNull @Param("anchors") Collection<AnchorEntity> anchorEntities);\r
70 \r
71     @Query(value =\r
72         "SELECT * FROM FRAGMENT WHERE (anchor_id = :anchor) AND (xpath = (:xpath) OR xpath LIKE "\r
73             + "CONCAT(:xpath,'\\[@%]')) AND attributes @> jsonb_build_object(:leafName , :leafValue)",\r
74         nativeQuery = true)\r
75     // Above query will match an xpath with or without the index for a list [@key=value] and match anchor id,\r
76     // leaf name and leaf value\r
77     List<FragmentEntity> getByAnchorAndXpathAndLeafAttributes(@Param("anchor") int anchorId, @Param("xpath")\r
78         String xpathPrefix, @Param("leafName") String leafName, @Param("leafValue") Object leafValue);\r
79 \r
80     @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND xpath LIKE CONCAT('%/',:descendantName)",\r
81         nativeQuery = true)\r
82     // Above query will match the anchor id and last descendant name\r
83     List<FragmentEntity> getByAnchorAndXpathEndsInDescendantName(@Param("anchor") int anchorId,\r
84                                                                  @Param("descendantName") String descendantName);\r
85 \r
86     @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND (xpath LIKE CONCAT('%/',:descendantName) OR "\r
87         + "xpath LIKE CONCAT('%/', :descendantName,'\\[@%]')) AND attributes @> :leafDataAsJson\\:\\:jsonb",\r
88         nativeQuery = true)\r
89     // Above query will match the anchor id, last descendant name and all parameters passed into leafDataASJson with the\r
90     // attribute values of the requested data node eg: {"leaf_name":"value", "another_leaf_name":"another value"}​​​​​​\r
91     List<FragmentEntity> getByAnchorAndDescendentNameAndLeafValues(@Param("anchor") int anchorId,\r
92         @Param("descendantName") String descendantName, @Param("leafDataAsJson") String leafDataAsJson);\r
93 }\r