Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

CdbSQLApiFolder.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbSQLApiFolder.cc,v 1.1 2005/09/06 18:01:10 gapon Exp $
00003 
00004 /// The implementation of the CdbSQLApiFolder class.
00005 /**
00006   * @see CdbSQLApiFolder
00007   */
00008 
00009 #include "BaBar/BaBar.hh"
00010 
00011 #include "CdbSQL/CdbSQLApiFolder.hh"
00012 #include "CdbSQL/CdbSQLApiCondition.hh"
00013 #include "CdbSQL/CdbSQL.hh"
00014 #include "CdbSQL/CdbSQLOrigin.hh"
00015 #include "CdbSQL/CdbSQLFolder.hh"
00016 #include "CdbSQL/CdbSQLConditionAtFolder.hh"
00017 #include "CdbSQL/CdbSQLCondition.hh"
00018 
00019 #include "CdbBase/CdbPathName.hh"
00020 #include "CdbBase/CdbDatabase.hh"
00021 #include "CdbBase/CdbView.hh"
00022 #include "CdbBase/CdbVectorAdapterItr.hh"
00023 
00024 #include <assert.h>
00025 
00026 #include <iostream>
00027 using std::cout;
00028 using std::endl;
00029 
00030 namespace {
00031 
00032   /////////////////////////////////
00033   // Class: IteratorOfSubFolders //
00034   /////////////////////////////////
00035 
00036     class IteratorOfSubFolders : public CdbVectorAdapterItr<const char*, CdbCPtr<CdbSQLFolder> > {
00037 
00038     private:
00039 
00040       /// The default constructor (NOT IMPLEMENTED)
00041 
00042         IteratorOfSubFolders( );
00043 
00044       /// The assignment operator (NOT IMPLEMENTED)
00045 
00046         IteratorOfSubFolders& operator=( const IteratorOfSubFolders& );
00047 
00048     protected:
00049 
00050       /// The copy constructor
00051 
00052         IteratorOfSubFolders( const IteratorOfSubFolders& theItr ) :
00053             CdbVectorAdapterItr<const char*, CdbCPtr<CdbSQLFolder> >( theItr )
00054         { }
00055 
00056     public:
00057 
00058       /// The normal constructor
00059       /**
00060         * @see CdbVectorAdapterItr::CdbVectorAdapterItr()
00061         */
00062         IteratorOfSubFolders( const std::vector< CdbCPtr<CdbSQLFolder> >& theList ) :
00063             CdbVectorAdapterItr<const char*, CdbCPtr<CdbSQLFolder> >( theList )
00064         { }
00065 
00066       /// Make a clone of itself
00067       /**
00068         * @see CdbIItr::clone()
00069         */
00070         virtual CdbIItr<const char*>* clone( ) const
00071         {
00072             return new IteratorOfSubFolders( *this );
00073         }
00074 
00075     protected:
00076 
00077       /// User defined translation for the currently reffered value
00078       /**
00079         * @see CdbVectorAdapterItr::CdbVectorAdapterItr()
00080         */
00081         virtual const char* toValue( const CdbCPtr<CdbSQLFolder>& thePtr ) const
00082         {
00083             return thePtr->name( ).c_str( );
00084         }
00085     };
00086 
00087   ////////////////////////////////
00088   // Class IteratorOfConditions //
00089   ////////////////////////////////
00090 
00091     class IteratorOfConditions : public CdbVectorAdapterItr<const char*, CdbCPtr<CdbSQLConditionAtFolder> > {
00092 
00093     private:
00094 
00095       /// The default constructor (NOT IMPLEMENTED)
00096 
00097         IteratorOfConditions( );
00098 
00099       /// The assignment operator (NOT IMPLEMENTED)
00100 
00101         IteratorOfConditions& operator=( const IteratorOfConditions& );
00102 
00103     protected:
00104 
00105       /// The copy constructor
00106 
00107         IteratorOfConditions( const IteratorOfConditions& theItr ) :
00108             CdbVectorAdapterItr<const char*, CdbCPtr<CdbSQLConditionAtFolder> >( theItr )
00109         { }
00110 
00111     public:
00112 
00113       /// The normal constructor
00114       /**
00115         * @see CdbVectorAdapterItr::CdbVectorAdapterItr()
00116         */
00117         IteratorOfConditions( const std::vector< CdbCPtr<CdbSQLConditionAtFolder> >& theList ) :
00118             CdbVectorAdapterItr<const char*, CdbCPtr<CdbSQLConditionAtFolder> >( theList )
00119         { }
00120 
00121       /// Make a clone of itself
00122       /**
00123         * @see CdbIItr::clone()
00124         */
00125         virtual CdbIItr<const char*>* clone( ) const
00126         {
00127             return new IteratorOfConditions( *this );
00128         }
00129 
00130     protected:
00131 
00132       /// User defined translation for the currently reffered value
00133       /**
00134         * @see CdbVectorAdapterItr::CdbVectorAdapterItr()
00135         */
00136         virtual const char* toValue( const CdbCPtr<CdbSQLConditionAtFolder>& thePtr ) const
00137         {
00138             return thePtr->name( ).c_str( );
00139         }
00140     };
00141 }
00142 
00143 CdbFolder*
00144 CdbSQLApiFolder::clone( ) const
00145 {
00146     CdbSQLApiFolder* mySelf = const_cast<CdbSQLApiFolder*>( this );
00147     return new CdbSQLApiFolder( mySelf->parentView( ),
00148                                 mySelf->parentFolder( ),
00149                                 mySelf->name( ),
00150                                 _myFolderPtr,
00151                                 _myMasterSQLOriginPtr,
00152                                 _myLocalSQLOriginPtr );
00153 }
00154 
00155 CdbSQLApiFolder::CdbSQLApiFolder( const CdbViewPtr&            theParentViewPtr,
00156                                   const CdbFolderPtr&          theParentFolderPtr,
00157                                   const char*                  theName,
00158                                   const CdbCPtr<CdbSQLFolder>& theFolderPtr,
00159                                   const CdbCPtr<CdbSQLOrigin>& theMasterOriginPtr,
00160                                   const CdbCPtr<CdbSQLOrigin>& theLocalOriginPtr ) :
00161     CdbFolder( theParentViewPtr,
00162                theParentFolderPtr,
00163                theName ),
00164     _myFolderPtr         (theFolderPtr),
00165     _myMasterSQLOriginPtr(theMasterOriginPtr),
00166     _myLocalSQLOriginPtr (theLocalOriginPtr)
00167 {
00168     assert( !theFolderPtr.isNull( ));
00169     assert( !theMasterOriginPtr.isNull( ));
00170     assert( !theLocalOriginPtr.isNull( ));
00171 }
00172 
00173 CdbSQLApiFolder::~CdbSQLApiFolder( )
00174 { }
00175 
00176 bool
00177 CdbSQLApiFolder::isValid( )
00178 {
00179     return true;
00180 }
00181 
00182 bool
00183 CdbSQLApiFolder::isOpen( )
00184 {
00185     return true;
00186 }
00187 
00188 CdbStatus
00189 CdbSQLApiFolder::open( )
00190 {
00191     return CdbStatus::Success;
00192 }
00193 
00194 CdbStatus
00195 CdbSQLApiFolder::close( )
00196 {
00197     return CdbStatus::Success;
00198 }
00199 
00200 BdbTime
00201 CdbSQLApiFolder::created( ) const
00202 {
00203     return _myFolderPtr->created( );
00204 }
00205 
00206 std::string
00207 CdbSQLApiFolder::description( ) const
00208 {
00209     return _myFolderPtr->description( );
00210 }
00211 
00212 CdbStatus
00213 CdbSQLApiFolder::findFolder( CdbFolderPtr& thePtr,
00214                              const char*   theName )
00215 {
00216     const char* errorStr = "CdbSQLApiFolder::findFolder() -- ERROR";
00217 
00218     CdbStatus status = CdbStatus::Error;
00219 
00220   // Verify parameters
00221 
00222     if( 0 == theName ) {
00223         cout << errorStr << endl
00224              << "    Non 0 pointer onto a folder name was expected." << endl;
00225 
00226         return CdbStatus::IllegalParameters;
00227     }
00228 
00229     CdbPathName fPath( theName );
00230 
00231     if( ! ( fPath.isValid( ) &&
00232             ! fPath.isRoot( ) &&
00233             ! fPath.isAbsolute( ) &&
00234             ! fPath.isComposite( ))) {
00235 
00236         cout << errorStr << endl
00237              << "    Unsupported format of the folder name. Simple non-root folder" << endl
00238              << "    name was expected." << endl
00239              << "        CURRENT FOLDER : \"" << name( ) << "\"" << endl
00240              << "        SUB-FOLDER     : \"" << theName << "\"" << endl;
00241         return CdbStatus::IllegalParameters;
00242     }
00243 
00244    // Find out persistent folder for specified name
00245 
00246     CdbCPtr<CdbSQLFolder> folderPtr;
00247     if( CdbStatus::Success != ( status = _myFolderPtr->find_folder( folderPtr,
00248                                                                     std::string( theName )))) {
00249         cout << errorStr << endl
00250              << "    Failed to find a subfolder in a scope of the current folder." << endl
00251              << "        CURRENT FOLDER : \"" << name( ) << "\"" << endl
00252              << "        SUB-FOLDER     : \"" << theName << "\"" << endl;
00253         return status;
00254     }
00255 
00256   // Construct transient CDB API object
00257 
00258     thePtr = new CdbSQLApiFolder( parentView( ),
00259                                   CdbFolderPtr( this->clone( )),
00260                                   theName,
00261                                   folderPtr,
00262                                   _myMasterSQLOriginPtr,
00263                                   _myLocalSQLOriginPtr );
00264     return CdbStatus::Success;
00265 }
00266 
00267 CdbStatus
00268 CdbSQLApiFolder::folderIterator( CdbFolderItr& theItr )
00269 {
00270     const char* errorStr = "CdbSQLApiFolder::folderIterator() -- ERROR.";
00271 
00272     CdbStatus status = CdbStatus::Error;
00273 
00274   // Find all folders in the scope of the current pesistent folder
00275 
00276     std::vector< CdbCPtr<CdbSQLFolder> > folders;
00277     if( CdbStatus::Success != ( status = _myFolderPtr->folders( folders ))) {
00278         cout << errorStr << endl
00279              << "    Failed to obtain a list of sub-folders at the current folder." << endl
00280              << "        FOLDER NAME : \"" << name( ) << "\"." << endl;
00281         return status;
00282     }
00283     theItr = CdbFolderItr( new IteratorOfSubFolders( folders ));
00284 
00285     return CdbStatus::Success;
00286 }
00287 
00288 CdbStatus
00289 CdbSQLApiFolder::findCondition( CdbConditionPtr& thePtr,
00290                                 const char*      theName )
00291 {
00292     const char* errorStr = "CdbSQLApiFolder::findCondition() -- ERROR";
00293 
00294     CdbStatus status = CdbStatus::Error;
00295 
00296   // Verify parameters
00297 
00298     if( 0 == theName ) {
00299         cout << errorStr << endl
00300              << "    Non 0 pointer onto a condition name was expected." << endl;
00301         return CdbStatus::IllegalParameters;
00302     }
00303 
00304     CdbPathName cPath( theName );
00305 
00306     if( ! ( cPath.isValid( ) &&
00307             ! cPath.isRoot( ) &&
00308             ! cPath.isAbsolute( ) &&
00309             ! cPath.isComposite( ))) {
00310 
00311         cout << errorStr << endl
00312              << "    Unsupported format of the condition name. Simple non-root" << endl
00313              << "    name was expected." << endl
00314              << "        CURRENT FOLDER : \"" << name( ) << "\"" << endl
00315              << "        CONDITION      : \"" << theName << "\"" << endl;
00316         return CdbStatus::IllegalParameters;
00317     }
00318 
00319    // Find out persistent condition (at folder) for specified name
00320 
00321     CdbCPtr<CdbSQLConditionAtFolder> conditionAtFolderPtr;
00322     if( CdbStatus::Success != ( status = _myFolderPtr->find_condition( conditionAtFolderPtr,
00323                                                                        std::string( theName )))) {
00324         cout << errorStr << endl
00325              << "    Failed to find the specified condition at the current folder." << endl
00326              << "        CURRENT FOLDER : \"" << name( ) << "\"" << endl
00327              << "        CONDITION      : \"" << theName << "\"" << endl;
00328         return status;
00329     }
00330 
00331   // Find out the "physical" condition corresponding to the found one
00332 
00333     CdbCPtr<CdbSQLCondition> conditionPtr;
00334     {
00335         CdbId conditionId = conditionAtFolderPtr->physicalId( );
00336 
00337       // Find a persistent origin where the specified physical condition is supposed to belong to.
00338 
00339         CdbCPtr<CdbSQLOrigin> originPtr;
00340         if( CdbStatus::Success != ( status = findOriginObject( originPtr,
00341                                                                conditionId.origin ))) {
00342             cout << errorStr << endl
00343                  << "    An owner origin for the 'physical' condition was not found." << endl
00344                  << "        'PHYSICAL' CONDITION ID : \"" << conditionId << "\"." << endl;
00345             return status;
00346         }
00347         if( CdbStatus::Success != ( status = originPtr->find_condition( conditionPtr,
00348                                                                         conditionId.local ))) {
00349             cout << errorStr << endl
00350                  << "    Failed to find the condition object for identifier: " << conditionId.local << endl
00351                  << "    in a scope of the origin with identifier: " << conditionId.origin << endl
00352                  << "    The database may not be properly initialized/loaded." << endl;
00353             return status;
00354         }
00355     }
00356 
00357   // Construct transient CDB API object
00358 
00359     thePtr = new CdbSQLApiCondition( CdbFolderPtr( this->clone( )),
00360                                      parentView( )->parent( ),
00361                                      theName,
00362                                      conditionAtFolderPtr,
00363                                      conditionPtr,
00364                                      _myMasterSQLOriginPtr,
00365                                      _myLocalSQLOriginPtr );
00366     return CdbStatus::Success;
00367 }
00368 
00369 CdbStatus
00370 CdbSQLApiFolder::conditionIterator( CdbConditionItr& theItr )
00371 {
00372     const char* errorStr = "CdbSQLApiFolder::conditionIterator() -- ERROR.";
00373 
00374     CdbStatus status = CdbStatus::Error;
00375 
00376   // Find all conditions in the scope of the current pesistent folder
00377 
00378     std::vector< CdbCPtr<CdbSQLConditionAtFolder> > conditions;
00379     if( CdbStatus::Success != ( status = _myFolderPtr->conditions( conditions ))) {
00380         cout << errorStr << endl
00381              << "    Failed to obtain a list of conditions at the current folder." << endl
00382              << "        CURRENT FOLDER : \"" << name( ) << "\"." << endl;
00383         return status;
00384     }
00385     theItr = CdbConditionItr( new IteratorOfConditions( conditions ));
00386 
00387     return CdbStatus::Success;
00388 }
00389 
00390 CdbStatus
00391 CdbSQLApiFolder::findOriginObject( CdbCPtr<CdbSQLOrigin>& thePtr,
00392                                    unsigned short         theId )
00393 {
00394     const char* errorStr = "CdbSQLApiFolder::findOriginObject() -- ERROR.";
00395 
00396   // A little optimization to use already found and cached MASTER and local
00397   // origin objects in case if one of them gets requested.
00398  
00399     CdbCPtr<CdbSQLOrigin> originPtr;
00400 
00401     if(      theId == _myMasterSQLOriginPtr->id( )) originPtr = _myMasterSQLOriginPtr;
00402     else if( theId == _myLocalSQLOriginPtr->id( ))  originPtr = _myLocalSQLOriginPtr;
00403     else {
00404 
00405         CdbStatus status = _myMasterSQLOriginPtr->parent( )->find_origin( originPtr,
00406                                                                           theId );
00407         if( CdbStatus::Success != status ) {
00408             cout << errorStr << endl
00409                  << "    Failed to find the origin by its identifier." << endl
00410                  << "        ORIGIN ID : " << theId << endl;
00411             return status;
00412         }
00413     }
00414     thePtr = originPtr;
00415 
00416     return CdbStatus::Success;
00417 }
00418 
00419 /////////////////
00420 // End Of File //
00421 /////////////////

Generated on Mon Dec 5 18:22:12 2005 for CDB by doxygen1.3-rc3