Skip to content

Commit f3be596

Browse files
committed
1 parent 2d7d285 commit f3be596

14 files changed

Lines changed: 93 additions & 142 deletions

File tree

src/main/kotlin/com/github/mgramin/sqlboot/exceptions/BootException.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,33 @@ package com.github.mgramin.sqlboot.exceptions
3232
*/
3333
class BootException : RuntimeException {
3434

35+
val errorCode: Int
36+
get() = field
37+
38+
3539
/**
3640
* Ctor.
3741
*
3842
* @param message Error message
3943
*/
40-
constructor(message: String) : super(message) {}
44+
constructor(message: String) : super(message) {
45+
this.errorCode = 0
46+
}
4147

4248
/**
4349
* Ctor.
4450
*
45-
* @param message Error message
4651
* @param cause Cause
4752
*/
48-
constructor(message: String, cause: Throwable) : super(message, cause) {}
53+
constructor(cause: Throwable) : super(cause) {
54+
this.errorCode = 0
55+
}
4956

5057
/**
51-
* Ctor.
5258
*
53-
* @param cause Cause
5459
*/
55-
constructor(cause: Throwable) : super(cause) {}
60+
constructor(message: String, errorCode: Int) : super(message) {
61+
this.errorCode = errorCode
62+
}
63+
5664
}

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/CheckedConnection.kt

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,27 @@
2424

2525
package com.github.mgramin.sqlboot.model.connection
2626

27-
import com.fasterxml.jackson.annotation.JsonIgnore
28-
import org.slf4j.LoggerFactory
29-
import javax.sql.DataSource
30-
3127
class CheckedConnection(private val origin: DbConnection) : DbConnection {
3228

33-
override fun getProperties() = origin.getProperties()
34-
3529
private val health: String
3630

37-
private val logger = LoggerFactory.getLogger(this::class.java)
38-
39-
@JsonIgnore
40-
override fun getDataSource(): DataSource {
41-
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
42-
}
43-
44-
override fun getName() = origin.getName()
45-
46-
override fun getHealth(): String {
47-
return health
48-
}
49-
50-
// override fun getDataSource() = origin.getDataSource()
51-
52-
override fun paginationQueryTemplate() = origin.paginationQueryTemplate()
53-
5431
init {
5532
health = try {
56-
logger.info("Check connection...")
5733
origin.getDataSource().connection
5834
"Ok"
5935
} catch (e: Exception) {
6036
e.message.toString()
6137
}
6238
}
6339

40+
override fun name() = origin.name()
41+
42+
override fun properties() = origin.properties()
43+
44+
override fun health() = health
45+
46+
override fun paginationQueryTemplate() = origin.paginationQueryTemplate()
47+
48+
override fun getDataSource() = origin.getDataSource()
49+
6450
}

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/DbConnection.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ import javax.sql.DataSource
2828

2929
interface DbConnection {
3030

31-
fun getName(): String
31+
fun name(): String
3232

33-
fun getHealth(): String
33+
fun health(): String
3434

3535
fun getDataSource(): DataSource
3636

3737
fun paginationQueryTemplate(): String
3838

39-
fun getProperties(): Map<String, Any>
39+
fun properties(): Map<String, Any>
4040

4141
}

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/DbConnectionList.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ import org.springframework.stereotype.Service
3838
@ConfigurationProperties(prefix = "conf")
3939
open class DbConnectionList(val connections: List<SimpleDbConnection>) {
4040

41-
fun getConnectionByName(name: String) = connections.first { v -> v.getName().equals(name, ignoreCase = true) }
41+
fun getConnectionByName(name: String) = connections.first { v -> v.name().equals(name, ignoreCase = true) }
4242

43-
fun getConnectionsByMask(name: String) = connections.filter { v -> v.getName().matches(name.toRegex()) }
43+
fun getConnectionsByMask(name: String) = connections.filter { v -> v.name().matches(name.toRegex()) }
4444

4545
}

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/FakeDbConnection.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ package com.github.mgramin.sqlboot.model.connection
2727
import javax.sql.DataSource
2828

2929
class FakeDbConnection : DbConnection {
30-
override fun getProperties(): Map<String, Any> {
30+
31+
override fun properties(): Map<String, Any> {
3132
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
3233
}
3334

34-
override fun getHealth(): String {
35+
override fun health(): String {
3536
return "Ok"
3637
}
3738

38-
override fun getName(): String {
39+
override fun name(): String {
3940
return "Simple fake connection"
4041
}
4142

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/SimpleDbConnection.kt

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,27 @@ import org.springframework.core.io.Resource
3434
* @version $Id: f221782080d430e77aed80ef8446745687c350f4 $
3535
* @since 0.1
3636
*/
37-
open class SimpleDbConnection : DbConnection {
37+
open class SimpleDbConnection(
38+
var name: String? = null,
39+
@JsonIgnore var baseFolder: Resource? = null,
40+
var url: String? = null,
41+
var user: String? = null,
42+
@JsonIgnore var password: String? = null,
43+
var driverClassName: String? = null,
44+
var properties: String? = null,
45+
var paginationQueryTemplate: String? = null
46+
) : DbConnection {
47+
48+
override fun name() = this.name!!
3849

3950
override fun paginationQueryTemplate() = this.paginationQueryTemplate!!
4051

41-
override fun getName(): String {
42-
return name!!
43-
}
44-
45-
fun setName(name: String) {
46-
this.name = name
47-
}
48-
49-
private var name: String? = null
50-
51-
52-
@JsonIgnore // TODO fix json serialization for Resource class
53-
var baseFolder: Resource? = null
54-
var url: String? = null
55-
var user: String? = null
56-
@JsonIgnore
57-
var password: String? = null
58-
var driverClassName: String? = null
59-
private var properties: String? = null
60-
61-
var paginationQueryTemplate: String? = null
62-
52+
override fun health() = "UKNOWN"
6353

6454
private var dataSource: DataSource? = null
6555

66-
override fun getHealth() = "UKNOWN"
67-
68-
constructor()
56+
override fun properties() = JSONObject(properties).toMap()
6957

70-
override fun getProperties(): Map<String, Any> {
71-
return JSONObject(properties).toMap()
72-
}
73-
74-
fun setProperties(properties: String) {
75-
this.properties = properties
76-
}
7758

7859
@JsonIgnore
7960
override fun getDataSource(): DataSource {

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/composite/FsResourceTypes.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import com.github.mgramin.sqlboot.template.generator.impl.GroovyTemplateGenerato
3838
import com.github.mgramin.sqlboot.tools.files.file.impl.SimpleFile
3939
import reactor.core.publisher.Flux
4040
import java.io.File
41+
import java.lang.Exception
4142
import java.nio.charset.Charset
4243
import java.nio.charset.StandardCharsets.UTF_8
4344

@@ -85,17 +86,25 @@ class FsResourceTypes(
8586
}
8687

8788
override fun read(uri: Uri): Flux<DbResource> {
89+
try {
8890
return resourceTypes
8991
.asSequence()
9092
.first { v -> v.name().equals(uri.type(), ignoreCase = true) }
9193
.read(uri)
94+
} catch (e : Exception) {
95+
throw BootException("Type not found", 404)
96+
}
9297
}
9398

9499
override fun metaData(): Map<String, String> {
95-
return resourceTypes
96-
.asSequence()
97-
.first { v -> v.name().equals(uri.type(), ignoreCase = true) }
98-
.metaData()
100+
try {
101+
return resourceTypes
102+
.asSequence()
103+
.first { v -> v.name().equals(uri.type(), ignoreCase = true) }
104+
.metaData()
105+
} catch (e : Exception) {
106+
throw BootException("Type not found", 404)
107+
}
99108
}
100109

101110
}

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/sql/SqlResourceType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class SqlResourceType(
6969
return@map createQuery(uri, connection).execute(hashMapOf("uri" to uri))
7070
.map<Map<String, Any>?> {
7171
val mutableMap = it.toMutableMap()
72-
mutableMap["database"] = connection.getName()
72+
mutableMap["database"] = connection.name()
7373
mutableMap
7474
}
7575
}

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/wrappers/header/DbNameWrapper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class DbNameWrapper(private val origin: ResourceType,
6565

6666
override fun headers(): Map<String, Any> {
6767
val headers = it.headers().toMutableMap()
68-
headers["database"] = dbConnection.getName()
68+
headers["database"] = dbConnection.name()
6969
return headers
7070
}
7171

src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/ApiController.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package com.github.mgramin.sqlboot.rest.controllers
2626

2727
import com.fasterxml.jackson.core.JsonProcessingException
28+
import com.github.mgramin.sqlboot.exceptions.BootException
2829
import com.github.mgramin.sqlboot.model.connection.DbConnectionList
2930
import com.github.mgramin.sqlboot.model.resource.DbResource
3031
import com.github.mgramin.sqlboot.model.resourcetype.Metadata
@@ -423,7 +424,15 @@ class ApiController {
423424
val connections = dbConnectionList.getConnectionsByMask(connectionName!!)
424425
val result = ArrayList<DbResource>()
425426
val fsResourceTypes = FsResourceTypes(connections, uri)
426-
val collect = fsResourceTypes.read(uri).collectList().block()
427+
val collect: MutableList<DbResource>?
428+
try {
429+
collect = fsResourceTypes.read(uri).collectList().block()
430+
} catch (e: BootException) {
431+
if (e.errorCode == 404) {
432+
return ResponseEntity(result, HttpStatus.NOT_FOUND)
433+
}
434+
return ResponseEntity(result, HttpStatus.INTERNAL_SERVER_ERROR)
435+
}
427436
result.addAll(collect)
428437

429438
return if (result.isEmpty()) {

0 commit comments

Comments
 (0)