簡述:
Portal的核心功能就是Components文件夾中的類(Configuration組件,及AnnouncementsDB,ContactsDB,DocumentDB等...);它相當於多層結構中的Model層.
其中Configuration組件又是這個核心的核心,它用於對網站的配置做操作.它分成三大部分.
1.Configuration類,它用於操作一個配置站點用的SiteConfiguration(繼承自DataSet)類型的SiteSetting,主要是對四個表(Global,Tabs,Modules,ModuleDefinitions)的操作以及對整個數據集資料的獲取,因為對它們的操作,其實都是對同一個xml文件的操作,所在放在同一個類中應該比較方便.
2.SiteSetting中有多個門戶PortalSettings,每個PortalSettings有由多個TabSettings組成,每個TabSettings又包含有多個ModuleSettings.所以定義了3個類(PortalSettings,TabSettings,ModuleSettings);它們是依次嵌套的.
下面是它們的字段:
{
public int PortalId; // 門戶站點Id
public String PortalName; // 門戶站點名稱
public bool AlwaysShowEditButton; // 是否顯示編輯按鈕
public ArrayList DesktopTabs = new ArrayList(); // 門戶站點“桌面瀏覽器”顯示部分的導航欄標簽的ArrayList
public ArrayList MobileTabs = new ArrayList();
public TabSettings ActiveTab = new TabSettings(); // 當前標簽
// ...public PortalSettings(int tabIndex, int tabId)
}
public class TabSettings
{
public int TabIndex; // 標簽索引
public int TabId; // 標簽Id
public String TabName; // 標簽名稱
public int TabOrder; // 標簽排序號
public String MobileTabName; // “移動設備瀏覽器”上顯示的標簽名稱
public String AuthorizedRoles; // 可訪問角色
public bool ShowMobile; // 是否在“移動設備瀏覽器”上顯示
public ArrayList Modules = new ArrayList(); // 當前標簽下的模塊對像list
}
public class ModuleSettings : IComparable
{
public int ModuleId; // 模塊Id
public int ModuleDefId; // 用於定義該模塊的原始模板ID
public int TabId; // 隸屬標簽Id
public int CacheTime; // 緩存時間
public int ModuleOrder; // 模塊序號
public String PaneName; // 顯示在那個板塊中(左中右)
public String ModuleTitle; // 模塊標題
public String AuthorizedEditRoles; // 可編輯角色
public bool ShowMobile; // 是否在移動瀏覽器上顯示
public String DesktopSrc; // 桌面源
public String MobileSrc; // 移動源
// ....public int CompareTo(object value)
}
3.因為每次在頁面中只顯示一個TabSettings的內容,所以要有一個導航條,為管理導航條,又定了一個類TabStripDetails;
同時為了可以配置每個TabSettings及ModuleSettings的順序,又定義了兩個類TabItem,ModuleItem,並且都實現了CompareTo接口;
以上三個類都比較簡單,只保留了必要的屬性字段.
{
public int TabId; // 標簽Id
public String TabName; // 標簽名稱
public int TabOrder; // 標簽排序號
public String AuthorizedRoles; // 可訪問角色
}
public class TabItem : IComparable
{
private int _tabOrder;
private String _name;
private int _id;
// ...屬性字段定義
// ...public int CompareTo(object value)
}
public class ModuleItem : IComparable
{
private int _moduleOrder; // 模塊序號
private String _title; // 模塊名稱
private String _pane; // 放置模塊的面板名稱
private int _id; // 模塊ID
private int _defId; // 定義該模塊的模板ID
// ...屬性字段定義
// ...public int CompareTo(object value)
}
附Configuration類的源代碼:
{
//
// GLOBAL
//
#region 更新站點設置信息
//
// PORTAL
//
// *********************************************************************
//
// UpdatePortalInfo() Method <a name="UpdatePortalInfo"></a>
//
// The UpdatePortalInfo method updates the name and access settings for the portal.
// These settings are stored in the Xml file PortalCfg.xml.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
/// <summary>
/// 更新站點設置信息
/// </summary>
/// <param name="portalId"> 站點Id </param>
/// <param name="portalName"> 站點名稱 </param>
/// <param name="alwaysShow"> 是否總是顯示編輯按鈕 </param>
public void UpdatePortalInfo ( int portalId, String portalName, bool alwaysShow)
{
// Obtain SiteSettings from Current Context
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Get first record of the "Global" element
// 找到當前站點的設置信息行
SiteConfiguration.GlobalRow globalRow = siteSettings.Global.FindByPortalId(portalId);
// Update the values
// 更新
globalRow.PortalId = portalId;
globalRow.PortalName = portalName;
globalRow.AlwaysShowEditButton = alwaysShow;
// Save the changes
// 保存設置
SaveSiteSettings();
}
#endregion
//
// TABS
//
#region 添加新的標簽信息
// *********************************************************************
//
// AddTab Method <a name="AddTab"></a>
//
// The AddTab method adds a new tab to the portal. These settings are
// stored in the Xml file PortalCfg.xml.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
/// <summary>
/// 添加新的標簽信息
/// </summary>
/// <param name="portalId"> 門戶站點Id </param>
/// <param name="tabName"> 新標簽名稱 </param>
/// <param name="tabOrder"> 新標簽位置 </param>
/// <returns> 新標簽的Id </returns>
public int AddTab ( int portalId, String tabName, int tabOrder)
{
// Obtain SiteSettings from Current Context
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Create a new TabRow from the Tab table
// 創建標簽行
SiteConfiguration.TabRow newRow = siteSettings.Tab.NewTabRow();
// Set the properties on the new row
// 設置新的標簽行
newRow.TabName = tabName;
newRow.TabOrder = tabOrder;
newRow.MobileTabName = String.Empty;
newRow.ShowMobile = true ;
newRow.AccessRoles = " All Users; " ;
// Add the new TabRow to the Tab table
// 添加到Tab表中
siteSettings.Tab.AddTabRow(newRow);
// Save the changes
SaveSiteSettings();
// Return the new TabID
return newRow.TabId;
}
#endregion
#region 更新標簽信息
// *********************************************************************
//
// UpdateTab Method <a name="UpdateTab"></a>
//
// The UpdateTab method updates the settings for the specified tab.
// These settings are stored in the Xml file PortalCfg.xml.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
/// <summary>
/// 更新標簽信息
/// </summary>
/// <param name="portalId"></param>
/// <param name="tabId"></param>
/// <param name="tabName"></param>
/// <param name="tabOrder"></param>
/// <param name="authorizedRoles"></param>
/// <param name="mobileTabName"></param>
/// <param name="showMobile"></param>
public void UpdateTab ( int portalId, int tabId, String tabName, int tabOrder, String authorizedRoles, String mobileTabName, bool showMobile) {
// Obtain SiteSettings from Current Context
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Find the appropriate tab in the Tab table and set the properties
SiteConfiguration.TabRow tabRow = siteSettings.Tab.FindByTabId(tabId);
tabRow.TabName = tabName;
tabRow.TabOrder = tabOrder;
tabRow.AccessRoles = authorizedRoles;
tabRow.MobileTabName = mobileTabName;
tabRow.ShowMobile = showMobile;
// Save the changes
SaveSiteSettings();
}
#endregion
#region 更新標簽排序號
// *********************************************************************
//
// UpdateTabOrder Method <a name="UpdateTabOrder"></a>
//
// The UpdateTabOrder method changes the position of the tab with respect
// to other tabs in the portal. These settings are stored in the Xml
// file PortalCfg.xml.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
/// <summary>
/// 更新標簽排序號
/// </summary>
/// <param name="tabId"></param>
/// <param name="tabOrder"></param>
public void UpdateTabOrder ( int tabId, int tabOrder)
{
// Obtain SiteSettings from Current Context
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Find the appropriate tab in the Tab table and set the property
SiteConfiguration.TabRow tabRow = siteSettings.Tab.FindByTabId(tabId);
tabRow.TabOrder = tabOrder;
// Save the changes
SaveSiteSettings();
}
#endregion
#region 刪除標簽信息
// *********************************************************************
//
// DeleteTab Method <a name="DeleteTab"></a>
//
// The DeleteTab method deletes the selected tab and its modules from
// the settings which are stored in the Xml file PortalCfg.xml. This
// method also deletes any data from the database associated with all
// modules within this tab.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
// + <a href="DeleteModule.htm" style="color:green">DeleteModule stored procedure</a>
//
// *********************************************************************
/// <summary>
/// 刪除標簽信息
/// </summary>
/// <param name="tabId"></param>
public void DeleteTab( int tabId)
{
//
// Delete the Tab in the XML file
// 刪除XML文件中標簽相關信息
// Obtain SiteSettings from Current Context
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Find the appropriate tab in the Tab table
SiteConfiguration.TabDataTable tabTable = siteSettings.Tab;
SiteConfiguration.TabRow tabRow = siteSettings.Tab.FindByTabId(tabId);
//
// Delete information in the Database relating to each Module being deleted
// 刪除數據庫中該標簽相關的信息
// Create Instance of Connection and Command Object
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings[ " connectionString " ]);
SqlCommand myCommand = new SqlCommand( " Portal_DeleteModule " , myConnection);
// Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure;
// Add Parameters to SPROC
SqlParameter parameterModuleID = new SqlParameter( " @ModuleID " , SqlDbType.Int, 4 );
myConnection.Open();
foreach (SiteConfiguration.ModuleRow moduleRow in tabRow.GetModuleRows())
{
myCommand.Parameters.Clear();
parameterModuleID.Value = moduleRow.ModuleId;
myCommand.Parameters.Add(parameterModuleID);
// Open the database connection and execute the command
myCommand.ExecuteNonQuery();
}
// Close the connection
myConnection.Close();
// Finish removing the Tab row from the Xml file
// 從Xml文件中移出
tabTable.RemoveTabRow(tabRow);
// Save the changes
SaveSiteSettings();
}
#endregion
//
// MODULES
//
#region 修改模塊排序號
// *********************************************************************
//
// UpdateModuleOrder Method <a name="UpdateModuleOrder"></a>
//
// The UpdateModuleOrder method updates the order in which the modules
// in a tab are displayed. These settings are stored in the Xml file
// PortalCfg.xml.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
/// <summary>
/// 修改模塊排序號
/// </summary>
/// <param name="ModuleId"></param>
/// <param name="ModuleOrder"></param>
/// <param name="pane"></param>
public void UpdateModuleOrder ( int ModuleId, int ModuleOrder, String pane)
{
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// 找到模塊行的設置
SiteConfiguration.ModuleRow moduleRow = siteSettings.Module.FindByModuleId(ModuleId);
// 賦新值
moduleRow.ModuleOrder = ModuleOrder;
moduleRow.PaneName = pane;
// Save the changes
SaveSiteSettings();
}
#endregion
#region 添加新的模塊
// *********************************************************************
//
// AddModule Method <a name="AddModule"></a>
//
// The AddModule method adds Portal Settings for a new Module within
// a Tab. These settings are stored in the Xml file PortalCfg.xml.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
/// <summary>
/// 添加新的模塊
/// </summary>
/// <param name="tabId"> 所屬標簽Id </param>
/// <param name="moduleOrder"> 模塊排序號 </param>
/// <param name="paneName"> 顯示在那個框架中 </param>
/// <param name="title"> 標題 </param>
/// <param name="moduleDefId"> 模板Id </param>
/// <param name="cacheTime"> 緩存時間 </param>
/// <param name="editRoles"> 修改角色 </param>
/// <param name="showMobile"> 是否顯示在移動設備瀏覽器上 </param>
/// <returns> 新模塊Id </returns>
public int AddModule( int tabId, int moduleOrder, String paneName, String title, int moduleDefId, int cacheTime, String editRoles, bool showMobile)
{
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// 新建模塊行
SiteConfiguration.ModuleRow newModule = siteSettings.Module.NewModuleRow();
// 設置模塊行的值
newModule.ModuleDefId = moduleDefId;
newModule.ModuleOrder = moduleOrder;
newModule.ModuleTitle = title;
newModule.PaneName = paneName;
newModule.EditRoles = editRoles;
newModule.CacheTimeout = cacheTime;
newModule.ShowMobile = showMobile;
newModule.TabRow = siteSettings.Tab.FindByTabId(tabId);
// 添加新的一行到設置的Module表中
siteSettings.Module.AddModuleRow(newModule);
// Save the changes
SaveSiteSettings();
// 返回新模塊If
return newModule.ModuleId;
}
#endregion
#region 更新模塊
// *********************************************************************
//
// UpdateModule Method <a name="UpdateModule"></a>
//
// The UpdateModule method updates the Portal Settings for an existing
// Module within a Tab. These settings are stored in the Xml file
// PortalCfg.xml.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
public int UpdateModule( int moduleId, int moduleOrder, String paneName, String title, int cacheTime, String editRoles, bool showMobile)
{
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Find the appropriate Module in the Module table and update the properties
SiteConfiguration.ModuleRow moduleRow = siteSettings.Module.FindByModuleId(moduleId);
moduleRow.ModuleOrder = moduleOrder;
moduleRow.ModuleTitle = title;
moduleRow.PaneName = paneName;
moduleRow.CacheTimeout = cacheTime;
moduleRow.EditRoles = editRoles;
moduleRow.ShowMobile = showMobile;
// Save the changes
SaveSiteSettings();
// Return the existing Module ID
return moduleId;
}
#endregion
#region 刪除模塊
// *********************************************************************
//
// DeleteModule Method <a name="DeleteModule"></a>
//
// The DeleteModule method deletes a specified Module from the settings
// stored in the Xml file PortalCfg.xml. This method also deletes any
// data from the database associated with this module.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
// + <a href="DeleteModule.htm" style="color:green">DeleteModule stored procedure</a>
//
// *********************************************************************
public void DeleteModule( int moduleId)
{
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
//
// Delete information in the Database relating to Module being deleted
//
// Create Instance of Connection and Command Object
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings[ " connectionString " ]);
SqlCommand myCommand = new SqlCommand( " Portal_DeleteModule " , myConnection);
// Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure;
// Add Parameters to SPROC
SqlParameter parameterModuleID = new SqlParameter( " @ModuleID " , SqlDbType.Int, 4 );
myConnection.Open();
parameterModuleID.Value = moduleId;
myCommand.Parameters.Add(parameterModuleID);
// Open the database connection and execute the command
myCommand.ExecuteNonQuery();
myConnection.Close();
// Finish removing Module
siteSettings.Module.RemoveModuleRow(siteSettings.Module.FindByModuleId(moduleId));
// Save the changes
SaveSiteSettings();
}
#endregion
#region 更新模塊設置信息
// *********************************************************************
//
// UpdateModuleSetting Method <a name="UpdateModuleSetting"></a>
//
// The UpdateModuleSetting Method updates a single module setting
// in the configuration file. If the value passed in is String.Empty,
// the Setting element is deleted if it exists. If not, either a
// matching Setting element is updated, or a new Setting element is
// created.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
public void UpdateModuleSetting( int moduleId, String key, String val)
{
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Find the appropriate Module in the Module table
SiteConfiguration.ModuleRow moduleRow = siteSettings.Module.FindByModuleId(moduleId);
// Find the first (only) settings element
SiteConfiguration.SettingsRow settingsRow;
if (moduleRow.GetSettingsRows().Length > 0 )
{
settingsRow = moduleRow.GetSettingsRows()[ 0 ];
}
else
{
// Add new settings element
settingsRow = siteSettings.Settings.NewSettingsRow();
// Set the parent relationship
settingsRow.ModuleRow = moduleRow;
siteSettings.Settings.AddSettingsRow(settingsRow);
}
// Find the child setting elements
SiteConfiguration.SettingRow settingRow;
SiteConfiguration.SettingRow[] settingRows = settingsRow.GetSettingRows();
if (settingRows.Length == 0 )
{
// If there are no Setting elements at all, add one with the new name and value,
// but only if the value is not empty
if (val != String.Empty)
{
settingRow = siteSettings.Setting.NewSettingRow();
// Set the parent relationship and data
settingRow.SettingsRow = settingsRow;
settingRow.Name = key;
settingRow.Setting_Text = val;
siteSettings.Setting.AddSettingRow(settingRow);
}
}
else
{
// Update existing setting element if it matches
bool found = false ;
Int32 i;
// Find which row matches the input parameter "key" and update the
// value. If the value is String.Empty, however, delete the row.
for (i = 0 ; i < settingRows.Length; i ++ )
{
if (settingRows[i].Name == key)
{
if (val == String.Empty)
{
// Delete the row
siteSettings.Setting.RemoveSettingRow(settingRows[i]);
}
else
{
// Update the value
settingRows[i].Setting_Text = val;
}
found = true ;
}
}
if (found == false )
{
// Setting elements exist, however, there is no matching Setting element.
// Add one with new name and value, but only if the value is not empty
if (val != String.Empty)
{
settingRow = siteSettings.Setting.NewSettingRow();
// Set the parent relationship and data
settingRow.SettingsRow = settingsRow;
settingRow.Name = key;
settingRow.Setting_Text = val;
siteSettings.Setting.AddSettingRow(settingRow);
}
}
}
// Save the changes
SaveSiteSettings();
}
#endregion
#region 獲取模塊設置信息(以哈希表的形式返回)
// *********************************************************************
//
// GetModuleSettings Method <a name="GetModuleSettings"></a>
//
// The GetModuleSettings Method returns a hashtable of custom,
// module-specific settings from the configuration file. This method is
// used by some user control modules (Xml, Image, etc) to access misc
// settings.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
/// <summary>
/// 獲取模塊設置信息(以哈希表的形式返回)
/// </summary>
/// <param name="moduleId"></param>
/// <returns></returns>
public static Hashtable GetModuleSettings( int moduleId)
{
// Create a new Hashtable
Hashtable _settingsHT = new Hashtable();
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Find the appropriate Module in the Module table
SiteConfiguration.ModuleRow moduleRow = siteSettings.Module.FindByModuleId(moduleId);
// Find the first (only) settings element
if (moduleRow.GetSettingsRows().Length > 0 )
{
SiteConfiguration.SettingsRow settingsRow = moduleRow.GetSettingsRows()[ 0 ];
if (settingsRow != null )
{
// Find the child setting elements and add to the hashtable
// 將設置值添加到hashtable中,如:圖片地址,大小,XML/XSL的地址等
foreach (SiteConfiguration.SettingRow sRow in settingsRow.GetSettingRows())
{
_settingsHT[sRow.Name] = sRow.Setting_Text;
}
}
}
return _settingsHT;
}
#endregion
//
// MODULE DEFINITIONS
//
#region 取得模塊定義信息 ,以DataRow[]返回
// *********************************************************************
//
// GetModuleDefinitions() Method <a name="GetModuleDefinitions"></a>
//
// The GetModuleDefinitions method returns a list of all module type
// definitions for the portal.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
public DataRow[] GetModuleDefinitions( int portalId)
{
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Find the appropriate Module in the Module table
return siteSettings.ModuleDefinition.Select();
}
#endregion
#region 添加模塊定義
// *********************************************************************
//
// AddModuleDefinition() Method <a name="AddModuleDefinition"></a>
//
// The AddModuleDefinition add the definition for a new module type
// to the portal.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
public int AddModuleDefinition( int portalId, String name, String desktopSrc, String mobileSrc)
{
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Create new ModuleDefinitionRow
SiteConfiguration.ModuleDefinitionRow newModuleDef = siteSettings.ModuleDefinition.NewModuleDefinitionRow();
// Set the parameter values
newModuleDef.FriendlyName = name;
newModuleDef.DesktopSourceFile = desktopSrc;
newModuleDef.MobileSourceFile = mobileSrc;
// Add the new ModuleDefinitionRow to the ModuleDefinition table
siteSettings.ModuleDefinition.AddModuleDefinitionRow(newModuleDef);
// Save the changes
SaveSiteSettings();
// Return the new ModuleDefID
return newModuleDef.ModuleDefId;
}
#endregion
#region 刪除模塊定義
// *********************************************************************
//
// DeleteModuleDefinition() Method <a name="DeleteModuleDefinition"></a>
//
// The DeleteModuleDefinition method deletes the specified module type
// definition from the portal. Each module which is related to the
// ModuleDefinition is deleted from each tab in the configuration
// file, and all data relating to each module is deleted from the
// database.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
// + <a href="DeleteModule.htm" style="color:green">DeleteModule Stored Procedure</a>
//
// *********************************************************************
public void DeleteModuleDefinition( int defId)
{
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
//
// Delete information in the Database relating to each Module being deleted
//
// Create Instance of Connection and Command Object
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings[ " connectionString " ]);
SqlCommand myCommand = new SqlCommand( " Portal_DeleteModule " , myConnection);
// Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure;
// Add Parameters to SPROC
SqlParameter parameterModuleID = new SqlParameter( " @ModuleID " , SqlDbType.Int, 4 );
myConnection.Open();
foreach (SiteConfiguration.ModuleRow moduleRow in siteSettings.Module.Select())
{
if (moduleRow.ModuleDefId == defId)
{
myCommand.Parameters.Clear();
parameterModuleID.Value = moduleRow.ModuleId;
myCommand.Parameters.Add(parameterModuleID);
// Delete the xml module associated with the ModuleDef
// in the configuration file
siteSettings.Module.RemoveModuleRow(moduleRow);
// Open the database connection and execute the command
myCommand.ExecuteNonQuery();
}
}
myConnection.Close();
// Finish removing Module Definition
siteSettings.ModuleDefinition.RemoveModuleDefinitionRow(siteSettings.ModuleDefinition.FindByModuleDefId(defId));
// Save the changes
SaveSiteSettings();
}
#endregion
#region 更新模塊定義
// *********************************************************************
//
// UpdateModuleDefinition() Method <a name="UpdateModuleDefinition"></a>
//
// The UpdateModuleDefinition method updates the settings for the
// specified module type definition.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
public void UpdateModuleDefinition( int defId, String name, String desktopSrc, String mobileSrc)
{
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Find the appropriate Module in the Module table and update the properties
SiteConfiguration.ModuleDefinitionRow modDefRow = siteSettings.ModuleDefinition.FindByModuleDefId(defId);
modDefRow.FriendlyName = name;
modDefRow.DesktopSourceFile = desktopSrc;
modDefRow.MobileSourceFile = mobileSrc;
// Save the changes
SaveSiteSettings();
}
#endregion
#region 取得單個模塊定義
// *********************************************************************
//
// GetSingleModuleDefinition Method
//
// The GetSingleModuleDefinition method returns a ModuleDefinitionRow
// object containing details about a specific module definition in the
// configuration file.
//
// Other relevant sources:
// + <a href="#SaveSiteSettings" style="color:green">SaveSiteSettings() method</a>
// + <a href="PortalCfg.xml" style="color:green">PortalCfg.xml</a>
//
// *********************************************************************
public SiteConfiguration.ModuleDefinitionRow GetSingleModuleDefinition( int defId)
{
// 從HttpContext中獲取全局設置對像
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Find the appropriate Module in the Module table
return siteSettings.ModuleDefinition.FindByModuleDefId(defId);
}
#endregion
#region 獲取站點全局設置(以站點設置數據集的形式返回數據)
// *********************************************************************
//
// GetSiteSettings Static Method
//
// The Configuration.GetSiteSettings Method returns a typed
// dataset of the all of the site configuration settings from the
// XML configuration file. This method is used in Global.asax to
// push the settings into the current HttpContext, so that all of the
// pages, content modules and classes throughout the rest of the request
// may access them.
//
// The SiteConfiguration object is cached using the ASP.NET Cache API,
// with a file-change dependency on the XML configuration file. Normallly,
// this method just returns a copy of the object in the cache. When the
// configuration is updated and changes are saved to the the XML file,
// the SiteConfiguration object is evicted from the cache. The next time
// this method runs, it will read from the XML file again and insert a
// fresh copy of the SiteConfiguration into the cache.
//
// *********************************************************************
/// <summary>
/// 獲取站點全局設置(以站點設置數據集的形式返回數據)
/// </summary>
/// <returns></returns>
public static SiteConfiguration GetSiteSettings()
{
// 從緩存中獲取站點設置數據集
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Cache[ " SiteSettings " ];
// If the SiteConfiguration isn't cached, load it from the XML file and add it into the cache.
// 如果在緩存中沒有則從XML文件中讀取並寫入緩存
if (siteSettings == null )
{
// Create the dataset
// 創建站點設置數據集
siteSettings = new SiteConfiguration();
// Retrieve the location of the XML configuration file
// 找到全局設置文件的物理路徑
string configFile = HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings[ " configFile " ]);
// Set the AutoIncrement property to true for easier adding of rows
siteSettings.Tab.TabIdColumn.AutoIncrement = true ;
siteSettings.Module.ModuleIdColumn.AutoIncrement = true ;
siteSettings.ModuleDefinition.ModuleDefIdColumn.AutoIncrement = true ;
// Load the XML data into the DataSet
// 從XML文件中讀出數據填充數據集
siteSettings.ReadXml(configFile);
// Store the dataset in the cache
// 將數據集存儲到緩存中
HttpContext.Current.Cache.Insert( " SiteSettings " , siteSettings, new CacheDependency(configFile));
}
return siteSettings;
}
#endregion
#region 保存站點設置
// *********************************************************************
//
// SaveSiteSettings Method <a name="SaveSiteSettings"></a>
//
// The Configuration.SaveSiteSettings overwrites the the XML file with the
// settings in the SiteConfiguration object in context. The object will in
// turn be evicted from the cache and be reloaded from the XML file the next
// time GetSiteSettings() is called.
//
// *********************************************************************
/// <summary>
/// 保存站點設置
/// </summary>
public void SaveSiteSettings()
{
// Obtain SiteSettings from the Cache
// 原來的:從Cache中獲取站點設置信息數據集(好像是個Bug,因為每次更新數據是更新的HttpContext.Current.Items中的)
// SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Cache["SiteSettings"];
// 修改後的
SiteConfiguration siteSettings = (SiteConfiguration) HttpContext.Current.Items[ " SiteSettings " ];
// Check the object
// 如果Cache中沒有,則重新構建
if (siteSettings == null )
{
// If SaveSiteSettings() is called once, the cache is cleared. If it is
// then called again before Global.Application_BeginRequest is called,
// which reloads the cache, the siteSettings object will be Null
// 如果SaveSiteSettings()被調用過一次後,Cache就回被清除。如果它再一次被調用在Global.Application_BeginRequest前siteSettings為null則重新寫Cache
siteSettings = GetSiteSettings();
}
string configFile = HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings[ " configFile " ]);
// Object is evicted from the Cache here.
// 將變更後的數據集寫入到Xml文件
siteSettings.WriteXml(configFile);
}
#endregion
}