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 "../../common/Translator.h"
29 #include "../Exception.h"
30 #include "DataSourceInfo.h"
31 #include "DataSourceInfoManager.h"
32 
33 // STL
34 #include <cassert>
35 
36 // Boost
37 #include <boost/foreach.hpp>
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() == 0)
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  return false;
63 
64  ++dsInfoIt;
65  }
66  m_datasources[ds->getId()] = ds;
67  }
68  return true;
69 }
70 
71 void te::da::DataSourceInfoManager::remove(const std::string& id)
72 {
73  std::map<std::string, DataSourceInfoPtr>::iterator it = m_datasources.find(id);
74 
75  if(it == m_datasources.end())
76  throw Exception((boost::format(TE_TR("There isn't a data source with the given id (%1%) in data source manager!")) % id).str());
77 
78  m_datasources.erase(it);
79 }
80 
81 void te::da::DataSourceInfoManager::removeByType(const std::string& dsTypeName)
82 {
83  std::map<std::string, DataSourceInfoPtr>::iterator it = m_datasources.begin();
84 
85  while(it!=m_datasources.end())
86  if(it->second->getType() == dsTypeName)
87  m_datasources.erase(it++);
88  else
89  ++it;
90 }
91 
92 
93 void te::da::DataSourceInfoManager::getByType(const std::string& dsTypeName, std::vector<DataSourceInfoPtr>& datasources) const
94 {
95  std::map<std::string, DataSourceInfoPtr>::const_iterator it = m_datasources.begin();
96  std::map<std::string, DataSourceInfoPtr>::const_iterator itend = m_datasources.end();
97 
98  while(it != itend)
99  {
100  if(it->second->getType() == dsTypeName)
101  datasources.push_back(it->second);
102 
103  ++it;
104  }
105 }
106 
108 {
109  std::map<std::string, DataSourceInfoPtr>::const_iterator dsInfoIt = m_datasources.begin();
110  while (dsInfoIt != m_datasources.end())
111  {
112  std::string dsConInfo = dsInfoIt->second->getConnInfoAsString();
113 
114  if (connInfo == dsConInfo)
115  return dsInfoIt->second;
116 
117  ++dsInfoIt;
118  }
119  return DataSourceInfoPtr();
120 }
121 
123 {
124  return m_datasources.size();
125 }
126 
128 {
129  return m_datasources.begin();
130 }
131 
133 {
134  return m_datasources.begin();
135 }
136 
138 {
139  return m_datasources.end();
140 }
141 
143 {
144  return m_datasources.end();
145 }
146 
148 {
149 }
150 
152 {
153 }
154 
std::map< std::string, DataSourceInfoPtr >::iterator iterator
bool add(const DataSourceInfoPtr &ds)
std::map< std::string, DataSourceInfoPtr >::const_iterator const_iterator
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:346
DataSourceInfoPtr get(const std::string &id) const
A singleton to keep all the registered data sources.
DataSourceInfoPtr getByConnInfo(std::string connInfo)
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