Add another Child to a Fragment that has already at least one Child
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentRepository.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  *  Copyright (C) 2020 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.Optional;\r
26 import javax.validation.constraints.NotNull;\r
27 import org.checkerframework.checker.nullness.qual.NonNull;\r
28 import org.onap.cps.spi.entities.AnchorEntity;\r
29 import org.onap.cps.spi.entities.DataspaceEntity;\r
30 import org.onap.cps.spi.entities.FragmentEntity;\r
31 import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;\r
32 import org.springframework.data.jpa.repository.JpaRepository;\r
33 import org.springframework.data.jpa.repository.Modifying;\r
34 import org.springframework.data.jpa.repository.Query;\r
35 import org.springframework.data.repository.query.Param;\r
36 import org.springframework.stereotype.Repository;\r
37 \r
38 @Repository\r
39 public interface FragmentRepository extends JpaRepository<FragmentEntity, Long> {\r
40 \r
41     Optional<FragmentEntity> findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
42         @NonNull AnchorEntity anchorEntity, @NonNull String xpath);\r
43 \r
44     default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
45         @NonNull AnchorEntity anchorEntity, @NonNull String xpath) {\r
46         return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath)\r
47             .orElseThrow(() -> new NotFoundInDataspaceException(dataspaceEntity.getName(), xpath));\r
48     }\r
49 \r
50     @Modifying\r
51     @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)")\r
52     void deleteByAnchorIn(@NotNull @Param("anchors") Collection<AnchorEntity> anchorEntities);\r
53 \r
54 }