GridFSFileEditorWidgetController.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package MemoryX::gui-plugins::GridFSFileEditorWidgetController
19 * @author Mirko Waechter (waechter at kit dot edu)
20 * @date 2014
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
26
27#include <QComboBox>
28#include <QListWidgetItem>
29#include <QProgressDialog>
30
31#include <mongo/client/dbclient.h>
32
33namespace armarx
34{
35
37
38 {
39 widget.setupUi(getWidget());
40 getWidget()->setEnabled(false);
41 }
42
46
47 void
49 {
50 }
51
52 void
54 {
55 }
56
57 void
62
63 void
65 {
66 commonStoragePrx = getProxy<memoryx::CommonStorageInterfacePrx>("CommonStorage");
67
68 widget.cbDBNames->clear();
69
70 auto stdNames = commonStoragePrx->getDBNames();
71 for (auto& name : stdNames)
72 {
73 widget.cbDBNames->addItem(QString::fromStdString(name));
74 }
75 widget.cbDBNames->setCurrentIndex(0);
76 connectSlots();
77 emit connected();
79 }
80
81 void
83 {
84 // getWidget()->setEnabled(false);
86 }
87
88 void
89 GridFSFileEditorWidgetController::connectSlots()
90 {
91 connect(widget.listFileNames,
92 SIGNAL(currentTextChanged(QString)),
93 this,
94 SLOT(loadFileContent(QString)),
95 Qt::UniqueConnection);
96 connect(widget.btnSaveFile,
97 SIGNAL(clicked()),
98 this,
99 SLOT(saveContentToFile()),
100 Qt::UniqueConnection);
101 connect(widget.btnDelUnreferencedFiles,
102 SIGNAL(clicked()),
103 this,
105 Qt::UniqueConnection);
106 connect(this, SIGNAL(connected()), SLOT(loadFileNames()), Qt::UniqueConnection);
107 connect(widget.cbDBNames,
108 SIGNAL(currentIndexChanged(QString)),
109 SLOT(loadFileNames()),
110 Qt::UniqueConnection);
111 connect(widget.btnLoadDB, SIGNAL(clicked()), SLOT(loadFileNames()), Qt::UniqueConnection);
112 }
113
114 void
116 {
117 auto dbName = widget.cbDBNames->currentText().toStdString();
118 NameList ids = commonStoragePrx->getFileIdList(dbName);
119 auto colls = commonStoragePrx->getCollectionNames(dbName);
120 std::map<std::string, memoryx::CollectionInterfacePrx> collPrxs;
121
122 for (auto coll : colls)
123 {
124 collPrxs[coll] = commonStoragePrx->requestCollection(coll);
125 }
126 QProgressDialog progress(
127 "Deleting unreferenced files", "Cancel", 0, ids.size(), getWidget());
128 progress.setWindowModality(Qt::WindowModal);
129 int i = 0;
130 for (auto fileId : ids)
131 {
132 int found = 0;
133 auto query = whereQuery(fileId);
134
135 for (auto coll : colls)
136 {
137 auto prx = collPrxs[coll];
138 auto list = prx->findByConstraintQuery("", query);
139
140 if (list.size() > 0)
141 {
142 found += list.size();
143 }
144 qApp->processEvents();
145 progress.setValue(i);
146 if (progress.wasCanceled())
147 {
148 break;
149 }
150 }
151 if (progress.wasCanceled())
152 {
153 break;
154 }
155
156 auto filePrx = commonStoragePrx->getFileProxyById(dbName, fileId);
157
158 if (found == 0)
159 {
160 ARMARX_IMPORTANT_S << "Removing file: " << filePrx->getFilename();
161 commonStoragePrx->removeFileById(dbName, fileId);
162 }
163 else
164 {
165 ARMARX_INFO_S << filePrx->getFilename() << " has " << found
166 << " references - not deleting";
167 }
168 i++;
169 }
170
171 for (auto& coll : collPrxs)
172 {
173 commonStoragePrx->releaseCollection(coll.second);
174 }
175
177 }
178
179 void
181 {
182 auto stdDBName =
183 dbName.isEmpty() ? widget.cbDBNames->currentText().toStdString() : dbName.toStdString();
184 NameList filesNames = commonStoragePrx->getFileNameList(stdDBName);
185 NameList ids = commonStoragePrx->getFileIdList(stdDBName);
186 widget.listFileNames->clear();
187
188 for (size_t i = 0; i < filesNames.size(); i++)
189 {
190 auto filename = filesNames.at(i);
191 auto id = ids.at(i);
192 ARMARX_INFO << "file: " << filename;
193 QListWidgetItem* item = new QListWidgetItem();
194 item->setText(QString::fromStdString(filename));
195 item->setData(Qt::UserRole, QString::fromStdString(id));
196 widget.listFileNames->addItem(item);
197 }
198
199 widget.listFileNames->sortItems();
200 widget.textEdit->clear();
201 }
202
203 void
205 {
206 std::string content;
207
208 if (commonStoragePrx->getTextFileByName(
209 widget.cbDBNames->currentText().toStdString(), fileName.toStdString(), content))
210 {
211 widget.textEdit->clear();
212 widget.textEdit->insertPlainText(QString::fromStdString(content));
213 currentId = widget.listFileNames->currentItem()->data(Qt::UserRole).toString();
214 }
215 }
216
217 void
219 {
220 auto dbName = widget.cbDBNames->currentText().toStdString();
221 auto newId = commonStoragePrx->storeTextFile(
222 dbName,
223 widget.textEdit->toPlainText().toStdString(),
224 widget.listFileNames->currentItem()->text().toStdString());
225 ARMARX_INFO << "stored " << widget.listFileNames->currentItem()->text().toStdString()
226 << " in grid fs: " << newId;
227 replaceFileDocID(currentId, QString::fromStdString(newId));
228 commonStoragePrx->removeFileById(dbName, currentId.toStdString());
229 }
230
231 void
232 GridFSFileEditorWidgetController::replaceFileDocID(QString fileId, const QString& newId)
233 {
234
235 auto query = whereQuery(fileId.toStdString());
236 auto dbName = widget.cbDBNames->currentText().toStdString();
237 auto colls = commonStoragePrx->getCollectionNames(dbName);
238
239 for (auto coll : colls)
240 {
241 auto prx = commonStoragePrx->requestCollection(coll);
242
243 auto list = prx->findByConstraintQuery("", query);
244
245 for (memoryx::DBStorableData data : list)
246 {
247 QString json = QString::fromStdString(data.JSON);
248 json = json.replace(fileId, newId);
249 data.JSON = json.toStdString();
250 prx->save(data);
251 }
252
253 commonStoragePrx->releaseCollection(prx);
254 }
255 }
256
257 std::string
258 GridFSFileEditorWidgetController::whereQuery(std::string fileId) const
259 {
260 std::string query = "function(){\
261 var objs = []; \
262 objs.push(this); \
263 while(objs.length > 0) \
264 { \
265 var obj = objs.pop(); \
266 var keys = Object.keySet(obj); \
267 for(var i = 0; i < keys.length;i++) \
268 { \
269 if(keys[i] == \"docId\" && obj[keys[i]] === \"" +
270 fileId + "\") \
271 return true; \
272 if(isObject(obj[keys[i]])) \
273 { \
274 objs.push(obj[keys[i]]); \
275 } \
276 } \
277 } \
278 return false; \
279 }";
280 return query;
281 }
282} // namespace armarx
auto newId
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
void enableMainWidgetAsync(bool enable)
This function enables/disables the main widget asynchronously (if called from a non qt thread).
virtual ~GridFSFileEditorWidgetController()
Controller destructor.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_INFO_S
Definition Logging.h:202
#define ARMARX_IMPORTANT_S
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:210
This file offers overloads of toIce() and fromIce() functions for STL container types.