TDbiConfigStream.cxx

Go to the documentation of this file.
00001 // $Id: TDbiConfigStream.cxx,v 1.1 2011/01/18 05:49:19 finch Exp $
00002 
00003 #include <sstream>
00004 
00005 
00006 
00007 #include "TDbiConfigStream.hxx"
00008 #include "TDbiFieldType.hxx"
00009 #include "TDbiTableProxy.hxx"
00010 #include "TDbiDatabaseManager.hxx"
00011 #include "TDbiWriter.hxx"
00012 #include <TSK_DBI_Log.hxx>
00013 #include <MsgFormat.h>
00014 #include "TDbiRegistry.hxx"
00015 #include "TDbiCfg.hxx"
00016 #include "TDbiCascader.hxx"
00017 using std::endl;
00018 ClassImp(TDbiConfigStream)
00019 
00020 //   Definition of static data members
00021 //   *********************************
00022 
00023 
00024 TVldContext  TDbiConfigStream::fgDefaultContext(DbiDetector::kNear,
00025                                   DbiSimFlag::kData,
00026                                   TVldTimeStamp() );
00027 
00028 //    Definition of all member functions (static or otherwise)
00029 //    *******************************************************
00030 //
00031 //    -  ordered: ctors, dtor, operators then in alphabetical order.
00032 
00033 //.....................................................................
00034 
00035 TDbiConfigStream::TDbiConfigStream() :
00036 fCFSet(0)
00037 {
00038 //
00039 //
00040 //  Purpose:  Default constructor
00041 //
00042 //  Arguments: None.
00043 //
00044 //  Return:    n/a
00045 //
00046 //  Contact:   N. West
00047 //
00048 //  Specification:-
00049 //  =============
00050 //
00051 //  o Create dummy ConfigStream.
00052 
00053 
00054 //  Program Notes:-
00055 //  =============
00056 
00057 //  None.
00058 
00059 
00060   SK_DBI_Debug( "Creating TDbiConfigStream" << "  ");
00061 }
00062 
00063 //.....................................................................
00064 
00065 TDbiConfigStream::TDbiConfigStream(const string& SoftName,
00066                                  const std::string& ConfigName,
00067                                  TVldContext vc,
00068                                  TDbi::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 //  Purpose:  Standard constructor
00078 //
00079 //  Arguments:
00080 //    SoftwName    in   Name of the software system to be configured
00081 //    ConfigName   in   Name of the configuration set.
00082 //                        Default: "default"
00083 //    vc           in   Context.
00084 //                        Default: TDbi::fgDefaultContext.
00085 //    task         in   The task of the configuration.
00086 //                        Default: 0
00087 //    tableName    in   Name of configuration data table.
00088 //                        Default: "SOFTWARE_CONFIGURATION"
00089 //
00090 //  Return:    n/a
00091 //
00092 //  Contact:   N. West
00093 //
00094 //  Specification:-
00095 //  =============
00096 //
00097 //  o Create ConfigStream from specified table.
00098 
00099 
00100 //  Program Notes:-
00101 //  =============
00102 
00103 //  None.
00104 
00105 
00106   SK_DBI_Debug( "Creating TDbiConfigStream" << "  ");
00107 
00108   // Search for row matching software and configuration names.
00109   int rowNum = fCFSetTable.GetNumRows()-1;
00110   while ( rowNum >= 0 ) {
00111     fCFSet = fCFSetTable.GetRow(rowNum);
00112     if (    fCFSet->GetParamValue(0) == fSoftwName
00113          && fCFSet->GetParamValue(1) == fConfigName ) {
00114       fVRec = *fCFSetTable.GetValidityRec(fCFSet);
00115       SK_DBI_Log( "TDbiConfigStream for " << fSoftwName
00116                                  << "," << fConfigName
00117                                  << " has validity rec: " << fVRec
00118                                  << " and aggregate no.: " << fCFSet->GetAggregateNo()
00119                                  << "  ");
00120       return;
00121     }
00122     --rowNum;
00123   }
00124 
00125   // Cannot find matching row, leave configuration data as null
00126   // and set up a validity rec that can be used if creating a
00127   // new row.
00128 
00129   fCFSet = 0;
00130 
00131   fVRec.SetDbNo(0);
00132   fVRec.SetTableProxy(&TDbiResultSetHandle<TDbiConfigSet>::GetTableProxy(tableName));
00133   TVldTimeStamp start(1970,1,1,0,0,0);
00134   TVldTimeStamp   end(2038,1,1,0,0,0);
00135   TVldRange vr(127,127,start,end,"TDbiConfigStream");
00136   fVRec.SetVldRange(vr);
00137   SK_DBI_Log( "TDbiConfigStream for " << fSoftwName
00138                              << "," << fConfigName
00139                              << " has no existing entry; creating validity rec: " << fVRec << "  ");
00140 
00141 }
00142 
00143 //.....................................................................
00144 
00145 TDbiConfigStream::~TDbiConfigStream() {
00146 //
00147 //
00148 //  Purpose: Destructor
00149 //
00150 //  Arguments:
00151 //    None.
00152 //
00153 //  Return:    n/a
00154 //
00155 //  Contact:   N. West
00156 //
00157 //  Specification:-
00158 //  =============
00159 //
00160 //  o  Destroy ConfigStream.
00161 
00162 
00163 //  Program Notes:-
00164 //  =============
00165 
00166 //  None.
00167 
00168 
00169   SK_DBI_Debug( "Destroying TDbiConfigStream" << "  ");
00170 
00171 }
00172 
00173 //.....................................................................
00174 
00175 
00176   ostream& operator<<(ostream& os, const TDbiConfigStream& cfStream) {
00177 
00178 //
00179 //
00180 //  Purpose:  Output status of TDbiConfigStream on ostream.
00181 //
00182 //  Arguments:
00183 //    os           in    ostream to be outout
00184 //    cfStream     in    TDbiConfigStream whise status is to be output.
00185 //
00186 //  Return:    Updated ostream
00187 //
00188 //  Contact:   N. West
00189 //
00190 //  Specification:-
00191 //  =============
00192 //
00193 //  o Output status of TDbiConfigStream on ostream.
00194 
00195 //  Program Notes:-
00196 //  =============
00197 
00198 //  None.
00199 
00200   const TDbiConfigSet* cfSet = cfStream.GetConfigSet();
00201 
00202   if ( cfSet )  os << "TDbiConfigSet contains: " << *cfSet << endl;
00203   else          os << "TDbiConfigSet is empty! " << endl;
00204   return os;
00205 }
00206 
00207 //.....................................................................
00208 
00209 const TDbiConfigStream& TDbiConfigStream::operator>>(TDbiRegistry* reg) {
00210 //
00211 //
00212 //  Purpose:  Stream configuration data into TDbiRegistry object.
00213 //
00214 //  Arguments:
00215 //    reg          in    Empty TDbiRegistry object (contents ignored)
00216 //                 out   Filled TDbiRegistry object.
00217 //
00218 //  Return:    Original TDbiConfigStream object.
00219 //
00220 //  Contact:   N. West
00221 //
00222 //  Specification:-
00223 //  =============
00224 //
00225 //  o Erase the contents of reg and refill from the TDbiConfigSet
00226 //    owned by this TDbiConfigStream.
00227 
00228 //  Program Notes:-
00229 //  =============
00230 
00231 //  None.
00232 
00233   if ( ! reg ) return *this;
00234 
00235 //  Record the current state of reg and then clear it.
00236 
00237   Bool_t keysLocked   = reg->KeysLocked();
00238   Bool_t valuesLocked = reg->ValuesLocked();
00239   reg->UnLockKeys();
00240   reg->UnLockValues();
00241   reg->Clear();
00242 
00243 //  Use the owned TDbiConfigSet to fill reg.
00244 
00245   if ( fCFSet ) {
00246     UInt_t numParams = fCFSet->GetNumParams();
00247 
00248 //  Handle configuration tables.
00249 
00250     if ( numParams == 3 && fCFSet->GetParamName(2) == "CONFIG_DATA" ) {
00251       istringstream is(fCFSet->GetParamValue(2));
00252       reg->ReadStream(is);
00253     }
00254     else {
00255          SK_DBI_Severe( "Attempting to fill TDbiRegistry  from a table with "
00256                              << numParams << " columns (should be 3) using column named "
00257                              << fCFSet->GetParamName(2) << " (should be CONFIG_DATA)." << "  ");
00258     }
00259   }
00260   if ( keysLocked   ) reg->LockKeys();
00261   if ( valuesLocked ) reg->LockValues();
00262   return *this;
00263 }
00264 //.....................................................................
00265 
00266 TDbiConfigStream& TDbiConfigStream::operator<<(const TDbiRegistry* reg) {
00267 //
00268 //
00269 //  Purpose:  Stream configuration data from TDbiRegistry object.
00270 //
00271 //  Arguments:
00272 //
00273 //  Contact:   N. West
00274 //
00275 //  Specification:-
00276 //  =============
00277 //
00278 //  o Refill internal fCFSetModified from reg..
00279 
00280 //  Program Notes:-
00281 //  =============
00282 
00283 //  This does NOT write to the database.  To do that first use this
00284 //  method to refill the configuration and then call the Write method.
00285 
00286   if ( fSoftwName == "" ) {
00287        SK_DBI_Severe( "Cannot fill (<<): No software name defined." << "  ");
00288     return *this;
00289   }
00290 
00291   TDbiFieldType stringType(TDbi::kString);
00292 
00293   ostringstream os;
00294   reg->PrintStream(os);
00295   fCFSetModified.Clear();
00296   fCFSetModified.PushBack("SOFTW_NAME", fSoftwName,  stringType);
00297   fCFSetModified.PushBack("CONFIG_NAME",fConfigName, stringType);
00298   fCFSetModified.PushBack("CONFIG_DATA",os.str(),    stringType);
00299   if ( fVRec.GetAggregateNo() > 0 ) fCFSetModified.SetAggregateNo( fVRec.GetAggregateNo());
00300   fCFSet =  &fCFSetModified;
00301   return *this;
00302 
00303 }
00304 
00305 
00306 //.....................................................................
00307 
00308 Bool_t TDbiConfigStream::Write(UInt_t dbNo,
00309                               const std::string& logComment,
00310                               Bool_t localTest ) {
00311 //
00312 //
00313 //  Purpose:  Write configuration data to the database.
00314 //
00315 //  Arguments:
00316 //    dbNo         in    Database number in cascade (starting at 0).
00317 //                         Default: 0.
00318 //    logComment   in    Reason for update.
00319 //                         Default: "".
00320 //    localTest    in    Set true to use local SEQNOs (doesn't require authorising DB).
00321 //                         Default: false.
00322 //
00323 //  Return:        True if I/O successful.
00324 
00325   if ( ! fCFSet ) {
00326        SK_DBI_Severe( "No configuration data to write out." << "  ");
00327     return false;
00328   }
00329 
00330   // If no aggregate number has been asigned so far, but fCFSet non-zero, then must
00331   // be creating a new software/config combination with the data in fCFSetModified.
00332   // Use a global seqno number (or local if localTest) to define a unique aggregate number.
00333   int requireGlobal = localTest ? -1 : 1;
00334   if ( fVRec.GetAggregateNo() < 0 ) {
00335     TDbiCascader& cas = TDbiDatabaseManager::Instance().GetCascader();
00336     Int_t aggNo = cas.AllocateSeqNo(fVRec.GetTableProxy()->GetTableName(),requireGlobal,dbNo);
00337     if ( aggNo <= TDbi::kMAXLOCALSEQNO && ! localTest ) {
00338          SK_DBI_Severe( "Cannot write out configuration data: no authorising entry in cascade." << "  ");
00339       return false;
00340     }
00341     fVRec.SetAggregateNo(aggNo);
00342     fCFSetModified.SetAggregateNo(aggNo);
00343     SK_DBI_Debug( "Aggregate number: " << aggNo
00344                       << " allocated to entry " << fSoftwName
00345                       << "," << fConfigName
00346                       << " in table " << fVRec.GetTableProxy()->GetTableName() << "  ");
00347   }
00348   TDbiWriter<TDbiConfigSet> writer(fVRec,dbNo,logComment);
00349   writer.SetRequireGlobalSeqno(requireGlobal);
00350   writer.SetOverlayCreationDate();
00351   writer << *fCFSet;
00352   return writer.Close();
00353 
00354 }
00355 
00356 
00357 

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1