From 192e0f80bd2da45f1c612411fded2e805ac205de Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 26 May 2008 13:56:30 +0000 Subject: Completed the prefilling of the fields, from the db and pentominos, according to their timestamps. --- server/src/database.cc | 2 +- server/src/database.h | 4 +++- server/src/luaquerymapper.cc | 18 +++++++++++++++--- server/src/luaquerymapper.h | 5 ++++- server/src/queryparser.cc | 10 ++++++++++ server/src/queryparser.h | 4 +++- server/src/queryresult.h | 2 ++ server/src/server.cc | 19 +++++++++++-------- server/src/widgetgenerator.cc | 44 +++++++++++++++++++++++++++++++++++++++---- server/xml/example2.xml | 12 ++++++------ 10 files changed, 95 insertions(+), 25 deletions(-) (limited to 'server') diff --git a/server/src/database.cc b/server/src/database.cc index 1bee954..62454a2 100644 --- a/server/src/database.cc +++ b/server/src/database.cc @@ -96,7 +96,7 @@ void Database::commit(std::string user, Values Database::getValues(std::string cpr, - std::vector< std::string > &fields, + Fieldnames &fields, time_t oldest) { Values values; diff --git a/server/src/database.h b/server/src/database.h index 0958b15..8200129 100644 --- a/server/src/database.h +++ b/server/src/database.h @@ -43,6 +43,8 @@ public: }; typedef std::map< std::string, Value > Values; +typedef std::vector< std::string > Fieldnames; + class Database { public: Database(std::string hostname = "localhost", @@ -59,7 +61,7 @@ public: // Get a list of values from the db Values getValues(std::string cpr, - std::vector< std::string > &fieldnames, + Fieldnames &fieldnames, time_t oldest = 0); // Connect to the db diff --git a/server/src/luaquerymapper.cc b/server/src/luaquerymapper.cc index 67b780d..c99b48b 100644 --- a/server/src/luaquerymapper.cc +++ b/server/src/luaquerymapper.cc @@ -26,13 +26,18 @@ */ #include "luaquerymapper.h" +#include + static std::string loadresultstring(QueryResult &res, std::string group = "") { std::string s; + std::stringstream timestamp; timestamp << res.timestamp; std::map< std::string, std::string >::iterator v = res.values.begin(); while(v != res.values.end()) { - s += group + (*v).first + " = \"" + (*v).second + "\"\n"; + s += group + (*v).first + " = {}\n"; + s += group + (*v).first + ".value = \"" + (*v).second + "\"\n"; + s += group + (*v).first + ".timestamp = " + timestamp.str() + "\n"; v++; } @@ -81,7 +86,7 @@ LUAQueryMapper::~LUAQueryMapper() lua_close(L); } -std::string LUAQueryMapper::map(const std::string &mapper) +Value LUAQueryMapper::map(const std::string &mapper) { int s = luaL_loadbuffer(L, mapper.c_str(), mapper.size(), "mapper"); switch(s) { @@ -100,7 +105,14 @@ std::string LUAQueryMapper::map(const std::string &mapper) // Run the loaded code lua_pcall(L, 0, LUA_MULTRET, 0); - return lua_tostring(L, lua_gettop(L)); + Value v; + + v.timestamp = lua_tointeger(L, lua_gettop(L)); + lua_pop(L, 1); + v.value = lua_tostring(L, lua_gettop(L)); + lua_pop(L, 1); + + return v; } #ifdef TEST_LUAQUERYMAPPER diff --git a/server/src/luaquerymapper.h b/server/src/luaquerymapper.h index 838429d..d5e03b0 100644 --- a/server/src/luaquerymapper.h +++ b/server/src/luaquerymapper.h @@ -32,6 +32,9 @@ #include #include +// For class Value +#include "database.h" + /** * The LUAQueryMapper class takes the result of an external data query and * applies the associated map. @@ -45,7 +48,7 @@ public: * Applies the mapping program to the result-namespace, and returns the * resulting value. */ - std::string map(const std::string &mapper); + Value map(const std::string &mapper); private: lua_State *L; diff --git a/server/src/queryparser.cc b/server/src/queryparser.cc index 187bc76..fc64c76 100644 --- a/server/src/queryparser.cc +++ b/server/src/queryparser.cc @@ -29,6 +29,7 @@ QueryParser::QueryParser(std::string document) { this->document = document; + this->timestamp = 0; // Make sure we always contain a valid xml document. if(this->document == "") this->document = ""; @@ -40,8 +41,17 @@ QueryParser::QueryParser(std::string document) void QueryParser::startTag(std::string name, std::map< std::string, std::string> attributes) { + if(name == "results") { + // What to do here!? + } + + if(name == "result") { + this->timestamp = atol(attributes["timestamp"].c_str()); + } + if(name == "group") { QueryResult q; + q.timestamp = this->timestamp; stack.back()->groups[attributes["name"]] = q; stack.push_back(&stack.back()->groups[attributes["name"]]); } diff --git a/server/src/queryparser.h b/server/src/queryparser.h index 4367bba..ea952e7 100644 --- a/server/src/queryparser.h +++ b/server/src/queryparser.h @@ -27,6 +27,8 @@ #ifndef __PRACRO_QUERYPARSER_H__ #define __PRACRO_QUERYPARSER_H__ +#include + #include "queryresult.h" #include "saxparser.h" @@ -52,7 +54,7 @@ private: // For read int p; std::string document; - + time_t timestamp; std::vector< QueryResult * > stack; }; diff --git a/server/src/queryresult.h b/server/src/queryresult.h index 32cfa20..617d957 100644 --- a/server/src/queryresult.h +++ b/server/src/queryresult.h @@ -27,11 +27,13 @@ #ifndef __PRACRO_QUERYRESULT_H__ #define __PRACRO_QUERYRESULT_H__ +#include #include #include class QueryResult { public: + time_t timestamp; std::map< std::string, std::string > values; std::map< std::string, QueryResult > groups; }; diff --git a/server/src/server.cc b/server/src/server.cc index 77b3a6f..e85c35c 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -58,24 +58,28 @@ static void connection(TCPSocket &socket) Database db; - /* // // Handle commits // if(transaction.commits.size() > 0) { - time_t now = time(NULL); - Commits::iterator i = transaction.commits.begin(); while(i != transaction.commits.end()) { Commit &commit = *i; - Database db; - db.post(transaction.user, transaction.cpr, now, commit); - + /* Macro macro; MacroParser parser(commit.macro, macro); parser.parse(); + */ + + Macro macro; + macro.attributes["name"] = commit.macro; + macro.attributes["version"] = "1.0";//commit.version; + + db.commit(transaction.user, transaction.cpr, macro, commit.fields); + + /* std::string resume = resume_parser(macro.format.c_str(), commit); std::string journal_commit_addr = config()->lookup("journal_commit_addr"); @@ -84,11 +88,10 @@ static void connection(TCPSocket &socket) journal_commit(transaction.cpr.c_str(), transaction.user.c_str(), journal_commit_addr.c_str(), journal_commit_port, resume.c_str(), resume.length()); - + */ i++; } } - */ // // Handle requests diff --git a/server/src/widgetgenerator.cc b/server/src/widgetgenerator.cc index 5211188..52752cc 100644 --- a/server/src/widgetgenerator.cc +++ b/server/src/widgetgenerator.cc @@ -31,11 +31,15 @@ static void send_macro_widget(Macro ¯o, TCPSocket &socket, std::string tabs, LUAQueryMapper &mapper, - Database &db) + Values &values) { + std::string prefilled; + time_t timestamp = 0; + socket.write(tabs + "<" + widget.attributes["type"]); std::map< std::string, std::string >::iterator p = widget.attributes.begin(); + // Check if the field has a map, and fill in the value if it has... if(widget.attributes.find("map") != widget.attributes.end()) { std::string luamap; @@ -50,10 +54,22 @@ static void send_macro_widget(Macro ¯o, // printf("LUAMAP: %s\n", luamap.c_str()); fflush(stdout); - if(luamap != "") widget.attributes["value"] = mapper.map(luamap); + if(luamap != "") { + Value value = mapper.map(luamap); + widget.attributes["value"] = value.value; + timestamp = value.timestamp; + prefilled = "pentominos"; + } // widget.attributes.erase(widget.attributes.find("map")); } + // Check if there is a previously stored value in the db... + if(values.find(widget.attributes["name"]) != values.end() && + (prefilled == "" || values[widget.attributes["name"]].timestamp > timestamp)) { + widget.attributes["value"] = values[widget.attributes["name"]].value; + prefilled = "pracro"; + } + while(p != widget.attributes.end()) { if(p->first != "type" && p->first != "map") { socket.write(" " + p->first + "=\"" + p->second + "\""); @@ -61,6 +77,8 @@ static void send_macro_widget(Macro ¯o, p++; } + if(prefilled != "") socket.write(" prefilled=\"" + prefilled + "\""); + if(widget.widgets.size() == 0) { // If node is empty, use short tag form socket.write("/>\n"); return; @@ -70,13 +88,31 @@ static void send_macro_widget(Macro ¯o, std::vector< Widget >::iterator w = widget.widgets.begin(); while(w != widget.widgets.end()) { - send_macro_widget(macro, *w, socket, tabs + " ", mapper, db); + send_macro_widget(macro, *w, socket, tabs + " ", mapper, values); w++; } socket.write(tabs + "\n"); } +static void get_fields(Widget &widget, Fieldnames &fields) +{ + if(widget.attributes.find("value") != widget.attributes.end()) { + fields.push_back(widget.attributes["name"]); + } + + std::vector< Widget >::iterator w = widget.widgets.begin(); + while(w != widget.widgets.end()) { + get_fields(*w, fields); + w++; + } +} + void widgetgenerator(TCPSocket &socket, Macro ¯o, LUAQueryMapper &mapper, Database &db) { - send_macro_widget(macro, macro.window, socket, " ", mapper, db); + Fieldnames fields; + get_fields(macro.window, fields); + + Values values = db.getValues("2003791613", fields); + + send_macro_widget(macro, macro.window, socket, " ", mapper, values); } diff --git a/server/xml/example2.xml b/server/xml/example2.xml index 65164bc..4f2c790 100644 --- a/server/xml/example2.xml +++ b/server/xml/example2.xml @@ -9,15 +9,15 @@ -- LUA program - return right.axis + return math.pi, 1234567890 -- LUA program - return right.sphere + return right.sphere.value, right.sphere.timestamp -- LUA program - return right.cyl + return right.cyl.value, right.cyl.timestamp