ctkXnatObject.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 "ctkXnatDataModel.h"
  18. #include "ctkXnatSession.h"
  19. #include "ctkXnatDefaultSchemaTypes.h"
  20. #include <QDateTime>
  21. #include <QDebug>
  22. #include <QStringList>
  23. #include <QVariant>
  24. //----------------------------------------------------------------------------
  25. ctkXnatObject::ctkXnatObject(const ctkXnatObject&)
  26. {
  27. throw ctkRuntimeException("Copy constructor not implemented");
  28. }
  29. //----------------------------------------------------------------------------
  30. ctkXnatObject::ctkXnatObject(ctkXnatObject* parent, const QString& schemaType)
  31. : d_ptr(new ctkXnatObjectPrivate())
  32. {
  33. this->setParent(parent);
  34. this->setSchemaType(schemaType);
  35. }
  36. //----------------------------------------------------------------------------
  37. ctkXnatObject::ctkXnatObject(ctkXnatObjectPrivate& dd, ctkXnatObject* parent, const QString& schemaType)
  38. : d_ptr(&dd)
  39. {
  40. this->setParent(parent);
  41. this->setSchemaType(schemaType);
  42. }
  43. //----------------------------------------------------------------------------
  44. ctkXnatObject::~ctkXnatObject()
  45. {
  46. Q_D(ctkXnatObject);
  47. foreach (ctkXnatObject* child, d->children)
  48. {
  49. delete child;
  50. }
  51. }
  52. //----------------------------------------------------------------------------
  53. QString ctkXnatObject::id() const
  54. {
  55. return property("ID");
  56. }
  57. //----------------------------------------------------------------------------
  58. void ctkXnatObject::setId(const QString& id)
  59. {
  60. setProperty("ID", id);
  61. }
  62. //----------------------------------------------------------------------------
  63. QString ctkXnatObject::name() const
  64. {
  65. return property("name");
  66. }
  67. //----------------------------------------------------------------------------
  68. void ctkXnatObject::setName(const QString& name)
  69. {
  70. setProperty("name", name);
  71. }
  72. //----------------------------------------------------------------------------
  73. QString ctkXnatObject::description() const
  74. {
  75. return property("description");
  76. }
  77. //----------------------------------------------------------------------------
  78. void ctkXnatObject::setDescription(const QString& description)
  79. {
  80. setProperty("description", description);
  81. }
  82. //----------------------------------------------------------------------------
  83. QString ctkXnatObject::childDataType() const
  84. {
  85. return "Resources";
  86. }
  87. QDateTime ctkXnatObject::lastModifiedTimeOnServer()
  88. {
  89. Q_D(ctkXnatObject);
  90. QUuid queryId = this->session()->httpHead(this->resourceUri());
  91. QMap<QByteArray, QByteArray> header = this->session()->httpHeadSync(queryId);
  92. QVariant lastModifiedHeader = header.value("Last-Modified");
  93. QDateTime lastModifiedTime;
  94. if (lastModifiedHeader.isValid())
  95. {
  96. QStringList dateformates;
  97. // In case http date formate RFC 822 ( "Sun, 06 Nov 1994 08:49:37 GMT" )
  98. dateformates<<"ddd, dd MMM yyyy HH:mm:ss";
  99. // In case http date formate ANSI ( "Sun Nov 6 08:49:37 1994" )
  100. dateformates<<"ddd MMM d HH:mm:ss yyyy";
  101. // In case http date formate RFC 850 ( "Sunday, 06-Nov-94 08:49:37 GMT" )
  102. dateformates<<"dddd, dd-MMM-yy HH:mm:ss";
  103. QString dateText = lastModifiedHeader.toString();
  104. // Remove "GMT" addition at the end of the http timestamp
  105. if (dateText.indexOf("GMT") != -1)
  106. {
  107. dateText = dateText.left(dateText.length()-4);
  108. }
  109. foreach (QString format, dateformates)
  110. {
  111. lastModifiedTime = QDateTime::fromString(dateText, format);
  112. if (lastModifiedTime.isValid())
  113. break;
  114. }
  115. }
  116. return lastModifiedTime;
  117. }
  118. void ctkXnatObject::setLastModifiedTime(const QDateTime &lastModifiedTime)
  119. {
  120. Q_D(ctkXnatObject);
  121. if (d->lastModifiedTime < lastModifiedTime)
  122. {
  123. d->lastModifiedTime = lastModifiedTime;
  124. }
  125. }
  126. //----------------------------------------------------------------------------
  127. QString ctkXnatObject::property(const QString& name) const
  128. {
  129. Q_D(const ctkXnatObject);
  130. ctkXnatObjectPrivate::PropertyMapConstInterator iter = d->properties.find(name);
  131. if (iter != d->properties.end())
  132. {
  133. return iter.value();
  134. }
  135. return QString::null;
  136. }
  137. //----------------------------------------------------------------------------
  138. void ctkXnatObject::setProperty(const QString& name, const QVariant& value)
  139. {
  140. Q_D(ctkXnatObject);
  141. d->properties.insert(name, value.toString());
  142. }
  143. //----------------------------------------------------------------------------
  144. const QMap<QString, QString>& ctkXnatObject::properties() const
  145. {
  146. Q_D(const ctkXnatObject);
  147. return d->properties;
  148. }
  149. //----------------------------------------------------------------------------
  150. ctkXnatObject* ctkXnatObject::parent() const
  151. {
  152. Q_D(const ctkXnatObject);
  153. return d->parent;
  154. }
  155. //----------------------------------------------------------------------------
  156. void ctkXnatObject::setParent(ctkXnatObject* parent)
  157. {
  158. Q_D(ctkXnatObject);
  159. if (d->parent != parent)
  160. {
  161. if (d->parent)
  162. {
  163. d->parent->remove(this);
  164. }
  165. if (parent)
  166. {
  167. parent->add(this);
  168. }
  169. }
  170. }
  171. //----------------------------------------------------------------------------
  172. QList<ctkXnatObject*> ctkXnatObject::children() const
  173. {
  174. Q_D(const ctkXnatObject);
  175. return d->children;
  176. }
  177. //----------------------------------------------------------------------------
  178. void ctkXnatObject::add(ctkXnatObject* child)
  179. {
  180. Q_D(ctkXnatObject);
  181. if (child->parent() != this)
  182. {
  183. child->d_func()->parent = this;
  184. }
  185. if (!d->children.contains(child))
  186. {
  187. d->children.push_back(child);
  188. }
  189. else
  190. {
  191. qWarning() << "ctkXnatObject::add(): Child already exists";
  192. }
  193. }
  194. //----------------------------------------------------------------------------
  195. void ctkXnatObject::remove(ctkXnatObject* child)
  196. {
  197. Q_D(ctkXnatObject);
  198. if (!d->children.removeOne(child))
  199. {
  200. qWarning() << "ctkXnatObject::remove(): Child does not exist";
  201. }
  202. }
  203. //----------------------------------------------------------------------------
  204. void ctkXnatObject::reset()
  205. {
  206. Q_D(ctkXnatObject);
  207. // d->properties.clear();
  208. d->children.clear();
  209. d->fetched = false;
  210. }
  211. //----------------------------------------------------------------------------
  212. bool ctkXnatObject::isFetched() const
  213. {
  214. Q_D(const ctkXnatObject);
  215. return d->fetched;
  216. }
  217. //----------------------------------------------------------------------------
  218. QString ctkXnatObject::schemaType() const
  219. {
  220. return this->property("xsiType");
  221. }
  222. //----------------------------------------------------------------------------
  223. void ctkXnatObject::fetch()
  224. {
  225. Q_D(ctkXnatObject);
  226. if (!d->fetched)
  227. {
  228. this->fetchImpl();
  229. d->fetched = true;
  230. }
  231. }
  232. //----------------------------------------------------------------------------
  233. ctkXnatSession* ctkXnatObject::session() const
  234. {
  235. const ctkXnatObject* xnatObject = this;
  236. while (ctkXnatObject* parent = xnatObject->parent())
  237. {
  238. xnatObject = parent;
  239. }
  240. const ctkXnatDataModel* dataModel = dynamic_cast<const ctkXnatDataModel*>(xnatObject);
  241. return dataModel ? dataModel->session() : NULL;
  242. }
  243. //----------------------------------------------------------------------------
  244. void ctkXnatObject::setSchemaType(const QString& schemaType)
  245. {
  246. this->setProperty("xsiType", schemaType);
  247. }
  248. //----------------------------------------------------------------------------
  249. void ctkXnatObject::download(const QString& /*zipFilename*/)
  250. {
  251. }
  252. //----------------------------------------------------------------------------
  253. void ctkXnatObject::upload(const QString& /*zipFilename*/)
  254. {
  255. }
  256. //----------------------------------------------------------------------------
  257. bool ctkXnatObject::exists() const
  258. {
  259. return this->session()->exists(this);
  260. }
  261. //----------------------------------------------------------------------------
  262. void ctkXnatObject::commit ()
  263. {
  264. Q_D(ctkXnatObject);
  265. QString query = this->resourceUri();
  266. QDateTime remoteModTime = this->lastModifiedTimeOnServer();
  267. // If the object has been modified on the server, perform an update
  268. if (d->lastModifiedTime < remoteModTime)
  269. {
  270. qDebug()<<"Object maybe overwritten on server!";
  271. // TODO update from server, since modification time is not really supported
  272. // by xnat right now this is not of high priority
  273. // something like this->updateImpl
  274. }
  275. // Creating the update query
  276. query.append(QString("?%1=%2").arg("xsi:type", this->schemaType()));
  277. const QMap<QString, QString>& properties = this->properties();
  278. QMapIterator<QString, QString> itProperties(properties);
  279. while (itProperties.hasNext())
  280. {
  281. itProperties.next();
  282. if (itProperties.key() == "ID")
  283. continue;
  284. query.append(QString("&%1=%2").arg(itProperties.key(), itProperties.value()));
  285. }
  286. // Execute the update
  287. QUuid queryID = this->session()->httpPut(query);
  288. const QList<QVariantMap> results = this->session()->httpSync(queryID);
  289. // If this xnat object did not exist before on the server set the ID returned by Xnat
  290. QVariant id = results[0]["ID"];
  291. if (id.isValid())
  292. {
  293. this->setProperty("ID", id.toString());
  294. }
  295. // Finally update the modification time on the server
  296. remoteModTime = this->lastModifiedTimeOnServer();
  297. d->lastModifiedTime = remoteModTime;
  298. }
  299. //----------------------------------------------------------------------------
  300. void ctkXnatObject::fetchResources(const QString& path)
  301. {
  302. QString query = this->resourceUri() + path;
  303. ctkXnatSession* const session = this->session();
  304. QUuid queryId = session->httpGet(query);
  305. QList<ctkXnatObject*> resources = session->httpResults(queryId,
  306. ctkXnatDefaultSchemaTypes::XSI_RESOURCE);
  307. foreach (ctkXnatObject* resource, resources)
  308. {
  309. QString label = resource->property("label");
  310. if (label.isEmpty())
  311. {
  312. label = "NO NAME";
  313. }
  314. resource->setProperty("label", label);
  315. this->add(resource);
  316. }
  317. }
  318. //----------------------------------------------------------------------------
  319. void ctkXnatObject::erase()
  320. {
  321. this->session()->remove(this);
  322. this->parent()->remove(this);
  323. }