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 "../../../../core/translator/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  m_ui->m_providerComboBox->addItem("Microsoft.ACE.OLEDB.15.0");
60 #endif
61 
62 // connect signal and slots
63  connect(m_ui->m_applyPushButton, SIGNAL(pressed()), this, SLOT(applyPushButtonPressed()));
64  //connect(m_ui->m_helpPushButton, SIGNAL(pressed()), this, SLOT(helpPushButtonPressed()));
65  connect(m_ui->m_searchDatabaseToolButton, SIGNAL(pressed()), this, SLOT(searchDatabaseToolButtonPressed()));
66 
67  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
68  m_ui->m_helpPushButton->setPageReference("plugins/ado/ado_creator.html");
69 }
70 
72 {
73 }
74 
76 {
77  return m_datasource;
78 }
79 
81 {
82  return m_driver;
83 }
84 
86 {
87  try
88  {
89  // check if driver is loaded
90  if(te::da::DataSourceFactory::find("ADO") == 0)
91  throw te::qt::widgets::Exception(TE_TR("Sorry! No data access driver loaded for ADO data sources!"));
92 
93  // get data source connection info based on form data
94  const std::string connInfo = getConnectionInfo(true);
95 
96  {
97  // create database
98  te::da::DataSource::create("ADO", connInfo);
99 
100  // Connect
101  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("ADO", connInfo);
102  ds->open();
103 
104  m_driver.reset(ds.release());
105  }
106 
107  if(m_driver.get() == 0)
108  throw te::qt::widgets::Exception(TE_TR("Could not open ADO data source due to an unknown error!"));
109 
110  QString title = m_ui->m_fileLineEdit->text().trimmed();
111 
112  // Connect
113  if(m_datasource.get() == 0)
114  {
115  // create a new data source based on form data
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("ADO");
129  m_datasource->setType("ADO");
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  catch(const std::exception& e)
140  {
141  QMessageBox::warning(this,
142  tr("TerraLib Qt Components"),
143  tr(e.what()));
144  return;
145  }
146  catch(...)
147  {
148  QMessageBox::warning(this,
149  tr("TerraLib Qt Components"),
150  tr("Unknown error while opening ADO database!"));
151  return;
152  }
153 
154  accept();
155 }
156 
158 {
159  QMessageBox::warning(this,
160  tr("TerraLib Qt Components"),
161  tr("Not implemented yet!\nWe will provide it soon!"));
162 }
163 
165 {
166  QString fileName = QFileDialog::getSaveFileName(this, tr("Save ADO Database"), te::qt::widgets::GetFilePathFromSettings("vector"), tr("Database files (*.accdb *.mdb);; All Files (*.*)"), 0, QFileDialog::ReadOnly);
167 
168  if(fileName.isEmpty())
169  return;
170 
171  m_ui->m_fileLineEdit->setText(fileName);
172 
173  QFileInfo info(fileName);
174 
175  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "vector");
176 }
177 
178 const std::string te::qt::plugins::ado::ADOCreatorDialog::getConnectionInfo(bool getPrivateKeys) const
179 {
180  std::string strURI = "file://"; // The base of the URI
181  QString qstr; // Auxiliary string used to hold temporary data
182 
183  qstr = m_ui->m_fileLineEdit->text().trimmed();
184 
185  if (getPrivateKeys)
186  {
187  // The password
188  qstr = m_ui->m_passwordLineEdit->text().trimmed();
189  strURI += qstr.isEmpty() ? "" : "user:" + std::string(qstr.toUtf8().data()) + "@";
190  }
191 
192  // Path to the database
193  qstr = m_ui->m_fileLineEdit->text().trimmed();
194  strURI += qstr.isEmpty() ? "" : qstr.toUtf8().data();
195 
196  qstr = m_ui->m_providerComboBox->currentText().trimmed();
197 
198  // The provider
199  qstr = m_ui->m_providerComboBox->currentText().trimmed();
200  if (!qstr.isEmpty())
201  strURI += "?PROVIDER=" + std::string(qstr.toUtf8().data());
202 
203  // Create Metadata Tables?
204  if (m_ui->m_createOGCTablesCheckBox->isChecked())
205  strURI += "&CREATE_OGC_METADATA_TABLES=TRUE";
206 
207  return strURI;
208 }
static std::unique_ptr< DataSource > create(const std::string &dsType, const std::string &connInfo)
It creates a new repository for a data source.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
boost::shared_ptr< DataSource > DataSourcePtr
te::da::DataSourceInfoPtr m_datasource
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
const te::da::DataSourcePtr & getDriver() const
std::unique_ptr< Ui::ADOCreatorDialogForm > m_ui
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
const te::da::DataSourceInfoPtr & getDataSource() const
ADOCreatorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
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.
const std::string getConnectionInfo(bool getPrivateKeys) const
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr