All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataExchangerWizard.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/plugins/exchanger/DataExchangerWizard.cpp
22 
23  \brief A Qt dialog for ....
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/DataSet.h"
28 #include "../../../dataaccess/dataset/DataSetAdapter.h"
29 #include "../../../dataaccess/dataset/DataSetType.h"
30 #include "../../../dataaccess/dataset/DataSetTypeConverter.h"
31 #include "../../../dataaccess/datasource/DataSource.h"
32 #include "../../../dataaccess/datasource/DataSourceInfo.h"
33 #include "../../../dataaccess/datasource/DataSourceManager.h"
34 #include "../../../dataaccess/datasource/DataSourceTransactor.h"
35 #include "../../../dataaccess/utils/Utils.h"
36 #include "../../../geometry/GeometryProperty.h"
37 #include "../../../qt/widgets/dataset/selector/DataSetSelectorWizardPage.h"
38 #include "../../../qt/widgets/datasource/selector/DataSourceSelectorWidget.h"
39 #include "../../../qt/widgets/datasource/selector/DataSourceSelectorWizardPage.h"
40 #include "../../../qt/widgets/help/HelpPushButton.h"
41 #include "../../../qt/widgets/utils/ScopedCursor.h"
42 #include "DataExchangerWizard.h"
43 #include "DataExchangeStatus.h"
46 #include "ui_DataExchangerWizardForm.h"
47 #include "ui_DataExchangeSummaryWizardPageForm.h"
48 #include "ui_DataSetOptionsWizardPageForm.h"
49 #include "ui_DataSourceSelectorWidgetForm.h"
50 
51 
52 // STL
53 #include <cassert>
54 
55 // Boost
56 //#include <boost/chrono.hpp>
57 
58 // Qt
59 #include <QAbstractButton>
60 #include <QMessageBox>
61 #include <QVBoxLayout>
62 
64  : QWizard(parent, f),
65  m_ui(new Ui::DataExchangerWizardForm)
66 {
67 // setup controls
68  m_ui->setupUi(this);
69 
70 // add pages
72  m_datasourceSelectorPage->setTitle(tr("Data Source Selection"));
73  m_datasourceSelectorPage->setSubTitle(tr("Please, select the data source where the data is stored"));
74  m_datasourceSelectorPage->getSelectorWidget()->setSelectionMode(QAbstractItemView::SingleSelection);
75  //m_datasourceSelectorPage->getSelectorWidget()->setButtonsEnabled(false);
76  m_datasourceSelectorPage->getSelectorWidget()->getForm()->m_mainLayout->setStretch(0, 49);
77  m_datasourceSelectorPage->getSelectorWidget()->getForm()->m_mainLayout->setStretch(1, 100);
78 
79  m_datasourceSelectorPage->getSelectorWidget()->showDataSourceWithRasterSupport(false);
80 
82 
84  m_datasetSelectorPage->setTitle(tr("Dataset Selection"));
85  m_datasetSelectorPage->setSubTitle(tr("Please, select the datasets you want to transfer to another data source"));
87 
89  m_targetSelectorPage->setTitle(tr("Target Data Source"));
90  m_targetSelectorPage->setSubTitle(tr("Please, select the target data source"));
91  m_targetSelectorPage->getSelectorWidget()->setSelectionMode(QAbstractItemView::SingleSelection);
92  //m_targetSelectorPage->getSelectorWidget()->setButtonsEnabled(false);
93  m_targetSelectorPage->getSelectorWidget()->getForm()->m_mainLayout->setStretch(0, 49);
94  m_targetSelectorPage->getSelectorWidget()->getForm()->m_mainLayout->setStretch(1, 100);
96 
98  //m_datasetOptionsPage->showSimpleMode(true); // USED TO HIDE ADVANCED OPTIONS
99  //m_datasetOptionsPage->setFinalPage(true);
100  //m_datasetOptionsPage->setCommitPage(true);
101  //m_datasetOptionsPage->setTitle(tr("Transfer Options"));
102  //m_datasetOptionsPage->setSubTitle(tr("You can provide more information on how the datasets will be transferred and mapped to the target data source"));
104 
105 
107  m_summaryPage->setFinalPage(true);
108  setPage(PAGE_SUMMARY, m_summaryPage.get());
109 
110 // connect signals and slots
111  connect(this->button(QWizard::NextButton), SIGNAL(pressed()), this, SLOT(next()));
112  connect(this->button(QWizard::BackButton), SIGNAL(pressed()), this, SLOT(back()));
113  connect(this->button(QWizard::CommitButton), SIGNAL(pressed()), this, SLOT(commit()));
114  //connect(this->button(QWizard::FinishButton), SIGNAL(pressed()), this, SLOT(finish()));
115 
117 
118  this->setButton(QWizard::HelpButton, helpButton);
119 
120  helpButton->setPageReference("widgets/exchanger_all/exchanger_all.html");
121 }
122 
124 {
125 }
126 
128 {
129  return QWizard::nextId();
130 }
131 
133 {
134  std::list<te::da::DataSourceInfoPtr> datasources = m_datasourceSelectorPage->getSelectorWidget()->getSelecteds();
135 
136  if(datasources.empty())
137  return te::da::DataSourceInfoPtr();
138  else
139  return datasources.front();
140 }
141 
143 {
144  std::list<te::da::DataSourceInfoPtr> datasources = m_targetSelectorPage->getSelectorWidget()->getSelecteds();
145 
146  if(datasources.empty())
147  return te::da::DataSourceInfoPtr();
148  else
149  return datasources.front();
150 }
151 
153 {
154  this->setOption(QWizard::HaveCustomButton1, false);
155 
156  if(currentId() == PAGE_SUMMARY)
157  {
158  this->setButtonText(QWizard::CustomButton1, tr("Apply"));
159  this->setOption(QWizard::HaveCustomButton1, true);
160 
161  connect(this->button(QWizard::CustomButton1), SIGNAL(clicked()), m_datasetOptionsPage.get(), SLOT(applyChanges()));
162  }
163 
164  QWizard::back();
165 }
166 
168 {
169  this->setOption(QWizard::HaveCustomButton1, false);
170 
171  if(currentId() == PAGE_DATASOURCE_SELECTION)
172  {
173  m_datasetSelectorPage->set(getDataSource(), true);
174  }
175  else if(currentId() == PAGE_TARGET_DATASOURCE)
176  {
177  std::list<te::da::DataSetTypePtr> datasets = m_datasetSelectorPage->getCheckedDataSets();
178  m_datasetOptionsPage->set(datasets, getDataSource(), getTargetDataSource());
179 
180  this->setButtonText(QWizard::CustomButton1, tr("Apply"));
181  this->setOption(QWizard::HaveCustomButton1, true);
182 
183  connect(this->button(QWizard::CustomButton1), SIGNAL(clicked()), m_datasetOptionsPage.get(), SLOT(applyChanges()));
184  }
185  else if(currentId() == PAGE_DATASET_OPTIONS)
186  {
187  exchange();
188  }
189 
190  QWizard::next();
191 }
192 
194 {
195  ScopedCursor wcursor(Qt::WaitCursor);
196 
197 // get input data source
198  te::da::DataSourceInfoPtr ids = getDataSource();
199 
200  if(ids.get() == 0)
201  return;
202 
203  te::da::DataSourcePtr idatasource = te::da::DataSourceManager::getInstance().get(ids->getId(), ids->getAccessDriver(), ids->getConnInfo());
204 
205  if(idatasource.get() == 0)
206  return;
207 
208 // get output data source
209  te::da::DataSourceInfoPtr ods = getTargetDataSource();
210 
211  if(ods.get() == 0)
212  return;
213 
214  te::da::DataSourcePtr odatasource = te::da::DataSourceManager::getInstance().get(ods->getId(), ods->getAccessDriver(), ods->getConnInfo());
215 
216  if(odatasource.get() == 0)
217  return;
218 
219 // get selected datasets and modified datasets
220  std::list<DataExchangeStatus> result;
221 
222  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*> odatasets = m_datasetOptionsPage->getDatasets();
223 
224  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = odatasets.begin();
225 
226  while(it != odatasets.end())
227  {
228  te::da::DataSetTypePtr idset = it->first;
229  te::da::DataSetType* odset = it->second->getResult();
231 
232  std::auto_ptr<te::da::DataSourceTransactor> t;
233 
234  try
235  {
236  std::map<std::string,std::string> nopt;
237 
238  //boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
239 
240  std::auto_ptr<te::da::DataSet> dataset(idatasource->getDataSet(idset->getName()));
241 
242  t = odatasource->getTransactor();
243 
244  t->begin();
245 
246  // stay tunned: create can change idset!
247  t->createDataSet(odset, nopt);
248 
249  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataset.get(), it->second));
250  if(geomProp)
251  dsAdapter->setSRID(geomProp->getSRID());
252 
253  if(dataset->moveBeforeFirst())
254  t->add(odset->getName(), dsAdapter.get(), ods->getConnInfo());
255 
256  // boost::chrono::duration<double> sec = boost::chrono::system_clock::now() - start;
257 
258  DataExchangeStatus status;
259  status.m_dataset = odset;
260  status.m_successful = true;
261  //status.m_time = sec;
262 
263  result.push_back(status);
264 
265  t->commit();
266  }
267  catch(const std::exception& e)
268  {
269  DataExchangeStatus status;
270  status.m_dataset = odset;
271  status.m_successful = false;
272  status.m_exceptionMsg = e.what();
273 
274  result.push_back(status);
275 
276  t->rollBack();
277  }
278  catch(...)
279  {
280  DataExchangeStatus status;
281  status.m_dataset = odset;
282  status.m_successful = false;
283  status.m_exceptionMsg = tr("Unknown error!").toStdString();
284 
285  result.push_back(status);
286 
287  t->rollBack();
288  }
289 
290  ++it;
291  }
292 
293  m_summaryPage->set(result);
294 }
295 
296 
Geometric property.
DataExchangerWizard(QWidget *parent=0, Qt::WindowFlags f=0)
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
std::auto_ptr< DataSourceSelectorWizardPage > m_datasourceSelectorPage
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A class that models the description of a dataset.
Definition: DataSetType.h:72
std::auto_ptr< DataSetOptionsWizardPage > m_datasetOptionsPage
void setPageReference(const QString &ref)
Sets the documentation page reference.
te::da::DataSourceInfoPtr getTargetDataSource() const
std::auto_ptr< DataExchangeSummaryWizardPage > m_summaryPage
std::auto_ptr< Ui::DataExchangerWizardForm > m_ui
std::auto_ptr< DataSetSelectorWizardPage > m_datasetSelectorPage
int getSRID() const
It returns the spatial reference system identifier associated to this property.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
std::auto_ptr< DataSourceSelectorWizardPage > m_targetSelectorPage
te::da::DataSourceInfoPtr getDataSource() const
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:644
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
An object that when created shows a cursor during its scope.
Definition: ScopedCursor.h:48
const std::string & getName() const
It returns the property name.
Definition: Property.h:127