All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ADOCreatorDialog.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/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 <QtGui/QFileDialog>
45 #include <QtGui/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  m_ui->m_providerComboBox->addItem("Microsoft.Jet.OLEDB.4.0");
56 
57 // connect signal and slots
58  connect(m_ui->m_applyPushButton, SIGNAL(pressed()), this, SLOT(applyPushButtonPressed()));
59  //connect(m_ui->m_helpPushButton, SIGNAL(pressed()), this, SLOT(helpPushButtonPressed()));
60  connect(m_ui->m_searchDatabaseToolButton, SIGNAL(pressed()), this, SLOT(searchDatabaseToolButtonPressed()));
61 
62  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
63  m_ui->m_helpPushButton->setPageReference("plugins/ado/ado_creator.html");
64 }
65 
67 {
68 }
69 
71 {
72  return m_datasource;
73 }
74 
76 {
77  return m_driver;
78 }
79 
81 {
82  try
83  {
84 // check if driver is loaded
85  if(te::da::DataSourceFactory::find("ADO") == 0)
86  throw te::qt::widgets::Exception(TR_QT_WIDGETS("Sorry! No data access driver loaded for ADO data sources!"));
87 
88 // get data source connection info based on form data
89  std::map<std::string, std::string> dsInfo;
90 
91  getConnectionInfo(dsInfo);
92 
93  // create database
94  te::da::DataSource::create("ADO", dsInfo);
95 
96  // Connect
97  std::map<std::string, std::string> connInfo;
98  connInfo["DB_NAME"] = dsInfo["DB_NAME"];
99  connInfo["PROVIDER"] = dsInfo["PROVIDER"];
100  if(!dsInfo["PASSWORD"].empty())
101  connInfo["PASSWORD"] = dsInfo["PASSWORD"];
102 
103  std::auto_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("ADO");
104  ds->setConnectionInfo(connInfo);
105  ds->open();
106 
107  m_driver.reset(ds.release());
108 
109  if(m_driver.get() == 0)
110  throw te::qt::widgets::Exception(TR_QT_WIDGETS("Could not open ADO data source due to an unknown error!"));
111 
112  QString title = m_ui->m_fileLineEdit->text().trimmed();
113 
114  if(m_datasource.get() == 0)
115  {
116 // create a new data source based on form data
117  m_datasource.reset(new te::da::DataSourceInfo);
118 
119  m_datasource->setConnInfo(connInfo);
120 
121  boost::uuids::basic_random_generator<boost::mt19937> gen;
122  boost::uuids::uuid u = gen();
123  std::string dsId = boost::uuids::to_string(u);
124 
125  m_datasource->setId(dsId);
126  m_driver->setId(dsId);
127  m_datasource->setTitle(title.toUtf8().data());
128  m_datasource->setDescription("");
129  m_datasource->setAccessDriver("ADO");
130  m_datasource->setType("ADO");
131  }
132  else
133  {
134  m_driver->setId(m_datasource->getId());
135  m_datasource->setConnInfo(connInfo);
136  m_datasource->setTitle(title.toUtf8().data());
137  m_datasource->setDescription("");
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 ADO database!"));
152  return;
153  }
154 
155  accept();
156 }
157 
159 {
160  QMessageBox::warning(this,
161  tr("TerraLib Qt Components"),
162  tr("Not implemented yet!\nWe will provide it soon!"));
163 }
164 
166 {
167  QString fileName = QFileDialog::getSaveFileName(this, tr("Save ADO Database"), te::qt::widgets::GetFilePathFromSettings("vector"), tr("Database files (*.accdb *.mdb);; All Files (*.*)"), 0, QFileDialog::ReadOnly);
168 
169  if(fileName.isEmpty())
170  return;
171 
172  m_ui->m_fileLineEdit->setText(fileName);
173 
174  QFileInfo info(fileName);
175 
176  te::qt::widgets::AddFilePathToSettings(info.absolutePath(), "vector");
177 }
178 
179 void te::qt::plugins::ado::ADOCreatorDialog::getConnectionInfo(std::map<std::string, std::string>& connInfo) const
180 {
181 // clear input
182  connInfo.clear();
183 
184  QString qstr = m_ui->m_fileLineEdit->text().trimmed();
185 
186  if(!qstr.isEmpty())
187  connInfo["DB_NAME"] = qstr.toStdString();
188 
189  qstr = m_ui->m_passwordLineEdit->text().trimmed();
190 
191  if(!qstr.isEmpty())
192  connInfo["PASSWORD"] = qstr.toStdString();
193 
194  qstr = m_ui->m_providerComboBox->currentText().trimmed();
195 
196  if(!qstr.isEmpty())
197  connInfo["PROVIDER"] = qstr.toStdString();
198 
199  if(m_ui->m_createOGCTablesCheckBox->isChecked())
200  connInfo["CREATE_OGC_METADATA_TABLES"] = "TRUE";
201 }
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
const te::da::DataSourceInfoPtr & getDataSource() const
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
Definition: Utils.cpp:360
A class that represents a data source component.
TEQTWIDGETSEXPORT void AddFilePathToSettings(const QString &path, const QString &typeFile)
Save last used path in QSettings.
Definition: Utils.cpp:351
static std::auto_ptr< DataSource > make(const std::string &dsType)
void getConnectionInfo(std::map< std::string, std::string > &connInfo) const
static bool find(const std::string &dsType)
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
ADOCreatorDialog(QWidget *parent=0, Qt::WindowFlags f=0)
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
std::auto_ptr< Ui::ADOCreatorDialogForm > m_ui
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr