All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  layer->setSRID(gp->getSRID());
217 
218  // style
219  layer->setStyle(te::se::CreateFeatureTypeStyle(gp->getGeometryType()));
220 
221  return layer;
222 }
223 
225 {
226  m_dataSourcePage.reset(new te::qt::widgets::DataSourceSelectorWizardPage(this));
227  m_dataSetPage.reset(new te::qt::widgets::DataSetWizardPage(this));
228  m_fieldPage.reset(new te::qt::widgets::FieldsWizardPage(this));
229  m_groupByPage.reset(new te::qt::widgets::GroupByWizardPage(this));
230  m_layerAttrPage.reset(new te::qt::widgets::LayerAttributesWizardPage(this));
231  m_orderByPage.reset(new te::qt::widgets::OrderByWizardPage(this));
232  m_whereClausePage.reset(new te::qt::widgets::WhereClauseWizardPage(this));
233 
234  addPage(m_dataSourcePage.get());
235  addPage(m_dataSetPage.get());
236  addPage(m_fieldPage.get());
237  addPage(m_whereClausePage.get());
238  addPage(m_groupByPage.get());
239  addPage(m_orderByPage.get());
240  addPage(m_layerAttrPage.get());
241 }
242 
244 {
245  std::string dsId = m_ds->getId();
246 
247  std::vector<std::string> datasetNames;
248 
249  te::da::GetDataSetNames(datasetNames, dsId);
250 
251  m_dataSetPage->getWidget()->setDataSetNames(datasetNames);
252 }
253 
255 {
256  //get the dataset names
257  std::vector<std::string> datasetNames = m_ds->getDataSetNames();
258 
259  std::vector<std::pair<std::string, std::string> > dataSetSelecteds;
260 
261  m_dataSetPage->getWidget()->getDataSetNames(dataSetSelecteds);
262 
263  std::vector<std::string> inputProperties;
264  std::vector<std::string> geomProperties;
265 
266  int srid = 0;
267 
268  //get properties for each data set
269  for(size_t t = 0; t < dataSetSelecteds.size(); ++t)
270  {
271  //alias name
272  std::string alias = dataSetSelecteds[t].second;
273 
274  //data set name
275  std::string dataSetName = dataSetSelecteds[t].first;
276 
277  //get datasettype
278  std::auto_ptr<te::da::DataSetType> dsType(0);
279 
280  for(unsigned int i = 0; i < datasetNames.size(); ++i)
281  {
282  if(datasetNames[i] == dataSetName)
283  dsType = m_ds->getDataSetType(datasetNames[i]);
284  }
285 
286  if(dsType.get())
287  {
288  for(size_t i = 0; i < dsType->size(); ++i)
289  {
290  std::string propName = dsType->getProperty(i)->getName();
291  std::string fullName = alias + "." + propName;
292 
293  if(dsType->getProperty(i)->getType() == te::dt::GEOMETRY_TYPE)
294  {
295  te::gm::GeometryProperty* geomProp = dynamic_cast<te::gm::GeometryProperty*>(dsType->getProperty(i));
296 
297  if(geomProp)
298  srid = geomProp->getSRID();
299 
300  geomProperties.push_back(fullName);
301  }
302  else
303  inputProperties.push_back(fullName);
304  }
305  }
306  }
307 
308  //set values in other pages
309  m_fieldPage->getWidget()->setInputValues(inputProperties);
310  m_fieldPage->getWidget()->clearOutputValues();
311  m_fieldPage->getWidget()->setFixedOutputValues(geomProperties, "geometry");
312  m_groupByPage->getWidget()->setInputValues(inputProperties);
313  m_whereClausePage->getWidget()->setAttributeList(inputProperties);
314  m_whereClausePage->getWidget()->setGeomAttributeList(geomProperties, srid);
315  m_orderByPage->getWidget()->setAttributeList(inputProperties);
316 }
317 
319 {
320 
321  te::da::DataSourceCapabilities dsCap = m_ds->getCapabilities();
322 
324 
325  std::vector<std::string> vecOperators;
326 
327  std::set<std::string>::iterator it;
328 
329  //Arithmetic Operators
330  it = queryCap.getArithmeticOperators().begin();
331 
332  while(it != queryCap.getArithmeticOperators().end())
333  {
334  vecOperators.push_back(*it);
335 
336  ++it;
337  }
338 
339  //Comparsion Operators
340  it = queryCap.getComparsionOperators().begin();
341 
342  while(it != queryCap.getComparsionOperators().end())
343  {
344  vecOperators.push_back(*it);
345 
346  ++it;
347  }
348 
349  m_whereClausePage->getWidget()->setOperatorsList(vecOperators);
350 
351  //Spatial Operators
352  std::vector<std::string> vecSpatialOperators;
353 
354  it = queryCap.getSpatialTopologicOperators().begin();
355 
356  while(it != queryCap.getSpatialTopologicOperators().end())
357  {
358  vecSpatialOperators.push_back(*it);
359 
360  ++it;
361  }
362 
363  m_whereClausePage->getWidget()->setSpatialOperatorsList(vecSpatialOperators);
364 
365  //Logical Operators
366  std::vector<std::string> vecConnectors;
367 
368  it = queryCap.getLogicalOperators().begin();
369 
370  while(it != queryCap.getLogicalOperators().end())
371  {
372  vecConnectors.push_back(*it);
373 
374  ++it;
375  }
376 
377  m_whereClausePage->getWidget()->setConnectorsList(vecConnectors);
378 }
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:176
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