[CPS] Re-structuring the packages for better understanding
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / SchemaSetRepository.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Pantheon.tech
4  *  Modifications Copyright (C) 2022 TechMahindra Ltd.
5  *  Modifications Copyright (C) 2023 Nordix Foundation
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.repository;
23
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.stream.Collectors;
28 import javax.validation.constraints.NotNull;
29 import org.onap.cps.spi.entities.DataspaceEntity;
30 import org.onap.cps.spi.entities.SchemaSetEntity;
31 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
32 import org.springframework.data.jpa.repository.JpaRepository;
33 import org.springframework.data.jpa.repository.Modifying;
34 import org.springframework.data.jpa.repository.Query;
35 import org.springframework.data.repository.query.Param;
36 import org.springframework.stereotype.Repository;
37
38 @Repository
39 public interface SchemaSetRepository extends JpaRepository<SchemaSetEntity, Integer> {
40
41     Optional<SchemaSetEntity> findByDataspaceAndName(@NotNull DataspaceEntity dataspaceEntity,
42         @NotNull String schemaSetName);
43
44     /**
45      * Gets schema sets by dataspace.
46      * @param dataspaceEntity dataspace entity
47      * @return list of schema set entity
48      */
49     Collection<SchemaSetEntity> findByDataspace(@NotNull DataspaceEntity dataspaceEntity);
50
51     Integer countByDataspace(@NotNull DataspaceEntity dataspaceEntity);
52
53     /**
54      * Gets a schema set by dataspace and schema set name.
55      *
56      * @param dataspaceEntity dataspace entity
57      * @param schemaSetName   schema set name
58      * @return schema set entity
59      * @throws SchemaSetNotFoundException if SchemaSet not found
60      */
61     default SchemaSetEntity getByDataspaceAndName(@NotNull final DataspaceEntity dataspaceEntity,
62         @NotNull final String schemaSetName) {
63         return findByDataspaceAndName(dataspaceEntity, schemaSetName)
64             .orElseThrow(() -> new SchemaSetNotFoundException(dataspaceEntity.getName(), schemaSetName));
65     }
66
67     /**
68      * Gets all schema sets for a given dataspace.
69      *
70      * @param dataspaceEntity dataspace entity
71      * @return list of schema set entity
72      * @throws SchemaSetNotFoundException if SchemaSet not found
73      */
74     default List<SchemaSetEntity> getByDataspace(@NotNull final DataspaceEntity dataspaceEntity) {
75         return findByDataspace(dataspaceEntity).stream().collect(Collectors.toList());
76     }
77
78     /**
79      * Delete multiple schema sets in a given dataspace.
80      * @param dataspaceEntity dataspace entity
81      * @param schemaSetNames  schema set names
82      */
83     @Modifying
84     @Query("DELETE FROM SchemaSetEntity s WHERE s.dataspace = :dataspaceEntity AND s.name IN (:schemaSetNames)")
85     void deleteByDataspaceAndNameIn(@NotNull @Param("dataspaceEntity") final DataspaceEntity dataspaceEntity,
86                                     @NotNull @Param("schemaSetNames") final Collection<String> schemaSetNames);
87 }