00001
00002
00003 #include <sstream>
00004
00005 #include "Configurable/Cfg.h"
00006 #include "DatabaseInterface/DbiCascader.h"
00007 #include "DatabaseInterface/DbiConfigStream.h"
00008 #include "DatabaseInterface/DbiFieldType.h"
00009 #include "DatabaseInterface/DbiTableProxy.h"
00010 #include "DatabaseInterface/DbiTableProxyRegistry.h"
00011 #include "DatabaseInterface/DbiWriter.h"
00012 #include "LeakChecker/Lea.h"
00013 #include "MessageService/MsgService.h"
00014 #include "Registry/Registry.h"
00015
00016 ClassImp(DbiConfigStream)
00017
00018
00019
00020
00021 CVSID("$Id: DbiConfigStream.cxx,v 1.18 2006/10/11 08:25:54 west Exp $");
00022
00023 VldContext DbiConfigStream::fgDefaultContext(Detector::kNear,
00024 SimFlag::kData,
00025 VldTimeStamp() );
00026
00027
00028
00029
00030
00031
00032
00033
00034 DbiConfigStream::DbiConfigStream() :
00035 fCFSet(0)
00036 {
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 LEA_CTOR
00059
00060 MSG("Dbi", Msg::kDebug) << "Creating DbiConfigStream" << endl;
00061 }
00062
00063
00064
00065 DbiConfigStream::DbiConfigStream(const string& SoftName,
00066 const std::string& ConfigName,
00067 VldContext vc,
00068 Dbi::Task task,
00069 const std::string& tableName) :
00070 fCFSet(0),
00071 fCFSetTable(tableName,vc,task),
00072 fConfigName(ConfigName),
00073 fSoftwName(SoftName)
00074 {
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 LEA_CTOR
00106
00107 MSG("Dbi", Msg::kDebug) << "Creating DbiConfigStream" << endl;
00108
00109
00110 int rowNum = fCFSetTable.GetNumRows()-1;
00111 while ( rowNum >= 0 ) {
00112 fCFSet = fCFSetTable.GetRow(rowNum);
00113 if ( fCFSet->GetParamValue(0) == fSoftwName
00114 && fCFSet->GetParamValue(1) == fConfigName ) {
00115 fVRec = *fCFSetTable.GetValidityRec(fCFSet);
00116 MSG("Dbi", Msg::kSynopsis) << "DbiConfigStream for " << fSoftwName
00117 << "," << fConfigName
00118 << " has validity rec: " << fVRec
00119 << " and aggregate no.: " << fCFSet->GetAggregateNo()
00120 << endl;
00121 return;
00122 }
00123 --rowNum;
00124 }
00125
00126
00127
00128
00129
00130 fCFSet = 0;
00131
00132 fVRec.SetDbNo(0);
00133 fVRec.SetTableProxy(&DbiResultPtr<DbiConfigSet>::GetTableProxy(tableName));
00134 VldTimeStamp start(1970,1,1,0,0,0);
00135 VldTimeStamp end(2038,1,1,0,0,0);
00136 VldRange vr(127,127,start,end,"DbiConfigStream");
00137 fVRec.SetVldRange(vr);
00138 MSG("Dbi", Msg::kSynopsis) << "DbiConfigStream for " << fSoftwName
00139 << "," << fConfigName
00140 << " has no existing entry; creating validity rec: " << fVRec << endl;
00141
00142 }
00143
00144
00145
00146 DbiConfigStream::~DbiConfigStream() {
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 LEA_DTOR
00170
00171 MSG("Dbi", Msg::kDebug) << "Destroying DbiConfigStream" << endl;
00172
00173 }
00174
00175
00176
00177
00178 ostream& operator<<(ostream& os, const DbiConfigStream& cfStream) {
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 const DbiConfigSet* cfSet = cfStream.GetConfigSet();
00203
00204 if ( cfSet ) os << "DbiConfigSet contains: " << *cfSet << endl;
00205 else os << "DbiConfigSet is empty! " << endl;
00206 return os;
00207 }
00208
00209
00210
00211 const DbiConfigStream& DbiConfigStream::operator>>(Registry* reg) {
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 if ( ! reg ) return *this;
00236
00237
00238
00239 Bool_t keysLocked = reg->KeysLocked();
00240 Bool_t valuesLocked = reg->ValuesLocked();
00241 reg->UnLockKeys();
00242 reg->UnLockValues();
00243 reg->Clear();
00244
00245
00246
00247 if ( fCFSet ) {
00248 UInt_t numParams = fCFSet->GetNumParams();
00249
00250
00251
00252 if ( numParams == 3 && fCFSet->GetParamName(2) == "CONFIG_DATA" ) {
00253 istringstream is(fCFSet->GetParamValue(2));
00254 reg->ReadStream(is);
00255 }
00256 else {
00257 MAXMSG("Dbi",Msg::kError,20) << "Attempting to fill Registry from a table with "
00258 << numParams << " columns (should be 3) using column named "
00259 << fCFSet->GetParamName(2) << " (should be CONFIG_DATA)." << endl;
00260 }
00261 }
00262 if ( keysLocked ) reg->LockKeys();
00263 if ( valuesLocked ) reg->LockValues();
00264 return *this;
00265 }
00266
00267
00268 DbiConfigStream& DbiConfigStream::operator<<(const Registry* reg) {
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 if ( fSoftwName == "" ) {
00289 MAXMSG("Dbi",Msg::kError,20) << "Cannot fill (<<): No software name defined." << endl;
00290 return *this;
00291 }
00292
00293 DbiFieldType stringType(Dbi::kString);
00294
00295 ostringstream os;
00296 reg->PrintStream(os);
00297 fCFSetModified.Clear();
00298 fCFSetModified.PushBack("SOFTW_NAME", fSoftwName, stringType);
00299 fCFSetModified.PushBack("CONFIG_NAME",fConfigName, stringType);
00300 fCFSetModified.PushBack("CONFIG_DATA",os.str(), stringType);
00301 if ( fVRec.GetAggregateNo() > 0 ) fCFSetModified.SetAggregateNo( fVRec.GetAggregateNo());
00302 fCFSet = &fCFSetModified;
00303 return *this;
00304
00305 }
00306
00307
00308
00309
00310 Bool_t DbiConfigStream::Write(UInt_t dbNo,
00311 const std::string& logComment,
00312 Bool_t localTest ) {
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 if ( ! fCFSet ) {
00328 MAXMSG("Dbi",Msg::kError,20) << "No configuration data to write out." << endl;
00329 return false;
00330 }
00331
00332
00333
00334
00335 int requireGlobal = localTest ? -1 : 1;
00336 if ( fVRec.GetAggregateNo() < 0 ) {
00337 DbiCascader& cas = DbiTableProxyRegistry::Instance().GetCascader();
00338 Int_t aggNo = cas.AllocateSeqNo(fVRec.GetTableProxy()->GetTableName(),requireGlobal,dbNo);
00339 if ( aggNo <= Dbi::kMAXLOCALSEQNO && ! localTest ) {
00340 MAXMSG("Dbi",Msg::kError,20) << "Cannot write out configuration data: no authorising entry in cascade." << endl;
00341 return false;
00342 }
00343 fVRec.SetAggregateNo(aggNo);
00344 fCFSetModified.SetAggregateNo(aggNo);
00345 MSG("Dbi",Msg::kDebug) << "Aggregate number: " << aggNo
00346 << " allocated to entry " << fSoftwName
00347 << "," << fConfigName
00348 << " in table " << fVRec.GetTableProxy()->GetTableName() << endl;
00349 }
00350 DbiWriter<DbiConfigSet> writer(fVRec,dbNo,logComment);
00351 writer.SetRequireGlobalSeqno(requireGlobal);
00352 writer.SetOverlayCreationDate();
00353 writer << *fCFSet;
00354 return writer.Close();
00355
00356 }
00357
00358