00001
00002
00003 #include <cassert>
00004 #include <sstream>
00005
00006 #include "Catalog.h"
00007
00008 using namespace Yosokumo;
00009
00010 Catalog::Catalog() :
00011 userIdentifier ("ABCDEF0123456789"),
00012 userName (""),
00013 catalogLocation("")
00014 {}
00015
00016 Catalog::Catalog(
00017 const std::string &userIdentifier,
00018 const std::string &userName) :
00019 userIdentifier (userIdentifier),
00020 userName (userName),
00021 catalogLocation("")
00022 {}
00023
00024 Catalog::Catalog(const Catalog &rhs)
00025 {
00026 operator=(rhs);
00027 }
00028
00029 Catalog::~Catalog()
00030 {}
00031
00032 Catalog &Catalog::operator=(const Catalog& rhs)
00033 {
00034 if (this == &rhs)
00035 return *this;
00036
00037 copyCatalog(*this, rhs);
00038
00039 return *this;
00040 }
00041
00042
00043
00044 bool Catalog::operator==(const Catalog &rhs) const
00045 {
00046 if
00047 (!(
00048 userIdentifier == rhs.userIdentifier &&
00049 userName == rhs.userName &&
00050 catalogLocation == rhs.catalogLocation &&
00051 size() == rhs.size()
00052 ))
00053 return false;
00054
00055 StudyConstIterator iter;
00056
00057 for (iter = begin(); iter != end(); ++iter)
00058 {
00059 Study study = iter->second;
00060 const std::string studyIdentifier = study.getStudyIdentifier();
00061 Study rhsStudy;
00062
00063 if (!rhs.getStudy(studyIdentifier, rhsStudy))
00064 return false;
00065
00066 if (study != rhsStudy)
00067 return false;
00068 }
00069
00070 return true;
00071 }
00072
00073 bool Catalog::operator!=(const Catalog &rhs) const
00074 {
00075 return !(*this == rhs);
00076 }
00077
00078
00079
00080 void Catalog::copyCatalog(Catalog &t, const Catalog &s)
00081 {
00082 t.setUserIdentifier (s.getUserIdentifier() );
00083 t.setUserName (s.getUserName() );
00084 t.setCatalogLocation(s.getCatalogLocation());
00085
00086 StudyConstIterator iter;
00087
00088 for (iter = s.begin(); iter != s.end(); ++iter)
00089 t.addStudy(iter->second);
00090 }
00091
00092
00093
00094
00095 void Catalog::setUserIdentifier(const std::string &id)
00096 {
00097 userIdentifier = id;
00098 }
00099
00100 std::string Catalog::getUserIdentifier() const
00101 {
00102 return userIdentifier;
00103 }
00104
00105 void Catalog::setUserName(const std::string &name)
00106 {
00107 userName = name;
00108 }
00109
00110 std::string Catalog::getUserName() const
00111 {
00112 return userName;
00113 }
00114
00115 void Catalog::setCatalogLocation(const std::string &loc)
00116 {
00117 catalogLocation = loc;
00118 }
00119
00120 std::string Catalog::getCatalogLocation() const
00121 {
00122 return catalogLocation;
00123 }
00124
00125
00126
00127 bool Catalog::addStudy(const Study &newStudy, Study &oldStudy)
00128 {
00129 std::pair<std::string, Study> keyStudy =
00130 std::make_pair(newStudy.getStudyIdentifier(), newStudy);
00131
00132 std::pair<StudyIterator, bool> ret;
00133
00134 ret = studyCollection.insert(keyStudy);
00135
00136 if (ret.second)
00137 return true;
00138
00139 StudyIterator iter = ret.first;
00140 oldStudy = iter->second;
00141
00142 studyCollection.erase(ret.first);
00143
00144 ret = studyCollection.insert(keyStudy);
00145
00146 assert(ret.second);
00147
00148 return false;
00149 }
00150
00151 bool Catalog::addStudy(const Study &newStudy)
00152 {
00153 Study oldStudy;
00154
00155 return addStudy(newStudy, oldStudy);
00156 }
00157
00158 bool Catalog::removeStudy(const std::string &studyIdentifier)
00159 {
00160 return (studyCollection.erase(studyIdentifier) == 1);
00161 }
00162
00163 void Catalog::clearStudies()
00164 {
00165 studyCollection.clear();
00166 }
00167
00168 bool Catalog::getStudy(const std::string &studyIdentifier, Study &foundStudy) const
00169 {
00170 StudyConstIterator iter;
00171
00172 iter = studyCollection.find(studyIdentifier);
00173
00174 if (iter == studyCollection.end())
00175 return false;
00176
00177 foundStudy = iter->second;
00178
00179 return true;
00180 }
00181
00182 bool Catalog::containsStudy(const std::string &studyIdentifier) const
00183 {
00184 return (studyCollection.find(studyIdentifier) != studyCollection.end());
00185 }
00186
00187 int Catalog::size() const
00188 {
00189 return studyCollection.size();
00190 }
00191
00192 bool Catalog::isEmpty() const
00193 {
00194 return studyCollection.empty();
00195 }
00196
00197 Catalog::StudyIterator Catalog::begin()
00198 {
00199 return studyCollection.begin();
00200 }
00201
00202 Catalog::StudyConstIterator Catalog::begin() const
00203 {
00204 return studyCollection.begin();
00205 }
00206
00207 Catalog::StudyIterator Catalog::end()
00208 {
00209 return studyCollection.end();
00210 }
00211
00212 Catalog::StudyConstIterator Catalog::end() const
00213 {
00214 return studyCollection.end();
00215 }
00216
00217
00218
00219
00220 std::string Catalog::toString() const
00221 {
00222 return toStringInternal(false);
00223 }
00224
00225 std::string Catalog::toStringInternal(bool showAll) const
00226 {
00227 std::stringstream s;
00228
00229 s <<
00230 "Catalog:" << "\n" <<
00231 " userIdentifier = " << getUserIdentifier() << "\n" <<
00232 " userName = " << getUserName() << "\n"
00233 ;
00234
00235 if (showAll)
00236 s <<
00237 " catalogLocation = " << getCatalogLocation() << "\n"
00238 ;
00239
00240 s <<
00241 " Studies:" << "\n"
00242 ;
00243
00244 StudyConstIterator iter;
00245
00246 for (iter = begin(); iter != end(); ++iter)
00247 {
00248 Study study = iter->second;
00249 s << " " << study.getStudyIdentifier();
00250 s << " " << study.getStudyName() << "\n";
00251 }
00252
00253 return s.str();
00254 }
00255
00256