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 <mongo/client/dbclient.h>
28 
29 #include <QListWidgetItem>
30 #include <QProgressDialog>
31 #include <QComboBox>
32 
33 
34 namespace armarx
35 {
36 
38 
39  {
40  widget.setupUi(getWidget());
41  getWidget()->setEnabled(false);
42  }
43 
44 
46  { }
47 
48 
50  {
51 
52  }
53 
54 
56  {
57 
58  }
59 
60 
62  {
63  usingProxy("CommonStorage");
64 
65  }
66 
67 
69  {
70  commonStoragePrx = getProxy<memoryx::CommonStorageInterfacePrx>("CommonStorage");
71 
72  widget.cbDBNames->clear();
73 
74  auto stdNames = commonStoragePrx->getDBNames();
75  for (auto& name : stdNames)
76  {
77  widget.cbDBNames->addItem(QString::fromStdString(name));
78  }
79  widget.cbDBNames->setCurrentIndex(0);
80  connectSlots();
81  emit connected();
83  }
84 
85 
87  {
88  // getWidget()->setEnabled(false);
89  enableMainWidgetAsync(false);
90  }
91 
92 
93  void GridFSFileEditorWidgetController::connectSlots()
94  {
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);
98  connect(this, SIGNAL(connected()), SLOT(loadFileNames()), Qt::UniqueConnection);
99  connect(widget.cbDBNames, SIGNAL(currentIndexChanged(QString)), SLOT(loadFileNames()), Qt::UniqueConnection);
100  connect(widget.btnLoadDB, SIGNAL(clicked()), SLOT(loadFileNames()), Qt::UniqueConnection);
101  }
102 
104  {
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;
109 
110  for (auto coll : colls)
111  {
112  collPrxs[coll] = commonStoragePrx->requestCollection(coll);
113  }
114  QProgressDialog progress("Deleting unreferenced files", "Cancel", 0, ids.size(), getWidget());
115  progress.setWindowModality(Qt::WindowModal);
116  int i = 0;
117  for (auto fileId : ids)
118  {
119  int found = 0;
120  auto query = whereQuery(fileId);
121 
122  for (auto coll : colls)
123  {
124  auto prx = collPrxs[coll];
125  auto list = prx->findByConstraintQuery("", query);
126 
127  if (list.size() > 0)
128  {
129  found += list.size();
130  }
131  qApp->processEvents();
132  progress.setValue(i);
133  if (progress.wasCanceled())
134  {
135  break;
136  }
137  }
138  if (progress.wasCanceled())
139  {
140  break;
141  }
142 
143  auto filePrx = commonStoragePrx->getFileProxyById(dbName, fileId);
144 
145  if (found == 0)
146  {
147  ARMARX_IMPORTANT_S << "Removing file: " << filePrx->getFilename();
148  commonStoragePrx->removeFileById(dbName, fileId);
149  }
150  else
151  {
152  ARMARX_INFO_S << filePrx->getFilename() << " has " << found << " references - not deleting";
153  }
154  i++;
155 
156  }
157 
158  for (auto& coll : collPrxs)
159  {
160  commonStoragePrx->releaseCollection(coll.second);
161  }
162 
163  loadFileNames();
164  }
165 
166 
167 
168 
170  {
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();
175 
176  for (size_t i = 0; i < filesNames.size(); i++)
177  {
178  auto filename = filesNames.at(i);
179  auto id = ids.at(i);
180  ARMARX_INFO << "file: " << filename;
181  QListWidgetItem* item = new QListWidgetItem();
182  item->setText(QString::fromStdString(filename));
183  item->setData(Qt::UserRole, QString::fromStdString(id));
184  widget.listFileNames->addItem(item);
185 
186  }
187 
188  widget.listFileNames->sortItems();
189  widget.textEdit->clear();
190  }
191 
193  {
194  std::string content;
195 
196  if (commonStoragePrx->getTextFileByName(widget.cbDBNames->currentText().toStdString(), fileName.toStdString(), content))
197  {
198  widget.textEdit->clear();
199  widget.textEdit->insertPlainText(QString::fromStdString(content));
200  currentId = widget.listFileNames->currentItem()->data(Qt::UserRole).toString();
201  }
202 
203  }
204 
206  {
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());
214  }
215 
216 
217  void GridFSFileEditorWidgetController::replaceFileDocID(QString fileId, const QString& newId)
218  {
219 
220  auto query = whereQuery(fileId.toStdString());
221  auto dbName = widget.cbDBNames->currentText().toStdString();
222  auto colls = commonStoragePrx->getCollectionNames(dbName);
223 
224  for (auto coll : colls)
225  {
226  auto prx = commonStoragePrx->requestCollection(coll);
227 
228  auto list = prx->findByConstraintQuery("", query);
229 
230  for (memoryx::DBStorableData data : list)
231  {
232  QString json = QString::fromStdString(data.JSON);
233  json = json.replace(fileId, newId);
234  data.JSON = json.toStdString();
235  prx->save(data);
236 
237  }
238 
239  commonStoragePrx->releaseCollection(prx);
240  }
241  }
242 
243  std::string GridFSFileEditorWidgetController::whereQuery(std::string fileId) const
244  {
245  std::string query = "function(){\
246  var objs = []; \
247  objs.push(this); \
248  while(objs.length > 0) \
249  { \
250  var obj = objs.pop(); \
251  var keys = Object.keySet(obj); \
252  for(var i = 0; i < keys.length;i++) \
253  { \
254  if(keys[i] == \"docId\" && obj[keys[i]] === \"" + fileId + "\") \
255  return true; \
256  if(isObject(obj[keys[i]])) \
257  { \
258  objs.push(obj[keys[i]]); \
259  } \
260  } \
261  } \
262  return false; \
263  }";
264  return query;
265  }
266 }
armarx::GridFSFileEditorWidgetController::onConnectComponent
void onConnectComponent() override
Definition: GridFSFileEditorWidgetController.cpp:68
ARMARX_IMPORTANT_S
#define ARMARX_IMPORTANT_S
Definition: Logging.h:203
list
list(APPEND SOURCES ${QT_RESOURCES}) set(COMPONENT_LIBS ArmarXGui ArmarXCoreObservers ArmarXCoreEigen3Variants PlotterController $
Definition: CMakeLists.txt:49
armarx::GridFSFileEditorWidgetController::loadFileNames
void loadFileNames(QString dbName="")
Definition: GridFSFileEditorWidgetController.cpp:169
GridFSFileEditorWidgetController.h
armarx::GridFSFileEditorWidgetController::deleteUnreferencedFiles
void deleteUnreferencedFiles()
Definition: GridFSFileEditorWidgetController.cpp:103
armarx::GridFSFileEditorWidgetController::connected
void connected()
armarx::GridFSFileEditorWidgetController::onDisconnectComponent
void onDisconnectComponent() override
Definition: GridFSFileEditorWidgetController.cpp:86
armarx::GridFSFileEditorWidgetController::saveContentToFile
void saveContentToFile()
Definition: GridFSFileEditorWidgetController.cpp:205
newId
auto newId
Definition: GraspingManager.cpp:90
armarx::GridFSFileEditorWidgetController::saveSettings
void saveSettings(QSettings *settings) override
Definition: GridFSFileEditorWidgetController.cpp:55
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
filename
std::string filename
Definition: VisualizationRobot.cpp:83
armarx::GridFSFileEditorWidgetController::~GridFSFileEditorWidgetController
virtual ~GridFSFileEditorWidgetController()
Controller destructor.
Definition: GridFSFileEditorWidgetController.cpp:45
armarx::GridFSFileEditorWidgetController::loadSettings
void loadSettings(QSettings *settings) override
Definition: GridFSFileEditorWidgetController.cpp:49
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ArmarXWidgetController::enableMainWidgetAsync
void enableMainWidgetAsync(bool enable)
This function enables/disables the main widget asynchronously (if called from a non qt thread).
Definition: ArmarXWidgetController.cpp:174
armarx::ArmarXWidgetController::getWidget
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
Definition: ArmarXWidgetController.cpp:54
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
armarx::GridFSFileEditorWidgetController::GridFSFileEditorWidgetController
GridFSFileEditorWidgetController()
Controller Constructor.
Definition: GridFSFileEditorWidgetController.cpp:37
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
armarx::GridFSFileEditorWidgetController::loadFileContent
void loadFileContent(const QString &fileName)
Definition: GridFSFileEditorWidgetController.cpp:192
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::GridFSFileEditorWidgetController::onInitComponent
void onInitComponent() override
Definition: GridFSFileEditorWidgetController.cpp:61