CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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           break;
00091         case MappingElement::OraReference :
00092           iT->second.m_dataColumns.insert( std::make_pair( dataCols[0],
00093                                                            coral::AttributeSpecification::typeNameForId( typeid(int) ) ));
00094           iT->second.m_dataColumns.insert( std::make_pair( dataCols[1],
00095                                                            coral::AttributeSpecification::typeNameForId( typeid(int) ) ));
00096           break;
00097         case MappingElement::NamedReference :
00098           iT->second.m_dataColumns.insert( std::make_pair( dataCols[0],
00099                                                            coral::AttributeSpecification::typeNameForId( typeid(std::string) ) ));   
00100           break;
00101         case MappingElement::UniqueReference :
00102           iT->second.m_dataColumns.insert( std::make_pair( dataCols[0],
00103                                                            coral::AttributeSpecification::typeNameForId( typeid(std::string) ) ));
00104           iT->second.m_dataColumns.insert( std::make_pair( dataCols[1],
00105                                                            coral::AttributeSpecification::typeNameForId( typeid(int) ) ));
00106           break;
00107         default:
00108           break;
00109       }
00110     }
00111     TableInfo currT = iT->second;
00112     for( MappingElement::const_iterator iM = element.begin();
00113          iM != element.end(); ++iM ){
00114       scanElement( iM->second, currT, isDependency, tableHierarchy, tableMap );
00115     }
00116   }
00117   
00118 }
00119 
00120 std::vector<ora::TableInfo> ora::MappingTree::tables() const {
00121   std::vector<std::string> tableHierarchy;
00122   std::map<std::string,TableInfo> tableMap;
00123   TableInfo mainTable;
00124   bool isDependency = false;
00125   if( m_parentTable.get() ){
00126     isDependency = true;
00127     mainTable = *m_parentTable;
00128   }
00129   scanElement( m_element, mainTable, isDependency, tableHierarchy, tableMap );
00130   std::vector<TableInfo> ret;
00131   for( std::vector<std::string>::const_iterator iT = tableHierarchy.begin();
00132        iT != tableHierarchy.end(); ++iT ){
00133     std::map<std::string,TableInfo>::const_iterator iM = tableMap.find( *iT );
00134     ret.push_back( iM->second );
00135   }
00136   return ret;
00137 }
00138 
00139 void ora::MappingTree::printXML( std::ostream& outputStream ) const {
00140   outputStream << "<?xml version=\'1.0\' encoding=\"UTF-8\"?>" << std::endl;
00141   outputStream << "<!DOCTYPE OraDatabase SYSTEM \"InMemory\">" << std::endl;
00142   outputStream << " <OraDatabase>" << std::endl;
00143   outputStream << "  <Mapping version=\""<<m_version<<"\" >"<< std::endl;
00144   m_element.printXML( outputStream, "   " );
00145   outputStream << "  </Mapping >"<< std::endl;
00146   outputStream << " </OraDatabase>" << std::endl;
00147 }