QueryLayerBuilderWizard.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/QueryLayerBuilderWizard.cpp
22 
23  \brief A Qt dialog that allows users to create query builder based on TerraLib query framework.
24 */
25 
26 // TerraLib
28 
29 // TerraLib
30 #include "../../../dataaccess/dataset/DataSet.h"
31 #include "../../../dataaccess/dataset/DataSetType.h"
32 #include "../../../dataaccess/datasource/DataSource.h"
33 #include "../../../dataaccess/datasource/DataSourceCapabilities.h"
34 #include "../../../dataaccess/datasource/DataSourceManager.h"
35 #include "../../../dataaccess/query/QueryCapabilities.h"
36 #include "../../../dataaccess/query/Select.h"
37 #include "../../../dataaccess/utils/Utils.h"
38 #include "../../../geometry/GeometryProperty.h"
39 #include "../../../maptools/QueryLayer.h"
40 #include "../../../se/Utils.h"
41 #include "../../widgets/help/HelpPushButton.h"
42 #include "../datasource/selector/DataSourceSelectorWizardPage.h"
43 #include "../datasource/selector/DataSourceSelectorWidget.h"
44 #include "../utils/DoubleListWidget.h"
45 #include "DataSetWidget.h"
46 #include "DataSetWizardPage.h"
47 #include "FieldsWizardPage.h"
48 #include "GroupByWizardPage.h"
50 #include "OrderByWidget.h"
51 #include "OrderByWizardPage.h"
52 #include "WhereClauseWidget.h"
53 #include "WhereClauseWizardPage.h"
54 
55 // STL
56 #include <cassert>
57 
58 // Qt
59 #include <QMessageBox>
60 #include <QIcon>
61 
62 // Boost
63 #include <boost/uuid/random_generator.hpp>
64 #include <boost/uuid/uuid_io.hpp>
65 
66 
68  : QWizard(parent)
69 {
70  m_ds.reset();
71 
72  //configure the wizard
73  this->setWizardStyle(QWizard::ModernStyle);
74  //this->setFixedSize(640, 480);
75  this->setWindowTitle(tr("Query Layer Builder"));
76 
77  this->setOption(QWizard::HaveHelpButton, true);
78  this->setOption(QWizard::HelpButtonOnRight, false);
79 
81 
82  this->setButton(QWizard::HelpButton, helpButton);
83 
84  helpButton->setPageReference("widgets/query/query_layer.html");
85 
86  addPages();
87 }
88 
90 {
91 
92 }
93 
95 {
96  if(currentPage() == m_dataSourcePage.get())
97  {
98  std::list<te::da::DataSourceInfoPtr> list = m_dataSourcePage->getSelectorWidget()->getSelecteds();
99 
100  if(list.empty())
101  {
102  return false;
103  }
104 
105  te::da::DataSourceInfoPtr dsInfo = *list.begin();
106  te::da::DataSourcePtr dataSource = te::da::DataSourceManager::getInstance().get(dsInfo->getId(), dsInfo->getType(), dsInfo->getConnInfo());
107 
108  if(!dataSource->isOpened())
109  dataSource->open();
110 
111  setDataSource(dataSource);
112 
113  return m_dataSourcePage->isComplete();
114  }
115  else if(currentPage() == m_dataSetPage.get())
116  {
117  getProperties();
118 
119  //used to get distinct values from a selected property
120  std::vector<std::pair<std::string, std::string> > vec;
121  m_dataSetPage->getWidget()->getDataSetNames(vec);
122  m_whereClausePage->getWidget()->setFromItems(vec);
123 
124  return m_dataSetPage->isComplete();
125  }
126  else if(currentPage() == m_fieldPage.get())
127  {
128  return m_fieldPage->isComplete();
129  }
130  else if(currentPage() == m_whereClausePage.get())
131  {
132  return m_whereClausePage->isComplete();
133  }
134  else if(currentPage() == m_groupByPage.get())
135  {
136  return m_groupByPage->isComplete();
137  }
138  else if(currentPage() == m_orderByPage.get())
139  {
140  return m_orderByPage->isComplete();
141  }
142  else if(currentPage() == m_layerAttrPage.get())
143  {
144  bool res = m_layerAttrPage->isComplete();
145 
146  if(!res)
147  QMessageBox::warning(this, tr("Warning"), tr("Layer name not defined."));
148 
149  return res;
150  }
151 
152  return false;
153 }
154 
156 {
157  assert(ds);
158 
159  m_ds = ds;
160 
161  m_whereClausePage->getWidget()->setDataSource(m_ds);
162 
163  getDataSets();
164 
165  getQueryCapabilities();
166 }
167 
168 void te::qt::widgets::QueryLayerBuilderWizard::setLayerList(std::list<te::map::AbstractLayerPtr>& layerList)
169 {
170  if(m_whereClausePage.get())
171  m_whereClausePage->getWidget()->setLayerList(layerList);
172 }
173 
175 {
176  //fields
177  te::da::Fields* fields = m_fieldPage->getFields();
178 
179  //from
180  te::da::From* from = m_dataSetPage->getWidget()->getFrom();
181 
182  //where
183  te::da::Where* w = m_whereClausePage->getWidget()->getWhere();
184 
185  //groupby
186  te::da::GroupBy* groupBy = m_groupByPage->getGroupBy();
187 
188  //order
189  te::da::OrderBy* orderBy = m_orderByPage->getWidget()->gerOrderBy();
190 
191  //build the select object
192  te::da::Select s(fields, from, w, groupBy, orderBy);
193 
194  return s;
195 }
196 
198 {
199  static boost::uuids::basic_random_generator<boost::mt19937> gen;
200  boost::uuids::uuid u = gen();
201  std::string id = boost::uuids::to_string(u);
202 
203  std::string title = m_layerAttrPage->getLayerName();
204 
205  te::da::Select* s = new te::da::Select(getSelectQuery());
206 
207  te::map::QueryLayerPtr layer(new te::map::QueryLayer(id, title));
208  layer->setDataSourceId(m_ds->getId());
209  layer->setRendererType("QUERY_LAYER_RENDERER");
210  layer->setQuery(s);
211  layer->computeExtent();
212 
213  // SRID
214  std::auto_ptr<const te::map::LayerSchema> schema(layer->getSchema());
216  if (gp)
217  {
218  layer->setSRID(gp->getSRID());
219 
220  // style
221  layer->setStyle(te::se::CreateFeatureTypeStyle(gp->getGeometryType()));
222  }
223 
224  return layer;
225 }
226 
228 {
229  m_dataSourcePage.reset(new te::qt::widgets::DataSourceSelectorWizardPage(this));
230  m_dataSetPage.reset(new te::qt::widgets::DataSetWizardPage(this));
231  m_fieldPage.reset(new te::qt::widgets::FieldsWizardPage(this));
232  m_groupByPage.reset(new te::qt::widgets::GroupByWizardPage(this));
233  m_layerAttrPage.reset(new te::qt::widgets::LayerAttributesWizardPage(this));
234  m_orderByPage.reset(new te::qt::widgets::OrderByWizardPage(this));
235  m_whereClausePage.reset(new te::qt::widgets::WhereClauseWizardPage(this));
236 
237  addPage(m_dataSourcePage.get());
238  addPage(m_dataSetPage.get());
239  addPage(m_fieldPage.get());
240  addPage(m_whereClausePage.get());
241  addPage(m_groupByPage.get());
242  addPage(m_orderByPage.get());
243  addPage(m_layerAttrPage.get());
244 }
245 
247 {
248  std::string dsId = m_ds->getId();
249 
250  std::vector<std::string> datasetNames;
251 
252  te::da::GetDataSetNames(datasetNames, dsId);
253 
254  m_dataSetPage->getWidget()->setDataSetNames(datasetNames);
255 }
256 
258 {
259  //get the dataset names
260  std::vector<std::string> datasetNames = m_ds->getDataSetNames();
261 
262  std::vector<std::pair<std::string, std::string> > dataSetSelecteds;
263 
264  m_dataSetPage->getWidget()->getDataSetNames(dataSetSelecteds);
265 
266  std::vector<std::string> inputProperties;
267  std::vector<std::string> geomProperties;
268 
269  int srid = 0;
270 
271  //get properties for each data set
272  for(size_t t = 0; t < dataSetSelecteds.size(); ++t)
273  {
274  //alias name
275  std::string alias = dataSetSelecteds[t].second;
276 
277  //data set name
278  std::string dataSetName = dataSetSelecteds[t].first;
279 
280  //get datasettype
281  std::auto_ptr<te::da::DataSetType> dsType(0);
282 
283  for(unsigned int i = 0; i < datasetNames.size(); ++i)
284  {
285  if(datasetNames[i] == dataSetName)
286  dsType = m_ds->getDataSetType(datasetNames[i]);
287  }
288 
289  if(dsType.get())
290  {
291  for(size_t i = 0; i < dsType->size(); ++i)
292  {
293  std::string propName = dsType->getProperty(i)->getName();
294  std::string fullName = alias + "." + propName;
295 
296  if(dsType->getProperty(i)->getType() == te::dt::GEOMETRY_TYPE)
297  {
298  te::gm::GeometryProperty* geomProp = dynamic_cast<te::gm::GeometryProperty*>(dsType->getProperty(i));
299 
300  if(geomProp)
301  srid = geomProp->getSRID();
302 
303  geomProperties.push_back(fullName);
304  }
305  else
306  inputProperties.push_back(fullName);
307  }
308  }
309  }
310 
311  //set values in other pages
312  m_fieldPage->getWidget()->setInputValues(inputProperties);
313  m_fieldPage->getWidget()->clearOutputValues();
314  m_fieldPage->getWidget()->setFixedOutputValues(geomProperties, "geometry");
315  m_groupByPage->getWidget()->setInputValues(inputProperties);
316  m_whereClausePage->getWidget()->setAttributeList(inputProperties);
317  m_whereClausePage->getWidget()->setGeomAttributeList(geomProperties, srid);
318  m_orderByPage->getWidget()->setAttributeList(inputProperties);
319 }
320 
322 {
323 
324  te::da::DataSourceCapabilities dsCap = m_ds->getCapabilities();
325 
327 
328  std::vector<std::string> vecOperators;
329 
330  std::set<std::string>::iterator it;
331 
332  //Arithmetic Operators
333  it = queryCap.getArithmeticOperators().begin();
334 
335  while(it != queryCap.getArithmeticOperators().end())
336  {
337  vecOperators.push_back(*it);
338 
339  ++it;
340  }
341 
342  //Comparsion Operators
343  it = queryCap.getComparsionOperators().begin();
344 
345  while(it != queryCap.getComparsionOperators().end())
346  {
347  vecOperators.push_back(*it);
348 
349  ++it;
350  }
351 
352  m_whereClausePage->getWidget()->setOperatorsList(vecOperators);
353 
354  //Spatial Operators
355  std::vector<std::string> vecSpatialOperators;
356 
357  it = queryCap.getSpatialTopologicOperators().begin();
358 
359  while(it != queryCap.getSpatialTopologicOperators().end())
360  {
361  vecSpatialOperators.push_back(*it);
362 
363  ++it;
364  }
365 
366  m_whereClausePage->getWidget()->setSpatialOperatorsList(vecSpatialOperators);
367 
368  //Logical Operators
369  std::vector<std::string> vecConnectors;
370 
371  it = queryCap.getLogicalOperators().begin();
372 
373  while(it != queryCap.getLogicalOperators().end())
374  {
375  vecConnectors.push_back(*it);
376 
377  ++it;
378  }
379 
380  m_whereClausePage->getWidget()->setConnectorsList(vecConnectors);
381 }
const std::set< std::string > & getComparsionOperators() const
This class is a wizard page with the DoubleListWidget component, used to create the FIELD object of a...
This file has the WhereClauseWizardPage class.
boost::ptr_vector< GroupByItem > GroupBy
A class that can be used to model a GROUP BY clause.
Definition: GroupBy.h:37
Geometric property.
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A Qt dialog that allows users to create query builder based on TerraLib query framework.
void setPageReference(const QString &ref)
Sets the documentation page reference.
This class is a wizard page with the WhereClauseWidget component.
This file has the FieldsWizardPage class.
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
Definition: OrderBy.h:37
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
const std::set< std::string > & getArithmeticOperators() const
A layer resulting from a query.
Definition: QueryLayer.h:50
This file has the OrderByWizardPage class.
const QueryCapabilities & getQueryCapabilities() const
void setDataSource(const te::da::DataSourcePtr &ds)
TESEEXPORT Style * CreateFeatureTypeStyle(const te::gm::GeomType &geomType)
Try creates an appropriate feature type style based on given geometry type.
Definition: Utils.cpp:284
const std::set< std::string > & getLogicalOperators() const
This file has the GroupByWizardPage class.
A class that informs the query support of a given data source.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
This file has the OrderByWidget class.
This file has the DataSetWidget class.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
const Fields * getFields() const
It returns the list of output expressions used to form the result set.
Definition: Select.cpp:932
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
Definition: Utils.cpp:153
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
const std::set< std::string > & getSpatialTopologicOperators() const
This class is used to define the layer attributes information.
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
This file has the DataSetWidget class.
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
void setLayerList(std::list< te::map::AbstractLayerPtr > &layerList)
boost::intrusive_ptr< QueryLayer > QueryLayerPtr
Definition: QueryLayer.h:183
This class is a wizard page with the DoubleListWidget component, used to create the GROUPBY object of...
This class is a wizard page with the DataSetWidget component.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
This file has the DataSetWizardPage class.
This file has the LayerAttributesWizardPage class.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr