wireup script execution
This commit is contained in:
parent
f35851d339
commit
1a22043cf2
2
.idea/kotlinc.xml
generated
2
.idea/kotlinc.xml
generated
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="KotlinJpsPluginSettings">
|
||||
<option name="version" value="1.9.0" />
|
||||
<option name="version" value="1.9.20" />
|
||||
</component>
|
||||
</project>
|
||||
4
api.http
4
api.http
@ -5,9 +5,9 @@ Authorization: {{auth-token}}
|
||||
|
||||
{
|
||||
"data": {
|
||||
"number": "KA01MU0556"
|
||||
"number": "TN36AS1206"
|
||||
},
|
||||
"uniqueIdentifier": "KA01MU0556"
|
||||
"uniqueIdentifier": "TN36AS1206"
|
||||
}
|
||||
|
||||
### create row, with autogenerated identifier
|
||||
|
||||
@ -11,4 +11,4 @@ app.iam.realm=forewarn-dev
|
||||
app.iam.client=forewarn
|
||||
app.iam.client_redirect_uri=http://localhost:9001/auth/code
|
||||
app.cache.redis_uri=redis://127.0.0.1:6379/0
|
||||
app.scripts.path=/tmp
|
||||
app.scripts.path=/Users/gowthaman.b/IdeaProjects/rmc_modules_api/src/main/resources/scripts
|
||||
@ -1,6 +1,6 @@
|
||||
plugins {
|
||||
kotlin("jvm") version "1.9.0"
|
||||
kotlin("kapt") version "1.9.0"
|
||||
kotlin("jvm") version "1.9.20"
|
||||
kotlin("kapt") version "1.9.20"
|
||||
id("idea")
|
||||
id("io.ebean") version "13.23.2"
|
||||
application
|
||||
@ -29,7 +29,8 @@ dependencies {
|
||||
implementation("ch.qos.logback:logback-classic:1.4.11")
|
||||
implementation("org.bitbucket.b_c:jose4j:0.9.3")
|
||||
implementation("redis.clients:jedis:5.0.2")
|
||||
implementation("org.jetbrains.kotlin:kotlin-scripting-jsr223:1.9.0")
|
||||
implementation("org.jetbrains.kotlin:kotlin-scripting-jsr223:1.9.20")
|
||||
implementation("org.jetbrains.kotlin:kotlin-script-runtime:1.9.20")
|
||||
api ("net.cactusthorn.config:config-core:0.81")
|
||||
kapt("net.cactusthorn.config:config-compiler:0.81")
|
||||
kapt("io.ebean:kotlin-querybean-generator:13.23.2")
|
||||
|
||||
@ -57,7 +57,8 @@ object Entities {
|
||||
val name = ctx.pathParam("name")
|
||||
val params = ctx.bodyAsClass<Map<String, Any>>()
|
||||
ctx.json(
|
||||
Scripting.execute(name, params)
|
||||
Scripting.
|
||||
execute(name, "execute", params, logger)
|
||||
)
|
||||
}
|
||||
|
||||
@ -202,6 +203,12 @@ object Entities {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!setupEntity.preSaveScript.isNullOrEmpty()) {
|
||||
val ok = Scripting.preSave(setupEntity.preSaveScript!!, "preSave", this, logger) as Boolean
|
||||
if (!ok) {
|
||||
throw BadRequestResponse("PreSave Failed")
|
||||
}
|
||||
}
|
||||
|
||||
if (setupEntity.approvalLevels > 0) {
|
||||
|
||||
@ -210,7 +217,8 @@ object Entities {
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun isValidDate(f: String) = try {
|
||||
|
||||
@ -1,17 +1,29 @@
|
||||
package com.restapi.integ
|
||||
|
||||
import com.restapi.config.AppConfig.Companion.appConfig
|
||||
import com.restapi.domain.DataModel
|
||||
import com.restapi.domain.Session
|
||||
import org.slf4j.Logger
|
||||
import java.io.File
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import javax.script.Invocable
|
||||
import javax.script.ScriptEngineManager
|
||||
|
||||
|
||||
object Scripting {
|
||||
private val engineMap = ConcurrentHashMap<String, Invocable>()
|
||||
|
||||
fun execute(name: String, params: Map<String, Any>): Any {
|
||||
private fun getEngine(scriptName: String) = engineMap.computeIfAbsent(scriptName) {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
||||
engine.eval(File(appConfig.scriptsPath(), "$name.kts").reader())
|
||||
val invocator = engine as Invocable
|
||||
return invocator.invokeFunction("execute", params)
|
||||
engine.eval(File(appConfig.scriptsPath(), scriptName).reader())
|
||||
engine as Invocable
|
||||
}
|
||||
|
||||
fun execute(scriptName: String, fnName: String, params: Map<String, Any>, logger: Logger): Any {
|
||||
return getEngine(scriptName).invokeFunction(fnName, params, Session.database, logger)
|
||||
}
|
||||
|
||||
fun preSave(scriptName: String, fnName: String, data: DataModel, logger: Logger): Any {
|
||||
return getEngine(scriptName).invokeFunction(fnName, data, Session.database, logger)
|
||||
}
|
||||
}
|
||||
@ -1,2 +1,4 @@
|
||||
insert into tenant_model(name, domain, created_by, modified_by) values ('compegence', 'https://www.compegence.com', 'system', 'system');
|
||||
insert into entity_model(name, tenant_id, created_by, modified_by) values ('vehicle', 'compegence', 'system', 'system');
|
||||
insert into tenant_model(name, domain, created_by, modified_by)
|
||||
values ('compegence', 'https://www.compegence.com', 'system', 'system');
|
||||
insert into entity_model(name, tenant_id, created_by, modified_by, pre_save_script, post_save_script)
|
||||
values ('vehicle', 'compegence', 'system', 'system', 'vehicle.kts', 'vehicle.kts');
|
||||
17
src/main/resources/scripts/vehicle.kts
Normal file
17
src/main/resources/scripts/vehicle.kts
Normal file
@ -0,0 +1,17 @@
|
||||
import com.restapi.domain.DataModel
|
||||
import io.ebean.Database
|
||||
import org.slf4j.Logger
|
||||
|
||||
fun execute(d: Map<String, Any>, db: Database, logger: Logger): Map<String, Any> {
|
||||
println("execute on $d")
|
||||
return d
|
||||
}
|
||||
|
||||
fun preSave(d: DataModel, db: Database, logger: Logger): Boolean {
|
||||
logger.warn("PreSave $d")
|
||||
return true
|
||||
}
|
||||
|
||||
fun postSave(d: DataModel, db: Database, logger: Logger) {
|
||||
println("PostSave $d")
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user