summaryrefslogtreecommitdiff
path: root/server/src/journalwriter.cc
diff options
context:
space:
mode:
authordeva <deva>2010-01-06 07:47:58 +0000
committerdeva <deva>2010-01-06 07:47:58 +0000
commit0e819eb42b4d680a99ae7b04702bfc9510495aee (patch)
treea425aeb98c679a0891a974b6f93090343d51a4e8 /server/src/journalwriter.cc
parent582913fc18937c87afe7d351e66095dccb435a9e (diff)
New artefact connection class (to later wrap libartefact). New environment class to hold all global resources. Made ConnectionPool a template class. Split journal code up into two files (class from commit code).
Diffstat (limited to 'server/src/journalwriter.cc')
-rw-r--r--server/src/journalwriter.cc198
1 files changed, 198 insertions, 0 deletions
diff --git a/server/src/journalwriter.cc b/server/src/journalwriter.cc
new file mode 100644
index 0000000..b84018a
--- /dev/null
+++ b/server/src/journalwriter.cc
@@ -0,0 +1,198 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * journalwriter.cc
+ *
+ * Tue Jan 5 15:52:54 CET 2010
+ * Copyright 2010 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 "journalwriter.h"
+
+#include "debug.h"
+#include "journal_commit.h"
+
+static inline bool iswhitespace(char c)
+{
+ return c == ' ' || c == '\n' || c == '\t';
+}
+
+/**
+ * Remove all spaces, tabs and newline trailing the string.
+ */
+static std::string stripTrailingWhitepace(std::string str)
+{
+ if(str == "") return str;
+
+ ssize_t end = str.size() - 1;
+
+ while(end && iswhitespace(str[end])
+ && (end>0 && str[end-1] & 0x80) == false // Make sure we are not in a utf8 character.
+ ) {
+ end--;
+ }
+ end++;
+
+ return str.substr(0, end);
+}
+
+/**
+ * Find all lines longer than 'width', and insert a newline in the
+ * first backward occurring space.
+ */
+static std::string addNewlines(std::string str, size_t width)
+{
+ std::string output;
+
+ std::string fraction;
+ size_t linelen = 0;
+ for(size_t i = 0; i < str.size(); i++) {
+
+ fraction += str[i];
+
+ if(iswhitespace(str[i])
+ && (i>0 && str[i-1] & 0x80) == false // Make sure we are not in a utf8 character.
+ ) {
+ if(linelen + fraction.size() > width) {
+ output[output.size() - 1] = '\n';
+ linelen = 0;
+ }
+ output += fraction;
+ linelen += fraction.size();
+ fraction = "";
+ }
+
+ if(str[i] == '\n') linelen = 0;
+
+ }
+ output += fraction;
+
+ return output;
+}
+
+JournalWriter::JournalWriter(std::string host, unsigned short int port)
+{
+ this->host = host;
+ this->port = port;
+}
+
+void JournalWriter::addEntry(Transaction &transaction, Commit &commit,
+ std::string resume, Template *templ)
+{
+ size_t index = 0;
+ std::vector< Macro >::iterator i = templ->macros.begin();
+ while(i != templ->macros.end()) {
+ Macro &m = *i;
+ if(commit.macro == m.attributes["name"]) break;
+ index++;
+ i++;
+ }
+
+ if(index >= templ->macros.size()) {
+ PRACRO_ERR(journal, "Could not find macro %s in template %s\n",
+ commit.macro.c_str(), templ->attributes["name"].c_str());
+ // return;
+ } else {
+ PRACRO_DEBUG(journal, "Found macro %s as index %u in template %s\n",
+ commit.macro.c_str(), index, templ->attributes["name"].c_str());
+ }
+
+ // First run - initialize username and cpr.
+ if(currentuser == "" && entrylist.size() == 0) currentuser = transaction.user;
+ if(currentcpr == "" && entrylist.size() == 0) currentcpr = transaction.cpr;
+
+ PRACRO_DEBUG(journal, "addEntry: template(%s)\n", templ->attributes["name"].c_str());
+
+ // Add the template resume as the header (ie. first entry) of the journal entry.
+ if(entrylist.size() == 0 && templ->attributes["name"] != "") {
+ std::string template_resume = templ->attributes["resume"];
+
+ PRACRO_DEBUG(journal, "TemplateResume: %s\n", template_resume.c_str());
+
+ if(template_resume != "") {
+ ResumeEntry re;
+ re.resume = template_resume;
+ re.macro = "template_header";
+ entrylist[-1] = re; // Make sure it comes first.
+ }
+ }
+
+ // Test if the username or the cpr has changed... if so, commit and clear the list.
+ if(currentuser != transaction.user || currentcpr != transaction.cpr) {
+ this->commit();
+ entrylist.clear();
+ }
+
+ // Strip trailing whitespace, and add newlines.
+ std::string r = stripTrailingWhitepace(addNewlines(resume, 60));
+ std::string m = commit.macro;
+
+ ResumeEntry re;
+ re.resume = r;
+ re.macro = m;
+ entrylist[index] = re;
+}
+
+void JournalWriter::commit()
+{
+ std::string resume;
+
+ // Iterate through all resumes, and create a string containing them all.
+ std::map< int, ResumeEntry >::iterator i = entrylist.begin();
+ while(i != entrylist.end()) {
+ if(resume != "") resume += "\n\n";
+ // resume += i->macro + "\n";
+ resume += i->second.resume;
+ i++;
+ }
+
+ if(resume == "") return;
+
+ // Connect to praxisuploadserver and commit all resumes in one bulk.
+ journal_commit(currentcpr.c_str(), currentuser.c_str(),
+ host.c_str(), port,
+ resume.c_str(), resume.size());
+}
+
+#ifdef TEST_JOURNALWRITER
+//Additional dependency files
+//deps:
+//Required cflags (autoconf vars may be used)
+//cflags:
+//Required link options (autoconf vars may be used)
+//libs:
+#include "test.h"
+
+TEST_BEGIN;
+
+std::string text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\neiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \n\n \t";
+
+std::string resume = stripTrailingWhitepace(addNewlines(text, 60));
+printf("[%s]\n", resume.c_str());
+
+resume = stripTrailingWhitepace(addNewlines("", 60));
+printf("[%s]\n", resume.c_str());
+
+// TODO: Put some testcode here (see test.h for usable macros).
+
+TEST_END;
+
+#endif/*TEST_JOURNALWRITER*/