All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataSourceCatalogManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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/DataSourceCatalogManager.cpp
22 
23  \brief This is a singleton for managing all data source catalog instances available in the system.
24 */
25 
26 // TerraLib
27 #include "../../common/Globals.h"
28 #include "../../common/STLUtils.h"
29 #include "../../common/StringUtils.h"
30 #include "../../common/Translator.h"
31 #include "../Exception.h"
32 #include "DataSource.h"
34 
35 // STL
36 #include <algorithm>
37 #include <cassert>
38 #include <memory>
39 
41 {
42  if(find(ds))
43  throw Exception(TR_DATAACCESS("There is already a catalog for the data source given!"));
44 
46 
47  catalog->setDataSource(ds.get());
48 
49  // Get the informations associated to the datasets of the data source and insert them into the catalog
50  if(!ds->isOpened())
51  ds->open();
52 
53  std::vector<std::string> datasetNames = ds->getDataSetNames();
54 
55  for(std::size_t i = 0; i < datasetNames.size(); ++i)
56  {
57  std::auto_ptr<te::da::DataSetType> dt = ds->getDataSetType(datasetNames[i]);
58  catalog->add(DataSetTypePtr(dt));
59  }
60 
61  // Get the sequences of the data source
62  std::vector<std::string> seqNames = ds->getSequenceNames();
63 
64  for(std::size_t i = 0; i < seqNames.size(); ++i)
65  {
66  std::auto_ptr<te::da::Sequence> seq = ds->getSequence(seqNames[i]);
67  catalog->add(seq.release());
68  }
69 
70  // Insert the catalog into the manager
71  insert(catalog);
72 
73  return catalog;
74 }
75 
76 
78 {
79  LockRead l(this);
80 
81  const_iterator it = m_catalogs.find(ds);
82 
83  if(it != m_catalogs.end())
84  return it->second;
85 
86  return create(ds);
87 }
88 
90 {
91  LockRead l(this);
92 
93  const_iterator it = m_catalogs.find(ds);
94 
95  if(it != m_catalogs.end())
96  return true;
97 
98  return false;
99 }
100 
102 {
103  if(catalog.get() == 0)
104  throw Exception(TR_DATAACCESS("Please, specifify a non-null data source to be managed!"));
105 
106  LockWrite l(this);
107 
108  DataSourcePtr ds(catalog->getDataSource());
109 
110  if(find(ds))
111  throw Exception(TR_DATAACCESS("There is already a catalog for the data source of the given catalog!"));
112 
113  m_catalogs[ds] = catalog;
114 }
115 
117 {
118  te::da::DataSourcePtr ds(catalog->getDataSource());
119 
120  if(ds.get() == 0)
121  return;
122 
123  detach(ds);
124 }
125 
127 {
128  LockWrite l(this);
129 
130  if(!find(ds))
131  throw Exception(TR_DATAACCESS("This catalog is not valid to be detached from the manager!"));
132 
133  iterator it = m_catalogs.find(ds);
134 
135  m_catalogs.erase(it->first);
136 
137  return it->second;
138 }
139 
140 void te::da::DataSourceCatalogManager::detachAll(const std::string& dsType)
141 {
142  LockWrite l(this);
143 
144  iterator it;
145  for(it = m_catalogs.begin(); it != m_catalogs.end(); ++it)
146  {
147  if(it->first->getType() == dsType)
148  m_catalogs.erase(it++);
149  else
150  ++it;
151  }
152 }
153 
155 {
156  LockWrite l(this);
157 
158  m_catalogs.clear();
159 }
160 
162 {
163 }
164 
166 {
167 }
DataSourceCatalogPtr get(DataSourcePtr ds)
It gets the catalog associated to the given data source. If there is no catalog registered in the man...
It represents the system catalog of a DataSource.
boost::shared_ptr< DataSourceCatalog > DataSourceCatalogPtr
void insert(const DataSourceCatalogPtr &catalog)
It inserts the data source catalog into the manager. The catalog must have a data source in order to ...
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
This is a singleton for managing all the data source catalog instances available in the system...
void detachAll()
All the catalogs are detached from the manager.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
std::map< DataSourcePtr, DataSourceCatalogPtr >::iterator iterator
#define TR_DATAACCESS(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
Definition: Config.h:95
bool find(const DataSourcePtr &ds) const
It tries to find the catalog associated to the given data source.
std::map< DataSourcePtr, DataSourceCatalogPtr >::const_iterator const_iterator
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
~DataSourceCatalogManager()
Singleton destructor.
void detach(DataSourceCatalogPtr catalog)
It changes the ownership of the data source catalog to the caller.
DataSourceCatalogPtr create(DataSourcePtr ds)
It creates a catalog for the given data source, loads their sequences and the information about the s...
DataSourceCatalogManager()
It initializes the singleton instance of the data source catalog manager.