ctkXnatObject.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*=============================================================================
  2. Library: XNAT/Core
  3. Copyright (c) University College London,
  4. Centre for Medical Image Computing
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkXnatObject.h"
  16. #include "ctkXnatObjectPrivate.h"
  17. #include "ctkXnatConstants.h"
  18. #include "ctkXnatDataModel.h"
  19. #include "ctkXnatDefaultSchemaTypes.h"
  20. #include "ctkXnatSession.h"
  21. #include <QDateTime>
  22. #include <QDebug>
  23. #include <QStringList>
  24. #include <QVariant>
  25. //----------------------------------------------------------------------------
  26. ctkXnatObject::ctkXnatObject(const ctkXnatObject&)
  27. {
  28. throw ctkRuntimeException("Copy constructor not implemented");
  29. }
  30. //----------------------------------------------------------------------------
  31. ctkXnatObject::ctkXnatObject(ctkXnatObject* parent, const QString& schemaType)
  32. : d_ptr(new ctkXnatObjectPrivate())
  33. {
  34. this->setParent(parent);
  35. this->setSchemaType(schemaType);
  36. }
  37. //----------------------------------------------------------------------------
  38. ctkXnatObject::ctkXnatObject(ctkXnatObjectPrivate& dd, ctkXnatObject* parent, const QString& schemaType)
  39. : d_ptr(&dd)
  40. {
  41. this->setParent(parent);
  42. this->setSchemaType(schemaType);
  43. }
  44. //----------------------------------------------------------------------------
  45. ctkXnatObject::~ctkXnatObject()
  46. {
  47. Q_D(ctkXnatObject);
  48. foreach (ctkXnatObject* child, d->children)
  49. {
  50. delete child;
  51. }
  52. }
  53. //----------------------------------------------------------------------------
  54. QString ctkXnatObject::id() const
  55. {
  56. return property(ctkXnatObjectFields::ID);
  57. }
  58. //----------------------------------------------------------------------------
  59. void ctkXnatObject::setId(const QString& id)
  60. {
  61. setProperty(ctkXnatObjectFields::ID, id);
  62. }
  63. //----------------------------------------------------------------------------
  64. QString ctkXnatObject::name() const
  65. {
  66. return property(ctkXnatObjectFields::NAME);
  67. }
  68. //----------------------------------------------------------------------------
  69. void ctkXnatObject::setName(const QString& name)
  70. {
  71. setProperty(ctkXnatObjectFields::NAME, name);
  72. }
  73. //----------------------------------------------------------------------------
  74. QString ctkXnatObject::description() const
  75. {
  76. Q_D(const ctkXnatObject);
  77. return d->description;
  78. }
  79. //----------------------------------------------------------------------------
  80. void ctkXnatObject::setDescription(const QString& description)
  81. {
  82. Q_D(ctkXnatObject);
  83. d->description = description;
  84. }
  85. //----------------------------------------------------------------------------
  86. QString ctkXnatObject::childDataType() const
  87. {
  88. return "Resources";
  89. }
  90. QDateTime ctkXnatObject::lastModifiedTime()
  91. {
  92. Q_D(ctkXnatObject);
  93. QUuid queryId = this->session()->httpHead(this->resourceUri());
  94. QMap<QByteArray, QByteArray> header = this->session()->httpHeadSync(queryId);
  95. QVariant lastModifiedHeader = header.value("Last-Modified");
  96. QDateTime lastModifiedTime;
  97. if (lastModifiedHeader.isValid())
  98. {
  99. QStringList dateformates;
  100. // In case http date formate RFC 822 ( "Sun, 06 Nov 1994 08:49:37 GMT" )
  101. dateformates<<"ddd, dd MMM yyyy HH:mm:ss";
  102. // In case http date formate ANSI ( "Sun Nov 6 08:49:37 1994" )
  103. dateformates<<"ddd MMM d HH:mm:ss yyyy";
  104. // In case http date formate RFC 850 ( "Sunday, 06-Nov-94 08:49:37 GMT" )
  105. dateformates<<"dddd, dd-MMM-yy HH:mm:ss";
  106. QString dateText = lastModifiedHeader.toString();
  107. // Remove "GMT" addition at the end of the http timestamp
  108. if (dateText.indexOf("GMT") != -1)
  109. {
  110. dateText = dateText.left(dateText.length()-4);
  111. }
  112. foreach (QString format, dateformates)
  113. {
  114. lastModifiedTime = QDateTime::fromString(dateText, format);
  115. if (lastModifiedTime.isValid())
  116. break;
  117. }
  118. }
  119. if (lastModifiedTime.isValid() && d->lastModifiedTime < lastModifiedTime)
  120. this->setLastModifiedTime(lastModifiedTime);
  121. return lastModifiedTime;
  122. }
  123. void ctkXnatObject::setLastModifiedTime(const QDateTime &lastModifiedTime)
  124. {
  125. Q_D(ctkXnatObject);
  126. if (d->lastModifiedTime < lastModifiedTime)
  127. {
  128. d->lastModifiedTime = lastModifiedTime;
  129. }
  130. }
  131. //----------------------------------------------------------------------------
  132. QString ctkXnatObject::property(const QString& name) const
  133. {
  134. Q_D(const ctkXnatObject);
  135. ctkXnatObjectPrivate::PropertyMapConstInterator iter = d->properties.find(name);
  136. if (iter != d->properties.end())
  137. {
  138. return iter.value();
  139. }
  140. return QString::null;
  141. }
  142. //----------------------------------------------------------------------------
  143. void ctkXnatObject::setProperty(const QString& name, const QVariant& value)
  144. {
  145. Q_D(ctkXnatObject);
  146. d->properties.insert(name, value.toString());
  147. }
  148. //----------------------------------------------------------------------------
  149. const QMap<QString, QString>& ctkXnatObject::properties() const
  150. {
  151. Q_D(const ctkXnatObject);
  152. return d->properties;
  153. }
  154. //----------------------------------------------------------------------------
  155. ctkXnatObject* ctkXnatObject::parent() const
  156. {
  157. Q_D(const ctkXnatObject);
  158. return d->parent;
  159. }
  160. //----------------------------------------------------------------------------
  161. void ctkXnatObject::setParent(ctkXnatObject* parent)
  162. {
  163. Q_D(ctkXnatObject);
  164. if (d->parent != parent)
  165. {
  166. if (d->parent)
  167. {
  168. d->parent->remove(this);
  169. }
  170. if (parent)
  171. {
  172. parent->add(this);
  173. }
  174. }
  175. }
  176. //----------------------------------------------------------------------------
  177. QList<ctkXnatObject*> ctkXnatObject::children() const
  178. {
  179. Q_D(const ctkXnatObject);
  180. return d->children;
  181. }
  182. //----------------------------------------------------------------------------
  183. void ctkXnatObject::add(ctkXnatObject* child)
  184. {
  185. Q_D(ctkXnatObject);
  186. if (child->parent() != this)
  187. {
  188. child->d_func()->parent = this;
  189. }
  190. if (!d->children.contains(child))
  191. {
  192. d->children.push_back(child);
  193. }
  194. else
  195. {
  196. qWarning() << "ctkXnatObject::add(): Child already exists";
  197. }
  198. }
  199. //----------------------------------------------------------------------------
  200. void ctkXnatObject::remove(ctkXnatObject* child)
  201. {
  202. Q_D(ctkXnatObject);
  203. if (!d->children.removeOne(child))
  204. {
  205. qWarning() << "ctkXnatObject::remove(): Child does not exist";
  206. }
  207. }
  208. //----------------------------------------------------------------------------
  209. void ctkXnatObject::reset()
  210. {
  211. Q_D(ctkXnatObject);
  212. // d->properties.clear();
  213. d->children.clear();
  214. d->fetched = false;
  215. }
  216. //----------------------------------------------------------------------------
  217. bool ctkXnatObject::isFetched() const
  218. {
  219. Q_D(const ctkXnatObject);
  220. return d->fetched;
  221. }
  222. //----------------------------------------------------------------------------
  223. QString ctkXnatObject::schemaType() const
  224. {
  225. return this->property("xsiType");
  226. }
  227. //----------------------------------------------------------------------------
  228. void ctkXnatObject::setSchemaType(const QString& schemaType)
  229. {
  230. this->setProperty("xsiType", schemaType);
  231. }
  232. //----------------------------------------------------------------------------
  233. void ctkXnatObject::fetch()
  234. {
  235. Q_D(ctkXnatObject);
  236. if (!d->fetched)
  237. {
  238. this->fetchImpl();
  239. d->fetched = true;
  240. }
  241. }
  242. //----------------------------------------------------------------------------
  243. ctkXnatSession* ctkXnatObject::session() const
  244. {
  245. const ctkXnatObject* xnatObject = this;
  246. while (ctkXnatObject* parent = xnatObject->parent())
  247. {
  248. xnatObject = parent;
  249. }
  250. const ctkXnatDataModel* dataModel = dynamic_cast<const ctkXnatDataModel*>(xnatObject);
  251. return dataModel ? dataModel->session() : NULL;
  252. }
  253. //----------------------------------------------------------------------------
  254. void ctkXnatObject::download(const QString& /*zipFilename*/)
  255. {
  256. }
  257. //----------------------------------------------------------------------------
  258. void ctkXnatObject::upload(const QString& /*zipFilename*/)
  259. {
  260. }
  261. //----------------------------------------------------------------------------
  262. bool ctkXnatObject::exists() const
  263. {
  264. return this->session()->exists(this);
  265. }
  266. //----------------------------------------------------------------------------
  267. void ctkXnatObject::save()
  268. {
  269. this->session()->save(this);
  270. }
  271. //----------------------------------------------------------------------------
  272. void ctkXnatObject::fetchResources(const QString& path)
  273. {
  274. QString query = this->resourceUri() + path;
  275. ctkXnatSession* const session = this->session();
  276. QUuid queryId = session->httpGet(query);
  277. QList<ctkXnatObject*> resources = session->httpResults(queryId,
  278. ctkXnatDefaultSchemaTypes::XSI_RESOURCE);
  279. foreach (ctkXnatObject* resource, resources)
  280. {
  281. QString label = resource->property(ctkXnatObjectFields::LABEL);
  282. if (label.isEmpty())
  283. {
  284. label = "NO NAME";
  285. }
  286. resource->setProperty(ctkXnatObjectFields::LABEL, label);
  287. this->add(resource);
  288. }
  289. }
  290. //----------------------------------------------------------------------------
  291. void ctkXnatObject::erase()
  292. {
  293. this->session()->remove(this);
  294. this->parent()->remove(this);
  295. }