summaryrefslogtreecommitdiffstats
path: root/src/Common/UserLists/Util.cpp
blob: 483383e86686d1c432b7b11c5e05af82c48af8b2 (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
/*
 * Util.cpp
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "Util.h"
#include "UserList.h"
#include "UserListDiff.h"

namespace Mad {
namespace Common {
namespace UserLists {

void Util::serializeUserListEntry(const UserListEntry *user, XmlData::List::iterator entry) {
  std::set<Core::String> details = user->getDetailList();
  for(std::set<Core::String>::iterator detail = details.begin(); detail != details.end(); ++detail)
    entry->set(*detail, user->getDetail(*detail));

  entry->set("name", user->getName());
  entry->set("group", user->getGroup());
}

UserListEntry Util::deserializeUserListEntry(XmlData::List::const_iterator entry) {
  UserListEntry user(entry->get<const Core::String&>("name"), entry->get<const Core::String&>("group"));

  std::set<Core::String> details = entry->getChildren();
  for(std::set<Core::String>::iterator detail = details.begin(); detail != details.end(); ++detail) {
    if(*detail == "name" || *detail == "group")
      continue;

    user.setDetail(*detail, entry->get<const Core::String&>(*detail));
  }

  return user;
}

void Util::serializeUserList(const UserList *list, XmlData *data) {
  XmlData::List *userList = data->createList("users");

  for(UserList::const_iterator user = list->begin(); user != list->end(); ++user)
    serializeUserListEntry(&*user, userList->addEntry());
}

boost::shared_ptr<UserList> Util::deserializeUserList(const XmlData *data) {
  boost::shared_ptr<UserList> users(new UserList);

  const XmlData::List *userList = data->getList("users");

  if(userList) {
    for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user)
      users->addUser(deserializeUserListEntry(user));
  }

  return users;
}

void Util::serializeUserListDiff(const UserListDiff *diff, XmlData *data) {
  XmlData::List *userList = data->createList("addedUsers");
  for(std::set<UserListEntry>::const_iterator user = diff->getAddedUsers().begin(); user != diff->getAddedUsers().end(); ++user)
      serializeUserListEntry(&*user, userList->addEntry());

  userList = data->createList("deletedUsers");
  for(std::set<UserListEntry>::const_iterator user = diff->getDeletedUsers().begin(); user != diff->getDeletedUsers().end(); ++user)
    serializeUserListEntry(&*user, userList->addEntry());

  userList = data->createList("updatedUsers");
  const UserListDiff::EntryListMap::left_map &updatedUsers = diff->getUpdatedUsers().left;
  for(UserListDiff::EntryListMap::left_const_iterator user = updatedUsers.begin(); user != updatedUsers.end(); ++user) {
    XmlData::List *entryList = userList->addEntry()->createList("entry");
    serializeUserListEntry(&user->first, entryList->addEntry());
    serializeUserListEntry(&user->second, entryList->addEntry());
  }
}

boost::shared_ptr<UserListDiff> Util::deserializeUserListDiff(const XmlData *data) {
  boost::shared_ptr<UserListDiff> diff(new UserListDiff);

  const XmlData::List *userList = data->getList("addedUsers");
  if(userList) {
    for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user)
      diff->insertAddedUser(deserializeUserListEntry(user));
  }

  userList = data->getList("deletedUsers");
  if(userList) {
    for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user)
      diff->insertDeletedUser(deserializeUserListEntry(user));
  }

  userList = data->getList("updatedUsers");
  if(userList) {
    for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user) {
      const XmlData::List *entryList = user->getList("entry");
      if(entryList->getSize() != 2)
        continue;

      diff->insertUpdatedUser(deserializeUserListEntry(entryList->begin()), deserializeUserListEntry(entryList->begin()+1));
    }
  }

  return diff;
}

}
}
}