All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QueryBuilderWizard.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/query/QueryBuilderWizard.cpp
22 
23  \brief A Qt dialog that allows users to create query builder based on TerraLib query framework.
24 */
25 
26 // TerraLib
27 #include "QueryBuilderWizard.h"
28 
29 #include "../../../dataaccess/dataset/DataSet.h"
30 #include "../../../dataaccess/dataset/DataSetType.h"
31 #include "../../../dataaccess/query/SQLDialect.h"
32 #include "../../../dataaccess/datasource/DataSourceManager.h"
33 //#include "../../../dataaccess/datasource/DataSourceTransactor.h"
34 //#include "../../../dataaccess/datasource/DataSourceCatalogLoader.h"
35 #include "../../../dataaccess/utils/Utils.h"
36 #include "../utils/DoubleListWidget.h"
37 #include "ui_DoubleListWidgetForm.h"
38 #include "ui_QueryBuilderWizardForm.h"
39 
40 
41 // Qt
42 #include <QMessageBox>
43 
44 
46  : QWizard(parent),
47  m_ui(new Ui::QueryBuilderWizardForm)
48 {
49  m_ui->setupUi(this);
50 
51  //fill interface
53  getOperators();
54  getConnectors();
55  getOrders();
56 
57  // set icons
58  m_ui->m_addDataSetPushButton->setIcon(QIcon::fromTheme("list-add"));
59  m_ui->m_removeDataSetPushButton->setIcon(QIcon::fromTheme("list-remove"));
60 
61  m_ui->m_addWhereClausePushButton->setIcon(QIcon::fromTheme("list-add"));
62  m_ui->m_removeWhereClausePushButton->setIcon(QIcon::fromTheme("list-remove"));
63 
64  m_ui->m_addOrderPushButton->setIcon(QIcon::fromTheme("list-add"));
65  m_ui->m_removeOrderPushButton->setIcon(QIcon::fromTheme("list-remove"));
66 
67  // properties
68  m_propertyList.reset(new DoubleListWidget(m_ui->m_propertyWizardPage));
69  m_propertyList->getForm()->m_leftItemsLabel->setText(tr("Available Properties"));
70  m_propertyList->getForm()->m_rightItemsLabel->setText(tr("Used Properties"));
71  m_ui->m_propertyGridLayout->addWidget(m_propertyList.get());
72 
73  // group by
74  m_groupByList.reset(new DoubleListWidget(m_ui->m_groupByWizardPage));
75  m_groupByList->getForm()->m_leftItemsLabel->setText(tr("Available Properties"));
76  m_groupByList->getForm()->m_rightItemsLabel->setText(tr("Used Properties"));
77  m_ui->m_groupByGridLayout->addWidget(m_groupByList.get());
78 
79  //connects
80  connect(m_ui->m_dataSourceComboBox, SIGNAL(activated(const QString&)), this, SLOT(onDataSourceComboBoxActivated(const QString&)));
81  connect(m_ui->m_dataSetComboBox, SIGNAL(activated(const QString&)), this, SLOT(onDataSetComboBoxActivated(const QString&)));
82  connect(m_ui->m_addDataSetPushButton, SIGNAL(clicked()), this, SLOT(onAddDataSetPushButtonClicked()));
83  connect(m_ui->m_removeDataSetPushButton, SIGNAL(clicked()), this, SLOT(onRemoveDataSetPushButtonClicked()));
84  connect(m_ui->m_addWhereClausePushButton, SIGNAL(clicked()), this, SLOT(onAddWhereClausePushButtonClicked()));
85  connect(m_ui->m_removeWhereClausePushButton, SIGNAL(clicked()), this, SLOT(onRemoveWhereClausePushButtonClicked()));
86  connect(m_ui->m_addOrderPushButton, SIGNAL(clicked()), this, SLOT(onAddOrderPushButton()));
87  connect(m_ui->m_removeOrderPushButton, SIGNAL(clicked()), this, SLOT(onRemoveOrderPushButton()));
88 }
89 
91 {
92  m_bimapOperator.clear();
93  m_bimapConnector.clear();
94  m_bimapOrder.clear();
95 }
96 
98 {
99  if(currentPage() == m_ui->m_dataSourceWizardPage)
100  {
101  return checkDataSourcePage();
102  }
103  else if(currentPage() == m_ui->m_dataSetWizardPage)
104  {
105  return checkDataSetPage();
106  }
107  else if(currentPage() == m_ui->m_propertyWizardPage)
108  {
109  return checkPropertiesPage();
110  }
111  else if(currentPage() == m_ui->m_whereClauseWizardPage)
112  {
113  return checkWhereClausePage();
114  }
115  else if(currentPage() == m_ui->m_groupByWizardPage)
116  {
117  return checkGroupByPage();
118  }
119  else if(currentPage() == m_ui->m_orderByWizardPage)
120  {
121  return checkOrderByPage();
122  }
123 
124  return true;
125 }
126 
128 {
130 
131  QStringList list;
132 
133  while(it != te::da::DataSourceManager::getInstance().end())
134  {
135  list.push_back(it->first.c_str());
136 
137  ++it;
138  }
139 
140  m_ui->m_dataSourceComboBox->clear();
141  m_ui->m_dataSourceComboBox->addItems(list);
142 }
143 
145 {
146  std::string dsId = m_ui->m_dataSourceComboBox->currentText().toStdString();
147 
148  std::vector<std::string> datasetNames;
149 
150  te::da::GetDataSetNames(datasetNames, dsId);
151 
152  QStringList list;
153 
154  for(size_t t = 0; t < datasetNames.size(); ++t)
155  {
156  list.push_back(datasetNames[t].c_str());
157  }
158 
159  m_ui->m_dataSetComboBox->clear();
160  m_ui->m_dataSetComboBox->addItems(list);
161 
162  if(datasetNames.empty() == false)
163  onDataSetComboBoxActivated(m_ui->m_dataSetComboBox->currentText());
164 }
165 
167 {
168  //get dataset list
169  std::string dsId = m_ui->m_dataSourceComboBox->currentText().toStdString();
171  std::vector<std::string> datasetNames = ds->getDataSetNames();
172 
173  int row = m_ui->m_dataSetTableWidget->rowCount();
174 
175  std::vector<std::string> inputProperties;
176  QStringList list;
177 
178  //get properties for each data set
179  for(int i = 0; i < row; ++i)
180  {
181  //alias name
182  QTableWidgetItem* itemAlias = m_ui->m_dataSetTableWidget->item(i, 1);
183  std::string alias = itemAlias->text().toStdString();
184 
185  //data set name
186  QTableWidgetItem* itemDataSet = m_ui->m_dataSetTableWidget->item(i, 0);
187  std::string dataSetName = itemDataSet->text().toStdString();
188 
189  //get datasettype
190  std::auto_ptr<te::da::DataSetType> dsType;
191  for(unsigned int i = 0; i < datasetNames.size(); ++i)
192  {
193  if(datasetNames[i] == dataSetName)
194  dsType = ds->getDataSetType(datasetNames[i]);
195  }
196 
197  if(dsType.get())
198  {
199  for(size_t t = 0; t < dsType->size(); ++t)
200  {
201  std::string propName = dsType->getProperty(t)->getName();
202  std::string fullName = alias + "." + propName;
203 
204  inputProperties.push_back(fullName);
205  list.push_back(fullName.c_str());
206  }
207  }
208 
209  }
210 
211  m_propertyList->setInputValues(inputProperties);
212  m_groupByList->setInputValues(inputProperties);
213 
214  m_ui->m_restrictValueComboBox->clear();
215  m_ui->m_restrictValueComboBox->addItems(list);
216 
217  m_ui->m_valuePropertyComboBox->clear();
218  m_ui->m_valuePropertyComboBox->addItems(list);
219 
220  m_ui->m_orderPropertyComboBox->clear();
221  m_ui->m_orderPropertyComboBox->addItems(list);
222 }
223 
225 {
226  m_bimapOperator.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOperator_Plus, tr("+").toStdString()));
227  m_bimapOperator.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOperator_Minus, tr("-").toStdString()));
228  m_bimapOperator.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOperator_Multi, tr("*").toStdString()));
229  m_bimapOperator.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOperator_Div, tr("/").toStdString()));
230  m_bimapOperator.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOperator_Equal, tr("=").toStdString()));
231  m_bimapOperator.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOperator_Dif, tr("<>").toStdString()));
232  m_bimapOperator.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOperator_Great, tr(">").toStdString()));
233  m_bimapOperator.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOperator_GreatEqual, tr(">=").toStdString()));
234  m_bimapOperator.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOperator_Less, tr("<").toStdString()));
235  m_bimapOperator.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOperator_LessEqual, tr("<=").toStdString()));
236 
237  m_ui->m_OperatorComboBox->clear();
238 
239  boost::bimap<int, std::string>::iterator it = m_bimapOperator.begin();
240 
241  while(it != m_bimapOperator.end())
242  {
243  m_ui->m_OperatorComboBox->addItem(it->right.c_str());
244  ++it;
245  }
246 }
247 
249 {
250  m_bimapConnector.insert(boost::bimap<int, std::string>::value_type(QueryBuilderConnector_NONE, tr("---").toStdString()));
251  m_bimapConnector.insert(boost::bimap<int, std::string>::value_type(QueryBuilderConnector_AND, tr("AND").toStdString()));
252  m_bimapConnector.insert(boost::bimap<int, std::string>::value_type(QueryBuilderConnector_OR, tr("OR").toStdString()));
253  m_bimapConnector.insert(boost::bimap<int, std::string>::value_type(QueryBuilderConnector_NOT, tr("NOT").toStdString()));
254  m_bimapConnector.insert(boost::bimap<int, std::string>::value_type(QueryBuilderConnector_LIKE, tr("LIKE").toStdString()));
255 
256  m_ui->m_connectorComboBox->clear();
257 
258  boost::bimap<int, std::string>::left_const_iterator it;
259  it = m_bimapConnector.left.find(QueryBuilderConnector_NONE);
260  m_ui->m_connectorComboBox->addItem(it->second.c_str());
261  it = m_bimapConnector.left.find(QueryBuilderConnector_AND);
262  m_ui->m_connectorComboBox->addItem(it->second.c_str());
263  it = m_bimapConnector.left.find(QueryBuilderConnector_OR);
264  m_ui->m_connectorComboBox->addItem(it->second.c_str());
265  it = m_bimapConnector.left.find(QueryBuilderConnector_NOT);
266  m_ui->m_connectorComboBox->addItem(it->second.c_str());
267  it = m_bimapConnector.left.find(QueryBuilderConnector_LIKE);
268  m_ui->m_connectorComboBox->addItem(it->second.c_str());
269 }
270 
272 {
273  m_bimapOrder.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOrder_ASC, tr("ASC").toStdString()));
274  m_bimapOrder.insert(boost::bimap<int, std::string>::value_type(QueryBuilderOrder_DESC, tr("DESC").toStdString()));
275 
276  m_ui->m_orderComboBox->clear();
277 
278  boost::bimap<int, std::string>::iterator it = m_bimapOrder.begin();
279 
280  while(it != m_bimapOrder.end())
281  {
282  m_ui->m_orderComboBox->addItem(it->right.c_str());
283  ++it;
284  }
285 }
286 
288 {
289  if(m_ui->m_dataSourceComboBox->currentText().isEmpty())
290  {
291  QMessageBox::warning(this, tr("Query Builder"), tr("Data Source not selected."));
292  return false;
293  }
294  else
295  {
296  getDataSets();
297  }
298 
299  return true;
300 }
301 
303 {
304  int numRows = m_ui->m_dataSetTableWidget->rowCount();
305 
306  if(numRows == 0)
307  {
308  QMessageBox::warning(this, tr("Query Builder"), tr("No Data Set selected."));
309  return false;
310  }
311  else
312  {
313  getProperties();
314  }
315 
316  return true;
317 }
318 
320 {
321  if(m_propertyList->getOutputValues().empty())
322  {
323  QMessageBox::warning(this, tr("Query Builder"), tr("No Property selected."));
324  return false;
325  }
326 
327  return true;
328 }
329 
331 {
332  int numRows = m_ui->m_whereClauseTableWidget->rowCount();
333 
334  if(numRows == 0)
335  {
336  QMessageBox::warning(this, tr("Query Builder"), tr("No Where Clause defined."));
337  return false;
338  }
339 
340  return true;
341 }
342 
344 {
345  return true;
346 }
347 
349 {
350  return buildQuery();
351 }
352 
354 {
355  return true;
356 }
357 
359 {
360  std::string dataSourceName = value.toStdString();
361 
363 }
364 
366 {
367  if(value.isEmpty() == false)
368  m_ui->m_dataSetAliasLineEdit->setText(value);
369 }
370 
372 {
373  if(m_ui->m_dataSetComboBox->currentText().isEmpty())
374  {
375  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set not selected."));
376  return;
377  }
378 
379  if(m_ui->m_dataSetAliasLineEdit->text().isEmpty())
380  {
381  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set Alias not defined."));
382  return;
383  }
384 
385  std::string dataSetName = m_ui->m_dataSetComboBox->currentText().toStdString();
386  std::string aliasName = m_ui->m_dataSetAliasLineEdit->text().toStdString();
387 
388  int newrow = m_ui->m_dataSetTableWidget->rowCount();
389 
390  //check if already exist an alias with this name
391  for(int i = 0; i < newrow; ++i)
392  {
393  QTableWidgetItem* itemName = m_ui->m_dataSetTableWidget->item(i, 1);
394 
395  if(itemName->text().toStdString() == aliasName)
396  {
397  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set Alias already defined."));
398  return;
399  }
400  }
401 
402  //new entry
403  m_ui->m_dataSetTableWidget->insertRow(newrow);
404 
405  QTableWidgetItem* itemDataSet = new QTableWidgetItem(QString::fromStdString(dataSetName));
406  m_ui->m_dataSetTableWidget->setItem(newrow, 0, itemDataSet);
407 
408  QTableWidgetItem* itemAlias = new QTableWidgetItem(QString::fromStdString(aliasName));
409  m_ui->m_dataSetTableWidget->setItem(newrow, 1, itemAlias);
410 
411  m_ui->m_dataSetTableWidget->resizeColumnToContents(0);
412 }
413 
415 {
416  int row = m_ui->m_dataSetTableWidget->currentRow();
417 
418  if(row >= 0)
419  m_ui->m_dataSetTableWidget->removeRow(row);
420 
421  m_ui->m_dataSetTableWidget->resizeColumnToContents(0);
422 }
423 
425 {
426  if(m_ui->m_restrictValueComboBox->currentText().isEmpty())
427  {
428  QMessageBox::warning(this, tr("Query Builder"), tr("Restrict value not defined."));
429  return;
430  }
431 
432  if(m_ui->m_valuePropertyRadioButton->isChecked() == false &&
433  m_ui->m_valueValueRadioButton->isChecked() == false)
434  {
435  if(m_ui->m_valueValueComboBox->currentText().isEmpty())
436  {
437  QMessageBox::warning(this, tr("Query Builder"), tr("Value not defined."));
438  return;
439  }
440  }
441 
442  if(m_ui->m_valueValueRadioButton->isChecked())
443  {
444  if(m_ui->m_valueValueComboBox->currentText().isEmpty())
445  {
446  QMessageBox::warning(this, tr("Query Builder"), tr("Value not defined."));
447  return;
448  }
449  }
450 
451  int newrow = m_ui->m_whereClauseTableWidget->rowCount();
452 
453  std::string restrictValue = m_ui->m_restrictValueComboBox->currentText().toStdString();
454  std::string operatorStr = m_ui->m_OperatorComboBox->currentText().toStdString();
455  std::string connector = m_ui->m_connectorComboBox->currentText().toStdString();
456 
457  std::string valueStr = "";
458  if(m_ui->m_valuePropertyRadioButton->isChecked())
459  valueStr = m_ui->m_valuePropertyComboBox->currentText().toStdString();
460  else if(m_ui->m_valueValueRadioButton->isChecked())
461  valueStr = m_ui->m_valueValueComboBox->currentText().toStdString();
462 
463 //new entry
464  m_ui->m_whereClauseTableWidget->insertRow(newrow);
465 
466  QTableWidgetItem* itemProperty = new QTableWidgetItem(QString::fromStdString(restrictValue));
467  m_ui->m_whereClauseTableWidget->setItem(newrow, 0, itemProperty);
468 
469  QTableWidgetItem* itemOperator = new QTableWidgetItem(QString::fromStdString(operatorStr));
470  m_ui->m_whereClauseTableWidget->setItem(newrow, 1, itemOperator);
471 
472  QTableWidgetItem* itemValue = new QTableWidgetItem(QString::fromStdString(valueStr));
473  m_ui->m_whereClauseTableWidget->setItem(newrow, 2, itemValue);
474 
475  QTableWidgetItem* itemConnector = new QTableWidgetItem(QString::fromStdString(connector));
476  m_ui->m_whereClauseTableWidget->setItem(newrow, 3, itemConnector);
477 
478  m_ui->m_whereClauseTableWidget->resizeColumnsToContents();
479 }
480 
482 {
483  int row = m_ui->m_whereClauseTableWidget->currentRow();
484 
485  if(row >= 0)
486  m_ui->m_whereClauseTableWidget->removeRow(row);
487 
488  m_ui->m_whereClauseTableWidget->resizeColumnsToContents();
489 }
490 
492 {
493  if(m_ui->m_orderPropertyComboBox->currentText().isEmpty())
494  {
495  QMessageBox::warning(this, tr("Query Builder"), tr("Property value not defined."));
496  return;
497  }
498 
499  int newrow = m_ui->m_orderTableWidget->rowCount();
500 
501  std::string propertyValue = m_ui->m_orderPropertyComboBox->currentText().toStdString();
502  std::string order = m_ui->m_orderComboBox->currentText().toStdString();
503 
504 //new entry
505  m_ui->m_orderTableWidget->insertRow(newrow);
506 
507  QTableWidgetItem* itemProperty = new QTableWidgetItem(QString::fromStdString(propertyValue));
508  m_ui->m_orderTableWidget->setItem(newrow, 0, itemProperty);
509 
510  QTableWidgetItem* itemOrder = new QTableWidgetItem(QString::fromStdString(order));
511  m_ui->m_orderTableWidget->setItem(newrow, 1, itemOrder);
512 
513  m_ui->m_orderTableWidget->resizeColumnsToContents();
514 }
515 
517 {
518  int row = m_ui->m_orderTableWidget->currentRow();
519 
520  if(row >= 0)
521  m_ui->m_orderTableWidget->removeRow(row);
522 
523  m_ui->m_orderTableWidget->resizeColumnsToContents();
524 }
std::map< std::string, DataSourcePtr >::iterator iterator
std::auto_ptr< DoubleListWidget > m_propertyList
std::auto_ptr< Ui::QueryBuilderWizardForm > m_ui
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
std::auto_ptr< DoubleListWidget > m_groupByList
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
Definition: Utils.cpp:153
void onDataSetComboBoxActivated(const QString &value)
void onDataSourceComboBoxActivated(const QString &value)
A Qt dialog that allows users to create query builder based on TerraLib query framework.