summaryrefslogtreecommitdiff
path: root/server/src/sessionserialiser.cc
blob: 36d0a0dc8ad15a542e3eb5894f4f952d25aaf94c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            sessionserialiser.cc
 *
 *  Thu May 20 11:26:18 CEST 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 "sessionserialiser.h"

#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>

#include "journal.h"

#include "sessionparser.h"
#include "database.h"

#include "xml_encode_decode.h"
//#include "base64.h"

#include "environment.h"

#include <stdio.h>
#include <string.h>

#define PRE "pracro_session"

std::string getSessionFilename(const std::string &path,
                               const std::string &sessionid)
{
  return path + "/"PRE"." + sessionid;
}


static std::string itostr(int i)
{
  char sid[32];
  snprintf(sid, sizeof(sid), "%d", i);
  return sid;
}

SessionSerialiser::SessionSerialiser(Environment *e, std::string path)
 : env(e)
{
  this->path = path;
}

#define XENC(s) xml_encode(s)
#define XDEC(s) xml_decode(s)

//#define BENC(s) base64encode(s)
//#define BDEC(s) base64decode(s)

Session *SessionSerialiser::loadStr(const std::string &xml)
{
  //  SessionAutolock lock(*session);
  SessionParser parser;
  parser.parse(xml.data(), xml.length());

  Session *session = new Session(env,
                                 XDEC(parser.sessionid),
                                 XDEC(parser.patientid),
                                 XDEC(parser.templ));
  session->isreadonly = parser.status == "readonly";
  Journal *j = session->journal();
  //  j->setUser(XDEC(parser.userid));
  j->setPatientID(XDEC(parser.patientid));
  std::vector<SessionParser::Entry>::iterator i = parser.entries.begin();
  while(i != parser.entries.end()) {
    j->addEntry(XDEC(i->resume), XDEC(i->macro), XDEC(i->user), i->index);
    i++;
  }

  //  session->database()->restore(XDEC(parser.database));

  return session;
}

std::string SessionSerialiser::saveStr(Session *session)
{
  //  SessionAutolock lock(*session);

  std::string xml;

  xml += "<?xml version='1.0' encoding='UTF-8'?>\n";
  xml += "<session timestamp=\"" + itostr(time(NULL)) + "\""
    " status=\"" + XENC(session->isreadonly?"readonly":"") + "\""
    " id=\"" + XENC(session->id()) + "\""
    " template=\"" + XENC(session->templ) + "\""
    " patientid=\"" + XENC(session->patientid) + "\">\n";

  Journal *journal = session->journal();

  xml += "  <journal patientid=\"" + XENC(journal->patientID()) + "\">\n";

  std::map< int, Journal::ResumeEntry >::iterator i =
    journal->entrylist.begin();
  while(i != journal->entrylist.end()) {

    xml += "    <entry index=\""+itostr(i->first) + "\""
      " macro=\"" + XENC(i->second.macro) + "\""
      " user=\"" + XENC(i->second.user) + "\">\n";
    xml += "      <resume>" + XENC(i->second.resume) + "</resume>\n";
    xml += "    </entry>\n";

    i++;
  }

  xml += "  </journal>\n";

  std::string dbtype = "pgsql";
  xml += "  <database type=\""+dbtype+"\">"+
    //    XENC(session->database()->serialise())+
    "</database>\n";

  xml += "</session>\n";

  return xml;
}

Session *SessionSerialiser::load(const std::string &sessionid)
{
  // read xml from file
  std::string filename = getSessionFilename(path, sessionid);

  FILE *fp =  fopen(filename.c_str(), "r");
  std::string xml;
  while(!feof(fp)) {
    char str[64];
    memset(str, 0, sizeof(str));
    fread(str, sizeof(str) - 1, 1, fp);
    xml += str;
  }
  fclose(fp);

  Session *session = loadStr(xml);

  // delete file
  unlink(filename.c_str());
  
  return session;
}

void SessionSerialiser::save(Session *session)
{
  std::string filename = getSessionFilename(path, session->id());

  std::string xml = saveStr(session);

  // write xml to file
  FILE *fp =  fopen(filename.c_str(), "w");
  fwrite(xml.data(), xml.size(), 1, fp);
  fclose(fp);
}

static inline bool isxmlfile(std::string name)
{
  if(name.find(PRE,0) == std::string::npos) return false;

  struct stat s;
  stat(name.c_str(), &s);

  if(S_ISREG(s.st_mode) == false) return false;
  
  return true;
}

