27 #include <mongo/client/dbclient.h>
29 #include <QListWidgetItem>
30 #include <QProgressDialog>
70 commonStoragePrx = getProxy<memoryx::CommonStorageInterfacePrx>(
"CommonStorage");
72 widget.cbDBNames->clear();
74 auto stdNames = commonStoragePrx->getDBNames();
75 for (
auto& name : stdNames)
77 widget.cbDBNames->addItem(QString::fromStdString(name));
79 widget.cbDBNames->setCurrentIndex(0);
93 void GridFSFileEditorWidgetController::connectSlots()
95 connect(widget.listFileNames, SIGNAL(currentTextChanged(QString)),
this, SLOT(
loadFileContent(QString)), Qt::UniqueConnection);
96 connect(widget.btnSaveFile, SIGNAL(clicked()),
this, SLOT(
saveContentToFile()), Qt::UniqueConnection);
97 connect(widget.btnDelUnreferencedFiles, SIGNAL(clicked()),
this, SLOT(
deleteUnreferencedFiles()), Qt::UniqueConnection);
99 connect(widget.cbDBNames, SIGNAL(currentIndexChanged(QString)), SLOT(
loadFileNames()), Qt::UniqueConnection);
100 connect(widget.btnLoadDB, SIGNAL(clicked()), SLOT(
loadFileNames()), Qt::UniqueConnection);
105 auto dbName = widget.cbDBNames->currentText().toStdString();
106 NameList ids = commonStoragePrx->getFileIdList(dbName);
107 auto colls = commonStoragePrx->getCollectionNames(dbName);
108 std::map<std::string, memoryx::CollectionInterfacePrx> collPrxs;
110 for (
auto coll : colls)
112 collPrxs[coll] = commonStoragePrx->requestCollection(coll);
114 QProgressDialog progress(
"Deleting unreferenced files",
"Cancel", 0, ids.size(),
getWidget());
115 progress.setWindowModality(Qt::WindowModal);
117 for (
auto fileId : ids)
120 auto query = whereQuery(fileId);
122 for (
auto coll : colls)
124 auto prx = collPrxs[coll];
125 auto list = prx->findByConstraintQuery(
"", query);
129 found +=
list.size();
131 qApp->processEvents();
132 progress.setValue(i);
133 if (progress.wasCanceled())
138 if (progress.wasCanceled())
143 auto filePrx = commonStoragePrx->getFileProxyById(dbName, fileId);
148 commonStoragePrx->removeFileById(dbName, fileId);
152 ARMARX_INFO_S << filePrx->getFilename() <<
" has " << found <<
" references - not deleting";
158 for (
auto& coll : collPrxs)
160 commonStoragePrx->releaseCollection(coll.second);
171 auto stdDBName = dbName.isEmpty() ? widget.cbDBNames->currentText().toStdString() : dbName.toStdString();
172 NameList filesNames = commonStoragePrx->getFileNameList(stdDBName);
173 NameList ids = commonStoragePrx->getFileIdList(stdDBName);
174 widget.listFileNames->clear();
176 for (
size_t i = 0; i < filesNames.size(); i++)
181 QListWidgetItem* item =
new QListWidgetItem();
182 item->setText(QString::fromStdString(
filename));
183 item->setData(Qt::UserRole, QString::fromStdString(
id));
184 widget.listFileNames->addItem(item);
188 widget.listFileNames->sortItems();
189 widget.textEdit->clear();
196 if (commonStoragePrx->getTextFileByName(widget.cbDBNames->currentText().toStdString(), fileName.toStdString(), content))
198 widget.textEdit->clear();
199 widget.textEdit->insertPlainText(QString::fromStdString(content));
200 currentId = widget.listFileNames->currentItem()->data(Qt::UserRole).toString();
207 auto dbName = widget.cbDBNames->currentText().toStdString();
208 auto newId = commonStoragePrx->storeTextFile(dbName,
209 widget.textEdit->toPlainText().toStdString(),
210 widget.listFileNames->currentItem()->text().toStdString());
211 ARMARX_INFO <<
"stored " << widget.listFileNames->currentItem()->text().toStdString() <<
" in grid fs: " <<
newId;
212 replaceFileDocID(currentId, QString::fromStdString(
newId));
213 commonStoragePrx->removeFileById(dbName, currentId.toStdString());
217 void GridFSFileEditorWidgetController::replaceFileDocID(QString fileId,
const QString&
newId)
220 auto query = whereQuery(fileId.toStdString());
221 auto dbName = widget.cbDBNames->currentText().toStdString();
222 auto colls = commonStoragePrx->getCollectionNames(dbName);
224 for (
auto coll : colls)
226 auto prx = commonStoragePrx->requestCollection(coll);
228 auto list = prx->findByConstraintQuery(
"", query);
230 for (memoryx::DBStorableData
data :
list)
232 QString json = QString::fromStdString(
data.JSON);
233 json = json.replace(fileId,
newId);
234 data.JSON = json.toStdString();
239 commonStoragePrx->releaseCollection(prx);
243 std::string GridFSFileEditorWidgetController::whereQuery(std::string fileId)
const
245 std::string query =
"function(){\
248 while(objs.length > 0) \
250 var obj = objs.pop(); \
251 var keys = Object.keySet(obj); \
252 for(var i = 0; i < keys.length;i++) \
254 if(keys[i] == \"docId\" && obj[keys[i]] === \"" + fileId +
"\") \
256 if(isObject(obj[keys[i]])) \
258 objs.push(obj[keys[i]]); \