Revert "Migrate CPS to Spring-boot 3.0"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / YangResourceNativeRepositoryImpl.java
index e21fecb..f09a1a0 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2022 Nordix Foundation.
+ *  Copyright (C) 2022-2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 package org.onap.cps.spi.repository;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.StringJoiner;
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 import javax.persistence.Query;
+import lombok.extern.slf4j.Slf4j;
 import org.hibernate.type.StandardBasicTypes;
 import org.onap.cps.spi.model.ModuleReference;
 import org.springframework.stereotype.Repository;
 import org.springframework.transaction.annotation.Transactional;
 
+@Slf4j
 @Repository
 public class YangResourceNativeRepositoryImpl implements YangResourceNativeRepository {
 
@@ -39,20 +42,28 @@ public class YangResourceNativeRepositoryImpl implements YangResourceNativeRepos
 
     @Override
     @Transactional
-    public List<Long> getResourceIdsByModuleReferences(final Collection<ModuleReference> moduleReferences) {
+    public List<Integer> getResourceIdsByModuleReferences(final Collection<ModuleReference> moduleReferences) {
+        if (moduleReferences.isEmpty()) {
+            return Collections.emptyList();
+        }
         final Query query = entityManager.createNativeQuery(getCombinedSelectSqlQuery(moduleReferences))
             .unwrap(org.hibernate.query.NativeQuery.class)
-            .addScalar("id", StandardBasicTypes.LONG);
-        return query.getResultList();
+            .addScalar("id", StandardBasicTypes.INTEGER);
+        final List<Integer> yangResourceIds = query.getResultList();
+        if (yangResourceIds.size() != moduleReferences.size()) {
+            log.warn("ModuleReferences size : {} and QueryResult size : {}", moduleReferences.size(),
+                    yangResourceIds.size());
+        }
+        return yangResourceIds;
     }
 
     private String getCombinedSelectSqlQuery(final Collection<ModuleReference> moduleReferences) {
         final StringJoiner sqlQueryJoiner = new StringJoiner(" UNION ALL ");
-        moduleReferences.stream().forEach(moduleReference -> {
+        moduleReferences.forEach(moduleReference ->
             sqlQueryJoiner.add(String.format("SELECT id FROM yang_resource WHERE module_name='%s' and revision='%s'",
                 moduleReference.getModuleName(),
-                moduleReference.getRevision()));
-        });
+                moduleReference.getRevision()))
+        );
         return sqlQueryJoiner.toString();
     }
 }