Session *SessionSerialiser::findFromTupple(const std::string &patientid,
                                           const std::string &templ)
{
  DEBUG(sessionserialiser, "Looking for: PatientID %s - Template %s\n",
        patientid.c_str(), templ.c_str());

  DIR *dir = opendir(path.c_str());
  if(!dir) {
    ERR(sessionserialiser, "Could not open directory: %s - %s\n",
        path.c_str(), strerror(errno));
    return NULL;
  }

  struct dirent *dirent;
  while( (dirent = readdir(dir)) != NULL ) {

    std::string filename = path+"/"+dirent->d_name;
    
    DEBUG(sessionserialiser, "Looking at file: %s\n", filename.c_str());

    if(isxmlfile(filename)) {

      DEBUG(sessionserialiser, "Is xml file\n");

      // Load session file
      FILE *fp =  fopen(filename.c_str(), "r");
      std::string xml;
      while(!feof(fp)) {
        char str[64];
        memset(str, 0, sizeof(str));
        fread(str, sizeof(str) - 1, 1, fp);
        xml += str;
      }
      fclose(fp);

      Session *session = loadStr(xml);

      DEBUG(sessionserialiser, "PatientID %s - Template %s\n", 
            session->patientid.c_str(),
            session->templ.c_str());

      if(session->patientid == patientid &&
         session->templ == templ) {
        closedir(dir);
        unlink(filename.c_str());
        return session;
      }
    }
  }

  closedir(dir);

  return NULL;
}

#ifdef TEST_SESSIONSERIALISER
//deps: session.cc journal.cc debug.cc configuration.cc mutex.cc journal_commit.cc sessionparser.cc saxparser.cc xml_encode_decode.cc database.cc pracrodaopgsql.cc pracrodaotest.cc pracrodao.cc journal_uploadserver.cc log.cc environment.cc semaphore.cc artefact.cc macrolist.cc templatelist.cc entitylist.cc inotify.cc versionstr.cc exception.cc macroheaderparser.cc templateheaderparser.cc
//cflags: -I.. $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS) $(PQXX_CFLAGS) -DWITHOUT_ARTEFACT
//libs: $(PTHREAD_LIBS) $(EXPAT_LIBS) $(PQXX_LIBS)
#include "test.h"

#include <stdio.h>

#define SID "42"
#define PID "1234567890"
#define TID "sometemplate"

TEST_BEGIN;

// Make sure we start out on an empty session directory.
std::string spath = "/tmp/test_sessionserialiser";
while(mkdir(spath.c_str(), 0777) == -1 && errno == EEXIST) {
  spath += "X";
}

std::string xml;

debug_parse("+all");

Environment env;

{
  FILE *fp = fopen("/tmp/"PRE".42", "w");
  fclose(fp);
  TEST_TRUE(isxmlfile("/tmp/"PRE".42"), "Xml file");
  unlink("/tmp/"PRE".42");

  fp = fopen("/tmp/pracro_diller.42", "w");
  fclose(fp);
  TEST_FALSE(isxmlfile("/tmp/pracro_diller.42"), "Txt file");
  unlink("/tmp/pracro_diller.42");

  TEST_FALSE(isxmlfile("/tmp/"PRE".42"), "No file");
  TEST_FALSE(isxmlfile("/tmp/badname"), "No file, bad name");
}

{
  Session session(&env, SID, PID, TID);
  Journal *j = session.journal();
  j->addEntry("some text", "macro1", 0);
  j->addEntry("some more text", "macro2", 2);
  j->addEntry("yet some more text", "macro3", 1);
  SessionSerialiser s(&env, spath);
  xml = s.saveStr(&session);
  s.loadStr(xml);
  std::string xml2 = s.saveStr(&session);
  TEST_EQUAL_STR(xml, xml2, "Compare");
}

{
  Session session(&env, SID, PID, TID);
  Journal *j = session.journal();
  j->addEntry("some text", "macro1", 0);
  j->addEntry("some more text", "macro2", 2);
  j->addEntry("yet some more text", "macro3", 1);
  SessionSerialiser s(&env, spath);
  xml = s.saveStr(&session);
}
/*
{
  Session session(SID, PID, TID);
  SessionSerialiser s(spath);
  s.loadStr(xml);
  std::string xml2 = s.saveStr(&session);
  TEST_EQUAL_STR(xml, xml2, "Compare");
}
*/
{
  Session session(&env, SID, PID, TID);
  Journal *j = session.journal();
  j->addEntry("some text", "macro1", 0);
  j->addEntry("some more text", "macro2", 2);
  j->addEntry("yet some more text", "macro3", 1);
  SessionSerialiser s(&env, spath);
  s.save(&session);
}
/*
{
  Session session(SID, PID, TID);
  SessionSerialiser s(spath);
  s.load(SID);
  std::string xml2 = s.saveStr(&session);
  TEST_EQUAL_STR(xml, xml2, "Compare");
}
*/

{
  Session session(&env, SID, PID, TID);
  SessionSerialiser s(&env, spath);
  s.save(&session);
  Session *s1 = s.findFromTupple(PID, TID);
  TEST_NOTEQUAL(s1, NULL, "Found it?");
  TEST_EQUAL(s1->id(), SID, "Compare found tuple.");
  TEST_EQUAL(s1->patientid, PID, "Compare found tuple.");
  TEST_EQUAL(s1->templ, TID, "Compare found tuple.");
  delete s1;
}

TEST_END;

#endif/*TEST_SESSIONSERIALISER*/