eb2e82b6c9b955d5bce14e8e69d0d20473c42a7d
[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 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     @Modifying\r
52     @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)")\r
53     void deleteByAnchorIn(@NotNull @Param("anchors") Collection<AnchorEntity> anchorEntities);\r
54 \r
55     @Query(value =\r
56         "SELECT * FROM FRAGMENT WHERE (anchor_id = :anchor) AND (xpath = (:xpath) OR xpath LIKE "\r
57             + "CONCAT(:xpath,'\\[@%]')) AND attributes @> jsonb_build_object(:leafName , :leafValue)",\r
58         nativeQuery = true)\r
59     // Above query will match an xpath with or without the index for a list [@key=value] and match anchor id,\r
60     // leaf name and leaf value\r
61     List<FragmentEntity> getByAnchorAndXpathAndLeafAttributes(@Param("anchor") int anchorId, @Param("xpath")\r
62         String xpathPrefix, @Param("leafName") String leafName, @Param("leafValue") Object leafValue);\r
63 \r
64     @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND xpath LIKE CONCAT('%/',:descendantName)",\r
65         nativeQuery = true)\r
66     // Above query will match the anchor id and last descendant name\r
67     List<FragmentEntity> getByAnchorAndXpathEndsInDescendantName(@Param("anchor") int anchorId,\r
68                                                                  @Param("descendantName") String descendantName);\r
69 }\r