From 0acabe1d44111a3a118116d526b415572caf8050 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 8 Jul 2019 12:43:17 +0200 Subject: New tabulator-remote module. --- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'kvision-modules/kvision-server-spring-boot/src/main/kotlin') diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index d2b4f7ea..ab64c50d 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -498,6 +498,57 @@ actual open class KVServiceManager actual constructor(val serviceClass: } } + /** + * Binds a given function of the receiver as a tabulator component source + * @param function a function of the receiver + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + noinline function: T.(Int?, Int?, List?, List?) -> RemoteData + ) { + val routeDef = "route${this::class.simpleName}${counter++}" + addRoute(HttpMethod.POST, "/kv/$routeDef") { req, res, ctx -> + val service = ctx.getBean(serviceClass.java) + val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) + if (jsonRpcRequest.params.size == 4) { + val param1 = getParameter(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + val param3 = getParameter?>(jsonRpcRequest.params[2]) + val param4 = getParameter?>(jsonRpcRequest.params[3]) + try { + val result = function.invoke(service, param1, param2, param3, param4) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error" + ) + ) + ) + } + } else { + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = "Invalid parameters" + ) + ) + ) + } + } + } + /** * @suppress internal function */ -- cgit