Merge "Implement DMI Registration (NCMP-Side)"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentRepository.java
1 /*\r
2  * ============LICENSE_START=======================================================\r
3  * Copyright (C) 2020-2021 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>, FragmentRepositoryCpsPathQuery {\r
42 \r
43     Optional<FragmentEntity> findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
44                                                               @NonNull AnchorEntity anchorEntity,\r
45                                                               @NonNull String xpath);\r
46 \r
47     default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
48                                                            @NonNull AnchorEntity anchorEntity,\r
49                                                            @NonNull String xpath) {\r
50         return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath)\r
51             .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath));\r
52     }\r
53 \r
54     @Query(\r
55         value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND dataspace_id = :dataspace AND parent_id is NULL",\r
56         nativeQuery = true)\r
57     List<FragmentEntity> findRootsByDataspaceAndAnchor(@Param("dataspace") int dataspaceId,\r
58                                                        @Param("anchor") int anchorId);\r
59 \r
60     default FragmentEntity findFirstRootByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity,\r
61                                                              @NonNull AnchorEntity anchorEntity) {\r
62         return findRootsByDataspaceAndAnchor(dataspaceEntity.getId(), anchorEntity.getId()).stream().findFirst()\r
63             .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName()));\r
64     }\r
65 \r
66     List<FragmentEntity> findAllByAnchorAndXpathIn(@NonNull AnchorEntity anchorEntity,\r
67                                                    @NonNull Collection<String> xpath);\r
68 \r
69     @Modifying\r
70     @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)")\r
71     void deleteByAnchorIn(@NotNull @Param("anchors") Collection<AnchorEntity> anchorEntities);\r
72 }\r