5870fd9e9b5c9ce5c29feba0096474c74e657757
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / AnchorRepository.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Pantheon.tech
4  *  Modifications Copyright (C) 2021 Nordix Foundation
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.spi.repository;
22
23 import java.util.Collection;
24 import java.util.Optional;
25 import javax.validation.constraints.NotNull;
26 import org.onap.cps.spi.entities.AnchorEntity;
27 import org.onap.cps.spi.entities.DataspaceEntity;
28 import org.onap.cps.spi.entities.SchemaSetEntity;
29 import org.onap.cps.spi.exceptions.AnchorNotFoundException;
30 import org.springframework.data.jpa.repository.JpaRepository;
31 import org.springframework.data.jpa.repository.Query;
32 import org.springframework.data.repository.query.Param;
33
34 public interface AnchorRepository extends JpaRepository<AnchorEntity, Integer> {
35
36     Optional<AnchorEntity> findByDataspaceAndName(@NotNull DataspaceEntity dataspaceEntity, @NotNull String name);
37
38     default AnchorEntity getByDataspaceAndName(@NotNull DataspaceEntity dataspace,
39         @NotNull String anchorName) {
40         return findByDataspaceAndName(dataspace, anchorName)
41             .orElseThrow(() -> new AnchorNotFoundException(anchorName, dataspace.getName()));
42     }
43
44     Collection<AnchorEntity> findAllByDataspace(@NotNull DataspaceEntity dataspaceEntity);
45
46     Collection<AnchorEntity> findAllBySchemaSet(@NotNull SchemaSetEntity schemaSetEntity);
47
48     @Query(value = "SELECT anchor.* FROM yang_resource\n"
49         + "JOIN schema_set_yang_resources ON schema_set_yang_resources.yang_resource_id = yang_resource.id\n"
50         + "JOIN schema_set ON schema_set.id = schema_set_yang_resources.schema_set_id\n"
51         + "JOIN anchor ON anchor.schema_set_id = schema_set.id\n"
52         + "WHERE schema_set.dataspace_id = :dataspaceId AND module_name IN (:moduleNames)\n"
53         + "GROUP BY anchor.id, anchor.name, anchor.dataspace_id, anchor.schema_set_id\n"
54         + "HAVING COUNT(DISTINCT module_name) = :sizeOfModuleNames", nativeQuery = true)
55     Collection<AnchorEntity> getAnchorsByDataspaceIdAndModuleNames(@Param("dataspaceId") int dataspaceId,
56         @Param("moduleNames") Collection<String> moduleNames, @Param("sizeOfModuleNames") int sizeOfModuleNames);
57 }