All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PostGISCreatorDialog.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 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/pgis/PostGISCreatorDialog.cpp
22 
23  \brief A dialog window for showing the PostgreSQL creator widget.
24 */
25 
26 // TerraLib
27 #include "PostGISCreatorDialog.h"
28 #include "../../../../common/Translator.h"
29 #include "../../../../dataaccess/datasource/DataSource.h"
30 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
31 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
32 #include "../../../../dataaccess/datasource/DataSourceManager.h"
33 #include "../../../widgets/Exception.h"
34 #include "ui_PostGISCreatorDialogForm.h"
35 
36 // Boost
37 #include <boost/algorithm/string/case_conv.hpp>
38 #include <boost/lexical_cast.hpp>
39 #include <boost/uuid/random_generator.hpp>
40 #include <boost/uuid/uuid_io.hpp>
41 
42 // Qt
43 #include <QtGui/QMessageBox>
44 
46  : QDialog(parent, f),
47  m_ui(new Ui::PostGISCreatorDialogForm)
48 {
49  // add controls
50  m_ui->setupUi(this);
51 
52 // init controls
53  m_ui->m_advancedOptionsGroupBox->hide();
54 
55 // connect signal and slots
56  connect(m_ui->m_advancedOptionsCheckBox, SIGNAL(toggled(bool)), this, SLOT(advancedCreationOptionsCheckBoxToggled(bool)));
57  connect(m_ui->m_applyPushButton, SIGNAL(pressed()), this, SLOT(applyPushButtonPressed()));
58  connect(m_ui->m_closePushButton, SIGNAL(pressed()), this, SLOT(closePushButtonPressed()));
59  connect(m_ui->m_userNameLineEdit, SIGNAL(editingFinished()), this, SLOT(passwordLineEditEditingFinished()));
60  connect(m_ui->m_passwordLineEdit, SIGNAL(editingFinished()), this, SLOT(passwordLineEditEditingFinished()));
61 
62  m_ui->m_portLineEdit->setValidator(new QIntValidator(0, 9999, this));
63 
64  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
65  m_ui->m_helpPushButton->setPageReference("plugins/pgis/pgis_creator.html");
66 }
67 
69 {
70 
71 }
72 
74 {
75  try
76  {
77 // check if driver is loaded
78  if(te::da::DataSourceFactory::find("POSTGIS") == 0)
79  throw te::qt::widgets::Exception(TR_QT_WIDGETS("Sorry! No data access driver loaded for PostgreSQL data sources!"));
80 
81 // get data source connection info based on form data
82  std::map<std::string, std::string> dsInfo;
83 
84  getConnectionInfo(dsInfo);
85 
86 // create database
87  te::da::DataSource::create("POSTGIS", dsInfo);
88 
89  // Connect
90  std::map<std::string, std::string> connInfo;
91  connInfo["PG_HOST"] = dsInfo["PG_HOST"];
92  connInfo["PG_PORT"] = dsInfo["PG_PORT"];
93  connInfo["PG_USER"] = dsInfo["PG_USER"];
94  connInfo["PG_PASSWORD"] = dsInfo["PG_PASSWORD"];
95  connInfo["PG_DB_NAME"] = dsInfo["PG_NEWDB_NAME"];
96  connInfo["PG_MIN_POOL_SIZE"] = "1";
97  connInfo["PG_MAX_POOL_SIZE"] = "4";
98  connInfo["PG_CONNECT_TIMEOUT"] = "5";
99  if(!dsInfo["PG_NEWDB_ENCODING"].empty())
100  connInfo["PG_CLIENT_ENCODING"] = dsInfo["PG_NEWDB_ENCODING"];
101 
102  std::auto_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("POSTGIS");
103  ds->setConnectionInfo(connInfo);
104  ds->open();
105 
106  m_driver.reset(ds.release());
107 
108  if(m_driver.get() == 0)
109  throw te::qt::widgets::Exception(TR_QT_WIDGETS("Could not open POSTGIS data source due to an unknown error!"));
110 
111  QString title = m_ui->m_hostNameLineEdit->text().trimmed() + QString::fromStdString("@") + m_ui->m_newDatabaseNameLineEdit->text().trimmed() + QString::fromStdString("@") + m_ui->m_userNameLineEdit->text().trimmed();
112 
113  if(m_datasource.get() == 0)
114  {
115 // create a new data source based on form data
116  m_datasource.reset(new te::da::DataSourceInfo);
117 
118  m_datasource->setConnInfo(connInfo);
119 
120  boost::uuids::basic_random_generator<boost::mt19937> gen;
121  boost::uuids::uuid u = gen();
122  std::string dsId = boost::uuids::to_string(u);
123 
124  m_datasource->setId(dsId);
125  m_driver->setId(dsId);
126  m_datasource->setTitle(title.toUtf8().data());
127  m_datasource->setDescription("");
128  m_datasource->setAccessDriver("POSTGIS");
129  m_datasource->setType("POSTGIS");
130  }
131  else
132  {
133  m_driver->setId(m_datasource->getId());
134  m_datasource->setConnInfo(connInfo);
135  m_datasource->setTitle(title.toUtf8().data());
136  m_datasource->setDescription("");
137  }
138 
139  }
140  catch(const std::exception& e)
141  {
142  QMessageBox::warning(this,
143  tr("TerraLib Qt Components"),
144  tr(e.what()));
145  return;
146  }
147  catch(...)
148  {
149  QMessageBox::warning(this,
150  tr("TerraLib Qt Components"),
151  tr("Unknown error while opening PostgreSQL database!"));
152  return;
153  }
154 
155  accept();
156 }
157 
159 {
160  reject();
161 }
162 
164 {
165  m_ui->m_advancedOptionsGroupBox->setVisible(t);
166 }
167 
168 void te::qt::plugins::pgis::PostGISCreatorDialog::getConnectionInfo(std::map<std::string, std::string>& connInfo) const
169 {
170 // clear input
171  connInfo.clear();
172 
173 // get host
174  QString qstr = m_ui->m_hostNameLineEdit->text().trimmed();
175 
176  if(!qstr.isEmpty())
177  connInfo["PG_HOST"] = qstr.toStdString();
178 
179 // get port
180  qstr = m_ui->m_portLineEdit->text().trimmed();
181 
182  if(!qstr.isEmpty())
183  connInfo["PG_PORT"] = qstr.toStdString();
184 
185 // get user
186  qstr = m_ui->m_userNameLineEdit->text().trimmed();
187 
188  if(!qstr.isEmpty())
189  connInfo["PG_USER"] = qstr.toStdString();
190 
191 // get password
192  qstr = m_ui->m_passwordLineEdit->text().trimmed();
193 
194  if(!qstr.isEmpty())
195  connInfo["PG_PASSWORD"] = qstr.toStdString();
196 
197 // get dbname
198  qstr = m_ui->m_newDatabaseNameLineEdit->text().trimmed();
199 
200  if(!qstr.isEmpty())
201  connInfo["PG_NEWDB_NAME"] = qstr.toStdString();
202 
203 // get Template
204  qstr = m_ui->m_templateComboBox->currentText().trimmed();
205 
206  if(!qstr.isEmpty())
207  connInfo["PG_NEWDB_TEMPLATE"] = qstr.toStdString();
208 
209 // get Owner
210  qstr = m_ui->m_ownerComboBox->currentText().trimmed();
211 
212  if(!qstr.isEmpty())
213  connInfo["PG_NEWDB_OWNER"] = qstr.toStdString();
214 
215 // get Encoding
216  qstr = m_ui->m_encodingComboBox->currentText().trimmed();
217 
218  if(!qstr.isEmpty())
219  connInfo["PG_NEWDB_ENCODING"] = qstr.toStdString();
220 
221 // get Tablespace
222  qstr = m_ui->m_tablespaceLineEdit->text().trimmed();
223 
224  if(!qstr.isEmpty())
225  connInfo["PG_NEWDB_TABLESPACE"] = qstr.toStdString();
226 
227 // get Connections limit
228  if(!m_ui->m_noLimitConnectionsGroupBox->isChecked())
229  {
230  qstr = m_ui->m_connectionsLimitSpinBox->text().trimmed();
231 
232  if(!qstr.isEmpty())
233  {
234  if(boost::lexical_cast<int>(qstr.toStdString()) >= 1)
235  connInfo["PG_NEWDB_CONN_LIMIT"] = qstr.toStdString();
236  else
237  connInfo["PG_NEWDB_CONN_LIMIT"] = "-1";
238  }
239  }
240 }
241 
243 {
244  return m_datasource;
245 }
246 
248 {
249  return m_driver;
250 }
251 
253 {
254  try
255  {
256  if(m_ui->m_userNameLineEdit->text() != "" && m_ui->m_passwordLineEdit->text() != "")
257  {
258  std::map<std::string, std::string> dsInfo;
259  getConnectionInfo(dsInfo);
260 
261  // Get Templates/Databases
262  std::vector<std::string> templates = te::da::DataSource::getDataSourceNames("POSTGIS", dsInfo);
263  if(!templates.empty())
264  for(std::size_t i = 0; i < templates.size(); i++)
265  m_ui->m_templateComboBox->addItem(templates[i].c_str());
266 
267  m_ui->m_templateComboBox->setCurrentIndex(m_ui->m_templateComboBox->findText("postgis"));
268 
269  // Get Encodings
270  m_ui->m_encodingComboBox->addItem("");
271  std::vector<std::string> encodings = te::da::DataSource::getEncodings("POSTGIS", dsInfo);
272  if(!encodings.empty())
273  for(std::size_t i = 0; i < encodings.size(); i++)
274  m_ui->m_encodingComboBox->addItem(encodings[i].c_str());
275 
276  // Try to go the owners
277  m_ui->m_ownerComboBox->clear();
278  std::map<std::string, std::string> info;
279  std::map<std::string, std::string> aux;
280  getConnectionInfo(aux);
281  info["PG_HOST"] = aux["PG_HOST"].empty()?"localhost":aux["PG_HOST"];
282  info["PG_PORT"] = aux["PG_PORT"].empty()?"5432":aux["PG_PORT"];
283  info["PG_USER"] = aux["PG_USER"];
284  info["PG_PASSWORD"] = aux["PG_PASSWORD"];
285 
286  std::auto_ptr<te::da::DataSource> auxDs = te::da::DataSourceFactory::make("POSTGIS");
287  auxDs->setConnectionInfo(info);
288  auxDs->open();
289 
290  std::auto_ptr<te::da::DataSet> dsRoles = auxDs->query("select * from pg_roles");
291 
292  while(dsRoles->moveNext())
293  m_ui->m_ownerComboBox->addItem(dsRoles->getString("rolname").c_str());
294 
295  auxDs->close();
296  }
297  }
298  catch(...)
299  {
300  return;
301  }
302 }
static std::vector< std::string > getDataSourceNames(const std::string &dsType, const std::map< std::string, std::string > &info)
It returns the data source names available in the driver.
Definition: DataSource.cpp:508
static std::auto_ptr< DataSource > create(const std::string &dsType, const std::map< std::string, std::string > &dsInfo)
It creates a new repository for a data source.
Definition: DataSource.cpp:476
std::auto_ptr< Ui::PostGISCreatorDialogForm > m_ui
A class that represents a data source component.
PostGISCreatorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
static std::auto_ptr< DataSource > make(const std::string &dsType)
static std::vector< std::string > getEncodings(const std::string &dsType, const std::map< std::string, std::string > &info)
It gets the encoding names of the data source.
Definition: DataSource.cpp:518
static bool find(const std::string &dsType)
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
const te::da::DataSourceInfoPtr & getDataSource() const
const te::da::DataSourcePtr & getDriver() const
#define TR_QT_WIDGETS(message)
It marks a string in order to get translated. This is a special mark used in the TerraLib Qt Widgets ...
Definition: Config.h:60
void getConnectionInfo(std::map< std::string, std::string > &connInfo) const
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr