summaryrefslogtreecommitdiff
path: root/server/src/luapraxisd.cc
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/luapraxisd.cc')
-rw-r--r--server/src/luapraxisd.cc241
1 files changed, 241 insertions, 0 deletions
diff --git a/server/src/luapraxisd.cc b/server/src/luapraxisd.cc
new file mode 100644
index 0000000..6025aa5
--- /dev/null
+++ b/server/src/luapraxisd.cc
@@ -0,0 +1,241 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * luapraxisd.cc
+ *
+ * Wed Apr 27 11:59:53 CEST 2011
+ * Copyright 2011 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Pracro.
+ *
+ * Pracro is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Pracro is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Pracro; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "luapraxisd.h"
+
+#include "praxisd.h"
+
+#include <lauxlib.h>
+#include <strings.h>
+
+#include "debug.h"
+
+#define luaL_checkbool(L, i) \
+ (lua_isboolean(L,i) ? lua_toboolean(L,i) : luaL_checkint(L,i))
+
+typedef struct px_userdata {
+ Praxisd *px;
+} px_userdata;
+
+static int px_addcave(lua_State *L)
+{
+ px_userdata *pxu;
+ pxu = (px_userdata *)luaL_checkudata(L, 1, "Praxisd");
+ luaL_argcheck(L, pxu, 1, "Praxisd expected");
+
+ const char *cpr = luaL_checkstring(L, 2);
+ const char *cave = luaL_checkstring(L, 3);
+
+ std::string sogeord;
+ std::string sogetxt;
+
+ std::vector<Praxisd::cave_t> cavelist = pxu->px->diverse_get_cave("");
+ std::vector<Praxisd::cave_t>::iterator i = cavelist.begin();
+ while(i != cavelist.end()) {
+ Praxisd::cave_t &c = *i;
+ if(strcasecmp(cave, c.cave.c_str()) == 0) {
+ pxu->px->add_sogeord(cpr, c.sogenr, "");
+ return 0;
+ }
+ i++;
+ }
+
+ pxu->px->add_sogeord(cpr, "CA0003", cave); // CA0003 == 'ANDET'
+
+ return 0;
+}
+
+static int px_getcave(lua_State *L)
+{
+ px_userdata *pxu;
+ pxu = (px_userdata *)luaL_checkudata(L, 1, "Praxisd");
+ luaL_argcheck(L, pxu, 1, "Praxisd expected");
+
+ const char *cpr = luaL_checkstring(L, 2);
+
+ std::vector<Praxisd::cave_t> cavelist;
+
+ Praxisd::patient_t pat = pxu->px->patient_get_by_cpr(cpr);
+ std::vector<Praxisd::sogeord_t>::iterator i = pat.sogeord.begin();
+ while(i != pat.sogeord.end()) {
+ Praxisd::sogeord_t &s = *i;
+ if(s.sogenr[0] == 'C') {
+ std::string csogenr = s.sogenr.substr(1, s.sogenr.length() - 1);
+ std::vector<Praxisd::cave_t> cl = pxu->px->diverse_get_cave(csogenr);
+ if(cl.size() == 1) {
+ if(cl[0].cave == "ANDET") cl[0].cave = s.sogetxt;
+ cavelist.push_back(cl[0]);
+ }
+ }
+
+ i++;
+ }
+
+ lua_createtable(L, 0, cavelist.size());
+ int top = lua_gettop(L);
+
+ for(size_t i = 0; i < cavelist.size(); i++) {
+ lua_pushstring(L, cavelist[i].cave.c_str());
+ lua_rawseti(L, top, i);
+ }
+
+ return 1;
+}
+
+static int px_cavelist(lua_State *L)
+{
+ px_userdata *pxu;
+ pxu = (px_userdata *)luaL_checkudata(L, 1, "Praxisd");
+ luaL_argcheck(L, pxu, 1, "Praxisd expected");
+
+ const char *sogenr = luaL_checkstring(L, 2);
+
+ std::vector<Praxisd::cave_t> cavelist = pxu->px->diverse_get_cave(sogenr);
+
+ lua_createtable(L, 0, cavelist.size());
+ int top = lua_gettop(L);
+
+ for(size_t i = 0; i < cavelist.size(); i++) {
+ lua_pushstring(L, cavelist[i].cave.c_str());
+ lua_rawseti(L, top, i);
+ }
+
+ return 1;
+}
+
+static int px_new(lua_State *L)
+{
+ const char *host = luaL_checkstring(L, 1);
+ int port = luaL_checknumber(L, 2);
+
+ px_userdata *pxu;
+ pxu = (px_userdata *)lua_newuserdata(L, sizeof(px_userdata));
+
+ luaL_getmetatable(L, "Praxisd");
+ lua_setmetatable(L, -2);
+
+ pxu->px = new Praxisd(host, port);
+
+ return 1;
+}
+
+static int px_gc(lua_State *L)
+{
+ px_userdata *pxu;
+
+ pxu = (px_userdata *)luaL_checkudata(L, 1, "Praxisd");
+ luaL_argcheck(L, pxu, 1, "Praxisd expected");
+
+ delete pxu->px;
+
+ return 0;
+}
+
+static const struct luaL_Reg px_meths[] = {
+ {"__gc" ,px_gc},
+ {"cavelist", px_cavelist},
+ {"getcave", px_getcave},
+ {"addcave", px_addcave},
+ {NULL, NULL}
+};
+
+static const struct luaL_reg px_funcs[] = {
+ {"new", px_new},
+ {NULL, NULL}
+};
+
+void register_praxisd(lua_State *L)
+{
+ luaL_newmetatable(L, "Praxisd");
+ lua_pushliteral(L, "__index");
+ lua_pushvalue(L, -2);
+ lua_rawset(L, -3);
+ luaL_register(L, NULL, px_meths);
+ luaL_openlib (L, "Praxisd", px_funcs, 0);
+}
+
+#ifdef TEST_LUAPRAXISD
+//deps: praxisd.cc debug.cc saxparser.cc log.cc
+//cflags: -I.. $(LUA_CFLAGS) $(CURL_CFLAGS) $(EXPAT_CFLAGS)
+//libs: $(LUA_LIBS) $(CURL_LIBS) $(EXPAT_LIBS)
+#include "test.h"
+
+TEST_BEGIN;
+
+lua_State *L = luaL_newstate();
+if(L == NULL) {
+ ERR(luaresume, "Could not create LUA state.\n");
+}
+
+luaL_openlibs(L);
+
+register_praxisd(L);
+
+/////
+std::string program =
+ "px = Praxisd.new('localhost', 10000)\n"
+ "cl = px:cavelist('')\n"
+ "for i=1,#cl do\n"
+ " print(cl[i])\n"
+ "end\n"
+ "\n"
+ "--pcl = px:addcave('1505050505', 'LUMIGAN')\n"
+ "\n"
+ "pcl = px:addcave('1505050505', os.date('%H%M%S'))\n"
+ "\n"
+ "pcl = px:getcave('1505050505')\n"
+ "for i=1,#pcl do\n"
+ " print('p: ' .. pcl[i])\n"
+ "end\n"
+ ;
+
+//int top = lua_gettop(L);
+
+if(luaL_loadbuffer(L, program.c_str(), program.size(), "luapraxisd test")) {
+ ERR(luaresume, "loadbufer: %s\n", lua_tostring(L, lua_gettop(L)));
+}
+
+if(lua_pcall(L, 0, LUA_MULTRET, 0)) {
+ ERR(luaresume, "pcall: %s\n" , lua_tostring(L, lua_gettop(L)));
+}
+/*
+if(top != lua_gettop(L) - 1) {
+ ERR(luaresume, "Program did not return a single value.\n");
+}
+
+if(lua_isstring(L, lua_gettop(L)) == false) {
+ ERR(luaresume, "Program did not return a string value.\n");
+}
+
+std::string res = lua_tostring(L, lua_gettop(L));
+lua_pop(L, 1);
+*/
+lua_close(L);
+
+TEST_END;
+
+#endif/*TEST_LUAPRAXISD*/