BESCatalogList.cc

Go to the documentation of this file.
00001 // BESCatalogList.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004,2005 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00032 
00033 #include "BESCatalogList.h"
00034 #include "BESCatalog.h"
00035 #include "BESInfo.h"
00036 #include "BESSyntaxUserError.h"
00037 
00038 BESCatalogList *BESCatalogList::_instance = 0 ;
00039 
00044 BESCatalogList::~BESCatalogList()
00045 {
00046     catalog_iter i = _catalogs.begin() ;
00047     catalog_iter e = _catalogs.end() ;
00048     for( ; i != e; i++ )
00049     {
00050         BESCatalog *catalog = (*i).second ;
00051         if( catalog ) delete catalog ;
00052     }
00053 }
00054 
00062 bool
00063 BESCatalogList::add_catalog(BESCatalog * catalog)
00064 {
00065     bool stat = false;
00066     if (find_catalog(catalog->get_catalog_name()) == 0) {
00067 #if 0
00068         _catalogs[catalog->get_catalog_name()] = catalog;
00069 #endif
00070         string name = catalog->get_catalog_name() ;
00071         std::pair<const std::string, BESCatalog*> p =
00072             std::make_pair( name, catalog ) ;
00073         stat = _catalogs.insert(p).second;
00074 #if 0
00075         stat = true;
00076 #endif
00077     }
00078     return stat;
00079 }
00080 
00090 bool
00091 BESCatalogList::del_catalog( const string &catalog_name )
00092 {
00093     bool ret = false ;
00094     BESCatalog *cat = 0 ;
00095     BESCatalogList::catalog_iter i ;
00096     i = _catalogs.find( catalog_name ) ;
00097     if( i != _catalogs.end() )
00098     {
00099         cat = (*i).second;
00100         _catalogs.erase( i ) ;
00101         delete cat ;
00102         ret = true ;
00103     }
00104     return ret ;
00105 }
00106 
00113 BESCatalog *
00114 BESCatalogList::find_catalog( const string &catalog_name )
00115 {
00116     BESCatalog *ret = 0 ;
00117     BESCatalogList::catalog_citer i ;
00118     i = _catalogs.find( catalog_name ) ;
00119     if( i != _catalogs.end() )
00120     {
00121         ret = (*i).second;
00122     }
00123     return ret ;
00124 }
00125 
00158 void
00159 BESCatalogList::show_catalog( const string &container,
00160                               const string &coi,
00161                               BESInfo *info )
00162 {
00163     // if there is only one catalog then go to it to show the catalog
00164     if( _catalogs.size() == 1 )
00165     {
00166         catalog_citer i = _catalogs.begin() ;
00167         BESCatalog *catalog = (*i).second ;
00168         catalog->show_catalog( container, coi, info ) ;
00169     }
00170     else if( _catalogs.size() != 0 )
00171     {
00172         // This means there are more than one catalog. If the specified
00173         // container is empty then display the names of the catalogs
00174         if( container.empty() )
00175         {
00176             map<string,string> a1 ;
00177             a1["thredds_collection"] = "\"true\"" ;
00178             a1["isData"] = "\"false\"" ;
00179             info->begin_tag( "dataset", &a1 ) ;
00180             info->add_tag( "name", "/" ) ;
00181 
00182             a1["catalogRoot"] = "\"true\"" ;
00183             catalog_citer i = _catalogs.begin() ;
00184             catalog_citer e = _catalogs.end() ;
00185             for( ; i != e; i++ )
00186             {
00187                 string name = (*i).first ;
00188                 BESCatalog *catalog = (*i).second ;
00189                 info->begin_tag( "dataset", &a1 ) ;
00190                 info->add_tag( "name", name ) ;
00191                 info->end_tag( "dataset" ) ;
00192             }
00193 
00194             info->end_tag( "dataset" ) ;
00195         }
00196         else
00197         {
00198             // if a container is specified then the name of the catalog
00199             // comes first, followed by a colon. If no colon, then no
00200             // catalog specified, which means error
00201             string::size_type colon = container.find( ":" ) ;
00202             if( colon == string::npos )
00203             {
00204                 string serr = "You must specify a catalog to use." ;
00205                 throw BESSyntaxUserError( serr, __FILE__, __LINE__ ) ;
00206             }
00207             else
00208             {
00209                 // there is a colon. The name is the part before the colon.
00210                 string name = container.substr( 0, colon ) ;
00211                 string rest = container.substr( colon+1, container.length() - colon ) ;
00212                 BESCatalog *catalog = _catalogs[ name ] ;
00213                 if( catalog )
00214                 {
00215                     catalog->show_catalog( rest, coi, info ) ;
00216                 }
00217                 else
00218                 {
00219                     string serr = "The catalog " + name + " does not exist." ;
00220                     throw BESSyntaxUserError( serr, __FILE__, __LINE__ ) ;
00221                 }
00222             }
00223         }
00224     }
00225 }
00226 
00229 BESCatalogList *
00230 BESCatalogList::TheCatalogList()
00231 {
00232     if( _instance == 0 )
00233     {
00234         _instance = new BESCatalogList ;
00235     }
00236     return _instance ;
00237 }
00238 
00246 void
00247 BESCatalogList::dump( ostream &strm ) const
00248 {
00249     strm << BESIndent::LMarg << "BESCatalogList::dump - ("
00250                              << (void *)this << ")" << endl ;
00251     BESIndent::Indent() ;
00252     if( _catalogs.size() )
00253     {
00254         strm << BESIndent::LMarg << "catalog list:" << endl ;
00255         BESIndent::Indent() ;
00256         catalog_citer i = _catalogs.begin() ;
00257         catalog_citer e = _catalogs.end() ;
00258         for( ; i != e; i++ )
00259         {
00260             BESCatalog *catalog = (*i).second ;
00261             strm << BESIndent::LMarg << (*i).first << catalog << endl ;
00262         }
00263         BESIndent::UnIndent() ;
00264     }
00265     else
00266     {
00267         strm << BESIndent::LMarg << "catalog list: empty" << endl ;
00268     }
00269     BESIndent::UnIndent() ;
00270 }
00271 

Generated on Tue Mar 4 23:13:33 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.1