From 4edae3f518353bb21a02fcda2dfcff83c5a72fc3 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 26 Jan 2012 12:08:39 +0100 Subject: New onCommit scripting system. --- server/src/luaresume.cc | 121 ++++++------------------------------------------ 1 file changed, 13 insertions(+), 108 deletions(-) (limited to 'server/src/luaresume.cc') diff --git a/server/src/luaresume.cc b/server/src/luaresume.cc index 49de8be..1db4fb3 100644 --- a/server/src/luaresume.cc +++ b/server/src/luaresume.cc @@ -26,122 +26,27 @@ */ #include "luaresume.h" +#include +#include + #include "luautil.h" -#include "luapraxisd.h" #include "debug.h" #include -#define GLOBAL_POINTER "_pracroGlobalLUAObjectPointerThisShouldBeANameThatIsNotAccidentallyOverwritten" - -static int _value(lua_State *L) -{ - Pracro::checkParameters(L, - Pracro::T_STRING, - Pracro::T_END); - - std::string name = lua_tostring(L, lua_gettop(L)); - - lua_getglobal(L, GLOBAL_POINTER); - LUAResume *lua = (LUAResume*)lua_touserdata(L, lua_gettop(L)); - - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } - - std::string value = lua->value(name); - lua_pushstring(L, value.c_str()); - - return 1; -} - -LUAResume::LUAResume(Commit &c) - : commit(c) -{ - L = luaL_newstate(); - if(L == NULL) { - ERR(luaresume, "Could not create LUA state.\n"); - return; - } - - luaL_openlibs(L); - - lua_pushlightuserdata(L, this); // Push the pointer to 'this' instance - lua_setglobal(L, GLOBAL_POINTER); // Assign it to a global lua var. - - lua_register(L, "value", _value); - - register_praxisd(L); -} - -LUAResume::~LUAResume() -{ - lua_close(L); -} - -std::string LUAResume::value(std::string name) +LUAResume::LUAResume(Transaction &t, Commit &c) : LUAScript() { - if(commit.fields.find(name) == commit.fields.end()) { - ERR(luaresume, "LUAResume: No such field '%s'\n", name.c_str()); - return ""; + setEnv(LUAScript::ENV_PATIENTID, t.patientid); + setEnv(LUAScript::ENV_TEMPLATE, c.templ); + setEnv(LUAScript::ENV_MACRO, c.macro); + setEnv(LUAScript::ENV_USER, t.user); + + std::map::iterator i = c.fields.begin(); + while(i != c.fields.end()) { + addValue(i->first, i->second); + i++; } - - return commit.fields[name]; -} - -std::string LUAResume::run(std::string program) -{ - if(L == NULL) { - ERR(luaresume, "LUA state not initialized!"); - return ""; - } - - DEBUG(luaresume, "Running %s\n", program.c_str()); - - /* - lua_pushstring(L, value.toStdString().c_str()); - lua_setglobal(L, "value"); - - lua_pushstring(L, name.toStdString().c_str()); - lua_setglobal(L, "name"); - */ - - int top = lua_gettop(L); - - if(luaL_loadbuffer(L, program.c_str(), program.size(), - "lua resume generator")) { - ERR(luaresume, "loadbufer: %s\n", lua_tostring(L, lua_gettop(L))); - return ""; - } - - // Run the loaded code - if(lua_pcall(L, 0, LUA_MULTRET, 0)) { - ERR(luaresume, "pcall: %s\n" , lua_tostring(L, lua_gettop(L))); - return ""; - } - - if(top != lua_gettop(L) - 1) { - ERR(luaresume, "Program did not return a single value.\n"); - return ""; - } - - if(lua_isstring(L, lua_gettop(L)) == false) { - ERR(luaresume, "Program did not return a string value.\n"); - return ""; - } - - std::string res = lua_tostring(L, lua_gettop(L)); - lua_pop(L, 1); - - return res; -} - -void LUAResume::error(std::string message) -{ - ERR(luaresume, "LUA ERROR: %s\n", message.c_str()); } #ifdef TEST_LUARESUME -- cgit v1.2.3