CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/CondCore/ORA/src/MappingTree.cc

Go to the documentation of this file.
00001 #include "MappingTree.h"
00002 #include "CondCore/ORA/interface/Exception.h"
00003 // externals
00004 #include "CoralBase/AttributeSpecification.h"
00005 #include "CoralBase/Blob.h"
00006 
00007 ora::MappingTree::MappingTree():
00008   m_version( "" ),
00009   m_element(),
00010   m_parentTable(){
00011 }
00012 
00013 ora::MappingTree::MappingTree( const std::string& version ):
00014   m_version( version ),
00015   m_element(),
00016   m_parentTable(){
00017 }
00018 
00019 ora::MappingTree::MappingTree( const MappingTree& rhs ):
00020   m_version( rhs.m_version ),
00021   m_element( rhs.m_element ),
00022   m_parentTable(){
00023   if( rhs.m_parentTable.get()) m_parentTable.reset( new TableInfo( *rhs.m_parentTable ) );
00024 }
00025 
00026 ora::MappingTree::~MappingTree(){
00027 }
00028 
00029 ora::MappingTree& ora::MappingTree::operator=( const MappingTree& rhs ){
00030   if( this != &rhs ){
00031     m_version = rhs.m_version;
00032     m_element = rhs.m_element;
00033     m_parentTable.reset();
00034     if( rhs.m_parentTable.get()) m_parentTable.reset( new TableInfo( *rhs.m_parentTable ) );
00035   }
00036   return *this;
00037 }
00038 
00039 ora::MappingElement&
00040 ora::MappingTree::setTopElement( const std::string& className,
00041                                  const std::string& tableName,
00042                                  bool isDependency  ){
00043   std::string elementType = ora::MappingElement::classMappingElementType();
00044   if( isDependency ) elementType = ora::MappingElement::dependencyMappingElementType();
00045   m_element = ora::MappingElement( elementType,
00046                                    className,
00047                                    className,
00048                                    tableName );
00049   return m_element;
00050 }
00051 
00052 void ora::MappingTree::setDependency( const MappingTree& parentTree ){
00053   m_parentTable.reset( new TableInfo() );
00054   m_parentTable->m_tableName = parentTree.m_element.tableName();
00055   m_parentTable->m_idColumns = parentTree.m_element.columnNames();
00056 }
00057 
00058 void ora::MappingTree::override(const MappingTree& source)
00059 {
00060   if( className() == source.className() ) m_element.override( source.m_element );
00061 }
00062 
00063 namespace ora {
00064   void scanElement( const MappingElement& element,
00065                     const TableInfo& currentTable,
00066                     bool isDependency,
00067                     std::vector<std::string>& tableHierarchy,
00068                     std::map<std::string,TableInfo>& tableMap ){
00069     const std::string& tName = element.tableName();
00070     std::map<std::string,TableInfo>::iterator iT = tableMap.find( tName );
00071     if( iT == tableMap.end() ){
00072       tableHierarchy.push_back( tName );
00073       iT = tableMap.insert( std::make_pair( tName, TableInfo() ) ).first;
00074       iT->second.m_dependency = isDependency;
00075       iT->second.m_tableName = tName;
00076       iT->second.m_idColumns = element.columnNames();
00077       iT->second.m_parentTableName = currentTable.m_tableName;
00078       iT->second.m_refColumns = currentTable.m_idColumns;
00079     } else {
00080       const std::vector<std::string>& dataCols = element.columnNames();
00081       MappingElement::ElementType elementType = element.elementType();
00082       switch ( elementType ) {
00083         case MappingElement::Primitive :
00084           iT->second.m_dataColumns.insert( std::make_pair( dataCols[0],
00085                                                            element.variableType() ));
00086           break;
00087         case MappingElement::Blob :
00088           iT->second.m_dataColumns.insert( std::make_pair( dataCols[0],
00089                                                            coral::AttributeSpecification::typeNameForId( typeid(coral::Blob) ) ));
00090           iT->second.m_dataColumns.insert( std::make_pair( dataCols[1],
00091                                                            coral::AttributeSpecification::typeNameForId( typeid(std::string) ) ));
00092           iT->second.m_nullableColumns.insert( dataCols[1] );
00093           break;
00094         case MappingElement::OraReference :
00095           iT->second.m_dataColumns.insert( std::make_pair( dataCols[0],
00096                                                            coral::AttributeSpecification::typeNameForId( typeid(int) ) ));
00097           iT->second.m_dataColumns.insert( std::make_pair( dataCols[1],
00098                                                            coral::AttributeSpecification::typeNameForId( typeid(int) ) ));
00099           break;
00100         case MappingElement::NamedReference :
00101           iT->second.m_dataColumns.insert( std::make_pair( dataCols[0],
00102                                                            coral::AttributeSpecification::typeNameForId( typeid(std::string) ) ));   
00103           break;
00104         case MappingElement::UniqueReference :
00105           iT->second.m_dataColumns.insert( std::make_pair( dataCols[0],
00106                                                            coral::AttributeSpecification::typeNameForId( typeid(std::string) ) ));
00107           iT->second.m_dataColumns.insert( std::make_pair( dataCols[1],
00108                                                            coral::AttributeSpecification::typeNameForId( typeid(int) ) ));
00109           break;
00110         default:
00111           break;
00112       }
00113     }
00114     TableInfo currT = iT->second;
00115     for( MappingElement::const_iterator iM = element.begin();
00116          iM != element.end(); ++iM ){
00117       scanElement( iM->second, currT, isDependency, tableHierarchy, tableMap );
00118     }
00119   }
00120   
00121 }
00122 
00123 std::vector<ora::TableInfo> ora::MappingTree::tables() const {
00124   std::vector<std::string> tableHierarchy;
00125   std::map<std::string,TableInfo> tableMap;
00126   TableInfo mainTable;
00127   bool isDependency = false;
00128   if( m_parentTable.get() ){
00129     isDependency = true;
00130     mainTable = *m_parentTable;
00131   }
00132   scanElement( m_element, mainTable, isDependency, tableHierarchy, tableMap );
00133   std::vector<TableInfo> ret;
00134   for( std::vector<std::string>::const_iterator iT = tableHierarchy.begin();
00135        iT != tableHierarchy.end(); ++iT ){
00136     std::map<std::string,TableInfo>::const_iterator iM = tableMap.find( *iT );
00137     ret.push_back( iM->second );
00138   }
00139   return ret;
00140 }
00141 
00142 void ora::MappingTree::printXML( std::ostream& outputStream ) const {
00143   outputStream << "<?xml version=\'1.0\' encoding=\"UTF-8\"?>" << std::endl;
00144   outputStream << "<!DOCTYPE OraDatabase SYSTEM \"InMemory\">" << std::endl;
00145   outputStream << " <OraDatabase>" << std::endl;
00146   outputStream << "  <Mapping version=\""<<m_version<<"\" >"<< std::endl;
00147   m_element.printXML( outputStream, "   " );
00148   outputStream << "  </Mapping >"<< std::endl;
00149   outputStream << " </OraDatabase>" << std::endl;
00150 }