ctkXnatResource.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. if (this->name().length() == 0 || this->name() == "NO NAME")
  44. return QString("%1/%2").arg(parent()->resourceUri(), this->id());
  45. else
  46. return QString("%1/%2").arg(parent()->resourceUri(), this->name());
  47. }
  48. //----------------------------------------------------------------------------
  49. QString ctkXnatResource::id() const
  50. {
  51. return this->property(ID);
  52. }
  53. //----------------------------------------------------------------------------
  54. void ctkXnatResource::setId(const QString &id)
  55. {
  56. this->setProperty(ID, id);
  57. }
  58. //----------------------------------------------------------------------------
  59. QString ctkXnatResource::name() const
  60. {
  61. return this->label();
  62. }
  63. //----------------------------------------------------------------------------
  64. void ctkXnatResource::setName(const QString &name)
  65. {
  66. this->setLabel(name);
  67. }
  68. //----------------------------------------------------------------------------
  69. QString ctkXnatResource::label() const
  70. {
  71. return this->property(LABEL);
  72. }
  73. //----------------------------------------------------------------------------
  74. void ctkXnatResource::setLabel(const QString &label)
  75. {
  76. this->setProperty(LABEL, label);
  77. }
  78. //----------------------------------------------------------------------------
  79. void ctkXnatResource::setFormat(const QString &fileFormat)
  80. {
  81. this->setProperty(FORMAT, fileFormat);
  82. }
  83. //----------------------------------------------------------------------------
  84. QString ctkXnatResource::format() const
  85. {
  86. return this->property(FORMAT);
  87. }
  88. //----------------------------------------------------------------------------
  89. void ctkXnatResource::setContent(const QString &fileContent)
  90. {
  91. this->setProperty(CONTENT, fileContent);
  92. }
  93. //----------------------------------------------------------------------------
  94. QString ctkXnatResource::content() const
  95. {
  96. return this->property(CONTENT);
  97. }
  98. //----------------------------------------------------------------------------
  99. void ctkXnatResource::setTags(const QString &fileTags)
  100. {
  101. this->setProperty(TAGS, fileTags);
  102. }
  103. //----------------------------------------------------------------------------
  104. QString ctkXnatResource::tags() const
  105. {
  106. return this->property(TAGS);
  107. }
  108. //----------------------------------------------------------------------------
  109. void ctkXnatResource::reset()
  110. {
  111. ctkXnatObject::reset();
  112. }
  113. //----------------------------------------------------------------------------
  114. void ctkXnatResource::fetchImpl()
  115. {
  116. QString resourceFilesUri = this->resourceUri() + "/files";
  117. ctkXnatSession* const session = this->session();
  118. QUuid queryId = session->httpGet(resourceFilesUri);
  119. QList<ctkXnatObject*> files = session->httpResults(queryId,
  120. ctkXnatDefaultSchemaTypes::XSI_FILE);
  121. foreach (ctkXnatObject* file, files)
  122. {
  123. QString label = file->name();
  124. if (label.isEmpty())
  125. {
  126. label = "NO NAME";
  127. }
  128. file->setName(label);
  129. this->add(file);
  130. }
  131. }
  132. //----------------------------------------------------------------------------
  133. void ctkXnatResource::downloadImpl(const QString& filename)
  134. {
  135. QString query = this->resourceUri() + "/files";
  136. ctkXnatSession::UrlParameters parameters;
  137. parameters["format"] = "zip";
  138. this->session()->download(filename, query, parameters);
  139. }
  140. //----------------------------------------------------------------------------
  141. void ctkXnatResource::saveImpl(bool /*overwrite*/)
  142. {
  143. ctkXnatSession::UrlParameters urlParams;
  144. urlParams["xsi:type"] = this->schemaType();
  145. const QMap<QString, QString>& properties = this->properties();
  146. QMapIterator<QString, QString> itProperties(properties);
  147. while (itProperties.hasNext())
  148. {
  149. itProperties.next();
  150. if (itProperties.key() == ID || itProperties.key() == "xsiType")
  151. continue;
  152. urlParams[itProperties.key()] = itProperties.value();
  153. }
  154. QUuid queryId = this->session()->httpPut(this->resourceUri(), urlParams);
  155. session()->httpResults(queryId, ctkXnatDefaultSchemaTypes::XSI_RESOURCE);
  156. }