// // (C) Copyright 2003-2009 by Autodesk, Inc. // // Permission to use, copy, modify, and distribute this software in // object code form for any purpose and without fee is hereby granted, // provided that the above copyright notice appears in all copies and // that both that copyright notice and the limited warranty and // restricted rights notice below appear in all supporting // documentation. // // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. // // Use, duplication, or disclosure by the U.S. Government is subject to // restrictions set forth in FAR 52.227-19 (Commercial Computer // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable. // using System; using System.Collections.Generic; using System.Text; namespace Revit.SDK.Samples.RDBLink.CS { /// /// Preserves sorted tables by their relations /// class TableLevelSet : List { #region Fields /// /// TableInfoSet which contains all database information /// TableInfoSet m_tableInfoSet; #endregion #region Constructors /// /// Initialize TableInfoSet and create the list of TableLevel /// /// TableInfoSet public TableLevelSet(TableInfoSet tableInfoSet) : base() { m_tableInfoSet = tableInfoSet; //create the list of TableLevel foreach (string key in m_tableInfoSet.Keys) { this.Add(new TableLevel(key, GetLevel(key))); } //sort the list this.Sort(); } #endregion #region Methods /// /// Get the level of a table which indicates the position in the /// relation hierarchy of all tables /// /// Table id /// Level of the table private int GetLevel(string tableId) { int maxlevel = 0; TableInfo ti = m_tableInfoSet[tableId]; if (ti.ForeignKeys.Count > 0) maxlevel += 1; foreach (ForeignKey fKey in ti.ForeignKeys) { int level = GetLevel(fKey.RefTableId) + 1; maxlevel = level > maxlevel ? level : maxlevel; } return maxlevel; } #endregion }; /// /// Wrapper the table id and its level which is used to sort all tables /// class TableLevel : IComparable { #region Fields /// /// Table id /// string m_tableId; /// /// Table level /// int m_level; #endregion #region Properties /// /// Gets table id /// public string TableId { get { return m_tableId; } //set { m_tableId = value; } } /// /// Gets table level /// public int Level { get { return m_level; } //set { m_level = value; } } #endregion #region Constructors /// /// Initialize Table id and level /// /// Table id /// Table level public TableLevel(string tableId, int level) { m_tableId = tableId; m_level = level; } #endregion #region Methods /// /// Compare two TableLevel object /// /// TableLevel to compare /// zero if this table's level is equal to the other, /// bigger then zero if this table's level is bigger than the other, /// bigger then zero if this table's level is smaller than the other public int CompareTo(object obj) { TableLevel tl = obj as TableLevel; if (tl != null) { return this.Level.CompareTo(tl.Level); } return -1; } #endregion }; }