CPS-475 - Fix SQ codesmells introduced as aprt of CPS-887
[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.List;
27 import java.util.UUID;
28 import javax.persistence.EntityManager;
29 import javax.persistence.PersistenceContext;
30 import lombok.SneakyThrows;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.spi.model.ModuleReference;
33 import org.springframework.transaction.annotation.Transactional;
34
35 @Slf4j
36 @Transactional
37 public class ModuleReferenceRepositoryImpl implements ModuleReferenceQuery {
38
39     @PersistenceContext
40     private EntityManager entityManager;
41
42     @Override
43     @SneakyThrows
44     public Collection<ModuleReference> identifyNewModuleReferences(
45         final Collection<ModuleReference> moduleReferencesToCheck) {
46
47         if (moduleReferencesToCheck == null || moduleReferencesToCheck.isEmpty()) {
48             return Collections.emptyList();
49         }
50
51         final String tempTableName = "moduleReferencesToCheckTemp"
52             + UUID.randomUUID().toString().replace("-", "");
53
54         createTemporaryTable(tempTableName);
55         insertDataIntoTable(tempTableName, moduleReferencesToCheck);
56
57         return identifyNewModuleReferencesForCmHandle(tempTableName);
58     }
59
60     private void createTemporaryTable(final String tempTableName) {
61         final StringBuilder sqlStringBuilder = new StringBuilder("CREATE TEMPORARY TABLE " + tempTableName + "(");
62         sqlStringBuilder.append(" id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,");
63         sqlStringBuilder.append(" module_name varchar NOT NULL,");
64         sqlStringBuilder.append(" revision varchar NOT NULL");
65         sqlStringBuilder.append(");");
66
67         entityManager.createNativeQuery(sqlStringBuilder.toString()).executeUpdate();
68     }
69
70     private void insertDataIntoTable(final String tempTableName, final Collection<ModuleReference> moduleReferences) {
71         final StringBuilder sqlStringBuilder = new StringBuilder("INSERT INTO  " + tempTableName);
72         sqlStringBuilder.append(" (module_name, revision) ");
73         sqlStringBuilder.append(" VALUES ");
74
75         for (final ModuleReference moduleReference : moduleReferences) {
76             sqlStringBuilder.append("('");
77             sqlStringBuilder.append(moduleReference.getModuleName());
78             sqlStringBuilder.append("', '");
79             sqlStringBuilder.append(moduleReference.getRevision());
80             sqlStringBuilder.append("'),");
81         }
82
83         // replace last ',' with ';'
84         sqlStringBuilder.replace(sqlStringBuilder.length() - 1, sqlStringBuilder.length(), ";");
85
86         entityManager.createNativeQuery(sqlStringBuilder.toString()).executeUpdate();
87     }
88
89     private Collection<ModuleReference> identifyNewModuleReferencesForCmHandle(final String tempTableName) {
90         final String sql = String.format(
91             "SELECT %1$s.module_name, %1$s.revision"
92                 + " FROM %1$s LEFT JOIN yang_resource"
93                 + " ON yang_resource.module_name=%1$s.module_name"
94                 + " AND yang_resource.revision=%1$s.revision"
95                 + " WHERE yang_resource.module_name IS NULL;", tempTableName);
96
97         final List<Object[]> resultsAsObjects =
98             entityManager.createNativeQuery(sql).getResultList();
99
100         final List<ModuleReference> resultsAsModuleReferences = new ArrayList<>(resultsAsObjects.size());
101         for (final Object[] row : resultsAsObjects) {
102             resultsAsModuleReferences.add(new ModuleReference((String) row[0], (String) row[1]));
103         }
104
105         return resultsAsModuleReferences;
106     }
107 }