From 1bf7452b11e321ad3fb715cc160621f749c4e849 Mon Sep 17 00:00:00 2001
From: Domonkos Gulyas <domonkos.gulyas@ess.edu>
Date: Wed, 8 May 2024 09:32:49 +0200
Subject: [PATCH] CE-2748: Fix IOC instance generation error

---
 .../repository/api/IIocInstanceRepository.java       |  2 +-
 .../repository/impl/IocInstanceRepository.java       | 12 +++++++-----
 .../internal/iocinstance/IocInstanceService.java     |  3 ++-
 3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java b/src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java
index f5081e45..6e4fca35 100644
--- a/src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java
+++ b/src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java
@@ -54,5 +54,5 @@ public interface IIocInstanceRepository {
   @Transactional
   void deleteInstances(long typeId, long gitRepoId, String configName);
 
-  Long findOutputProjectForTypeAndConfig(String iocTypeName, String configurationName);
+  Long findOutputProjectForTypeAndConfig(long typeId, String configurationName);
 }
diff --git a/src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java b/src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java
index ca818589..4ae3647b 100644
--- a/src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java
+++ b/src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java
@@ -277,14 +277,16 @@ public class IocInstanceRepository implements IIocInstanceRepository {
   }
 
   @Override
-  public Long findOutputProjectForTypeAndConfig(String iocTypeName, String configurationName) {
+  public Long findOutputProjectForTypeAndConfig(long typeId, String configurationName) {
     return em
         .createQuery(
-            "SELECT i.gitProjectId FROM IocInstanceEntity i "
-                + "WHERE i.iocTypeName = :iocTypeName AND i.configurationName = :configurationName "
-                + "AND i.gitProjectId IS NOT NULL ORDER BY i.createdAt DESC",
+            "SELECT instance.gitProjectId FROM IocInstanceEntity instance "
+                + "WHERE instance.operation.id IN "
+                + "(SELECT operation.id FROM OperationEntity operation WHERE operation.iocType.id = :iocTypeId) "
+                + "AND instance.configurationName = :configurationName "
+                + "AND instance.gitProjectId IS NOT NULL ORDER BY instance.createdAt DESC",
             Long.class)
-        .setParameter("iocTypeName", iocTypeName)
+        .setParameter("iocTypeId", typeId)
         .setParameter("configurationName", configurationName)
         .getResultList()
         .stream()
diff --git a/src/main/java/eu/ess/ics/ce/template/service/internal/iocinstance/IocInstanceService.java b/src/main/java/eu/ess/ics/ce/template/service/internal/iocinstance/IocInstanceService.java
index 479d2860..82efd87d 100644
--- a/src/main/java/eu/ess/ics/ce/template/service/internal/iocinstance/IocInstanceService.java
+++ b/src/main/java/eu/ess/ics/ce/template/service/internal/iocinstance/IocInstanceService.java
@@ -347,7 +347,8 @@ public class IocInstanceService {
 
       Long latestOutputProjectId =
           iocInstanceRepository.findOutputProjectForTypeAndConfig(
-              iocInstanceEntity.getIocTypeName(), iocInstanceEntity.getConfigurationName());
+              iocInstanceEntity.getOperation().getIocType().getId(),
+              iocInstanceEntity.getConfigurationName());
 
       final OutputProjectDto outputProjectDto =
           gitLabService.createOrGetOutputProject(iocInstanceName, latestOutputProjectId);
-- 
GitLab