summaryrefslogtreecommitdiff
path: root/server/src/sessionserialiser.cc
blob: 10d449de31d6ed0c5984cc97aac04686b908e415 (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
/* -*- 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 "journalwriter.h"

#include "sessionparser.h"

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

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


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

SessionSerialiser::SessionSerialiser(std::string path, Session *session)
{
  this->session = session;
  this->path = path;
}

void SessionSerialiser::loadStr(const std::string &xml)
{
  //  SessionAutolock lock(*session);
  SessionParser parser;
  parser.parse(xml.data(), xml.length());
  JournalWriter *j = session->journal();
  j->currentuser = parser.userid;
  j->currentcpr = parser.patientid;
  std::vector<SessionParser::Entry>::iterator i = parser.entries.begin();
  while(i != parser.entries.end()) {
    j->addEntry(i->resume, i->macro, i->index);
    i++;
  }
}

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

  std::string xml;

  xml += "<?xml version='1.0' encoding='UTF-8'?>\n";
  xml += "<session timestamp=\""+itostr(time(NULL))+"\" "
    "id=\""+session->id()+"\">\n";

  JournalWriter *journal = session->journal();

  xml += "  <journal patientid=\"" + journal->currentcpr +
    "\" userid=\"" + journal->currentuser + "\">\n";

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

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

    i++;
  }

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

  return xml;
}

void SessionSerialiser::load()
{
  // read xml from file
  std::string filename = getSessionFilename(path, session->id());

  FILE *fp =  fopen(filename.c_str(), "r");
  char xml[2048];
  memset(xml, 0, sizeof(xml));
  fread(xml, sizeof(xml), 1, fp);
  fclose(fp);

  loadStr(xml);

  // delete file
  unlink(filename.c_str());

}

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

  std::string xml = saveStr();

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

#ifdef TEST_SESSIONSERIALISER
//deps: session.cc journalwriter.cc debug.cc configuration.cc mutex.cc journal_commit.cc sessionparser.cc saxparser.cc
//cflags: -I.. $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS)
//libs: $(PTHREAD_LIBS) $(EXPAT_LIBS)
#include "test.h"

#define SID "42"
#define SPATH "/tmp"

TEST_BEGIN;

std::string xml;

{
  Session session(SID);
  JournalWriter *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(SPATH, &session);
  xml = s.saveStr();
  s.loadStr(xml);
  std::string xml2 = s.saveStr();
  TEST_EQUAL_STR(xml, xml2, "Compare");
}

{
  Session session(SID);
  JournalWriter *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(SPATH, &session);
  xml = s.saveStr();
}

{
  Session session(SID);
  SessionSerialiser s(SPATH, &session);
  s.loadStr(xml);
  std::string xml2 = s.saveStr();
  TEST_EQUAL_STR(xml, xml2, "Compare");
}

{
  Session session(SID);
  JournalWriter *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(SPATH, &session);
  s.save();
}

{
  Session session(SID);
  SessionSerialiser s(SPATH, &session);
  s.load();
  std::string xml2 = s.saveStr();
  TEST_EQUAL_STR(xml, xml2, "Compare");
}


TEST_END;

#endif/*TEST_SESSIONSERIALISER*/