11package io .github .datacatering .datacaterer .core .ui .plan
22
3+ import io .github .datacatering .datacaterer .api .model .Constants .FORMAT
4+ import io .github .datacatering .datacaterer .core .config .ConfigParser
35import io .github .datacatering .datacaterer .core .ui .config .UiConfiguration .INSTALL_DIRECTORY
46import io .github .datacatering .datacaterer .core .ui .model .{Connection , GetConnectionsResponse , SaveConnectionsRequest }
57import org .apache .log4j .Logger
@@ -60,19 +62,58 @@ object ConnectionRepository {
6062
6163 def getConnection (name : String , masking : Boolean = true ): Connection = {
6264 LOGGER .debug(s " Getting connection details, connection-name= $name" )
65+
66+ // First try to get from saved connections file
6367 val connectionFile = Path .of(s " $connectionSaveFolder/ $name.csv " )
64- val tryConnection = Try (Connection .fromString(Files .readString(connectionFile), name, masking))
65- tryConnection match {
68+ val tryConnectionFromFile = if (connectionFile.toFile.exists()) {
69+ Try (Connection .fromString(Files .readString(connectionFile), name, masking))
70+ } else {
71+ Failure (new IllegalArgumentException (s " Connection file not found: $name" ))
72+ }
73+
74+ tryConnectionFromFile match {
6675 case Success (connection) => connection.copy(options = connection.options)
67- case Failure (exception) => throw exception
76+ case Failure (fileException) =>
77+ // If not found in file, try to get from application.conf
78+ LOGGER .debug(s " Connection not found in file, checking application.conf, connection-name= $name" )
79+ Try (getConnectionFromConfig(name, masking)) match {
80+ case Success (conn) => conn
81+ case Failure (configException) =>
82+ // If not found in either location, throw the original exception
83+ throw new IllegalArgumentException (s " Connection not found: $name" , fileException)
84+ }
85+ }
86+ }
87+
88+ /**
89+ * Get connection details from application.conf
90+ */
91+ private def getConnectionFromConfig (name : String , masking : Boolean = true ): Connection = {
92+ val connectionConfigs = ConfigParser .connectionConfigsByName
93+ connectionConfigs.get(name) match {
94+ case Some (config) =>
95+ val format = config.getOrElse(FORMAT , " unknown" )
96+ val options = if (masking) {
97+ config.map {
98+ case (key, value) if key.contains(" password" ) || key.contains(" token" ) => (key, " ***" )
99+ case (key, value) => (key, value)
100+ }
101+ } else {
102+ config
103+ }
104+ Connection (name, format, Some (" data-source" ), options - FORMAT )
105+ case None =>
106+ throw new IllegalArgumentException (s " Connection not found in application.conf: $name" )
68107 }
69108 }
70109
71110 private def getAllConnections (optConnectionGroupType : Option [String ], masking : Boolean = true ): GetConnectionsResponse = {
72111 LOGGER .debug(s " Getting all connection details, connection-group= ${optConnectionGroupType.getOrElse(" " )}" )
112+
113+ // Get connections from files
73114 val connectionPath = Path .of(connectionSaveFolder)
74115 if (! connectionPath.toFile.exists()) connectionPath.toFile.mkdirs()
75- val connections = Files .list(connectionPath)
116+ val fileConnections = Files .list(connectionPath)
76117 .iterator()
77118 .asScala
78119 .map(file => {
@@ -85,9 +126,38 @@ object ConnectionRepository {
85126 .filter(_.isSuccess)
86127 .map(_.get)
87128 .toList
129+
130+ // Get connections from application.conf
131+ val configConnections = getConnectionsFromConfig(masking)
132+
133+ // Merge connections, with file connections taking priority (deduplicating by name)
134+ val allConnections = (fileConnections ++ configConnections)
135+ .groupBy(_.name)
136+ .map(_._2.head) // Take first occurrence (file connection if exists, otherwise config)
137+ .toList
88138 .filter(conn => optConnectionGroupType.forall(conn.groupType.contains))
89139 .sortBy(_.name)
90- GetConnectionsResponse (connections)
140+
141+ GetConnectionsResponse (allConnections)
142+ }
143+
144+ /**
145+ * Get all connections from application.conf
146+ */
147+ private def getConnectionsFromConfig (masking : Boolean = true ): List [Connection ] = {
148+ val connectionConfigs = ConfigParser .connectionConfigsByName
149+ connectionConfigs.map { case (name, config) =>
150+ val format = config.getOrElse(FORMAT , " unknown" )
151+ val options = if (masking) {
152+ config.map {
153+ case (key, value) if key.contains(" password" ) || key.contains(" token" ) => (key, " ***" )
154+ case (key, value) => (key, value)
155+ }
156+ } else {
157+ config
158+ }
159+ Connection (name, format, Some (" data-source" ), options - FORMAT )
160+ }.toList
91161 }
92162
93163 private def removeConnection (connectionName : String ): Boolean = {
0 commit comments