WFSConnectorDialog.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/plugins/datasource/wfs/WFSConnectorDialog.cpp
22 
23  \brief A dialog window for showing the WFS connector widget.
24 */
25 
26 // TerraLib
27 #include "../../../../core/translator/Translator.h"
28 #include "../../../../dataaccess/datasource/DataSource.h"
29 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
30 #include "../../../../dataaccess/datasource/DataSourceManager.h"
31 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
32 #include "../../../widgets/Exception.h"
33 #include "WFSConnectorDialog.h"
34 #include "ui_WFSConnectorDialogForm.h"
35 
36 // Boost
37 #include <boost/algorithm/string/case_conv.hpp>
38 #include <boost/uuid/random_generator.hpp>
39 #include <boost/uuid/uuid_io.hpp>
40 #include <boost/lexical_cast.hpp>
41 
42 // Qt
43 #include <QMessageBox>
44 
46  : QDialog(parent, f),
47  m_ui(new Ui::WFSConnectorDialogForm)
48 {
49 // add controls
50  m_ui->setupUi(this);
51 
52 // connect signal and slots
53  connect(m_ui->m_openPushButton, SIGNAL(pressed()), this, SLOT(openPushButtonPressed()));
54  connect(m_ui->m_testPushButton, SIGNAL(pressed()), this, SLOT(testPushButtonPressed()));
55  connect(m_ui->m_helpPushButton, SIGNAL(pressed()), this, SLOT(helpPushButtonPressed()));
56 }
57 
59 
61 {
62  return m_datasource;
63 }
64 
66 {
67  return m_driver;
68 }
69 
71 {
72  m_datasource = ds;
73 
74  if(m_datasource.get() != nullptr)
75  {
76  const te::core::URI& connInfo = m_datasource->getConnInfo();
77 
78  if (!connInfo.uri().empty())
79  m_ui->m_serverLineEdit->setText(QString::fromUtf8(connInfo.uri().c_str()));
80 
81  m_ui->m_datasourceTitleLineEdit->setText(QString::fromUtf8(m_datasource->getTitle().c_str()));
82 
83  m_ui->m_dataSourceDescriptionTextEdit->setText(QString::fromUtf8(m_datasource->getDescription().c_str()));
84  }
85 }
86 
88 {
89  try
90  {
91  // Check if driver is loaded
92  if(te::da::DataSourceFactory::find("WFS") == 0)
93  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for WFS data sources!"));
94 
95  // Get the data source connection info based on form data
96  std::string dsInfo = getConnectionInfo();
97 
98  // Perform connection
99  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("WFS", dsInfo);
100  ds->open();
101  m_driver.reset(ds.release());
102 
103  if(m_driver.get() == nullptr)
104  throw te::qt::widgets::Exception(TE_TR("Could not open WFS data source due to an unknown error!"));
105 
106  QString title = m_ui->m_datasourceTitleLineEdit->text().trimmed();
107 
108  if(title.isEmpty())
109  title = m_ui->m_serverLineEdit->text().trimmed();
110 
111  if(m_datasource.get() == nullptr)
112  {
113  // Create a new data source based on the form data
115 
116  m_datasource->setConnInfo(dsInfo);
117 
118  boost::uuids::basic_random_generator<boost::mt19937> gen;
119  boost::uuids::uuid u = gen();
120  std::string dsId = boost::uuids::to_string(u);
121 
122  m_datasource->setId(dsId);
123  m_driver->setId(dsId);
124  m_datasource->setTitle(title.toUtf8().data());
125  m_datasource->setDescription(m_ui->m_dataSourceDescriptionTextEdit->toPlainText().trimmed().toUtf8().data());
126  m_datasource->setAccessDriver("WFS");
127  m_datasource->setType("WFS");
128  }
129  else
130  {
131  m_driver->setId(m_datasource->getId());
132  m_datasource->setConnInfo(dsInfo);
133  m_datasource->setTitle(title.toUtf8().data());
134  m_datasource->setDescription(m_ui->m_dataSourceDescriptionTextEdit->toPlainText().trimmed().toUtf8().data());
135  }
136  }
137  catch(const std::exception& e)
138  {
139  QMessageBox::warning(this,
140  tr("TerraLib Qt Components"),
141  tr(e.what()));
142  }
143  catch(...)
144  {
145  QMessageBox::warning(this,
146  tr("TerraLib Qt Components"),
147  tr("Unknown error while opening WFS data source!"));
148  }
149 
150  accept();
151 }
152 
154 {
155  try
156  {
157  // Check if driver is loaded
158  if(te::da::DataSourceFactory::find("WFS") == 0)
159  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for WFS data sources!"));
160 
161  // Perform connection
162  std::unique_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make("WFS", getConnectionInfo()));
163 
164  if(ds.get() == nullptr)
165  throw te::qt::widgets::Exception(TE_TR("Could not open WFS server!"));
166 
167  ds->open();
168 
169  QMessageBox::information(this,
170  tr("TerraLib Qt Components"),
171  tr("Data source is ok!"));
172 
173  ds->close();
174  }
175  catch(const std::exception& e)
176  {
177  QMessageBox::warning(this,
178  tr("TerraLib Qt Components"),
179  tr(e.what()));
180  }
181  catch(...)
182  {
183  QMessageBox::warning(this,
184  tr("TerraLib Qt Components"),
185  tr("Unknown error while testing WFS data source!"));
186  }
187 }
188 
190 {
191  QMessageBox::warning(this,
192  tr("TerraLib Qt Components"),
193  tr("Not implemented yet!\nWe will provide it soon!"));
194 }
195 
197 {
198  QString qstr; // Auxiliary string used to hold temporary data
199 
200  std::string strURI("WFS:"); // The base of the URI
201 
202  // Get the server URL
203  qstr = m_ui->m_serverLineEdit->text().trimmed();
204  if(qstr.isEmpty())
205  throw te::qt::widgets::Exception(TE_TR("Please define the server address first!"));
206 
207  strURI += qstr.toUtf8().data();
208 
209  return strURI;
210 }
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
std::unique_ptr< Ui::WFSConnectorDialogForm > m_ui
boost::shared_ptr< DataSource > DataSourcePtr
const te::da::DataSourcePtr & getDriver() const
static te::dt::Date ds(2010, 01, 01)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
WFSConnectorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
const te::da::DataSourceInfoPtr & getDataSource() const
void set(const te::da::DataSourceInfoPtr &ds)
const std::string & uri() const
Retrieving the full URI.
Definition: URI.cpp:88
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
A dialog window for showing the WFS connector widget.
A class that represents a data source component.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr