00001
00002
00003 #include <sstream>
00004 #include <cassert>
00005
00006 #include "Roster.h"
00007
00008 using namespace Yosokumo;
00009
00010
00011
00012 Roster::Roster() :
00013 studyIdentifier(""),
00014 studyName(""),
00015 rosterLocation("")
00016 {
00017 }
00018
00019 Roster::Roster(
00020 const std::string &studyIdentifier,
00021 const std::string &studyName) :
00022 studyIdentifier(studyIdentifier),
00023 studyName(studyName),
00024 rosterLocation("")
00025 {
00026 }
00027
00028
00029
00030 bool Roster::operator==(const Roster &rhs) const
00031 {
00032 if
00033 (!(
00034 studyIdentifier == rhs.studyIdentifier &&
00035 studyName == rhs.studyName &&
00036 rosterLocation == rhs.rosterLocation &&
00037 size() == rhs.size()
00038 ))
00039 return false;
00040
00041 RoleConstIterator iter;
00042
00043 for (iter = begin(); iter != end(); ++iter)
00044 {
00045 Role role = iter->second;
00046 const std::string userIdentifier = role.getUserIdentifier();
00047 Role rhsRole;
00048
00049 if (!rhs.getRole(userIdentifier, rhsRole))
00050 return false;
00051
00052 if (role != rhsRole)
00053 return false;
00054 }
00055
00056 return true;
00057 }
00058
00059 bool Roster::operator!=(const Roster &rhs) const
00060 {
00061 return !(*this == rhs);
00062 }
00063
00064
00065
00066
00067 void Roster::setStudyIdentifier(const std::string &id)
00068 {
00069 studyIdentifier = id;
00070 }
00071
00072 std::string Roster::getStudyIdentifier() const
00073 {
00074 return studyIdentifier;
00075 }
00076
00077 void Roster::setStudyName(const std::string &name)
00078 {
00079 studyName = name;
00080 }
00081
00082 std::string Roster::getStudyName() const
00083 {
00084 return studyName;
00085 }
00086
00087 void Roster::setRosterLocation(const std::string &loc)
00088 {
00089 rosterLocation = loc;
00090 }
00091
00092 std::string Roster::getRosterLocation() const
00093 {
00094 return rosterLocation;
00095 }
00096
00097
00098
00099 bool Roster::addRole(const Role &newRole, Role &oldRole)
00100 {
00101 std::pair<std::string, Role> keyRole =
00102 std::make_pair(newRole.getUserIdentifier(), newRole);
00103
00104 std::pair<RoleIterator, bool> ret;
00105
00106 ret = roleCollection.insert(keyRole);
00107
00108 if (ret.second)
00109 return true;
00110
00111 RoleIterator iter = ret.first;
00112 oldRole = iter->second;
00113
00114 roleCollection.erase(ret.first);
00115
00116 ret = roleCollection.insert(keyRole);
00117
00118 assert(ret.second);
00119
00120 return false;
00121 }
00122
00123 bool Roster::addRole(const Role &newRole)
00124 {
00125 Role oldRole;
00126
00127 return addRole(newRole, oldRole);
00128 }
00129
00130 bool Roster::removeRole(const std::string &userIdentifier)
00131 {
00132 return (roleCollection.erase(userIdentifier) == 1);
00133 }
00134
00135 void Roster::clearRoles()
00136 {
00137 roleCollection.clear();
00138 }
00139
00140 bool Roster::getRole(const std::string &userIdentifier, Role &foundRole) const
00141 {
00142 RoleConstIterator iter;
00143
00144 iter = roleCollection.find(userIdentifier);
00145
00146 if (iter == roleCollection.end())
00147 return false;
00148
00149 foundRole = iter->second;
00150
00151 return true;
00152 }
00153
00154 bool Roster::containsRole(std::string userIdentifier) const
00155 {
00156 return (roleCollection.find(userIdentifier) != roleCollection.end());
00157 }
00158
00159 int Roster::size() const
00160 {
00161 return roleCollection.size();
00162 }
00163
00164 bool Roster::isEmpty() const
00165 {
00166 return roleCollection.empty();
00167 }
00168
00169 Roster::RoleIterator Roster::begin()
00170 {
00171 return roleCollection.begin();
00172 }
00173
00174 Roster::RoleConstIterator Roster::begin() const
00175 {
00176 return roleCollection.begin();
00177 }
00178
00179 Roster::RoleIterator Roster::end()
00180 {
00181 return roleCollection.end();
00182 }
00183
00184 Roster::RoleConstIterator Roster::end() const
00185 {
00186 return roleCollection.end();
00187 }
00188
00189
00190
00191
00192 std::string Roster::toString() const
00193 {
00194 return toStringInternal(false);
00195 }
00196
00197 std::string Roster::toStringInternal(bool showAll) const
00198 {
00199 std::stringstream s;
00200
00201 s <<
00202 "Roster:" << "\n" <<
00203 " studyIdentifier = " << getStudyIdentifier() << "\n" <<
00204 " studyName = " << getStudyName() << "\n"
00205 ;
00206
00207 if (showAll)
00208 s <<
00209 " rosterLocation = " << getRosterLocation() << "\n"
00210 ;
00211
00212 s <<
00213 " Roles:" << "\n"
00214 ;
00215
00216 RoleConstIterator iter;
00217
00218 for (iter = begin(); iter != end(); ++iter)
00219 {
00220 Role role = iter->second;
00221 s << " " << role.getUserIdentifier();
00222 s << " " << role.getUserName() << "\n";
00223 }
00224
00225 return s.str();
00226 }
00227
00228