All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ADOCreatorDialog.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/connector/ado/ADOCreatorDialog.cpp
22 
23  \brief ....
24 */
25 
26 // TerraLib
27 #include "../../../../common/Translator.h"
28 #include "../../../../dataaccess/datasource/DataSource.h"
29 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
30 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
31 #include "../../../../dataaccess/datasource/DataSourceManager.h"
32 #include "../../../widgets/Exception.h"
33 #include "../../../widgets/Utils.h"
34 #include "ui_ADOCreatorDialogForm.h"
35 #include "ADOCreatorDialog.h"
36 
37 // Boost
38 #include <boost/algorithm/string/case_conv.hpp>
39 #include <boost/lexical_cast.hpp>
40 #include <boost/uuid/random_generator.hpp>
41 #include <boost/uuid/uuid_io.hpp>
42 
43 // Qt
44 #include <QFileDialog>
45 #include <QMessageBox>
46 
48  : QDialog(parent, f),
49  m_ui(new Ui::ADOCreatorDialogForm)
50 {
51 // add controls
52  m_ui->setupUi(this);
53 
54 // popule providers
55 #ifdef _M_IX86
56  m_ui->m_providerComboBox->addItem("Microsoft.Jet.OLEDB.4.0");
57 #else
58  m_ui->m_providerComboBox->addItem("Microsoft.ACE.OLEDB.12.0");
59 #endif
60 
61 // connect signal and slots
62  connect(m_ui->m_applyPushButton, SIGNAL(pressed()), this, SLOT(applyPushButtonPressed()));
63  //connect(m_ui->m_helpPushButton, SIGNAL(pressed()), this, SLOT(helpPushButtonPressed()));
64  connect(m_ui->m_searchDatabaseToolButton, SIGNAL(pressed()), this, SLOT(searchDatabaseToolButtonPressed()));
65 
66  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
67  m_ui->m_helpPushButton->setPageReference("plugins/ado/ado_creator.html");
68 }
69 
71 {
72 }
73 
75 {
76  return m_datasource;
77 }
78 
80 {
81  return m_driver;
82 }
83 
85 {
86  try
87  {
88 // check if driver is loaded
89  if(te::da::DataSourceFactory::find("ADO") == 0)
90  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for ADO data sources!"));
91 
92  // get data source connection info based on form data
93  std::map<std::string, std::string> dsInfo = getConnectionInfo(true);
94 
95  {
96  // create database
97  te::da::DataSource::create("ADO", dsInfo);
98 
99  // Connect
100  std::map<std::string, std::string> connInfo;
101  connInfo["DB_NAME"] = dsInfo["DB_NAME"];
102  connInfo["PROVIDER"] = dsInfo["PROVIDER"];
103  if(!dsInfo["PASSWORD"].empty())
104  connInfo["PASSWORD"] = dsInfo["PASSWORD"];
105 
106  std::auto_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("ADO");
107  ds->setConnectionInfo(connInfo);
108  ds->open();
109 
110  m_driver.reset(ds.release());
111  }
112 
113  if(m_driver.get() == 0)
114  throw te::qt::widgets::Exception(TE_TR("Could not open ADO data source due to an unknown error!"));
115 
116  QString title = m_ui->m_fileLineEdit->text().trimmed();
117 
118  // Connect
119  std::map<std::string, std::string> connInfo;
120  connInfo["DB_NAME"] = dsInfo["DB_NAME"];
121  connInfo["PROVIDER"] = dsInfo["PROVIDER"];
122  if(!dsInfo["PASSWORD"].empty() && m_ui->m_savePasswordCheckBox->isChecked())
123  connInfo["PASSWORD"] = dsInfo["PASSWORD"];
124 
125  if(m_datasource.get() == 0)
126  {
127 // create a new data source based on form data
128  m_datasource.reset(new te::da::DataSourceInfo);
129 
130  m_datasource->setConnInfo(connInfo);
131 
132  boost::uuids::basic_random_generator<boost::mt19937> gen;
133  boost::uuids::uuid u = gen();
134  std::string dsId = boost::uuids::to_string(u);
135 
136  m_datasource->setId(dsId);
137  m_driver->setId(dsId);
138  m_datasource->setTitle(title.toUtf8().data());
139  m_datasource->setDescription("");
140  m_datasource->setAccessDriver("ADO");
141  m_datasource->setType("ADO");
142  }
143  else
144  {
145  m_driver->setId(m_datasource->getId());
146  m_datasource->setConnInfo(connInfo);
147  m_datasource->setTitle(title.toUtf8().data());
148  m_datasource->setDescription("");
149  }
150  }
151  catch(const std::exception& e)
152  {
153  QMessageBox::warning(this,
154  tr("TerraLib Qt Components"),
155  tr(e.what()));
156  return;
157  }
158  catch(...)
159  {
160  QMessageBox::warning(this,
161  tr("TerraLib Qt Components"),
162  tr("Unknown error while opening ADO database!"));
163  return;
164  }
165 
166  accept();
167 }
168 
170 {
171  QMessageBox::warning(this,
172  tr("TerraLib Qt Components"),
173  tr("Not implemented yet!\nWe will provide it soon!"));
174 }
175 
177 {
178  QString fileName = QFileDialog::getSaveFileName(this, tr("Save ADO Database"), te::qt::widgets::GetFilePathFromSettings("vector"), tr("Database files (*.accdb *.mdb);; All Files (*.*)"), 0, QFileDialog::ReadOnly);
179 
180  if(fileName.isEmpty())
181  return;
182 
183  m_ui->m_fileLineEdit->setText(fileName);
184 
185  QFileInfo info(fileName);
186 
187  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "vector");
188 }
189 
190 std::map<std::string, std::string> te::qt::plugins::ado::ADOCreatorDialog::getConnectionInfo(bool getPrivateKeys) const
191 {
192  std::map<std::string, std::string> connInfo;
193 
194  QString qstr = m_ui->m_fileLineEdit->text().trimmed();
195 
196  if(!qstr.isEmpty())
197  connInfo["DB_NAME"] = qstr.toStdString();
198 
199  if(getPrivateKeys)
200  {
201  qstr = m_ui->m_passwordLineEdit->text().trimmed();
202 
203  if(!qstr.isEmpty())
204  connInfo["PASSWORD"] = qstr.toStdString();
205  }
206 
207  qstr = m_ui->m_providerComboBox->currentText().trimmed();
208 
209  if(!qstr.isEmpty())
210  connInfo["PROVIDER"] = qstr.toStdString();
211 
212  if(m_ui->m_createOGCTablesCheckBox->isChecked())
213  connInfo["CREATE_OGC_METADATA_TABLES"] = "TRUE";
214 
215  return connInfo;
216 }
static bool find(const std::string &dsType)
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
std::map< std::string, std::string > getConnectionInfo(bool getPrivateKeys) const
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
Definition: Utils.cpp:367
const te::da::DataSourcePtr & getDriver() const
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
static std::auto_ptr< DataSource > make(const std::string &dsType)
const te::da::DataSourceInfoPtr & getDataSource() const
ADOCreatorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
std::auto_ptr< Ui::ADOCreatorDialogForm > m_ui
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:510
A class that represents a data source component.
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
Definition: Utils.cpp:376
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr