From 5ab28f2c123f5c6f8e5480201335c74be9e70220 Mon Sep 17 00:00:00 2001 From: mgramin Date: Mon, 14 Jan 2019 12:36:24 +0300 Subject: [PATCH] #89 --- .../model/resource/impl/DbResourceImpl.kt | 22 ++++++---------- .../model/resource_type/ResourceType.kt | 12 ++++----- .../resource_type/impl/sql/SqlResourceType.kt | 11 +++----- .../wrappers/body/TemplateBodyWrapper.kt | 11 ++++---- .../wrappers/header/SelectWrapper.kt | 12 +++------ .../wrappers/list/CacheWrapper.kt | 26 +++++-------------- .../wrappers/list/LimitWrapper.kt | 10 ++----- .../wrappers/list/PageWrapper.kt | 4 +-- .../wrappers/header/SelectWrapperTest.kt | 1 - .../wrappers/list/CacheWrapperTest.kt | 1 - 10 files changed, 37 insertions(+), 73 deletions(-) diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource/impl/DbResourceImpl.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource/impl/DbResourceImpl.kt index 7d9d1553..9b50ad12 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource/impl/DbResourceImpl.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource/impl/DbResourceImpl.kt @@ -24,7 +24,6 @@ package com.github.mgramin.sqlboot.model.resource.impl -import com.github.mgramin.sqlboot.exceptions.BootException import com.github.mgramin.sqlboot.model.resource.DbResource import com.github.mgramin.sqlboot.model.resource_type.ResourceType import com.github.mgramin.sqlboot.model.uri.Uri @@ -32,17 +31,13 @@ import com.github.mgramin.sqlboot.model.uri.Uri /** * DB resource without body */ -class DbResourceImpl -/** - * - * @param name - * @param type - * @param uri - * @param headers - */ -(private val name: String, @field:Transient private val type: ResourceType, - @field:Transient private val uri: Uri, - private val headers: Map) : DbResource { +class DbResourceImpl( + private val name: String, + @field:Transient private val type: ResourceType, + @field:Transient private val uri: Uri, + private val headers: Map, + private val body: String = "" +) : DbResource { override fun name(): String { return name @@ -61,7 +56,6 @@ class DbResourceImpl } override fun body(): String { - throw BootException("Resource body not allow here.") + return body } - } diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/ResourceType.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/ResourceType.kt index 7e4a6bb8..2cd70bf7 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/ResourceType.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/ResourceType.kt @@ -85,12 +85,15 @@ interface ResourceType { @Throws(BootException::class) fun read(uri: Uri): Stream - @JsonAutoDetect(fieldVisibility = Visibility.ANY) - class Metadata(private val name: String, private val type: String, private val description: String, properties: Map?) { + class Metadata( + private val name: String, + private val type: String, + private val description: String + ) { private val properties: MutableMap - constructor(name: String, description: String) : this(name, "String", description, null) {} + constructor(name: String, description: String) : this(name, "String", description) init { this.properties = HashMap() @@ -105,7 +108,6 @@ interface ResourceType { prop["description"] = description this.properties.putAll(prop) } - } fun name(): String { @@ -119,7 +121,5 @@ interface ResourceType { fun properties(): Map { return properties } - } - } diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/impl/sql/SqlResourceType.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/impl/sql/SqlResourceType.kt index 026bb16a..4caf3590 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/impl/sql/SqlResourceType.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/impl/sql/SqlResourceType.kt @@ -45,7 +45,10 @@ import org.apache.commons.lang3.StringUtils.strip /** * Created by MGramin on 12.07.2017. */ -class SqlResourceType(@field:Transient private val selectQuery: SelectQuery, private val aliases: List) : ResourceType { +class SqlResourceType( + @field:Transient private val selectQuery: SelectQuery, + private val aliases: List +) : ResourceType { override fun aliases(): List { return aliases @@ -74,12 +77,6 @@ class SqlResourceType(@field:Transient private val selectQuery: SelectQuery, pri val headers = o.entries .map { strip(it.key, "@") to ofNullable(it.value).orElse("")} .toMap() - -// .collect(toMap( -// { k -> strip(k.key, "@") }, -// { v -> ofNullable(v.value).orElse("") } -// )) - DbResourceImpl(name, this, DbUri(this.name(), path.stream().map { it.toString() }.collect(toList())), headers) diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/body/TemplateBodyWrapper.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/body/TemplateBodyWrapper.kt index edce73a2..76722d3d 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/body/TemplateBodyWrapper.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/body/TemplateBodyWrapper.kt @@ -35,7 +35,10 @@ import java.util.stream.Stream /** * Created by MGramin on 18.07.2017. */ -class TemplateBodyWrapper(private val origin: ResourceType, private val templateGenerator: TemplateGenerator) : ResourceType { +class TemplateBodyWrapper( + private val origin: ResourceType, + private val templateGenerator: TemplateGenerator +) : ResourceType { override fun aliases(): List { return origin.aliases() @@ -48,14 +51,10 @@ class TemplateBodyWrapper(private val origin: ResourceType, private val template @Throws(BootException::class) override fun read(uri: Uri): Stream { return origin.read(uri) - .map { r -> - DbResourceBodyWrapper( - r, templateGenerator.generate(r.headers())) - } + .map { r -> DbResourceBodyWrapper(r, templateGenerator.generate(r.headers())) } } override fun metaData(): Map { return origin.metaData() } - } diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/header/SelectWrapper.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/header/SelectWrapper.kt index 635a9662..03728782 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/header/SelectWrapper.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/header/SelectWrapper.kt @@ -38,7 +38,7 @@ import java.util.stream.Stream /** * Created by MGramin on 18.07.2017. */ -class SelectWrapper(private val origin: ResourceType) : ResourceType { +class SelectWrapper(private val origin: ResourceType, private val parameterName: String = "select") : ResourceType { override fun aliases(): List { return origin.aliases() @@ -50,7 +50,7 @@ class SelectWrapper(private val origin: ResourceType) : ResourceType { @Throws(BootException::class) override fun read(uri: Uri): Stream { - val select = uri.params()[SELECT] + val select = uri.params()[parameterName] val resources = origin.read(uri) return if (select != null) { resources @@ -71,7 +71,7 @@ class SelectWrapper(private val origin: ResourceType) : ResourceType { } override fun metaData(uri: Uri): List { - val select = uri.params()[SELECT] + val select = uri.params()[parameterName] return if (select != null) { origin.metaData(uri).stream() .filter { e -> asList(*select.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()).contains(e.name()) } @@ -80,11 +80,5 @@ class SelectWrapper(private val origin: ResourceType) : ResourceType { origin.metaData(uri) } } - - companion object { - - private val SELECT = "select" - } - } diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/CacheWrapper.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/CacheWrapper.kt index cef646c7..5b713ba2 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/CacheWrapper.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/CacheWrapper.kt @@ -35,28 +35,17 @@ import javax.cache.Cache import javax.cache.CacheManager import javax.cache.Caching import javax.cache.configuration.MutableConfiguration -import javax.cache.spi.CachingProvider /** * @author Maksim Gramin (mgramin@gmail.com) * @version $Id: 822c72ab4745f06ca5b3062b4b0be1f9588596db $ * @since 0.1 */ -class CacheWrapper(private val origin: ResourceType) : ResourceType { +class CacheWrapper(private val origin: ResourceType, private val parameterName: String = "cache") : ResourceType { - private val cachingProvider: CachingProvider = Caching.getCachingProvider() - private val cacheManager: CacheManager - private val config: MutableConfiguration> - private var cache: Cache>? = null - - init { - cacheManager = cachingProvider.cacheManager - config = MutableConfiguration() - cache = cacheManager.getCache("simpleCache") - if (cache == null) { - cache = cacheManager.createCache("simpleCache", config) - } - } + private val cacheManager: CacheManager = Caching.getCachingProvider().cacheManager + private val cache: Cache> = + cacheManager.getCache("simpleCache") ?: cacheManager.createCache("simpleCache", MutableConfiguration()) override fun aliases(): List { return origin.aliases() @@ -68,11 +57,11 @@ class CacheWrapper(private val origin: ResourceType) : ResourceType { @Throws(BootException::class) override fun read(uri: Uri): Stream { - val cache = ofNullable(uri.params()["cache"]).orElse("true") - var cachedResources: List? = this.cache!!.get(uri.toString()) + val cache = ofNullable(uri.params()[parameterName]).orElse("true") + var cachedResources: List? = this.cache.get(uri.toString()) if (cachedResources == null || cache.equals("false", ignoreCase = true)) { cachedResources = origin.read(uri).collect(Collectors.toList()) - this.cache!!.put(uri.toString(), cachedResources) + this.cache.put(uri.toString(), cachedResources) } return cachedResources!!.stream() } @@ -80,5 +69,4 @@ class CacheWrapper(private val origin: ResourceType) : ResourceType { override fun metaData(): Map { return origin.metaData() } - } diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/LimitWrapper.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/LimitWrapper.kt index 555b832f..4baff047 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/LimitWrapper.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/LimitWrapper.kt @@ -37,7 +37,7 @@ import java.util.stream.Stream * @version $Id: 8035d736a6da6f6496cb9c0ebbe4e89d6ddd7b9f $ * @since 0.1 */ -class LimitWrapper(private val origin: ResourceType) : ResourceType { +class LimitWrapper(private val origin: ResourceType, private val parameterName: String = "limit") : ResourceType { override fun aliases(): List { return origin.aliases() @@ -50,7 +50,7 @@ class LimitWrapper(private val origin: ResourceType) : ResourceType { @Throws(BootException::class) override fun read(uri: Uri): Stream { val limit = ofNullable(uri.params()) - .map { v -> v[LIMIT] } + .map { v -> v[parameterName] } .orElse(null) ?: return origin.read(uri) return origin.read(uri).limit(parseLong(limit)) } @@ -58,10 +58,4 @@ class LimitWrapper(private val origin: ResourceType) : ResourceType { override fun metaData(): Map { return origin.metaData() } - - companion object { - - private val LIMIT = "limit" - } - } diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/PageWrapper.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/PageWrapper.kt index d3f2b41c..00b0d148 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/PageWrapper.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/PageWrapper.kt @@ -34,7 +34,7 @@ import java.util.stream.Stream class PageWrapper constructor( private val origin: ResourceType, - private val page: String = "page", + private val parameterName: String = "page", private val pageSize: Int = 10, private val delimiter: String = "," ) : ResourceType { @@ -52,7 +52,7 @@ class PageWrapper constructor( } override fun read(uri: Uri): Stream { - val pageParameter = uri.params()[page] + val pageParameter = uri.params()[parameterName] if (pageParameter != null) { val pageNumber = valueOf(substringBefore(pageParameter, delimiter)) val pageSize: Int diff --git a/src/test/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/header/SelectWrapperTest.kt b/src/test/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/header/SelectWrapperTest.kt index 25c60822..109378a5 100644 --- a/src/test/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/header/SelectWrapperTest.kt +++ b/src/test/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/header/SelectWrapperTest.kt @@ -53,5 +53,4 @@ class SelectWrapperTest { } assertEquals(3, resources.size) } - } \ No newline at end of file diff --git a/src/test/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/CacheWrapperTest.kt b/src/test/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/CacheWrapperTest.kt index f897ae44..91bb5b91 100644 --- a/src/test/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/CacheWrapperTest.kt +++ b/src/test/kotlin/com/github/mgramin/sqlboot/model/resource_type/wrappers/list/CacheWrapperTest.kt @@ -60,5 +60,4 @@ class CacheWrapperTest { ) verify(originType, times(1)).read(any()) } - } \ No newline at end of file