1 /*=============================================================================
2 
3   Library: XNAT/Core
4 
5   Copyright (c) University College London,
6     Centre for Medical Image Computing
7 
8   Licensed under the Apache License, Version 2.0 (the "License");
9   you may not use this file except in compliance with the License.
10   You may obtain a copy of the License at
11 
12     http://www.apache.org/licenses/LICENSE-2.0
13 
14   Unless required by applicable law or agreed to in writing, software
15   distributed under the License is distributed on an "AS IS" BASIS,
16   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   See the License for the specific language governing permissions and
18   limitations under the License.
19 
20 =============================================================================*/
21 
22 #include "ctkXnatFile.h"
23 
24 #include "ctkXnatObjectPrivate.h"
25 #include "ctkXnatSession.h"
26 
27 const QString ctkXnatFile::FILE_NAME = "Name";
28 const QString ctkXnatFile::FILE_TAGS = "file_tags";
29 const QString ctkXnatFile::FILE_FORMAT = "file_format";
30 const QString ctkXnatFile::FILE_CONTENT = "file_content";
31 
32 //----------------------------------------------------------------------------
33 class ctkXnatFilePrivate : public ctkXnatObjectPrivate
34 {
35 public:
36 
ctkXnatFilePrivate()37   ctkXnatFilePrivate()
38   : ctkXnatObjectPrivate()
39   {
40   }
41 
reset()42   void reset()
43   {
44   }
45 
46   QString localFilePath;
47 };
48 
49 
50 //----------------------------------------------------------------------------
ctkXnatFile(ctkXnatObject * parent,const QString & schemaType)51 ctkXnatFile::ctkXnatFile(ctkXnatObject* parent, const QString& schemaType)
52 : ctkXnatObject(*new ctkXnatFilePrivate(), parent, schemaType)
53 {
54 }
55 
56 //----------------------------------------------------------------------------
~ctkXnatFile()57 ctkXnatFile::~ctkXnatFile()
58 {
59 }
60 
61 //----------------------------------------------------------------------------
setName(const QString & name)62 void ctkXnatFile::setName(const QString &name)
63 {
64   this->setProperty(FILE_NAME, name);
65 }
66 
67 //----------------------------------------------------------------------------
name() const68 QString ctkXnatFile::name() const
69 {
70   return this->property(FILE_NAME);
71 }
72 
73 //----------------------------------------------------------------------------
setFileFormat(const QString & fileFormat)74 void ctkXnatFile::setFileFormat(const QString &fileFormat)
75 {
76   this->setProperty(FILE_FORMAT, fileFormat);
77 }
78 
79 //----------------------------------------------------------------------------
fileFormat() const80 QString ctkXnatFile::fileFormat() const
81 {
82   return this->property(FILE_FORMAT);
83 }
84 
85 //----------------------------------------------------------------------------
setFileContent(const QString & fileContent)86 void ctkXnatFile::setFileContent(const QString &fileContent)
87 {
88   this->setProperty(FILE_CONTENT, fileContent);
89 }
90 
91 //----------------------------------------------------------------------------
fileContent() const92 QString ctkXnatFile::fileContent() const
93 {
94   return this->property(FILE_CONTENT);
95 }
96 
97 //----------------------------------------------------------------------------
setFileTags(const QString & fileTags)98 void ctkXnatFile::setFileTags(const QString &fileTags)
99 {
100   this->setProperty(FILE_TAGS, fileTags);
101 }
102 
103 //----------------------------------------------------------------------------
fileTags() const104 QString ctkXnatFile::fileTags() const
105 {
106   return this->property(FILE_TAGS);
107 }
108 
109 //----------------------------------------------------------------------------
setLocalFilePath(const QString & filePath)110 void ctkXnatFile::setLocalFilePath(const QString &filePath)
111 {
112   Q_D(ctkXnatFile);
113   d->localFilePath = filePath;
114 }
115 
116 //----------------------------------------------------------------------------
localFilePath() const117 QString ctkXnatFile::localFilePath() const
118 {
119   Q_D(const ctkXnatFile);
120   return d->localFilePath;
121 }
122 
123 //----------------------------------------------------------------------------
resourceUri() const124 QString ctkXnatFile::resourceUri() const
125 {
126   return QString("%1/files/%2").arg(parent()->resourceUri(), this->name());
127 }
128 
129 //----------------------------------------------------------------------------
reset()130 void ctkXnatFile::reset()
131 {
132   ctkXnatObject::reset();
133 }
134 
135 //----------------------------------------------------------------------------
fetchImpl()136 void ctkXnatFile::fetchImpl()
137 {
138   // Does not make sense to fetch a file
139 }
140 
141 //----------------------------------------------------------------------------
downloadImpl(const QString & filename)142 void ctkXnatFile::downloadImpl(const QString& filename)
143 {
144   QString query = this->resourceUri();
145   this->session()->download(filename, query);
146 }
147 
148 //----------------------------------------------------------------------------
saveImpl(bool overwrite)149 void ctkXnatFile::saveImpl(bool overwrite)
150 {
151   Q_D(ctkXnatFile);
152 
153   ctkXnatSession::UrlParameters urlParams;
154   urlParams["xsiType"] = this->schemaType();
155   // Flag needed for file upload
156   urlParams["inbody"] = "true";
157 
158   // Creating the update query
159   const QMap<QString, QString>& properties = this->properties();
160   QMapIterator<QString, QString> itProperties(properties);
161   while (itProperties.hasNext())
162   {
163     itProperties.next();
164 
165     // Do not append these file specific properties since they require a slightly
166     // different key for uploading a file (e.g. instead of "file_format" only "format")
167     if (itProperties.key() == FILE_TAGS || itProperties.key() == FILE_FORMAT ||
168         itProperties.key() == FILE_CONTENT || itProperties.key() == "xsiType")
169       continue;
170 
171     urlParams[itProperties.key()] = itProperties.value();
172   }
173 
174   if (!this->fileFormat().isNull())
175     urlParams["format"] = this->fileFormat();
176   if (!this->fileContent().isNull())
177     urlParams["content"] = this->fileContent();
178   if (!this->fileContent().isNull())
179     urlParams["tags"] = this->fileTags();
180 
181   if (this->exists() && overwrite)
182     urlParams["overwrite"] = "true";
183 
184   this->session()->upload(this, urlParams);
185 }
186