Skip to content

Commit

Permalink
#89
Browse files Browse the repository at this point in the history
  • Loading branch information
mgramin committed Jan 14, 2019
1 parent 9d29530 commit 5ab28f2
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,20 @@

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

/**
* 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<String, Any>) : DbResource {
class DbResourceImpl(
private val name: String,
@field:Transient private val type: ResourceType,
@field:Transient private val uri: Uri,
private val headers: Map<String, Any>,
private val body: String = ""
) : DbResource {

override fun name(): String {
return name
Expand All @@ -61,7 +56,6 @@ class DbResourceImpl
}

override fun body(): String {
throw BootException("Resource body not allow here.")
return body
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@ interface ResourceType {
@Throws(BootException::class)
fun read(uri: Uri): Stream<DbResource>


@JsonAutoDetect(fieldVisibility = Visibility.ANY)
class Metadata(private val name: String, private val type: String, private val description: String, properties: Map<String, Any>?) {
class Metadata(
private val name: String,
private val type: String,
private val description: String
) {
private val properties: MutableMap<String, Any>

constructor(name: String, description: String) : this(name, "String", description, null) {}
constructor(name: String, description: String) : this(name, "String", description)

init {
this.properties = HashMap()
Expand All @@ -105,7 +108,6 @@ interface ResourceType {
prop["description"] = description
this.properties.putAll(prop)
}

}

fun name(): String {
Expand All @@ -119,7 +121,5 @@ interface ResourceType {
fun properties(): Map<String, Any> {
return properties
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) : ResourceType {
class SqlResourceType(
@field:Transient private val selectQuery: SelectQuery,
private val aliases: List<String>
) : ResourceType {

override fun aliases(): List<String> {
return aliases
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
return origin.aliases()
Expand All @@ -48,14 +51,10 @@ class TemplateBodyWrapper(private val origin: ResourceType, private val template
@Throws(BootException::class)
override fun read(uri: Uri): Stream<DbResource> {
return origin.read(uri)
.map { r ->
DbResourceBodyWrapper(
r, templateGenerator.generate(r.headers()))
}
.map { r -> DbResourceBodyWrapper(r, templateGenerator.generate(r.headers())) }
}

override fun metaData(): Map<String, String> {
return origin.metaData()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
return origin.aliases()
Expand All @@ -50,7 +50,7 @@ class SelectWrapper(private val origin: ResourceType) : ResourceType {

@Throws(BootException::class)
override fun read(uri: Uri): Stream<DbResource> {
val select = uri.params()[SELECT]
val select = uri.params()[parameterName]
val resources = origin.read(uri)
return if (select != null) {
resources
Expand All @@ -71,7 +71,7 @@ class SelectWrapper(private val origin: ResourceType) : ResourceType {
}

override fun metaData(uri: Uri): List<ResourceType.Metadata> {
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()) }
Expand All @@ -80,11 +80,5 @@ class SelectWrapper(private val origin: ResourceType) : ResourceType {
origin.metaData(uri)
}
}

companion object {

private val SELECT = "select"
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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 ([email protected])
* @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<String, List<DbResource>>
private var cache: Cache<String, List<DbResource>>? = 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<String, List<DbResource>> =
cacheManager.getCache("simpleCache") ?: cacheManager.createCache("simpleCache", MutableConfiguration())

override fun aliases(): List<String> {
return origin.aliases()
Expand All @@ -68,17 +57,16 @@ class CacheWrapper(private val origin: ResourceType) : ResourceType {

@Throws(BootException::class)
override fun read(uri: Uri): Stream<DbResource> {
val cache = ofNullable(uri.params()["cache"]).orElse("true")
var cachedResources: List<DbResource>? = this.cache!!.get(uri.toString())
val cache = ofNullable(uri.params()[parameterName]).orElse("true")
var cachedResources: List<DbResource>? = 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()
}

override fun metaData(): Map<String, String> {
return origin.metaData()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
return origin.aliases()
Expand All @@ -50,18 +50,12 @@ class LimitWrapper(private val origin: ResourceType) : ResourceType {
@Throws(BootException::class)
override fun read(uri: Uri): Stream<DbResource> {
val limit = ofNullable(uri.params())
.map<String> { v -> v[LIMIT] }
.map<String> { v -> v[parameterName] }
.orElse(null) ?: return origin.read(uri)
return origin.read(uri).limit(parseLong(limit))
}

override fun metaData(): Map<String, String> {
return origin.metaData()
}

companion object {

private val LIMIT = "limit"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -52,7 +52,7 @@ class PageWrapper constructor(
}

override fun read(uri: Uri): Stream<DbResource> {
val pageParameter = uri.params()[page]
val pageParameter = uri.params()[parameterName]
if (pageParameter != null) {
val pageNumber = valueOf(substringBefore(pageParameter, delimiter))
val pageSize: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ class SelectWrapperTest {
}
assertEquals(3, resources.size)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,4 @@ class CacheWrapperTest {
)
verify(originType, times(1)).read(any())
}

}

0 comments on commit 5ab28f2

Please sign in to comment.