Revert "Migrate CPS to Spring-boot 3.0"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / ModuleReferenceRepositoryImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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 java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import javax.persistence.EntityManager;
29 import javax.persistence.PersistenceContext;
30 import lombok.AllArgsConstructor;
31 import lombok.SneakyThrows;
32 import lombok.extern.slf4j.Slf4j;
33 import org.onap.cps.spi.model.ModuleReference;
34 import org.springframework.transaction.annotation.Transactional;
35
36 @Slf4j
37 @Transactional
38 @AllArgsConstructor
39 public class ModuleReferenceRepositoryImpl implements ModuleReferenceQuery {
40
41     @PersistenceContext
42     private EntityManager entityManager;
43
44     private TempTableCreator tempTableCreator;
45
46     @Override
47     @SneakyThrows
48     public Collection<ModuleReference> identifyNewModuleReferences(
49             final Collection<ModuleReference> moduleReferencesToCheck) {
50
51         if (moduleReferencesToCheck == null || moduleReferencesToCheck.isEmpty()) {
52             return Collections.emptyList();
53         }
54
55         final Collection<List<String>> sqlData = new HashSet<>(moduleReferencesToCheck.size());
56         for (final ModuleReference moduleReference : moduleReferencesToCheck) {
57             final List<String> row = new ArrayList<>(2);
58             row.add(moduleReference.getModuleName());
59             row.add(moduleReference.getRevision());
60             sqlData.add(row);
61         }
62
63         final String tempTableName = tempTableCreator.createTemporaryTable(
64             "moduleReferencesToCheckTemp", sqlData, "module_name", "revision");
65
66         return identifyNewModuleReferencesForCmHandle(tempTableName);
67     }
68
69     private Collection<ModuleReference> identifyNewModuleReferencesForCmHandle(final String tempTableName) {
70         final String sql = String.format(
71                 "SELECT %1$s.module_name, %1$s.revision"
72                         + " FROM %1$s LEFT JOIN yang_resource"
73                         + " ON yang_resource.module_name=%1$s.module_name"
74                         + " AND yang_resource.revision=%1$s.revision"
75                         + " WHERE yang_resource.module_name IS NULL;", tempTableName);
76
77         @SuppressWarnings("unchecked")
78         final List<Object[]> resultsAsObjects = entityManager.createNativeQuery(sql).getResultList();
79
80         final List<ModuleReference> resultsAsModuleReferences = new ArrayList<>(resultsAsObjects.size());
81         for (final Object[] row : resultsAsObjects) {
82             resultsAsModuleReferences.add(new ModuleReference((String) row[0], (String) row[1]));
83         }
84
85         return resultsAsModuleReferences;
86     }
87 }