ctkXnatResource.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "ctkXnatResource.h"
  16. #include "ctkXnatObjectPrivate.h"
  17. #include "ctkXnatSession.h"
  18. const QString ctkXnatResource::TAGS = "tags";
  19. const QString ctkXnatResource::FORMAT = "format";
  20. const QString ctkXnatResource::CONTENT = "content";
  21. const QString ctkXnatResource::ID = "xnat_abstractresource_id";
  22. //----------------------------------------------------------------------------
  23. class ctkXnatResourcePrivate : public ctkXnatObjectPrivate
  24. {
  25. public:
  26. ctkXnatResourcePrivate()
  27. : ctkXnatObjectPrivate()
  28. {
  29. }
  30. };
  31. //----------------------------------------------------------------------------
  32. ctkXnatResource::ctkXnatResource(ctkXnatObject* parent, const QString& schemaType)
  33. : ctkXnatObject(*new ctkXnatResourcePrivate(), parent, schemaType)
  34. {
  35. }
  36. //----------------------------------------------------------------------------
  37. ctkXnatResource::~ctkXnatResource()
  38. {
  39. }
  40. //----------------------------------------------------------------------------
  41. QString ctkXnatResource::resourceUri() const
  42. {
  43. return QString("%1/%2").arg(parent()->resourceUri(), this->name());
  44. }
  45. //----------------------------------------------------------------------------
  46. QString ctkXnatResource::id() const
  47. {
  48. return this->property(ID);
  49. }
  50. //----------------------------------------------------------------------------
  51. void ctkXnatResource::setId(const QString &id)
  52. {
  53. this->setProperty(ID, id);
  54. }
  55. //----------------------------------------------------------------------------
  56. QString ctkXnatResource::name() const
  57. {
  58. return this->label();
  59. }
  60. //----------------------------------------------------------------------------
  61. void ctkXnatResource::setName(const QString &name)
  62. {
  63. this->setLabel(name);
  64. }
  65. //----------------------------------------------------------------------------
  66. QString ctkXnatResource::label() const
  67. {
  68. return this->property(LABEL);
  69. }
  70. //----------------------------------------------------------------------------
  71. void ctkXnatResource::setLabel(const QString &label)
  72. {
  73. this->setProperty(LABEL, label);
  74. }
  75. //----------------------------------------------------------------------------
  76. void ctkXnatResource::setFormat(const QString &fileFormat)
  77. {
  78. this->setProperty(FORMAT, fileFormat);
  79. }
  80. //----------------------------------------------------------------------------
  81. QString ctkXnatResource::format() const
  82. {
  83. return this->property(FORMAT);
  84. }
  85. //----------------------------------------------------------------------------
  86. void ctkXnatResource::setContent(const QString &fileContent)
  87. {
  88. this->setProperty(CONTENT, fileContent);
  89. }
  90. //----------------------------------------------------------------------------
  91. QString ctkXnatResource::content() const
  92. {
  93. return this->property(CONTENT);
  94. }
  95. //----------------------------------------------------------------------------
  96. void ctkXnatResource::setTags(const QString &fileTags)
  97. {
  98. this->setProperty(TAGS, fileTags);
  99. }
  100. //----------------------------------------------------------------------------
  101. QString ctkXnatResource::tags() const
  102. {
  103. return this->property(TAGS);
  104. }
  105. //----------------------------------------------------------------------------
  106. void ctkXnatResource::reset()
  107. {
  108. ctkXnatObject::reset();
  109. }
  110. //----------------------------------------------------------------------------
  111. void ctkXnatResource::fetchImpl()
  112. {
  113. QString resourceFilesUri = this->resourceUri() + "/files";
  114. ctkXnatSession* const session = this->session();
  115. QUuid queryId = session->httpGet(resourceFilesUri);
  116. QList<ctkXnatObject*> files = session->httpResults(queryId,
  117. ctkXnatDefaultSchemaTypes::XSI_FILE);
  118. foreach (ctkXnatObject* file, files)
  119. {
  120. QString label = file->name();
  121. if (label.isEmpty())
  122. {
  123. label = "NO NAME";
  124. }
  125. file->setName(label);
  126. this->add(file);
  127. }
  128. }
  129. //----------------------------------------------------------------------------
  130. void ctkXnatResource::downloadImpl(const QString& filename)
  131. {
  132. QString query = this->resourceUri() + "/files";
  133. ctkXnatSession::UrlParameters parameters;
  134. parameters["format"] = "zip";
  135. this->session()->download(filename, query, parameters);
  136. }
  137. //----------------------------------------------------------------------------
  138. void ctkXnatResource::saveImpl()
  139. {
  140. if (!this->session()->exists(this))
  141. {
  142. QString query = this->resourceUri();
  143. query.append(QString("?%1=%2").arg("xsi:type", this->schemaType()));
  144. const QMap<QString, QString>& properties = this->properties();
  145. QMapIterator<QString, QString> itProperties(properties);
  146. while (itProperties.hasNext())
  147. {
  148. itProperties.next();
  149. if (itProperties.key() == ID || itProperties.key() == "xsiType")
  150. continue;
  151. query.append(QString("&%1=%2").arg(itProperties.key(), itProperties.value()));
  152. }
  153. QUuid queryId = this->session()->httpPut(query);
  154. session()->httpResults(queryId, ctkXnatDefaultSchemaTypes::XSI_RESOURCE);
  155. }
  156. else
  157. {
  158. // TODO Update resource
  159. }
  160. }