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
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.spi.repository;
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;
39 public interface SchemaSetRepository extends JpaRepository<SchemaSetEntity, Integer> {
41 Optional<SchemaSetEntity> findByDataspaceAndName(@NotNull DataspaceEntity dataspaceEntity,
42 @NotNull String schemaSetName);
45 * Gets schema sets by dataspace.
46 * @param dataspaceEntity dataspace entity
47 * @return list of schema set entity
49 Collection<SchemaSetEntity> findByDataspace(@NotNull DataspaceEntity dataspaceEntity);
51 Integer countByDataspace(@NotNull DataspaceEntity dataspaceEntity);
54 * Gets a schema set by dataspace and schema set name.
56 * @param dataspaceEntity dataspace entity
57 * @param schemaSetName schema set name
58 * @return schema set entity
59 * @throws SchemaSetNotFoundException if SchemaSet not found
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));
68 * Gets all schema sets for a given dataspace.
70 * @param dataspaceEntity dataspace entity
71 * @return list of schema set entity
72 * @throws SchemaSetNotFoundException if SchemaSet not found
74 default List<SchemaSetEntity> getByDataspace(@NotNull final DataspaceEntity dataspaceEntity) {
75 return findByDataspace(dataspaceEntity).stream().collect(Collectors.toList());
79 * Delete multiple schema sets in a given dataspace.
80 * @param dataspaceEntity dataspace entity
81 * @param schemaSetNames schema set names
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);