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
9 * 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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.repository;
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.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;
39 public class ModuleReferenceRepositoryImpl implements ModuleReferenceQuery {
42 private EntityManager entityManager;
46 public Collection<ModuleReference> identifyNewModuleReferences(
47 final Collection<ModuleReference> moduleReferencesToCheck) {
49 if (moduleReferencesToCheck == null || moduleReferencesToCheck.isEmpty()) {
50 return Collections.emptyList();
53 final String tempTableName = "moduleReferencesToCheckTemp"
54 + UUID.randomUUID().toString().replace("-", "");
56 createTemporaryTable(tempTableName);
57 insertDataIntoTable(tempTableName, moduleReferencesToCheck);
59 return identifyNewModuleReferencesForCmHandle(tempTableName);
62 private void createTemporaryTable(final String tempTableName) {
63 final StringBuilder sqlStringBuilder = new StringBuilder("CREATE TEMPORARY TABLE " + tempTableName + "(");
64 sqlStringBuilder.append(" id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,");
65 sqlStringBuilder.append(" module_name varchar NOT NULL,");
66 sqlStringBuilder.append(" revision varchar NOT NULL");
67 sqlStringBuilder.append(");");
69 entityManager.createNativeQuery(sqlStringBuilder.toString()).executeUpdate();
72 private void insertDataIntoTable(final String tempTableName, final Collection<ModuleReference> moduleReferences) {
73 final StringBuilder sqlStringBuilder = new StringBuilder("INSERT INTO " + tempTableName);
74 sqlStringBuilder.append(" (module_name, revision) ");
75 sqlStringBuilder.append(" VALUES ");
77 for (final ModuleReference moduleReference : moduleReferences) {
78 sqlStringBuilder.append("('");
79 sqlStringBuilder.append(moduleReference.getModuleName());
80 sqlStringBuilder.append("', '");
81 sqlStringBuilder.append(moduleReference.getRevision());
82 sqlStringBuilder.append("'),");
85 // replace last ',' with ';'
86 sqlStringBuilder.replace(sqlStringBuilder.length() - 1, sqlStringBuilder.length(), ";");
88 entityManager.createNativeQuery(sqlStringBuilder.toString()).executeUpdate();
91 private Collection<ModuleReference> identifyNewModuleReferencesForCmHandle(final String tempTableName) {
92 final String sql = String.format(
93 "SELECT %1$s.module_name, %1$s.revision"
94 + " FROM %1$s LEFT JOIN yang_resource"
95 + " ON yang_resource.module_name=%1$s.module_name"
96 + " AND yang_resource.revision=%1$s.revision"
97 + " WHERE yang_resource.module_name IS NULL;", tempTableName);
99 @SuppressWarnings("unchecked")
100 final List<Object[]> resultsAsObjects = entityManager.createNativeQuery(sql).getResultList();
102 final List<ModuleReference> resultsAsModuleReferences = new ArrayList<>(resultsAsObjects.size());
103 for (final Object[] row : resultsAsObjects) {
104 resultsAsModuleReferences.add(new ModuleReference((String) row[0], (String) row[1]));
107 return resultsAsModuleReferences;