00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "BaBar/BaBar.hh"
00010
00011 #include "CdbBdbShared/CdbBdbSFolderP.hh"
00012
00013 #include "CdbBdbShared/CdbBdbSViewP.hh"
00014 #include "CdbBdbShared/CdbBdbSFolderPItr.hh"
00015 #include "CdbBdbShared/CdbBdbSConditionAtFolderPItr.hh"
00016
00017 #include "CdbBase/CdbPathName.hh"
00018
00019 #include <assert.h>
00020
00021 #include <vector>
00022 using std::cout;
00023 using std::endl;
00024 using std::ostream;
00025
00026 CdbBdbSFolderP::CdbBdbSFolderP( const char* theName,
00027 const char* theDescription,
00028 const BdbTime& theCreationTime,
00029 const BdbRef(CdbBdbSViewP)& theParentViewRef,
00030 const BdbRef(CdbBdbSFolderP)& theParentFolderRef ) :
00031 _name(theName),
00032 _description(theDescription),
00033 _created(theCreationTime),
00034 _parentViewRef(theParentViewRef),
00035 _parentFolderRef(theParentFolderRef),
00036 _foldersMapRef(0),
00037 _conditionsMapRef(0)
00038 {
00039 assert( 0 != theName );
00040 assert( 0 != theDescription );
00041 assert(( BdbTime::minusInfinity != theCreationTime ) && ( BdbTime::plusInfinity != theCreationTime ));
00042 assert( ! BdbIsNull(theParentViewRef));
00043
00044 _foldersMapRef = new( ooThis( )) BdbMap( );
00045 _conditionsMapRef = new( ooThis( )) BdbMap( );
00046 }
00047
00048 CdbBdbSFolderP::~CdbBdbSFolderP( )
00049 {
00050
00051
00052
00053
00054
00055
00056 {
00057 std::vector<BdbRef(CdbBdbSFolderP)> folders;
00058 ooMapItr fItr( _foldersMapRef );
00059 while( fItr.next( )) folders.push_back((const BdbRef(CdbBdbSFolderP)&)( fItr->oid( )));
00060 for( std::vector<BdbRef(CdbBdbSFolderP)>::iterator i = folders.begin( ); i != folders.end( ); ++i ) BdbDelete(*i);
00061 }
00062 {
00063 std::vector<BdbRef(CdbBdbSConditionAtFolderP)> conditions;
00064 ooMapItr cItr( _conditionsMapRef );
00065 while( cItr.next( )) conditions.push_back((const BdbRef(CdbBdbSConditionAtFolderP)&)( cItr->oid( )));
00066 for( std::vector<BdbRef(CdbBdbSConditionAtFolderP)>::iterator i = conditions.begin( ); i != conditions.end( ); ++i ) BdbDelete(*i);
00067 }
00068
00069
00070
00071 BdbDelete(_foldersMapRef);
00072 BdbDelete(_conditionsMapRef);
00073 }
00074
00075 ooString(32)
00076 CdbBdbSFolderP::name( ) const
00077 {
00078 return _name.head( );
00079 }
00080
00081 ooString(32)
00082 CdbBdbSFolderP::description( ) const
00083 {
00084 return _description;
00085 }
00086
00087 BdbTime
00088 CdbBdbSFolderP::created( ) const
00089 {
00090 return _created;
00091 }
00092
00093 BdbRef(CdbBdbSViewP)
00094 CdbBdbSFolderP::parentView( ) const
00095 {
00096 return _parentViewRef;
00097 }
00098
00099 BdbRef(CdbBdbSFolderP)
00100 CdbBdbSFolderP::parentFolder( ) const
00101 {
00102 return _parentFolderRef;
00103 }
00104
00105 CdbStatus
00106 CdbBdbSFolderP::insert( const BdbRef(CdbBdbSFolderP)& theRef )
00107 {
00108 ooUpdate( );
00109
00110 const char* errorStr = "CdbBdbSFolderP::insert(folder) - ERROR";
00111
00112 CdbStatus result = CdbStatus::Error;
00113 do {
00114
00115
00116
00117 if( BdbIsNull(theRef)) {
00118 cout << errorStr << endl
00119 << " Illegal folder reference passed to the procedure." << endl;
00120 break;
00121 }
00122
00123 ooString(32) fName = theRef->name( );
00124 if( 0 == fName.head( )) {
00125 cout << errorStr << endl
00126 << " The passed folder does not have any name." << endl;
00127 break;
00128 }
00129
00130 CdbPathName fPathName( fName.head( ));
00131 if(( ! fPathName.isValid ( ))
00132 || fPathName.isAbsolute ( )
00133 || fPathName.isComposite( )) {
00134
00135 cout << errorStr << endl
00136 << " The passed folder's name has illegal format. It must be a simple" << endl
00137 << " string without path separators." << endl
00138 << " PASSED FOLDER'S NAME: \"" << fName.head( ) << "\"" << endl
00139 << " CURRENT PATH SEPARATOR: \"" << CdbPathName::separator( ) << "\"" << endl;
00140 break;
00141 }
00142
00143
00144
00145
00146 if(( ooThis( ) != theRef->parentFolder( )) ||
00147 ( _parentViewRef != theRef->parentView ( ))) {
00148 cout << errorStr << endl
00149 << " The passed folder has wrong parent folder or view. They have to be" << endl
00150 << " the current folder and the current folder's parent view." << endl
00151 << " PASSED FOLDER'S PARENT FOLDER: " << theRef->parentFolder( ).sprint( ) << endl
00152 << " CURRENT FOLDER: " << ooThis( ).sprint( ) << endl
00153 << " PASSED FOLDER'S PARENT VIEW: " << theRef->parentView( ).sprint( ) << endl
00154 << " CURRENT FOLDER'S PARENT VIEW: " << _parentViewRef.sprint( ) << endl;
00155 break;
00156 }
00157
00158
00159
00160 if( d_True == _foldersMapRef->isMember( fName.head( ))) {
00161 cout << errorStr << endl
00162 << " The current folder already has a child folder with such a name." << endl
00163 << " PASSED NAME: \"" << fName.head( ) << "\"" << endl;
00164 break;
00165 }
00166
00167
00168
00169 if( BdbcSuccess != _foldersMapRef->add( fName.head( ),
00170 theRef )) {
00171 cout << errorStr << endl
00172 << " Failed to insert specified object into the collection of child folders." << endl
00173 << " PASSED NAME: \"" << fName.head( ) << "\"" << endl;
00174 break;
00175 }
00176
00177
00178
00179 result = CdbStatus::Success;
00180
00181 } while( false );
00182
00183 return result;
00184 }
00185
00186 CdbStatus
00187 CdbBdbSFolderP::insert( const BdbRef(CdbBdbSConditionAtFolderP)& theRef )
00188 {
00189 ooUpdate( );
00190
00191 const char* errorStr = "CdbBdbSFolderP::insert(condition) - ERROR";
00192
00193 CdbStatus result = CdbStatus::Error;
00194 do {
00195
00196
00197
00198 if( BdbIsNull(theRef)) {
00199 cout << errorStr << endl
00200 << " Illegal condition reference passed to the procedure." << endl;
00201 break;
00202 }
00203
00204 ooString(32) cName = theRef->name( );
00205 if( 0 == cName.head( )) {
00206 cout << errorStr << endl
00207 << " The passed condition does not have any name." << endl;
00208 break;
00209 }
00210
00211 CdbPathName cPathName( cName.head( ));
00212 if(( ! cPathName.isValid ( ))
00213 || cPathName.isAbsolute ( )
00214 || cPathName.isComposite( )) {
00215
00216 cout << errorStr << endl
00217 << " The passed condition's name has illegal format. It must be a simple" << endl
00218 << " string without path separators." << endl
00219 << " PASSED CONDITION'S NAME: \"" << cName.head( ) << "\"" << endl
00220 << " CURRENT PATH SEPARATOR: \"" << CdbPathName::separator( ) << "\"" << endl;
00221 break;
00222 }
00223
00224
00225
00226 if( ooThis( ) != theRef->parentFolder( )) {
00227 cout << errorStr << endl
00228 << " The passed condition has wrong parent folder. It has to be" << endl
00229 << " the current folder." << endl
00230 << " PASSED CONDITION'S PARENT FOLDER: " << theRef->parentFolder( ).sprint( ) << endl
00231 << " CURRENT FOLDER: " << ooThis( ).sprint( ) << endl;
00232 break;
00233 }
00234
00235
00236
00237 if( d_True == _conditionsMapRef->isMember( cName.head( ))) {
00238 cout << errorStr << endl
00239 << " The current folder already has a condition with such a name." << endl
00240 << " PASSED NAME: \"" << cName.head( ) << "\"" << endl;
00241 break;
00242 }
00243
00244
00245
00246 if( BdbcSuccess != _conditionsMapRef->add( cName.head( ),
00247 theRef )) {
00248 cout << errorStr << endl
00249 << " Failed to insert specified object into the collection of conditions." << endl
00250 << " PASSED NAME: \"" << cName.head( ) << "\"" << endl;
00251 break;
00252 }
00253
00254
00255
00256 result = CdbStatus::Success;
00257
00258 } while( false );
00259
00260 return result;
00261 }
00262
00263 CdbStatus
00264 CdbBdbSFolderP::createFolder( const char* theFullPathName,
00265 const char* theDescription,
00266 const BdbTime& theCreationTime )
00267 {
00268 ooUpdate( );
00269
00270 const char* errorStr = "CdbBdbSFolderP::createFolder( ) - ERROR";
00271
00272 CdbStatus result = CdbStatus::Error;
00273 do {
00274
00275
00276
00277 CdbPathName fullPathName( theFullPathName );
00278
00279 if(( ! fullPathName.isValid ( ))
00280 || fullPathName.isAbsolute ( )
00281 || fullPathName.isRoot( )) {
00282
00283 cout << errorStr << endl
00284 << " The passed folder's name has illegal format. It must be a either simple" << endl
00285 << " or composite relative path." << endl
00286 << " PASSED NAME: \"" << theFullPathName << "\"" << endl;
00287 break;
00288 }
00289
00290
00291
00292
00293 BdbRef(CdbBdbSFolderP) fRef;
00294 {
00295 CdbPathName firstComponentName( fullPathName.first( ));
00296 assert( firstComponentName.isValid( ));
00297
00298 BdbRef(BdbPersObj) objRef;
00299 if( BdbcSuccess == _foldersMapRef->lookup( firstComponentName.toString( ).c_str( ),
00300 objRef )) {
00301
00302
00303
00304
00305
00306 fRef = (const BdbRef(CdbBdbSFolderP)&) objRef;
00307
00308 } else {
00309
00310
00311
00312 fRef = new( ooThis( )) CdbBdbSFolderP( firstComponentName.toString( ).c_str( ),
00313 theDescription,
00314 theCreationTime,
00315 _parentViewRef,
00316 ooThis( ));
00317
00318
00319
00320
00321 if( CdbStatus::Success != insert( fRef )) {
00322 BdbDelete( fRef );
00323 break;
00324 }
00325 }
00326 }
00327 assert( !BdbIsNull(fRef));
00328
00329
00330
00331
00332 if( fullPathName.isComposite( )) {
00333 result = fRef->createFolder( fullPathName.afterFirst( ).toString( ).c_str( ),
00334 theDescription,
00335 theCreationTime );
00336 } else {
00337 ;
00338 }
00339
00340
00341
00342 result = CdbStatus::Success;
00343
00344 } while( false );
00345
00346 return result;
00347 }
00348
00349 CdbStatus
00350 CdbBdbSFolderP::removeFolder( const CdbPathName& thePathName,
00351 bool forceFlag )
00352 {
00353 ooUpdate( );
00354
00355 const char* errorStr = "CdbBdbSFolderP::removeFolder( ) - ERROR";
00356 const char* fatalStr = "CdbBdbSFolderP::removeFolder( ) - FATAL INTERNAL ERROR";
00357
00358 CdbStatus result = CdbStatus::Error;
00359 do {
00360
00361
00362
00363 if(( ! thePathName.isValid ( ))
00364 || thePathName.isAbsolute ( )
00365 || thePathName.isComposite( )) {
00366
00367 cout << errorStr << endl
00368 << " The passed folder's name has illegal format. It must be a simple" << endl
00369 << " string without path separators." << endl
00370 << " PASSED NAME: \"" << thePathName << "\"" << endl
00371 << " CURRENT PATH SEPARATOR: \"" << CdbPathName::separator( ) << "\"" << endl;
00372 break;
00373 }
00374
00375
00376
00377 BdbRef(BdbPersObj) objRef;
00378 if( BdbcSuccess != _foldersMapRef->lookup( thePathName.toString( ).c_str( ),
00379 objRef )) {
00380 result = CdbStatus::NotFound;
00381 break;
00382 }
00383
00384 BdbRef(CdbBdbSFolderP) fRef = (const BdbRef(CdbBdbSFolderP)&) objRef;
00385
00386 if( BdbIsNull(fRef) || ! fRef.isValid( )) {
00387 cout << fatalStr << endl
00388 << " The internal map of the folders is corrupted." << endl;
00389 assert( 0 );
00390 break;
00391 }
00392
00393
00394
00395
00396 if( ! forceFlag ) {
00397
00398 CdbItr< const char* > fItr;
00399 if( CdbStatus::Success != fRef->folderIterator( fItr )) {
00400 cout << errorStr << endl
00401 << " Failed to setup asn iterator of folders." << endl;
00402 break;
00403 }
00404 CdbItr< const char* > cItr;
00405 if( CdbStatus::Success != fRef->conditionIterator( cItr )) {
00406 cout << errorStr << endl
00407 << " Failed to setup asn iterator of conditions." << endl;
00408 break;
00409 }
00410 if( fItr.next( ) || cItr.next( )) {
00411 cout << errorStr << endl
00412 << " The folder requested for removal is not empty. It contains either" << endl
00413 << " other sub-folders or conditions. Use 'force' mode to proceed with removal." << endl;
00414 break;
00415 }
00416 }
00417
00418
00419
00420 if( BdbcSuccess != _foldersMapRef->remove( thePathName.toString( ).c_str( ))) {
00421 cout << errorStr << endl
00422 << " Failed to remove the folder object from the map." << endl;
00423 break;
00424 }
00425 BdbDelete(fRef);
00426
00427
00428
00429 result = CdbStatus::Success;
00430
00431 } while( false );
00432
00433 return result;
00434 }
00435
00436 CdbStatus
00437 CdbBdbSFolderP::removeCondition( const CdbPathName& thePathName )
00438 {
00439 ooUpdate( );
00440
00441 const char* errorStr = "CdbBdbSFolderP::removeCondition( ) - ERROR";
00442 const char* fatalStr = "CdbBdbSFolderP::removeCondition( ) - FATAL INTERNAL ERROR";
00443
00444 CdbStatus result = CdbStatus::Error;
00445 do {
00446
00447
00448
00449 if(( ! thePathName.isValid ( ))
00450 || thePathName.isAbsolute ( )
00451 || thePathName.isComposite( )) {
00452
00453 cout << errorStr << endl
00454 << " The passed condition's name has illegal format. It must be a simple" << endl
00455 << " string without path separators." << endl
00456 << " PASSED NAME: \"" << thePathName << "\"" << endl
00457 << " CURRENT PATH SEPARATOR: \"" << CdbPathName::separator( ) << "\"" << endl;
00458 break;
00459 }
00460
00461
00462
00463 BdbRef(BdbPersObj) objRef;
00464 if( BdbcSuccess != _conditionsMapRef->lookup( thePathName.toString( ).c_str( ),
00465 objRef )) {
00466 result = CdbStatus::NotFound;
00467 break;
00468 }
00469
00470 BdbRef(CdbBdbSConditionAtFolderP) cRef = (const BdbRef(CdbBdbSConditionAtFolderP)&) objRef;
00471
00472 if( BdbIsNull(cRef) || ! cRef.isValid( )) {
00473 cout << fatalStr << endl
00474 << " The internal map of the conditions is corrupted." << endl;
00475 assert( 0 );
00476 break;
00477 }
00478
00479
00480
00481 if( BdbcSuccess != _conditionsMapRef->remove( thePathName.toString( ).c_str( ))) {
00482 cout << errorStr << endl
00483 << " Failed to remove the condition object from the map." << endl;
00484 break;
00485 }
00486 BdbDelete(cRef);
00487
00488
00489
00490 result = CdbStatus::Success;
00491
00492 } while( false );
00493
00494 return result;
00495 }
00496
00497 CdbStatus
00498 CdbBdbSFolderP::findFolder( const CdbPathName& thePathName,
00499 BdbRef(CdbBdbSFolderP)& theRef ) const
00500 {
00501 const char* errorStr = "CdbBdbSFolderP::findFolder( ) - ERROR";
00502 const char* fatalStr = "CdbBdbSFolderP::findFolder( ) - FATAL INTERNAL ERROR";
00503
00504 CdbStatus result = CdbStatus::Error;
00505 do {
00506
00507
00508
00509 if( ! ( thePathName.isValid( ) && ! thePathName.isAbsolute( ))) {
00510 cout << errorStr << endl
00511 << " Illegal path name passed to the procedure: \"" << thePathName << "\"" << endl;
00512 break;
00513 }
00514
00515
00516
00517
00518
00519
00520
00521 BdbRef(BdbPersObj) objRef;
00522
00523 if( BdbcSuccess != _foldersMapRef->lookup( thePathName.first( ).toString( ).c_str( ),
00524 objRef )) {
00525 result = CdbStatus::NotFound;
00526 break;
00527 }
00528
00529 BdbHandle(CdbBdbSFolderP) folderH;
00530 folderH = (const BdbRef(CdbBdbSFolderP)&) objRef;
00531
00532 if( BdbIsNull(folderH) || ! folderH.isValid( )) {
00533 cout << fatalStr << endl
00534 << " The internal map of the folders is corrupted." << endl;
00535 assert( 0 );
00536 break;
00537 }
00538
00539 if( ! thePathName.isComposite( )) theRef = folderH;
00540 else
00541 return folderH->findFolder( thePathName.afterFirst( ),
00542 theRef );
00543
00544
00545 result = CdbStatus::Success;
00546
00547 } while( false );
00548
00549 return result;
00550 }
00551
00552 CdbStatus
00553 CdbBdbSFolderP::findCondition( const CdbPathName& thePathName,
00554 BdbRef(CdbBdbSConditionAtFolderP)& theRef ) const
00555 {
00556 const char* errorStr = "CdbBdbSFolderP::findCondition( ) - ERROR";
00557 const char* fatalStr = "CdbBdbSFolderP::findCondition( ) - FATAL INTERNAL ERROR";
00558
00559 CdbStatus result = CdbStatus::Error;
00560 do {
00561
00562
00563
00564 if( ! ( thePathName.isValid( ) && ! thePathName.isAbsolute( ))) {
00565 cout << errorStr << endl
00566 << " Illegal path name passed to the procedure: \"" << thePathName << "\"" << endl;
00567 break;
00568 }
00569
00570
00571
00572
00573
00574
00575
00576 if( thePathName.isComposite( )) {
00577
00578
00579
00580
00581 BdbRef(BdbPersObj) objRef;
00582
00583 if( BdbcSuccess != _foldersMapRef->lookup( thePathName.first( ).toString( ).c_str( ),
00584 objRef )) {
00585 result = CdbStatus::NotFound;
00586 break;
00587 }
00588
00589 BdbHandle(CdbBdbSFolderP) folderH;
00590 folderH = (const BdbRef(CdbBdbSFolderP)&) objRef;
00591
00592 if( BdbIsNull(folderH) || ! folderH.isValid( )) {
00593 cout << fatalStr << endl
00594 << " The internal map of the folders is corrupted." << endl;
00595 assert( 0 );
00596 break;
00597 }
00598
00599 return folderH->findCondition( thePathName.afterFirst( ),
00600 theRef );
00601
00602 } else {
00603
00604
00605
00606 BdbRef(BdbPersObj) objRef;
00607
00608 if( BdbcSuccess != _conditionsMapRef->lookup( thePathName.first( ).toString( ).c_str( ),
00609 objRef )) {
00610 result = CdbStatus::NotFound;
00611 break;
00612 }
00613
00614 BdbHandle(CdbBdbSConditionAtFolderP) conditionH;
00615 conditionH = (const BdbRef(CdbBdbSConditionAtFolderP)&) objRef;
00616
00617 if( BdbIsNull(conditionH) || ! conditionH.isValid( )) {
00618 cout << fatalStr << endl
00619 << " The internal map of the conditions is corrupted." << endl;
00620 assert( 0 );
00621 break;
00622 }
00623
00624 theRef = conditionH;
00625 }
00626
00627
00628
00629 result = CdbStatus::Success;
00630
00631 } while( false );
00632
00633 return result;
00634 }
00635
00636 CdbStatus
00637 CdbBdbSFolderP::folderIterator( CdbItr< const char* >& theItr ) const
00638 {
00639
00640
00641
00642
00643 theItr = CdbItr< const char* >( new CdbBdbSFolderPItr( _foldersMapRef ));
00644 return CdbStatus::Success;
00645 }
00646
00647 CdbStatus
00648 CdbBdbSFolderP::conditionIterator( CdbItr< const char* >& theItr ) const
00649 {
00650
00651
00652
00653
00654 theItr = CdbItr< const char* >( new CdbBdbSConditionAtFolderPItr( _conditionsMapRef ));
00655 return CdbStatus::Success;
00656 }
00657
00658 void
00659 CdbBdbSFolderP::dump( ostream& o ) const
00660 {
00661 o << " NAME: \"" << _name.head( ) << "\"" << endl
00662 << " DESCRIPTION: \"" << _description.head( ) << "\"" << endl
00663 << " CREATED: " << _created << endl
00664 << " PARENT VIEW: " << _parentViewRef.sprint( ) << endl
00665 << "PARENT FOLDER: " << _parentFolderRef.sprint( ) << endl;
00666 }
00667
00668
00669
00670