c84ff427e6ac2e5674ee40fdce0373d41c938327
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / YangResourceNativeRepositoryImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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 jakarta.persistence.EntityManager;
24 import jakarta.persistence.PersistenceContext;
25 import jakarta.persistence.Query;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.StringJoiner;
30 import lombok.extern.slf4j.Slf4j;
31 import org.hibernate.type.StandardBasicTypes;
32 import org.onap.cps.spi.model.ModuleReference;
33 import org.springframework.stereotype.Repository;
34 import org.springframework.transaction.annotation.Transactional;
35
36 @Slf4j
37 @Repository
38 public class YangResourceNativeRepositoryImpl implements YangResourceNativeRepository {
39
40     @PersistenceContext
41     private EntityManager entityManager;
42
43     @Override
44     @Transactional
45     public List<Integer> getResourceIdsByModuleReferences(final Collection<ModuleReference> moduleReferences) {
46         if (moduleReferences.isEmpty()) {
47             return Collections.emptyList();
48         }
49         final Query query = entityManager.createNativeQuery(getCombinedSelectSqlQuery(moduleReferences))
50             .unwrap(org.hibernate.query.NativeQuery.class)
51             .addScalar("id", StandardBasicTypes.INTEGER);
52         final List<Integer> yangResourceIds = query.getResultList();
53         if (yangResourceIds.size() != moduleReferences.size()) {
54             log.warn("ModuleReferences size : {} and QueryResult size : {}", moduleReferences.size(),
55                     yangResourceIds.size());
56         }
57         return yangResourceIds;
58     }
59
60     private String getCombinedSelectSqlQuery(final Collection<ModuleReference> moduleReferences) {
61         final StringJoiner sqlQueryJoiner = new StringJoiner(" UNION ALL ");
62         moduleReferences.forEach(moduleReference ->
63             sqlQueryJoiner.add(String.format("SELECT id FROM yang_resource WHERE module_name='%s' and revision='%s'",
64                 moduleReference.getModuleName(),
65                 moduleReference.getRevision()))
66         );
67         return sqlQueryJoiner.toString();
68     }
69 }