From 20c86db6bcf125e4a771b4648096c63258faf6c6 Mon Sep 17 00:00:00 2001
From: Jan-Niclas Struewer <j.n.struewer@gmail.com>
Date: Mon, 30 Oct 2023 11:31:22 +0100
Subject: [PATCH] Added StartUpHandler to automatically add projects on startup

---
 .../iem/dataprovider/StartUpHandler.kt        | 30 +++++++++++++++++++
 .../configuration/InitialProjects.kt          | 29 ++++++++++++++++++
 src/main/resources/application-dev.properties |  1 +
 .../resources/application-local.properties    |  1 +
 .../resources/application-prod.properties     |  1 +
 5 files changed, 62 insertions(+)
 create mode 100644 src/main/kotlin/de/fraunhofer/iem/dataprovider/StartUpHandler.kt
 create mode 100644 src/main/kotlin/de/fraunhofer/iem/dataprovider/configuration/InitialProjects.kt

diff --git a/src/main/kotlin/de/fraunhofer/iem/dataprovider/StartUpHandler.kt b/src/main/kotlin/de/fraunhofer/iem/dataprovider/StartUpHandler.kt
new file mode 100644
index 00000000..d5a40af5
--- /dev/null
+++ b/src/main/kotlin/de/fraunhofer/iem/dataprovider/StartUpHandler.kt
@@ -0,0 +1,30 @@
+package de.fraunhofer.iem.dataprovider
+
+import de.fraunhofer.iem.dataprovider.configuration.InitialProjects
+import de.fraunhofer.iem.dataprovider.logger.getLogger
+import de.fraunhofer.iem.dataprovider.toolRun.service.ToolRunService
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import org.springframework.boot.context.event.ApplicationReadyEvent
+import org.springframework.context.event.EventListener
+import org.springframework.stereotype.Component
+
+@Component
+class StartUpHandler(val toolRunService: ToolRunService, val initialProjects: InitialProjects) {
+    private val logger = getLogger(javaClass)
+    val coroutineScope = CoroutineScope(Dispatchers.Default)
+
+    @EventListener(ApplicationReadyEvent::class)
+    fun queryProjects() {
+        logger.info("Loading initial projects $initialProjects")
+
+        initialProjects.projectIds.forEach { projectId ->
+            if (projectId >= 0) {
+                coroutineScope.launch {
+                    toolRunService.createToolRunForRepository(projectId)
+                }
+            }
+        }
+    }
+}
diff --git a/src/main/kotlin/de/fraunhofer/iem/dataprovider/configuration/InitialProjects.kt b/src/main/kotlin/de/fraunhofer/iem/dataprovider/configuration/InitialProjects.kt
new file mode 100644
index 00000000..07b929f7
--- /dev/null
+++ b/src/main/kotlin/de/fraunhofer/iem/dataprovider/configuration/InitialProjects.kt
@@ -0,0 +1,29 @@
+package de.fraunhofer.iem.dataprovider.configuration
+
+import org.springframework.boot.context.properties.ConfigurationProperties
+import org.springframework.boot.context.properties.ConfigurationPropertiesScan
+import org.springframework.validation.annotation.Validated
+
+@ConfigurationProperties(prefix = "projects")
+@ConfigurationPropertiesScan
+@Validated
+data class InitialProjects(
+    val projectIds: Array<Long> = emptyArray()
+) {
+    override fun equals(other: Any?): Boolean {
+        if (this === other) return true
+        if (javaClass != other?.javaClass) return false
+
+        other as InitialProjects
+
+        return projectIds.contentEquals(other.projectIds)
+    }
+
+    override fun hashCode(): Int {
+        return projectIds.contentHashCode()
+    }
+
+    override fun toString(): String {
+        return projectIds.contentDeepToString()
+    }
+}
diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties
index 3417ba43..e4f1d875 100644
--- a/src/main/resources/application-dev.properties
+++ b/src/main/resources/application-dev.properties
@@ -40,3 +40,4 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.CockroachDialect
 # DB Login data
 spring.datasource.url=jdbc:postgresql://${host}:26257/${DB_USER}?sslmode=${ssl_mode}&sslrootcert=${ca_crt}&sslcert=${ssl_cert}&sslkey=${ssl_key}
 spring.datasource.username=${DB_USER:}
+projects.project-ids=1108, 888, 438, 1189, 820, 788, 400, 1052
diff --git a/src/main/resources/application-local.properties b/src/main/resources/application-local.properties
index b4ac9ba7..621ffe35 100644
--- a/src/main/resources/application-local.properties
+++ b/src/main/resources/application-local.properties
@@ -41,3 +41,4 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.CockroachDialect
 spring.datasource.url=${DB_URL:}
 spring.datasource.username=${DB_USER:}
 spring.datasource.password=${DB_PW:}
+projects.project-ids=1108, 888, 438, 1189, 820, 788, 400, 1052
diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties
index aa3c4832..77eb18d8 100644
--- a/src/main/resources/application-prod.properties
+++ b/src/main/resources/application-prod.properties
@@ -41,3 +41,4 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.CockroachDialect
 # DB Login data
 spring.datasource.url=jdbc:postgresql://${host}:26257/${DB_USER}?sslmode=${ssl_mode}&sslrootcert=${ca_crt}&sslcert=${ssl_cert}&sslkey=${ssl_key}
 spring.datasource.username=${DB_USER:}
+projects.project-ids=1108, 888, 438, 1189, 820, 788, 400, 1052
-- 
GitLab