DataSourceInfoManager.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/dataaccess/datasource/DataSourceInfoManager.cpp
22 
23  \brief A singleton to keep all the registered data sources.
24 */
25 
26 // TerraLib
27 #include "../../common/STLUtils.h"
28 #include "../../core/uri/URI.h"
29 #include "../../core/translator/Translator.h"
30 #include "../Exception.h"
31 #include "DataSourceInfo.h"
32 #include "DataSourceInfoManager.h"
33 
34 // STL
35 #include <cassert>
36 
37 // Boost
38 #include <boost/format.hpp>
39 
41 {
42  std::map<std::string, DataSourceInfoPtr>::const_iterator it = m_datasources.find(id);
43 
44  return (it != m_datasources.end()) ? it->second : DataSourceInfoPtr();
45 }
46 
48 {
49  if (ds.get() == nullptr)
50  throw Exception(TE_TR("Can not insert a NULL data source into the manager!"));
51 
52  if (m_datasources.find(ds->getId()) == m_datasources.end())
53  {
54  std::string conInfo = ds->getConnInfoAsString();
55 
56  std::map<std::string, DataSourceInfoPtr>::const_iterator dsInfoIt = m_datasources.begin();
57  while (dsInfoIt != m_datasources.end())
58  {
59  std::string dsConInfo = dsInfoIt->second->getConnInfoAsString();
60 
61  if (conInfo == dsConInfo)
62  {
63  ds->setId(dsInfoIt->second->getId());
64  return false;
65  }
66  ++dsInfoIt;
67  }
68  m_datasources[ds->getId()] = ds;
69  }
70  return true;
71 }
72 
73 void te::da::DataSourceInfoManager::remove(const std::string& id)
74 {
75  std::map<std::string, DataSourceInfoPtr>::iterator it = m_datasources.find(id);
76 
77  if(it == m_datasources.end())
78  throw Exception((boost::format(TE_TR("There isn't a data source with the given id (%1%) in data source manager!")) % id).str());
79 
80  m_datasources.erase(it);
81 }
82 
83 void te::da::DataSourceInfoManager::removeByType(const std::string& dsTypeName)
84 {
85  std::map<std::string, DataSourceInfoPtr>::iterator it = m_datasources.begin();
86 
87  while(it!=m_datasources.end())
88  if(it->second->getType() == dsTypeName)
89  m_datasources.erase(it++);
90  else
91  ++it;
92 }
93 
94 
95 void te::da::DataSourceInfoManager::getByType(const std::string& dsTypeName, std::vector<DataSourceInfoPtr>& datasources) const
96 {
97  std::map<std::string, DataSourceInfoPtr>::const_iterator it = m_datasources.begin();
98  std::map<std::string, DataSourceInfoPtr>::const_iterator itend = m_datasources.end();
99 
100  while(it != itend)
101  {
102  if(it->second->getType() == dsTypeName)
103  datasources.push_back(it->second);
104 
105  ++it;
106  }
107 }
108 
110 {
111  std::map<std::string, DataSourceInfoPtr>::const_iterator dsInfoIt = m_datasources.begin();
112  while (dsInfoIt != m_datasources.end())
113  {
114  std::string dsConInfo = dsInfoIt->second->getConnInfoAsString();
115 
116  if (connInfo == dsConInfo)
117  return dsInfoIt->second;
118 
119  ++dsInfoIt;
120  }
121  return DataSourceInfoPtr();
122 }
123 
125 {
126  return m_datasources.size();
127 }
128 
130 {
131  return m_datasources.begin();
132 }
133 
135 {
136  return m_datasources.begin();
137 }
138 
140 {
141  return m_datasources.end();
142 }
143 
145 {
146  return m_datasources.end();
147 }
148 
150 
std::map< std::string, DataSourceInfoPtr >::iterator iterator
bool add(const DataSourceInfoPtr &ds)
Base exception class for plugin module.
std::map< std::string, DataSourceInfoPtr >::const_iterator const_iterator
static te::dt::Date ds(2010, 01, 01)
void getByType(const std::string &dsTypeName, std::vector< DataSourceInfoPtr > &datasources) const
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
DataSourceInfoPtr getByConnInfo(const std::string &connInfo)
DataSourceInfoPtr get(const std::string &id) const
A singleton to keep all the registered data sources.
A conteiner class for keeping information about a data source.
void remove(const std::string &id)
void removeByType(const std::string &dsTypeName)
std::map< std::string, DataSourceInfoPtr > m_datasources
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr