ctkXnatObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 "ctkXnatDefaultSchemaTypes.h"
  19. #include "ctkXnatSession.h"
  20. #include <QDateTime>
  21. #include <QDebug>
  22. #include <QStringList>
  23. #include <QVariant>
  24. const QString ctkXnatObject::ID = "ID";
  25. const QString ctkXnatObject::NAME = "name";
  26. const QString ctkXnatObject::LABEL = "label";
  27. //----------------------------------------------------------------------------
  28. ctkXnatObject::ctkXnatObject(const ctkXnatObject&)
  29. {
  30. throw ctkRuntimeException("Copy constructor not implemented");
  31. }
  32. //----------------------------------------------------------------------------
  33. ctkXnatObject::ctkXnatObject(ctkXnatObject* parent, const QString& schemaType)
  34. : d_ptr(new ctkXnatObjectPrivate())
  35. {
  36. this->setParent(parent);
  37. this->setSchemaType(schemaType);
  38. }
  39. //----------------------------------------------------------------------------
  40. ctkXnatObject::ctkXnatObject(ctkXnatObjectPrivate& dd, ctkXnatObject* parent, const QString& schemaType)
  41. : d_ptr(&dd)
  42. {
  43. this->setParent(parent);
  44. this->setSchemaType(schemaType);
  45. }
  46. //----------------------------------------------------------------------------
  47. ctkXnatObject::~ctkXnatObject()
  48. {
  49. Q_D(ctkXnatObject);
  50. foreach (ctkXnatObject* child, d->children)
  51. {
  52. delete child;
  53. }
  54. }
  55. //----------------------------------------------------------------------------
  56. QString ctkXnatObject::id() const
  57. {
  58. return this->property(ID);
  59. }
  60. //----------------------------------------------------------------------------
  61. void ctkXnatObject::setId(const QString& id)
  62. {
  63. this->setProperty(ID, id);
  64. }
  65. //----------------------------------------------------------------------------
  66. QString ctkXnatObject::name() const
  67. {
  68. return this->property(NAME);
  69. }
  70. //----------------------------------------------------------------------------
  71. void ctkXnatObject::setName(const QString& name)
  72. {
  73. this->setProperty(NAME, name);
  74. }
  75. //----------------------------------------------------------------------------
  76. QString ctkXnatObject::description() const
  77. {
  78. Q_D(const ctkXnatObject);
  79. return d->description;
  80. }
  81. //----------------------------------------------------------------------------
  82. void ctkXnatObject::setDescription(const QString& description)
  83. {
  84. Q_D(ctkXnatObject);
  85. d->description = description;
  86. }
  87. //----------------------------------------------------------------------------
  88. QString ctkXnatObject::childDataType() const
  89. {
  90. return "Resources";
  91. }
  92. QDateTime ctkXnatObject::lastModifiedTimeOnServer()
  93. {
  94. Q_D(ctkXnatObject);
  95. QUuid queryId = this->session()->httpHead(this->resourceUri());
  96. QMap<QByteArray, QByteArray> header = this->session()->httpHeadSync(queryId);
  97. QVariant lastModifiedHeader = header.value("Last-Modified");
  98. QDateTime lastModifiedTime;
  99. if (lastModifiedHeader.isValid())
  100. {
  101. QStringList dateformates;
  102. // In case http date formate RFC 822 ( "Sun, 06 Nov 1994 08:49:37 GMT" )
  103. dateformates<<"ddd, dd MMM yyyy HH:mm:ss";
  104. // In case http date formate ANSI ( "Sun Nov 6 08:49:37 1994" )
  105. dateformates<<"ddd MMM d HH:mm:ss yyyy";
  106. // In case http date formate RFC 850 ( "Sunday, 06-Nov-94 08:49:37 GMT" )
  107. dateformates<<"dddd, dd-MMM-yy HH:mm:ss";
  108. QString dateText = lastModifiedHeader.toString();
  109. // Remove "GMT" addition at the end of the http timestamp
  110. if (dateText.indexOf("GMT") != -1)
  111. {
  112. dateText = dateText.left(dateText.length()-4);
  113. }
  114. foreach (QString format, dateformates)
  115. {
  116. lastModifiedTime = QDateTime::fromString(dateText, format);
  117. if (lastModifiedTime.isValid())
  118. break;
  119. }
  120. }
  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& filename)
  255. {
  256. this->downloadImpl(filename);
  257. }
  258. //----------------------------------------------------------------------------
  259. void ctkXnatObject::upload(const QString& /*zipFilename*/)
  260. {
  261. }
  262. //----------------------------------------------------------------------------
  263. bool ctkXnatObject::exists() const
  264. {
  265. return this->session()->exists(this);
  266. }
  267. //----------------------------------------------------------------------------
  268. void ctkXnatObject::commit ()
  269. {
  270. Q_D(ctkXnatObject);
  271. QString query = this->resourceUri();
  272. QDateTime remoteModTime = this->lastModifiedTimeOnServer();
  273. // If the object has been modified on the server, perform an update
  274. if (d->lastModifiedTime < remoteModTime)
  275. {
  276. qDebug()<<"Object maybe overwritten on server!";
  277. // TODO update from server, since modification time is not really supported
  278. // by xnat right now this is not of high priority
  279. // something like this->updateImpl
  280. }
  281. // Creating the update query
  282. query.append(QString("?%1=%2").arg("xsi:type", this->schemaType()));
  283. const QMap<QString, QString>& properties = this->properties();
  284. QMapIterator<QString, QString> itProperties(properties);
  285. while (itProperties.hasNext())
  286. {
  287. itProperties.next();
  288. if (itProperties.key() == "ID")
  289. continue;
  290. query.append(QString("&%1=%2").arg(itProperties.key(), itProperties.value()));
  291. }
  292. // Execute the update
  293. QUuid queryID = this->session()->httpPut(query);
  294. const QList<QVariantMap> results = this->session()->httpSync(queryID);
  295. // If this xnat object did not exist before on the server set the ID returned by Xnat
  296. QVariant id = results[0]["ID"];
  297. if (id.isValid())
  298. {
  299. this->setProperty("ID", id.toString());
  300. }
  301. // Finally update the modification time on the server
  302. remoteModTime = this->lastModifiedTimeOnServer();
  303. d->lastModifiedTime = remoteModTime;
  304. }
  305. //----------------------------------------------------------------------------
  306. void ctkXnatObject::fetchResources(const QString& path)
  307. {
  308. QString query = this->resourceUri() + path;
  309. ctkXnatSession* const session = this->session();
  310. QUuid queryId = session->httpGet(query);
  311. QList<ctkXnatObject*> resources = session->httpResults(queryId,
  312. ctkXnatDefaultSchemaTypes::XSI_RESOURCE);
  313. foreach (ctkXnatObject* resource, resources)
  314. {
  315. QString label = resource->name();
  316. if (label.isEmpty())
  317. {
  318. label = "NO NAME";
  319. }
  320. resource->setName(label);
  321. this->add(resource);
  322. }
  323. }
  324. //----------------------------------------------------------------------------
  325. void ctkXnatObject::erase()
  326. {
  327. this->session()->remove(this);
  328. this->parent()->remove(this);
  329. }