Merge "Addition of missing licenses to files"
[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. All rights reserved.\r
4  *  Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.\r
5  * ================================================================================\r
6  * Licensed under the Apache License, Version 2.0 (the "License");\r
7  * you may not use this file except in compliance with the License.\r
8  * You may obtain a copy of the License at\r
9  *\r
10  *      http://www.apache.org/licenses/LICENSE-2.0\r
11  *\r
12  * Unless required by applicable law or agreed to in writing, software\r
13  * distributed under the License is distributed on an "AS IS" BASIS,\r
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
15  * See the License for the specific language governing permissions and\r
16  * limitations under the License.\r
17  *\r
18  * SPDX-License-Identifier: Apache-2.0\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 \r
22 package org.onap.cps.spi.repository;\r
23 \r
24 import java.util.Collection;\r
25 import java.util.List;\r
26 import java.util.Optional;\r
27 import javax.validation.constraints.NotNull;\r
28 import org.checkerframework.checker.nullness.qual.NonNull;\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.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> {\r
41 \r
42     Optional<FragmentEntity> findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
43         @NonNull AnchorEntity anchorEntity, @NonNull String xpath);\r
44 \r
45     default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
46         @NonNull AnchorEntity anchorEntity, @NonNull String xpath) {\r
47         return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath)\r
48             .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath));\r
49     }\r
50 \r
51     @Query(\r
52         value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND dataspace_id = :dataspace AND parent_id is NULL",\r
53         nativeQuery = true)\r
54     List<FragmentEntity> findRootsByDataspaceAndAnchor(\r
55         @Param("dataspace") int dataspaceId, @Param("anchor") int anchorId);\r
56 \r
57     default FragmentEntity findFirstRootByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity,\r
58         @NonNull AnchorEntity anchorEntity) {\r
59         return findRootsByDataspaceAndAnchor(dataspaceEntity.getId(), anchorEntity.getId()).stream().findFirst()\r
60             .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName()));\r
61     }\r
62 \r
63     List<FragmentEntity> findAllByAnchorAndXpathIn(@NonNull AnchorEntity anchorEntity,\r
64         @NonNull Collection<String> xpath);\r
65 \r
66     @Modifying\r
67     @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)")\r
68     void deleteByAnchorIn(@NotNull @Param("anchors") Collection<AnchorEntity> anchorEntities);\r
69 \r
70     @Query(value =\r
71         "SELECT * FROM FRAGMENT WHERE (anchor_id = :anchor) AND (xpath = (:xpath) OR xpath LIKE "\r
72             + "CONCAT(:xpath,'\\[@%]')) AND attributes @> jsonb_build_object(:leafName , :leafValue)",\r
73         nativeQuery = true)\r
74     // Above query will match an xpath with or without the index for a list [@key=value] and match anchor id,\r
75     // leaf name and leaf value\r
76     List<FragmentEntity> getByAnchorAndXpathAndLeafAttributes(@Param("anchor") int anchorId, @Param("xpath")\r
77         String xpathPrefix, @Param("leafName") String leafName, @Param("leafValue") Object leafValue);\r
78 \r
79     @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND xpath LIKE CONCAT('%/',:descendantName)",\r
80         nativeQuery = true)\r
81     // Above query will match the anchor id and last descendant name\r
82     List<FragmentEntity> getByAnchorAndXpathEndsInDescendantName(@Param("anchor") int anchorId,\r
83                                                                  @Param("descendantName") String descendantName);\r
84 \r
85     @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND (xpath LIKE CONCAT('%/',:descendantName) OR "\r
86         + "xpath LIKE CONCAT('%/', :descendantName,'\\[@%]')) AND attributes @> :leafDataAsJson\\:\\:jsonb",\r
87         nativeQuery = true)\r
88     // Above query will match the anchor id, last descendant name and all parameters passed into leafDataASJson with the\r
89     // attribute values of the requested data node eg: {"leaf_name":"value", "another_leaf_name":"another value"}​​​​​​\r
90     List<FragmentEntity> getByAnchorAndDescendentNameAndLeafValues(@Param("anchor") int anchorId,\r
91         @Param("descendantName") String descendantName, @Param("leafDataAsJson") String leafDataAsJson);\r
92 }\r