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) 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/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  setPage(PAGE_SUMMARY, m_summaryPage.get());
108 
109 // connect signals and slots
110  connect(this->button(QWizard::NextButton), SIGNAL(pressed()), this, SLOT(next()));
111  connect(this->button(QWizard::BackButton), SIGNAL(pressed()), this, SLOT(back()));
112  connect(this->button(QWizard::CommitButton), SIGNAL(pressed()), this, SLOT(commit()));
113  //connect(this->button(QWizard::FinishButton), SIGNAL(pressed()), this, SLOT(finish()));
114 
116 
117  this->setButton(QWizard::HelpButton, helpButton);
118 
119  helpButton->setPageReference("widgets/exchanger_all/exchanger_all.html");
120 }
121 
123 {
124 }
125 
127 {
128  return QWizard::nextId();
129 }
130 
132 {
133  std::list<te::da::DataSourceInfoPtr> datasources = m_datasourceSelectorPage->getSelectorWidget()->getSelecteds();
134 
135  if(datasources.empty())
136  return te::da::DataSourceInfoPtr();
137  else
138  return datasources.front();
139 }
140 
142 {
143  std::list<te::da::DataSourceInfoPtr> datasources = m_targetSelectorPage->getSelectorWidget()->getSelecteds();
144 
145  if(datasources.empty())
146  return te::da::DataSourceInfoPtr();
147  else
148  return datasources.front();
149 }
150 
152 {
153  this->setOption(QWizard::HaveCustomButton1, false);
154 
155  if(currentId() == PAGE_SUMMARY)
156  {
157  this->setButtonText(QWizard::CustomButton1, tr("Apply"));
158  this->setOption(QWizard::HaveCustomButton1, true);
159 
160  connect(this->button(QWizard::CustomButton1), SIGNAL(clicked()), m_datasetOptionsPage.get(), SLOT(applyChanges()));
161  }
162 
163  QWizard::back();
164 }
165 
167 {
168  this->setOption(QWizard::HaveCustomButton1, false);
169 
170  if(currentId() == PAGE_DATASOURCE_SELECTION)
171  {
172  m_datasetSelectorPage->set(getDataSource(), true);
173  }
174  else if(currentId() == PAGE_TARGET_DATASOURCE)
175  {
176  std::list<te::da::DataSetTypePtr> datasets = m_datasetSelectorPage->getCheckedDataSets();
177  m_datasetOptionsPage->set(datasets, getDataSource(), getTargetDataSource());
178 
179  this->setButtonText(QWizard::CustomButton1, tr("Apply"));
180  this->setOption(QWizard::HaveCustomButton1, true);
181 
182  connect(this->button(QWizard::CustomButton1), SIGNAL(clicked()), m_datasetOptionsPage.get(), SLOT(applyChanges()));
183  }
184 
185  QWizard::next();
186 }
187 
189 {
190  ScopedCursor wcursor(Qt::WaitCursor);
191 
192 // get input data source
193  te::da::DataSourceInfoPtr ids = getDataSource();
194 
195  if(ids.get() == 0)
196  return;
197 
198  te::da::DataSourcePtr idatasource = te::da::DataSourceManager::getInstance().get(ids->getId(), ids->getAccessDriver(), ids->getConnInfo());
199 
200  if(idatasource.get() == 0)
201  return;
202 
203 // get output data source
204  te::da::DataSourceInfoPtr ods = getTargetDataSource();
205 
206  if(ods.get() == 0)
207  return;
208 
209  te::da::DataSourcePtr odatasource = te::da::DataSourceManager::getInstance().get(ods->getId(), ods->getAccessDriver(), ods->getConnInfo());
210 
211  if(odatasource.get() == 0)
212  return;
213 
214 // get selected datasets and modified datasets
215  std::list<DataExchangeStatus> result;
216 
217  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*> odatasets = m_datasetOptionsPage->getDatasets();
218 
219  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = odatasets.begin();
220 
221  while(it != odatasets.end())
222  {
223  te::da::DataSetTypePtr idset = it->first;
224  te::da::DataSetType* odset = it->second->getResult();
226 
227  std::auto_ptr<te::da::DataSourceTransactor> t(0);
228 
229  try
230  {
231  std::map<std::string,std::string> nopt;
232 
233  //boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
234 
235  std::auto_ptr<te::da::DataSet> dataset(idatasource->getDataSet(idset->getName()));
236 
237  t = odatasource->getTransactor();
238 
239  t->begin();
240 
241  // stay tunned: create can change idset!
242  t->createDataSet(odset, nopt);
243 
244  std::auto_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataset.get(), it->second));
245  if(geomProp)
246  dsAdapter->setSRID(geomProp->getSRID());
247 
248  if(dataset->moveBeforeFirst())
249  t->add(odset->getName(), dsAdapter.get(), ods->getConnInfo());
250 
251  // boost::chrono::duration<double> sec = boost::chrono::system_clock::now() - start;
252 
253  DataExchangeStatus status;
254  status.m_dataset = odset;
255  status.m_successful = true;
256  //status.m_time = sec;
257 
258  result.push_back(status);
259 
260  t->commit();
261  }
262  catch(const std::exception& e)
263  {
264  DataExchangeStatus status;
265  status.m_dataset = odset;
266  status.m_successful = false;
267  status.m_exceptionMsg = e.what();
268 
269  result.push_back(status);
270 
271  t->rollBack();
272  }
273  catch(...)
274  {
275  DataExchangeStatus status;
276  status.m_dataset = odset;
277  status.m_successful = false;
278  status.m_exceptionMsg = tr("Unknown error!").toStdString();
279 
280  result.push_back(status);
281 
282  t->rollBack();
283  }
284 
285  ++it;
286  }
287 
288  m_summaryPage->set(result);
289 
290  next();
291 }
292 
293 
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:508
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:595
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:126