@@ -21,6 +21,12 @@ import java.sql.DriverManager
2121
2222lateinit var config: ConfigFile
2323
24+ fun debug (message : String ) {
25+ if (config.debug == true ) {
26+ println (message)
27+ }
28+ }
29+
2430fun Application.main () {
2531
2632 install(DefaultHeaders )
@@ -38,14 +44,14 @@ fun Application.main() {
3844 basic(" auth-keycloak-userpass" ) {
3945 // Obtain Token via KeyCloak
4046 validate { credentials ->
41- println (" auth-keycloak-userpass called for username ${credentials.name} " )
47+ debug (" auth-keycloak-userpass called for username ${credentials.name} " )
4248 // Note: validate must return Principal in case of successful authentication or null otherwise
43- getKCPrincipalOrNull(config, credentials.name, credentials.password) // .also { println (it) }
49+ getKCPrincipalOrNull(config, credentials.name, credentials.password) // .also { debug (it) }
4450 }
4551 }
4652 bearer(" auth-keycloak-bearer" ) {
4753 authenticate { bearerTokenCredential: BearerTokenCredential ->
48- println (" auth-keycloak-bearer called with token = ${bearerTokenCredential.token} " )
54+ debug (" auth-keycloak-bearer called with token = ${bearerTokenCredential.token} " )
4955 val groups = getKCgroups(config, bearerTokenCredential.token) ? : return @authenticate null
5056 TokenGroupsPrincipal (bearerTokenCredential.token, groups, rolesFromGroups(config, groups))
5157 }
@@ -69,7 +75,7 @@ fun Application.main() {
6975 DriverManager .getConnection(urlConditionDB, config.condition_db!! .user, config.condition_db!! .password)
7076 }
7177
72- // println ("Working Directory = ${System.getProperty("user.dir")}")
78+ // debug ("Working Directory = ${System.getProperty("user.dir")}")
7379 routing {
7480
7581 // Allows all resources to be statically available (including generated nica-ems.js file)
@@ -112,7 +118,7 @@ fun Application.main() {
112118 }
113119 }
114120 } catch (err: Exception ) {
115- println (" EMS database error: $err " )
121+ debug (" EMS database error: $err " )
116122 call.respond(HttpStatusCode .NotFound , " EMS database error: $err " )
117123 } finally {
118124 connEMD?.close()
@@ -173,7 +179,7 @@ fun Application.main() {
173179 // e.g. POST { "software_id": 100, "software_version": "22.1" }
174180 // Note: software_id is assigned automatically by the database, regardless of what is passed in JSON
175181 val roles = call.principal<WithRoles >()?.roles!!
176- // println ("Roles in post(SOFTWARE_URL): $roles")
182+ // debug ("Roles in post(SOFTWARE_URL): $roles")
177183 if (! (roles.isWriter or roles.isAdmin)) {
178184 call.respond(HttpStatusCode .Unauthorized )
179185 return @post // not really needed here but important in other places
@@ -183,7 +189,7 @@ fun Application.main() {
183189 val sw = call.receive<SoftwareVersion >()
184190 connEMD = newEMDConnection(config, this .context)
185191 val query = """ INSERT INTO software_ (software_version) VALUES ('${sw.software_version} ')"""
186- println (query)
192+ debug (query)
187193 connEMD!! .createStatement().executeUpdate(query)
188194 call.respond(HttpStatusCode .OK , " SW record was created" )
189195 } catch (err: PSQLException ) {
@@ -245,7 +251,7 @@ fun Application.main() {
245251 val storage = call.receive<Storage >()
246252 connEMD = newEMDConnection(config, this .context)
247253 val query = """ INSERT INTO storage_ (storage_name) VALUES ('${storage.storage_name} ')"""
248- println (query)
254+ debug (query)
249255
250256 connEMD!! .createStatement().executeUpdate(query)
251257 call.response.status(HttpStatusCode .OK )
@@ -373,7 +379,7 @@ fun Application.main() {
373379 connEMD!! .autoCommit = false
374380 val softwareMap = getSoftwareMap(connEMD!! )
375381 val storageMap = getStorageMap(connEMD!! )
376- println (" Received ${events.size} events!" )
382+ debug (" Received ${events.size} events!" )
377383 val fileData = mutableMapOf<Pair <Byte , String >, Int > ()
378384 events.forEach { event ->
379385 val software_id = softwareMap.str_to_id[event.software_version]
@@ -403,7 +409,7 @@ fun Application.main() {
403409 } else { // create file record
404410 val fileQuery =
405411 """ INSERT INTO file_ (storage_id, file_path) VALUES ($storage_id , '$file_path ')"""
406- println (fileQuery)
412+ debug (fileQuery)
407413 connEMD!! .createStatement().executeUpdate(fileQuery)
408414 val res2 = connEMD!! .createStatement().executeQuery(
409415 """ SELECT file_guid FROM file_ WHERE storage_id = $storage_id AND file_path = '$file_path '"""
@@ -412,12 +418,12 @@ fun Application.main() {
412418 file_guid = res2.getInt(" file_guid" )
413419 }
414420 fileData[Pair (storage_id, file_path)] = file_guid
415- println (" File GUID for $file_path = $file_guid " )
421+ debug (" File GUID for $file_path = $file_guid " )
416422 }
417423 }
418424 val stmt = connEMD!! .createStatement()
419425 events.forEach { event ->
420- println (" Creating event: $event " )
426+ debug (" Creating event: $event " )
421427 val software_id = softwareMap.str_to_id[event.software_version]
422428 val storage_id = storageMap.str_to_id[event.reference.storage_name]
423429 val file_guid = fileData[Pair (storage_id, event.reference.file_path)]!!
@@ -435,12 +441,12 @@ fun Application.main() {
435441 VALUES ($file_guid , ${event.reference.event_number} , $software_id , ${event.period_number} ,
436442 ${event.run_number} , $parameterValuesStr )
437443 """ .trimIndent()
438- println (query)
444+ debug (query)
439445 stmt.addBatch(query)
440446 }
441447 stmt.executeBatch()
442448 connEMD!! .commit()
443- println (" Success: ${events.size} event(s) were created" )
449+ debug (" Success: ${events.size} event(s) were created" )
444450 call.respond(HttpStatusCode .OK , " Success: ${events.size} event(s) were created" )
445451 } catch (err: PSQLException ) {
446452 if (err.toString().contains(" The connection attempt failed." )) {
@@ -470,7 +476,7 @@ fun Application.main() {
470476 connEMD!! .autoCommit = false
471477 val softwareMap = getSoftwareMap(connEMD!! )
472478 val storageMap = getStorageMap(connEMD!! )
473- println (" Received ${events.size} events!" )
479+ debug (" Received ${events.size} events!" )
474480 val fileData = mutableMapOf<Pair <Byte , String >, Int > ()
475481 events.forEach { event ->
476482 val software_id = softwareMap.str_to_id[event.software_version]
@@ -500,7 +506,7 @@ fun Application.main() {
500506 } else { // create file record
501507 val fileQuery =
502508 """ INSERT INTO file_ (storage_id, file_path) VALUES ($storage_id , '$file_path ')"""
503- println (fileQuery)
509+ debug (fileQuery)
504510 connEMD!! .createStatement().executeUpdate(fileQuery)
505511 val res2 = connEMD!! .createStatement().executeQuery(
506512 """ SELECT file_guid FROM file_ WHERE storage_id = $storage_id AND file_path = '$file_path '"""
@@ -509,12 +515,12 @@ fun Application.main() {
509515 file_guid = res2.getInt(" file_guid" )
510516 }
511517 fileData[Pair (storage_id, file_path)] = file_guid
512- println (" File GUID for $file_path = $file_guid " )
518+ debug (" File GUID for $file_path = $file_guid " )
513519 }
514520 }
515521 val stmt = connEMD!! .createStatement()
516522 events.forEach { event ->
517- println (" Creating event: $event " )
523+ debug (" Creating event: $event " )
518524 val software_id = softwareMap.str_to_id[event.software_version]
519525 val storage_id = storageMap.str_to_id[event.reference.storage_name]
520526 val file_guid = fileData[Pair (storage_id, event.reference.file_path)]!!
@@ -542,7 +548,7 @@ fun Application.main() {
542548 software_id=$software_id , period_number=${event.period_number} , run_number=${event.run_number} ,
543549 $parameterNamesEqValuesStr
544550 """ .trimIndent()
545- println (query)
551+ debug (query)
546552 stmt.addBatch(query)
547553 }
548554 stmt.executeBatch()
@@ -594,7 +600,7 @@ fun Application.main() {
594600 )
595601 if (res.next()) {
596602 val file_guid: Int = res.getInt(" file_guid" )
597- println (" File GUID ($storage_id , $file_path ) = $file_guid " )
603+ debug (" File GUID ($storage_id , $file_path ) = $file_guid " )
598604 fileData[Pair (storage_id, file_path)] = file_guid
599605 } else { // no such file
600606 call.respond(
@@ -607,14 +613,14 @@ fun Application.main() {
607613 }
608614 val stmt = connEMD!! .createStatement()
609615 events.forEach { event ->
610- println (" Deleting event: $event " )
616+ debug (" Deleting event: $event " )
611617 val storage_id = storageMap.str_to_id[event.reference.storage_name]
612618 val file_guid = fileData[Pair (storage_id, event.reference.file_path)]
613619 val query = """
614620 DELETE FROM ${page.db_table_name}
615621 WHERE (("file_guid" = $file_guid AND "event_number" = ${event.reference.event_number} ));
616622 """ .trimIndent()
617- println (query)
623+ debug (query)
618624 stmt.addBatch(query)
619625 }
620626 val res = stmt.executeBatch()
@@ -647,7 +653,7 @@ fun Application.main() {
647653 // Synchronous - build a file with some ROOT macro and return it
648654 get(" /eventFile" ) {
649655 // TODO Apply all filtering, build ROOT file
650- println (" Serving dummy eventFile..." )
656+ debug (" Serving dummy eventFile..." )
651657 val f =
652658 Thread .currentThread().getContextClassLoader().getResource(" static/downloadFile.bin" )!! .file
653659 call.respondFile(File (f))
0 commit comments