All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DSCopy.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/tools/dscopy/core/DSCopy.cpp
22 
23  \brief An utility class to provide methods to copy a Data Source
24  */
25 
26 // Terralib Data Source Copy Tool
27 #include "DSCopy.h"
28 #include "Utils.h"
29 
30 // TerraLib
33 #include <terralib/dataaccess/datasource/DataSourceCatalogLoader.h>
34 #include <terralib/dataaccess/dataset/DataSetPersistence.h>
35 
36 bool te::tools::dscopy::DSCopy::init(std::string & errorMessage)
37 {
38  return Utils::loadModules(errorMessage);
39 }
40 
41 bool te::tools::dscopy::DSCopy::copy(std::string dsTypeOrigin, std::string dsConnStrOrigin, std::string dsTypeDestination,
42  std::string dsConnStrDestination, std::vector<std::string*> datasets, std::string& errorMessage)
43 {
44 
45  te::da::DataSource* dsOrigin = te::da::DataSourceFactory::make(dsTypeOrigin);
46  dsOrigin->open(dsConnStrOrigin);
47 
48  if(dsOrigin == 0)
49  {
50  errorMessage = "Could not get a connection with the origin DataSource.";
51  return false;
52  }
53 
54  te::da::DataSource* dsDestination = te::da::DataSourceFactory::make(dsTypeDestination);
55  dsDestination->open(dsConnStrDestination);
56 
57  if(dsDestination == 0)
58  {
59  errorMessage = "Could not get a connection with the destination DataSource.";
60  return false;;
61  }
62 
63  if(!copyDataSets(dsOrigin, dsDestination, datasets, errorMessage))
64  {
65  return false;
66  }
67 
68  return true;
69 
70 }
71 
72 bool te::tools::dscopy::DSCopy::copy(te::da::DataSource* dsOrigin, te::da::DataSource* dsDestination, std::vector<std::string*> datasets, std::string& errorMessage)
73 {
74  return copyDataSets(dsOrigin, dsDestination, datasets, errorMessage);
75 }
76 
77 bool te::tools::dscopy::DSCopy::copyDataSets(te::da::DataSource* dsOrigin, te::da::DataSource* dsDestination, std::vector<std::string*> datasets, std::string& errorMessage)
78 {
79 
80  te::da::DataSourceTransactor* tOrigin = dsOrigin->getTransactor();
81  te::da::DataSourceCatalogLoader* cOrigin = tOrigin->getCatalogLoader();
82  cOrigin->loadCatalog();
83 
84  if(datasets.empty())
85  {
86  std::vector<std::string*> datasetsNames;
87  cOrigin->getDataSets(datasetsNames);
88  datasets = datasetsNames;
89  }
90 
91  te::da::DataSourceTransactor* tDestination = dsDestination->getTransactor();
92  te::da::DataSetPersistence* pDestination = tDestination->getDataSetPersistence();
93 
94  for(unsigned int i = 0; i < datasets.size(); i++)
95  {
96  te::da::DataSetType* datasettype = tOrigin->getCatalogLoader()->getDataSetType(*datasets[i]);
97 
98  te::da::DataSet* dataSet = tOrigin->getDataSet(*datasets[i]);
99 
100  pDestination->create(datasettype, dataSet);
101  }
102 
103  std::cout << "Copy executed successfully!" << std::endl << std::endl;
104  std::cout << "-------------------------" << std::endl;
105 
106  if(dsDestination->getType() == "MEM")
107  {
108  std::vector<std::string*> datasets;
109  dsDestination->getTransactor()->getCatalogLoader()->getDataSets(datasets);
110 
111  std::cout << std::endl << "In-memory data sets:" << std::endl << std::endl;
112  for(std::size_t i = 0; i < datasets.size(); i++)
113  std::cout << " " << *datasets[i] << std::endl;
114  }
115 
116  delete tOrigin;
117  delete tDestination;
118 
119  return true;
120 }
virtual void open()=0
It opens the data source and makes it ready for using.
A class that models the description of a dataset.
Definition: DataSetType.h:72
An utility class to provide methods to copy a Data Source.
bool copy(std::string dsTypeOrigin, std::string dsConnStrOrigin, std::string dsTypeDestination, std::string dsConnStrDestination, std::vector< std::string * > datasets, std::string &errorMessage)
Copy.
Definition: DSCopy.cpp:41
virtual std::auto_ptr< DataSourceTransactor > getTransactor()=0
It returns an object that can execute transactions in the context of a data source.
bool copyDataSets(te::da::DataSource *dsOrigin, te::da::DataSource *dsDestination, std::vector< std::string * > datasets, std::string &errorMessage)
Copy a list of Data Sets.
Definition: DSCopy.cpp:77
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:118
virtual std::auto_ptr< te::da::DataSetType > getDataSetType(const std::string &name)=0
It gets information about the given dataset.
bool init(std::string &errorMessage)
Init the application.
Definition: DSCopy.cpp:36
static std::auto_ptr< DataSource > make(const std::string &dsType)
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
virtual std::string getType() const =0
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
A factory for data sources.
virtual std::auto_ptr< DataSet > getDataSet(const std::string &name, te::common::TraverseType travType=te::common::FORWARDONLY, bool connected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)=0
It gets the dataset identified by the given name. A dataset can be connected or disconnected. A connected dataset, after its creation through the data source transactor, continues to depend on the connection given by its associated data source. Differently, a disconnected dataset, after its creation, no more depends of the connection given by the data source, and it continues to live after the connection has been released to the data source.
static bool loadModules(std::string &errorMessage)
Load Terralib modules.
Definition: Utils.cpp:7
Terralib Raster Manager Tool Utils.