make script work
This commit is contained in:
@@ -296,9 +296,9 @@ fun main(args: Array<String>) {
|
||||
post("/{entity}/next", Entities::getNextSeqNo, Roles(adminRole, viewRole, appAdmin))
|
||||
get("/{entity}/{id}", Entities::view, Roles(adminRole, viewRole, appAdmin))
|
||||
post("/{entity}/search", Entities::search, Roles(adminRole, viewRole, appAdmin))
|
||||
post("/{entity}/execute", Entities::execute, Roles(adminRole, createRole, appAdmin))
|
||||
post("/{entity}", Entities::create, Roles(adminRole, createRole, appAdmin))
|
||||
|
||||
|
||||
put("/{entity}/{id}", Entities::update, Roles(adminRole, updateRole, appAdmin))
|
||||
patch("/{entity}/{id}", Entities::patch, Roles(adminRole, updateRole, appAdmin))
|
||||
delete("/{entity}/{id}", Entities::delete, Roles(adminRole, Role.Standard(Action.DELETE), appAdmin))
|
||||
|
||||
@@ -259,6 +259,25 @@ object Entities {
|
||||
)
|
||||
}
|
||||
|
||||
data class Execute(val script: String, val fn: String, val params: Map<String, Any>)
|
||||
|
||||
fun execute(ctx: Context) {
|
||||
val entity = ctx.pathParam("entity")
|
||||
|
||||
val body = ctx.bodyAsClass<Execute>()
|
||||
|
||||
//may be approval flow is configured?
|
||||
val setupEntity = database.find(EntityModel::class.java)
|
||||
.where()
|
||||
.eq("name", entity)
|
||||
.findOne()
|
||||
|
||||
ctx.json(
|
||||
Scripting.execute(body.script, body.fn, body.params, setupEntity)
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
fun create(ctx: Context) {
|
||||
|
||||
val entity = ctx.pathParam("entity")
|
||||
@@ -346,7 +365,7 @@ object Entities {
|
||||
)
|
||||
|
||||
if (setupEntity != null && !setupEntity.postSaveScript.isNullOrEmpty()) {
|
||||
Scripting.execute(setupEntity.postSaveScript!!, "postSave", dataModel)
|
||||
Scripting.execute(setupEntity.postSaveScript!!, "postSave", dataModel, setupEntity)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.security.spec.PKCS8EncodedKeySpec
|
||||
import java.security.spec.X509EncodedKeySpec
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import kotlin.jvm.optionals.getOrDefault
|
||||
|
||||
|
||||
@@ -185,8 +186,11 @@ object Session {
|
||||
|
||||
private fun seqName(entity: String) = "sequence_${entity}_${currentTenant()}"
|
||||
|
||||
private val seqCreationCache = ConcurrentHashMap<String, Int>()
|
||||
fun creatSeq(entity: String): Int {
|
||||
return database.sqlUpdate("CREATE SEQUENCE IF NOT EXISTS ${seqName(entity)} START 1;").execute()
|
||||
return seqCreationCache.computeIfAbsent(entity){
|
||||
database.sqlUpdate("CREATE SEQUENCE IF NOT EXISTS ${seqName(entity)} START 1;").execute()
|
||||
}
|
||||
}
|
||||
|
||||
fun nextUniqId(entity: String): String {
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.restapi.integ
|
||||
|
||||
import com.restapi.config.AppConfig.Companion.appConfig
|
||||
import com.restapi.domain.DataModel
|
||||
import com.restapi.domain.EntityModel
|
||||
import com.restapi.domain.Session.currentTenant
|
||||
import com.restapi.domain.Session.currentUser
|
||||
import com.restapi.domain.Session.database
|
||||
@@ -21,11 +22,11 @@ object Scripting {
|
||||
engine as Invocable
|
||||
}
|
||||
|
||||
fun execute(scriptName: String, fnName: String, params: Map<String, Any>): Any {
|
||||
return getEngine(scriptName).invokeFunction(fnName, params, database, logger, currentUser(), currentTenant())
|
||||
fun execute(scriptName: String, fnName: String, params: Map<String, Any>, entityModel: EntityModel?,): Any {
|
||||
return getEngine(scriptName).invokeFunction(fnName, params, entityModel, database, logger, currentUser(), currentTenant())
|
||||
}
|
||||
|
||||
fun execute(scriptName: String, fnName: String, dataModel: DataModel): Any {
|
||||
return getEngine(scriptName).invokeFunction(fnName, dataModel, database, logger, currentUser(), currentTenant())
|
||||
fun execute(scriptName: String, fnName: String, dataModel: DataModel, entityModel: EntityModel?,): Any {
|
||||
return getEngine(scriptName).invokeFunction(fnName, dataModel, entityModel, database, logger, currentUser(), currentTenant())
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,18 @@
|
||||
import com.restapi.domain.DataModel
|
||||
import com.restapi.domain.EntityModel
|
||||
import io.ebean.Database
|
||||
import org.slf4j.Logger
|
||||
|
||||
fun execute(d: Map<String, Any>, db: Database, logger: Logger, user: String, tenant: String): Map<String, Any> {
|
||||
fun execute(d: Map<String, Any>, entityModel: EntityModel?, db: Database, logger: Logger, user: String, tenant: String): Map<String, Any> {
|
||||
println("execute on $d")
|
||||
return d
|
||||
}
|
||||
|
||||
fun preSave(d: DataModel, db: Database, logger: Logger, user: String, tenant: String): Boolean {
|
||||
fun preSave(d: DataModel, entityModel: EntityModel?, db: Database, logger: Logger, user: String, tenant: String): Boolean {
|
||||
logger.warn("PreSave $d")
|
||||
return true
|
||||
}
|
||||
|
||||
fun postSave(d: DataModel, db: Database, logger: Logger, user: String, tenant: String) {
|
||||
fun postSave(d: DataModel, entityModel: EntityModel?, db: Database, logger: Logger, user: String, tenant: String) {
|
||||
println("PostSave $d")
|
||||
}
|
||||
Reference in New Issue
Block a user