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 #include <ctime>
55 // Qt
56 #include <QAbstractButton>
57 #include <QMessageBox>
58 #include <QVBoxLayout>
59 
61  : QWizard(parent, f),
62  m_ui(new Ui::DataExchangerWizardForm)
63 {
64 // setup controls
65  m_ui->setupUi(this);
66 
67 // add pages
69  m_datasourceSelectorPage->setTitle(tr("Data Source Selection"));
70  m_datasourceSelectorPage->setSubTitle(tr("Please, select the data source where the data is stored"));
71  m_datasourceSelectorPage->getSelectorWidget()->setSelectionMode(QAbstractItemView::SingleSelection);
72  //m_datasourceSelectorPage->getSelectorWidget()->setButtonsEnabled(false);
73  m_datasourceSelectorPage->getSelectorWidget()->getForm()->m_mainLayout->setStretch(0, 49);
74  m_datasourceSelectorPage->getSelectorWidget()->getForm()->m_mainLayout->setStretch(1, 100);
75 
76  m_datasourceSelectorPage->getSelectorWidget()->showDataSourceWithRasterSupport(false);
77 
79 
81  m_datasetSelectorPage->setTitle(tr("Dataset Selection"));
82  m_datasetSelectorPage->setSubTitle(tr("Please, select the datasets you want to transfer to another data source"));
84 
86  m_targetSelectorPage->setTitle(tr("Target Data Source"));
87  m_targetSelectorPage->setSubTitle(tr("Please, select the target data source"));
88  m_targetSelectorPage->getSelectorWidget()->setSelectionMode(QAbstractItemView::SingleSelection);
89  //m_targetSelectorPage->getSelectorWidget()->setButtonsEnabled(false);
90  m_targetSelectorPage->getSelectorWidget()->getForm()->m_mainLayout->setStretch(0, 49);
91  m_targetSelectorPage->getSelectorWidget()->getForm()->m_mainLayout->setStretch(1, 100);
93 
95  //m_datasetOptionsPage->showSimpleMode(true); // USED TO HIDE ADVANCED OPTIONS
96  //m_datasetOptionsPage->setFinalPage(true);
97  //m_datasetOptionsPage->setCommitPage(true);
98  //m_datasetOptionsPage->setTitle(tr("Transfer Options"));
99  //m_datasetOptionsPage->setSubTitle(tr("You can provide more information on how the datasets will be transferred and mapped to the target data source"));
101 
102 
104  m_summaryPage->setFinalPage(true);
105  setPage(PAGE_SUMMARY, m_summaryPage.get());
106 
107 // connect signals and slots
108  connect(this->button(QWizard::NextButton), SIGNAL(pressed()), this, SLOT(next()));
109  connect(this->button(QWizard::BackButton), SIGNAL(pressed()), this, SLOT(back()));
110  connect(this->button(QWizard::CommitButton), SIGNAL(pressed()), this, SLOT(commit()));
111  //connect(this->button(QWizard::FinishButton), SIGNAL(pressed()), this, SLOT(finish()));
112 
114 
115  this->setButton(QWizard::HelpButton, helpButton);
116 
117  helpButton->setPageReference("widgets/exchanger_all/exchanger_all.html");
118 }
119 
121 
123 {
124  return QWizard::nextId();
125 }
126 
128 {
129  std::list<te::da::DataSourceInfoPtr> datasources = m_datasourceSelectorPage->getSelectorWidget()->getSelecteds();
130 
131  if(datasources.empty())
132  return te::da::DataSourceInfoPtr();
133  else
134  {
135  te::da::DataSourceInfoPtr dsInfo = datasources.front();
136 
137  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(dsInfo->getId(), dsInfo->getAccessDriver(), dsInfo->getConnInfo());
138 
139  if (ds.get() == nullptr)
140  return te::da::DataSourceInfoPtr();
141 
142  if (!ds->isOpened())
143  ds->open();
144 
145  return dsInfo;
146  }
147 }
148 
150 {
151  std::list<te::da::DataSourceInfoPtr> datasources = m_targetSelectorPage->getSelectorWidget()->getSelecteds();
152 
153  if(datasources.empty())
154  return te::da::DataSourceInfoPtr();
155  else
156  {
157  te::da::DataSourceInfoPtr dsInfo = datasources.front();
158 
159  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(dsInfo->getId(), dsInfo->getAccessDriver(), dsInfo->getConnInfo());
160 
161  if (ds.get() == nullptr)
162  return te::da::DataSourceInfoPtr();
163 
164  if (!ds->isOpened())
165  ds->open();
166 
167  return dsInfo;
168  }
169 }
170 
172 {
173  this->setOption(QWizard::HaveCustomButton1, false);
174 
175  if(currentId() == PAGE_SUMMARY)
176  {
177  //this->setButtonText(QWizard::CustomButton1, tr("Apply"));
178  //this->setOption(QWizard::HaveCustomButton1, true);
179 
180  //connect(this->button(QWizard::CustomButton1), SIGNAL(clicked()), m_datasetOptionsPage.get(), SLOT(applyChanges()));
181  }
182 
183  QWizard::back();
184 }
185 
187 {
188  this->setOption(QWizard::HaveCustomButton1, false);
189 
190  if(currentId() == PAGE_DATASOURCE_SELECTION)
191  {
192  m_datasetSelectorPage->set(getDataSource(), true);
193  }
194  else if(currentId() == PAGE_TARGET_DATASOURCE)
195  {
196  std::list<te::da::DataSetTypePtr> datasets = m_datasetSelectorPage->getCheckedDataSets();
198 
199  //this->setButtonText(QWizard::CustomButton1, tr("Apply"));
200  //this->setOption(QWizard::HaveCustomButton1, true);
201 
202  //connect(this->button(QWizard::CustomButton1), SIGNAL(clicked()), m_datasetOptionsPage.get(), SLOT(applyChanges()));
203  }
204  else if(currentId() == PAGE_DATASET_OPTIONS)
205  {
206  exchange();
207  }
208 
209  QWizard::next();
210 }
211 
213 {
214  ScopedCursor wcursor(Qt::WaitCursor);
215 
216 // get input data source
218 
219  if(ids.get() == nullptr)
220  return;
221 
222  te::da::DataSourcePtr idatasource = te::da::DataSourceManager::getInstance().get(ids->getId(), ids->getAccessDriver(), ids->getConnInfo());
223 
224  if(idatasource.get() == nullptr)
225  return;
226 
227  if (!idatasource->isOpened())
228  idatasource->open();
229 
230 // get output data source
232 
233  if(ods.get() == nullptr)
234  return;
235 
236  te::da::DataSourcePtr odatasource = te::da::DataSourceManager::getInstance().get(ods->getId(), ods->getAccessDriver(), ods->getConnInfo());
237 
238  if(odatasource.get() == nullptr)
239  return;
240 
241  if (!odatasource->isOpened())
242  odatasource->open();
243 
244 // get selected datasets and modified datasets
245  std::list<DataExchangeStatus> result;
246 
247  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*> odatasets = m_datasetOptionsPage->getDatasets();
248 
249  std::map<te::da::DataSetTypePtr, te::da::DataSetTypeConverter*>::iterator it = odatasets.begin();
250 
251  while(it != odatasets.end())
252  {
253  te::da::DataSetTypePtr idset = it->first;
254  te::da::DataSetType* odset = it->second->getResult();
255 
256  std::unique_ptr<te::da::DataSourceTransactor> t;
257 
258  try
259  {
260  std::map<std::string, std::string> nopt;
261 
262  time_t startTime;
263  time(&startTime);
264 
265  std::unique_ptr<te::da::DataSet> dataset(idatasource->getDataSet(idset->getName()));
266 
267  t = odatasource->getTransactor();
268 
269  t->begin();
270 
271  // stay tunned: create can change idset!
272  t->createDataSet(odset, nopt);
273 
274  std::unique_ptr<te::da::DataSetAdapter> dsAdapter(te::da::CreateAdapter(dataset.get(), it->second));
275 
276  if(dataset->moveBeforeFirst())
277  t->add(odset->getName(), dsAdapter.get(), nopt);
278 
279  time_t curtime;
280  time(&curtime);
281 
282  double diffInSec = difftime(curtime, startTime);
283  boost::chrono::duration<double> sec(diffInSec);
284 
285  DataExchangeStatus status;
286  status.m_dataset = odset;
287  status.m_successful = true;
288  status.m_time = sec;
289 
290  result.push_back(status);
291 
292  t->commit();
293  }
294  catch(const std::exception& e)
295  {
296  DataExchangeStatus status;
297  status.m_dataset = odset;
298  status.m_successful = false;
299  status.m_exceptionMsg = e.what();
300 
301  result.push_back(status);
302 
303  t->rollBack();
304  }
305  catch(...)
306  {
307  DataExchangeStatus status;
308  status.m_dataset = odset;
309  status.m_successful = false;
310  status.m_exceptionMsg = tr("Unknown error!").toUtf8().data();
311 
312  result.push_back(status);
313 
314  t->rollBack();
315  }
316 
317  ++it;
318  }
319 
320  m_summaryPage->set(result);
321 }
322 
323 
DataExchangerWizard(QWidget *parent=0, Qt::WindowFlags f=0)
std::unique_ptr< DataSetOptionsWizardPage > m_datasetOptionsPage
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
A class that models the description of a dataset.
Definition: DataSetType.h:72
void setPageReference(const QString &ref)
Sets the documentation page reference.
std::unique_ptr< DataSetSelectorWizardPage > m_datasetSelectorPage
te::da::DataSourceInfoPtr getTargetDataSource() const
boost::chrono::duration< double > m_time
static te::dt::Date ds(2010, 01, 01)
std::unique_ptr< Ui::DataExchangerWizardForm > m_ui
std::unique_ptr< DataSourceSelectorWizardPage > m_datasourceSelectorPage
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
std::unique_ptr< DataExchangeSummaryWizardPage > m_summaryPage
std::unique_ptr< DataSourceSelectorWizardPage > m_targetSelectorPage
te::da::DataSourceInfoPtr getDataSource() const
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
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