Update Model to allow Persisting of alternateId
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / YangResourceRepository.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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.List;
25 import java.util.Set;
26 import org.onap.cps.spi.entities.YangResourceEntity;
27 import org.onap.cps.spi.entities.YangResourceModuleReference;
28 import org.springframework.data.jpa.repository.JpaRepository;
29 import org.springframework.data.jpa.repository.Modifying;
30 import org.springframework.data.jpa.repository.Query;
31 import org.springframework.data.repository.query.Param;
32 import org.springframework.stereotype.Repository;
33
34 @Repository
35 public interface YangResourceRepository extends JpaRepository<YangResourceEntity, Integer>,
36     YangResourceNativeRepository, SchemaSetYangResourceRepository {
37
38     List<YangResourceEntity> findAllByChecksumIn(String[] checksums);
39
40     default List<YangResourceEntity> findAllByChecksumIn(final Collection<String> checksums) {
41         return findAllByChecksumIn(checksums.toArray(new String[0]));
42     }
43
44     @Query(value = """
45             SELECT DISTINCT
46                 yang_resource.module_name AS module_name,
47                 yang_resource.revision    AS revision
48             FROM
49                      dataspace
50                 JOIN schema_set ON schema_set.dataspace_id = dataspace.id
51                 JOIN schema_set_yang_resources ON schema_set_yang_resources.schema_set_id = schema_set.id
52                 JOIN yang_resource ON yang_resource.id = schema_set_yang_resources.yang_resource_id
53             WHERE
54                 dataspace.name = :dataspaceName
55             """, nativeQuery = true)
56     Set<YangResourceModuleReference> findAllModuleReferencesByDataspace(@Param("dataspaceName") String dataspaceName);
57
58     @Query(value = """
59             SELECT DISTINCT
60                 yang_resource.module_name AS module_name,
61                 yang_resource.revision    AS revision
62             FROM
63                      dataspace
64                 JOIN anchor ON anchor.dataspace_id = dataspace.id
65                 JOIN schema_set ON schema_set.id = anchor.schema_set_id
66                 JOIN schema_set_yang_resources ON schema_set_yang_resources.schema_set_id = schema_set.id
67                 JOIN yang_resource ON yang_resource.id = schema_set_yang_resources.yang_resource_id
68             WHERE
69                     dataspace.name = :dataspaceName
70                 AND anchor.name = :anchorName
71             """, nativeQuery = true)
72     Set<YangResourceModuleReference> findAllModuleReferencesByDataspaceAndAnchor(
73         @Param("dataspaceName") String dataspaceName, @Param("anchorName") String anchorName);
74
75     @Query(value = """
76             SELECT DISTINCT
77                 yang_resource.*
78             FROM
79                      dataspace
80                 JOIN anchor ON anchor.dataspace_id = dataspace.id
81                 JOIN schema_set ON schema_set.id = anchor.schema_set_id
82                 JOIN schema_set_yang_resources ON schema_set_yang_resources.schema_set_id = schema_set.id
83                 JOIN yang_resource ON yang_resource.id = schema_set_yang_resources.yang_resource_id
84             WHERE
85                     dataspace.name = :dataspaceName
86                 AND anchor.name = :anchorName
87             """, nativeQuery = true)
88     Set<YangResourceEntity> findAllModuleDefinitionsByDataspaceAndAnchor(
89             @Param("dataspaceName") String dataspaceName, @Param("anchorName") String anchorName);
90
91     @Query(value = """
92             SELECT DISTINCT
93                 yang_resource.*
94             FROM
95                      dataspace
96                 JOIN schema_set ON schema_set.dataspace_id = dataspace.id
97                 JOIN schema_set_yang_resources ON schema_set_yang_resources.schema_set_id = schema_set.id
98                 JOIN yang_resource ON yang_resource.id = schema_set_yang_resources.yang_resource_id
99             WHERE
100                     dataspace.name = :dataspaceName
101                 AND yang_resource.module_name = ANY ( :moduleNames )
102             """, nativeQuery = true)
103     Set<YangResourceModuleReference> findAllModuleReferencesByDataspaceAndModuleNames(
104             @Param("dataspaceName") String dataspaceName, @Param("moduleNames") String[] moduleNames);
105
106     default Set<YangResourceModuleReference> findAllModuleReferencesByDataspaceAndModuleNames(
107         final String dataspaceName, final Collection<String> moduleNames) {
108         return findAllModuleReferencesByDataspaceAndModuleNames(dataspaceName, moduleNames.toArray(new String[0]));
109     }
110
111     @Modifying
112     @Query(value = "DELETE FROM yang_resource yr WHERE NOT EXISTS "
113         + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true)
114     void deleteOrphans();
115 }