All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DSCopyDialogController.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/qt/DSCopyDialogController.cpp
22 
23  \brief Data Source Copy GUI Controller
24  */
25 
26 // DSCopy
27 #include "DSCopyDialogController.h"
28 #include "../core/DSCopy.h"
29 #include "../core/Utils.h"
30 
31 // TerraLib
34 #include <terralib/dataaccess/datasource/DataSourceCatalogLoader.h>
35 
37 {
38  std::string errorMessage;
39  Utils::loadModules(errorMessage);
40 }
41 
43 {
44 
45 }
46 
47 bool te::tools::dscopy::DSCopyDialogController::setOriginConnectionInfo(std::string dstype, std::string conn, std::string& errorMessage)
48 {
49 
50  try
51  {
52  if(dstype == "OGR")
53  {
54  m_dsOrigin = te::da::DataSourceFactory::make("OGR");
55  m_dsOrigin->open("connection_string="+conn);
56  }
57  else if(dstype == "GDAL")
58  {
59  m_dsOrigin = te::da::DataSourceFactory::make("GDAL");
60  m_dsOrigin->open("URI="+conn);
61  }
62  else if(dstype == "POSTGIS")
63  {
64  m_dsOrigin = te::da::DataSourceFactory::make("POSTGIS");
65  m_dsOrigin->open(conn);
66  }
67  else
68  {
69  errorMessage = "There is not a implementation for this Data Source Type yet!";
70  return false;
71  }
72 
73  if(!m_dsOrigin)
74  {
75  errorMessage = "Data Source invalid!";
76  return false;
77  }
78  else
79  return true;
80  }
81  catch(std::exception& e)
82  {
83  errorMessage = "Error ocurred: " + std::string(e.what());
84  return false;
85  }
86  catch(...)
87  {
88  errorMessage = "Error ocurred: unexpected!";
89  return false;
90  }
91 
92 }
93 
94 bool te::tools::dscopy::DSCopyDialogController::setOriginConnectionInfo(std::string dstype, std::string host,
95  std::string port, std::string dsname, std::string user, std::string pw, std::string& errorMessage)
96 {
97 
98  if(dstype == "POSTGIS")
99  {
100  std::string connStr = "host=" + host;
101  connStr += "&port=" + port;
102  connStr += "&dbname=" + dsname;
103  connStr += "&user=" + user;
104  connStr += "&password=" + pw;
105 
106  try
107  {
108 
109  m_dsOrigin = te::da::DataSourceFactory::make("POSTGIS");
110  m_dsOrigin->open(connStr);
111 
112  if(!m_dsOrigin)
113  {
114  errorMessage = "Data Source invalid!";
115  return false;
116  }
117  else
118  return true;
119 
120  }
121  catch(std::exception& e)
122  {
123  errorMessage = "Error ocurred: " + std::string(e.what());
124  return false;
125  }
126  catch(...)
127  {
128  errorMessage = "Error ocurred: unexpected!";
129  return false;
130  }
131  }
132  else
133  {
134  errorMessage = "There is not a implementation for this Data Source Type yet!";
135  return false;
136  }
137 }
138 
139 bool te::tools::dscopy::DSCopyDialogController::setDestinationConnectionInfo(std::string dstype, std::string conn, std::string& errorMessage)
140 {
141 
142  try
143  {
144  if(dstype == "OGR")
145  {
146  m_dsDestination = te::da::DataSourceFactory::make("OGR");
147  m_dsDestination->open("connection_string="+conn);
148  }
149  else if(dstype == "GDAL")
150  {
151  m_dsDestination = te::da::DataSourceFactory::make("GDAL");
152  m_dsDestination->open("URI="+conn);
153  }
154  else if(dstype == "POSTGIS")
155  {
156  m_dsDestination = te::da::DataSourceFactory::make("POSTGIS");
157  m_dsDestination->open(conn);
158  }
159  else
160  {
161  errorMessage = "There is not a implementation for this Data Source Type yet!";
162  return false;
163  }
164 
165  if(!m_dsDestination)
166  {
167  errorMessage = "Data Source invalid!";
168  return false;
169  }
170  else
171  return true;
172  }
173  catch(std::exception& e)
174  {
175  errorMessage = "Error ocurred: " + std::string(e.what());
176  return false;
177  }
178  catch(...)
179  {
180  errorMessage = "Error ocurred: unexpected!";
181  return false;
182  }
183 
184 }
185 
187  std::string port, std::string dsname, std::string user, std::string pw, std::string& errorMessage)
188 {
189 
190  if(dstype == "POSTGIS")
191  {
192  std::string connStr = "host=" + host;
193  connStr += "&port=" + port;
194  connStr += "&dbname=" + dsname;
195  connStr += "&user=" + user;
196  connStr += "&password=" + pw;
197 
198  try
199  {
200 
201  m_dsDestination = te::da::DataSourceFactory::make("POSTGIS");
202  m_dsDestination->open(connStr);
203 
204  if(!m_dsDestination)
205  {
206  errorMessage = "Data Source invalid!";
207  return false;
208  }
209  else
210  return true;
211 
212  }
213  catch(std::exception& e)
214  {
215  errorMessage = "Error ocurred: " + std::string(e.what());
216  return false;
217  }
218  catch(...)
219  {
220  errorMessage = "Error ocurred: unexpected!";
221  return false;
222  }
223  }
224  else
225  {
226  errorMessage = "There is not a implementation for this Data Source Type yet!";
227  return false;
228  }
229 }
230 
232 {
233  std::vector<std::string> dictionary;
234 
235  te::da::DataSourceFactory::dictionary_type::const_iterator it = te::da::DataSourceFactory::getDictionary().begin();
236 
237  while(it != te::da::DataSourceFactory::getDictionary().end())
238  {
239  if(it->first != "MEM")
240  {
241  dictionary.push_back(it->first.c_str());
242  }
243 
244  ++it;
245  }
246 
247  return dictionary;
248 }
249 
251 {
252  std::vector<std::string*> dataSetsNames;
253 
254  te::da::DataSourceTransactor* transactor = m_dsOrigin->getTransactor();
255 
256  te::da::DataSourceCatalogLoader* catalogLoader = transactor->getCatalogLoader();
257 
258  catalogLoader->getDataSets(dataSetsNames);
259 
260  return dataSetsNames;
261 }
262 
264 {
265  m_datasets = datasets;
266 }
267 
268 bool te::tools::dscopy::DSCopyDialogController::copy(std::string& errorMessage)
269 {
270  std::vector<std::string*> datasetsP;
271  for(size_t i = 0; i < m_datasets.size(); i++)
272  datasetsP.push_back(&m_datasets[i]);
273 
274  try
275  {
276  DSCopy ds;
277  //ds.init();
278  return ds.copy(m_dsOrigin, m_dsDestination, datasetsP, errorMessage);
279 
280  }
281  catch(std::exception& e)
282  {
283  errorMessage = "Error ocurred: " + std::string(e.what());
284  return false;
285  }
286  catch(...)
287  {
288  errorMessage = "Error ocurred: unexpected!";
289  return false;
290  }
291 }
void setDatasetsToCopy(std::vector< std::string > datasets)
bool setOriginConnectionInfo(std::string dstype, std::string path, std::string &errorMessage)
bool setDestinationConnectionInfo(std::string dstype, std::string path, std::string &errorMessage)
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
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...
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
Class with methods to manage the application.
Definition: DSCopy.h:49
A factory for data sources.
Data Source Copy GUI Controller.
static bool loadModules(std::string &errorMessage)
Load Terralib modules.
Definition: Utils.cpp:7