Merge "Schedule response to client"
[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-2023 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 org.onap.cps.spi.entities.AnchorEntity;
26 import org.onap.cps.spi.entities.DataspaceEntity;
27 import org.onap.cps.spi.entities.SchemaSetEntity;
28 import org.onap.cps.spi.exceptions.AnchorNotFoundException;
29 import org.springframework.data.jpa.repository.JpaRepository;
30 import org.springframework.data.jpa.repository.Modifying;
31 import org.springframework.data.jpa.repository.Query;
32 import org.springframework.data.repository.query.Param;
33 import org.springframework.stereotype.Repository;
34
35 @Repository
36 public interface AnchorRepository extends JpaRepository<AnchorEntity, Long> {
37
38     Optional<AnchorEntity> findByDataspaceAndName(DataspaceEntity dataspaceEntity, String name);
39
40     default AnchorEntity getByDataspaceAndName(DataspaceEntity dataspace, String anchorName) {
41         return findByDataspaceAndName(dataspace, anchorName)
42             .orElseThrow(() -> new AnchorNotFoundException(anchorName, dataspace.getName()));
43     }
44
45     Collection<AnchorEntity> findAllByDataspace(DataspaceEntity dataspaceEntity);
46
47     Collection<AnchorEntity> findAllBySchemaSet(SchemaSetEntity schemaSetEntity);
48
49     @Query(value = "SELECT * FROM anchor WHERE dataspace_id = :dataspaceId AND name = ANY (:anchorNames)",
50         nativeQuery = true)
51     Collection<AnchorEntity> findAllByDataspaceIdAndNameIn(@Param("dataspaceId") int dataspaceId,
52                                                            @Param("anchorNames") String[] anchorNames);
53
54     default Collection<AnchorEntity> findAllByDataspaceAndNameIn(final DataspaceEntity dataspaceEntity,
55                                                                  final Collection<String> anchorNames) {
56         return findAllByDataspaceIdAndNameIn(dataspaceEntity.getId(), anchorNames.toArray(new String[0]));
57     }
58
59     @Query(value = "SELECT a.* FROM anchor a"
60         + " LEFT OUTER JOIN schema_set s ON a.schema_set_id = s.id"
61         + " WHERE a.dataspace_id = :dataspaceId AND s.name = ANY (:schemaSetNames)",
62         nativeQuery = true)
63     Collection<AnchorEntity> findAllByDataspaceIdAndSchemaSetNameIn(@Param("dataspaceId") int dataspaceId,
64                                                                     @Param("schemaSetNames") String[] schemaSetNames);
65
66     default Collection<AnchorEntity> findAllByDataspaceAndSchemaSetNameIn(final DataspaceEntity dataspaceEntity,
67                                                                           final Collection<String> schemaSetNames) {
68         return findAllByDataspaceIdAndSchemaSetNameIn(dataspaceEntity.getId(), schemaSetNames.toArray(new String[0]));
69     }
70
71     Integer countByDataspace(DataspaceEntity dataspaceEntity);
72
73     @Query(value = """
74             SELECT
75                 anchor.name
76             FROM
77                      yang_resource
78                 JOIN schema_set_yang_resources ON schema_set_yang_resources.yang_resource_id = yang_resource.id
79                 JOIN schema_set ON schema_set.id = schema_set_yang_resources.schema_set_id
80                 JOIN anchor ON anchor.schema_set_id = schema_set.id
81             WHERE
82                     schema_set.dataspace_id = :dataspaceId
83                 AND module_name = ANY ( :moduleNames )
84             GROUP BY
85                 anchor.id,
86                 anchor.name,
87                 anchor.dataspace_id,
88                 anchor.schema_set_id
89             HAVING
90                 COUNT(DISTINCT module_name) = :sizeOfModuleNames
91             """, nativeQuery = true)
92     Collection<String> getAnchorNamesByDataspaceIdAndModuleNames(@Param("dataspaceId") int dataspaceId,
93                                                                  @Param("moduleNames") String[] moduleNames,
94                                                                  @Param("sizeOfModuleNames") int sizeOfModuleNames);
95
96     default Collection<String> getAnchorNamesByDataspaceIdAndModuleNames(final int dataspaceId,
97                                                                          final Collection<String> moduleNames,
98                                                                          final int sizeOfModuleNames) {
99         final String[] moduleNamesArray = moduleNames.toArray(new String[0]);
100         return getAnchorNamesByDataspaceIdAndModuleNames(dataspaceId, moduleNamesArray, sizeOfModuleNames);
101     }
102
103     @Modifying
104     @Query(value = "DELETE FROM anchor WHERE dataspace_id = :dataspaceId AND name = ANY (:anchorNames)",
105         nativeQuery = true)
106     void deleteAllByDataspaceIdAndNameIn(@Param("dataspaceId") int dataspaceId,
107                                          @Param("anchorNames") String[] anchorNames);
108
109     default void deleteAllByDataspaceAndNameIn(final DataspaceEntity dataspaceEntity,
110                                                final Collection<String> anchorNames) {
111         deleteAllByDataspaceIdAndNameIn(dataspaceEntity.getId(), anchorNames.toArray(new String[0]));
112     }
113
114     @Modifying
115     @Query(value = "UPDATE anchor SET schema_set_id =:schemaSetId WHERE id = :anchorId ", nativeQuery = true)
116     void updateAnchorSchemaSetId(@Param("schemaSetId") int schemaSetId, @Param("anchorId") long anchorId);
117
118 }