using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Globalization; using System.Xml; namespace SPSolutions.SharePoint.Alerts { public class SPAlertTemplateConfiguration { private List m_alertEventTypes; private List m_alertFilters; private List m_alertFrequencies; private SPAlertType m_alertType; private bool m_alwaysNotify; private SPAlertEventType m_defaultAlertEventType; private SPAlertFrequency m_defaultAlertFrequency; private DateTime m_defaultAlertFrequencyTime; private string m_defaultTitle; private List m_digestNotificationExludedFields; private string m_digestNotificationXml; private List m_immediateNotificationExludedFields; private string m_immediateNotificationXml; private bool m_showAlertEventTypes; private bool m_showAlertFilters; private bool m_showAlertFrequencies; private bool m_showAlertFrequencyTime; private XmlDocument m_xml; private bool m_isDirty; public bool IsDirty { get { return this.m_isDirty; } set { this.m_isDirty = value; } } public List AlertEventTypes { get { return this.m_alertEventTypes; } set { this.m_alertEventTypes = value; } } public List AlertFilters { get { return this.m_alertFilters; } set { this.m_alertFilters = value; } } public List AlertFrequencies { get { return this.m_alertFrequencies; } set { this.m_alertFrequencies = value; } } public SPAlertType AlertType { get { return this.m_alertType; } set { this.m_alertType = value; } } public bool AlwaysNotify { get { return this.m_alwaysNotify; } set { this.m_alwaysNotify = value; } } public SPAlertEventType DefaultAlertEventType { get { return this.m_defaultAlertEventType; } set { this.m_defaultAlertEventType = value; } } public SPAlertFrequency DefaultAlertFrequency { get { return this.m_defaultAlertFrequency; } set { if (this.m_defaultAlertFrequency != value) { this.m_defaultAlertFrequency = value; this.IsDirty = true; } } } public DateTime DefaultAlertFrequencyTime { get { return this.m_defaultAlertFrequencyTime; } set { this.m_defaultAlertFrequencyTime = value; } } public string DefaultTitle { get { return this.m_defaultTitle; } set { this.m_defaultTitle = value; } } public List DigestNotificationExcludedFields { get { return this.m_digestNotificationExludedFields; } set { this.m_digestNotificationExludedFields = value; } } public string DigestNotificaitonXml { get { return this.m_digestNotificationXml; } set { this.m_digestNotificationXml = value; } } public bool HasAlertEventTypes { get { return this.AlertEventTypes != null && this.AlertEventTypes.Count > 0; } } public bool HasAlertFilters { get { return this.AlertFilters != null && this.AlertFilters.Count > 0; } } public bool HasAlertFrequencies { get { return this.AlertFrequencies != null && this.AlertFrequencies.Count > 0; } } public List ImmediateNotificationExcludedFields { get { return this.m_immediateNotificationExludedFields; } set { this.m_immediateNotificationExludedFields = value; } } public string ImmediateNotificationXml { get { return this.m_immediateNotificationXml; } set { this.m_immediateNotificationXml = value; } } public bool ShowAlertEventTypes { get { return this.m_showAlertEventTypes; } set { this.m_showAlertEventTypes = value; } } public bool ShowAlertFilters { get { return this.m_showAlertFilters; } set { this.m_showAlertFilters = value; } } public bool ShowAlertFrequencies { get { return this.m_showAlertFrequencies; } set { this.m_showAlertFrequencies = value; } } public bool ShowAlertFrequencyImmediate { get { return this.AlertFrequencies.Contains(SPAlertFrequency.Immediate); } } public bool ShowAlertFrequencyDaily { get { return this.AlertFrequencies.Contains(SPAlertFrequency.Daily); } } public bool ShowAlertFrequencyWeekly { get { return this.AlertFrequencies.Contains(SPAlertFrequency.Weekly); } } public bool ShowAlertFrequencyTime { get { return this.m_showAlertFrequencyTime; } set { this.m_showAlertFrequencyTime = value; } } public XmlDocument Xml { get { return this.m_xml; } set { this.m_xml = value; } } private SPAlertTemplateConfiguration() { this.m_alertEventTypes = new List(); this.m_alertFilters = new List(); this.m_alertFrequencies = new List(); this.m_digestNotificationExludedFields = new List(); this.m_immediateNotificationExludedFields = new List(); this.m_showAlertEventTypes = true; this.m_showAlertFilters = true; this.m_showAlertFrequencies = true; this.m_showAlertFrequencyTime = true; this.m_alertFrequencies.Add(SPAlertFrequency.Immediate); this.m_alertFrequencies.Add(SPAlertFrequency.Daily); this.m_alertFrequencies.Add(SPAlertFrequency.Weekly); this.m_defaultAlertFrequency = 0; this.m_defaultAlertFrequencyTime = DateTime.Now; this.m_alwaysNotify = false; } public void UpdateXml() { try { this.UpdateAlertTemplateFrequency(); } finally { this.IsDirty = true; } } private void UpdateAlertTemplateFrequency() { XmlNode xmlNode = this.Xml.SelectSingleNode("AlertTemplate/Frequency"); if (xmlNode == null) { return; } xmlNode.Attributes["IsVisible"].Value = Convert.ToString(this.ShowAlertFrequencies); XmlAttribute xmlAttribute = xmlNode.Attributes["DefaultFrequency"]; if (this.DefaultAlertFrequency == SPAlertFrequency.Daily) { if (xmlAttribute != null) { xmlAttribute.Value = "Daily"; } } else { if (this.DefaultAlertFrequency == SPAlertFrequency.Weekly && xmlAttribute != null) { xmlAttribute.Value = "Weekly"; } } if (this.DefaultAlertFrequency == null && xmlAttribute != null) { xmlAttribute.Value = "Immediate"; } xmlAttribute = xmlNode.Attributes["DefaultTime"]; if (xmlAttribute != null) { xmlAttribute.Value = this.DefaultAlertFrequencyTime.ToUniversalTime().ToString(); } xmlAttribute = xmlNode.Attributes["ShowTime"]; if (xmlAttribute != null) { xmlAttribute.Value = Convert.ToString(this.ShowAlertFrequencyTime); } Dictionary dictionary = new Dictionary(3); dictionary.Add(SPAlertFrequency.Immediate, "ShowImmediate"); dictionary.Add(SPAlertFrequency.Daily, "ShowDaily"); dictionary.Add(SPAlertFrequency.Weekly, "ShowWeekly"); foreach (SPAlertFrequency current in dictionary.Keys) { xmlAttribute = xmlNode.Attributes[dictionary[current]]; if (this.AlertFrequencies.Contains(current)) { xmlAttribute.Value = "true"; } else { xmlAttribute.Value = "false"; } } } private void InitializeFromXml(XmlDocument xmlDoc) { this.Xml = xmlDoc; this.InitializeAlertTemplateFilters(); this.InitializeAlertTemplateEventTypes(); this.InitializeAlertTemplate(); this.InitializeAlertTemplateFrequency(); this.IsDirty = false; } private void InitializeAlertTemplateFilters() { XmlNode xmlNode = this.Xml.SelectSingleNode("AlertTemplate/Filters"); if (xmlNode == null) { return; } foreach (XmlNode xmlNode2 in xmlNode.ChildNodes) { string query = this.PopulateAlertFilterQueryWithCurrentUserID(xmlNode2.SelectSingleNode("Query")); SPAlertFilter item = new SPAlertFilter(xmlNode2.SelectSingleNode("FriendlyName").InnerText.Trim(), query, string.Empty); this.AlertFilters.Add(item); } XmlNode xmlNode3 = xmlNode.Attributes["IsVisible"]; this.ShowAlertFilters = (xmlNode3 != null && string.Compare(xmlNode3.Value, "true", true) == 0); } private string PopulateAlertFilterQueryWithCurrentUserID(XmlNode queryNode) { XmlNodeList xmlNodeList = queryNode.SelectNodes(".//Value[UserID]"); foreach (XmlNode xmlNode in xmlNodeList) { xmlNode.Attributes["type"].Value = "string"; xmlNode.InnerXml = SPContext.Current.Web.CurrentUser.LoginName; } return queryNode.OuterXml; } private void InitializeAlertTemplateEventTypes() { XmlNode xmlNode = this.Xml.SelectSingleNode("AlertTemplate/EventTypes"); if (xmlNode == null) { return; } foreach (XmlNode xmlNode2 in xmlNode.ChildNodes) { SPAlertEventType sPAlertEventType = new SPAlertEventType(xmlNode2.InnerText.Trim(), int.Parse(xmlNode2.Attributes["Mask"].Value.Substring(2), NumberStyles.HexNumber)); if (xmlNode2 != null && string.Compare(xmlNode2.Value, "true", true) == 0) { this.DefaultAlertEventType = sPAlertEventType; } this.AlertEventTypes.Add(sPAlertEventType); } XmlNode xmlNode3 = xmlNode.Attributes["IsVisible"]; this.ShowAlertEventTypes = (xmlNode3 != null && string.Compare(xmlNode3.Value, "true", true) == 0); } private void InitializeAlertTemplate() { XmlNode xmlNode = this.Xml.SelectSingleNode("AlertTemplate"); if (xmlNode == null) { return; } if (xmlNode.Attributes["DefaultTitle"] != null) { this.DefaultTitle = xmlNode.Attributes["DefaultTitle"].Value; } XmlNode xmlNode2 = xmlNode.Attributes["AlwaysNotify"]; this.AlwaysNotify = (xmlNode2 != null && string.Compare(xmlNode2.Value, "true", true) == 0); } private void InitializeAlertTemplateFrequency() { XmlNode xmlNode = this.Xml.SelectSingleNode("AlertTemplate/Frequency"); if (xmlNode == null) { return; } this.ShowAlertFrequencies = (string.Compare(xmlNode.Attributes["IsVisible"].Value, "true", true) == 0); XmlAttribute xmlAttribute = xmlNode.Attributes["DefaultFrequency"]; if (xmlAttribute != null && xmlAttribute.Value == "Daily") { this.DefaultAlertFrequency = SPAlertFrequency.Daily; } else { if (xmlAttribute != null && xmlAttribute.Value == "Weekly") { this.DefaultAlertFrequency = SPAlertFrequency.Weekly; } } xmlAttribute = xmlNode.Attributes["DefaultTime"]; this.DefaultAlertFrequencyTime = ((xmlAttribute == null) ? DateTime.Now : DateTime.Parse(xmlAttribute.Value)); xmlAttribute = xmlNode.Attributes["ShowTime"]; this.ShowAlertFrequencyTime = (xmlAttribute != null && string.Compare(xmlAttribute.Value, "true", true) == 0); Dictionary dictionary = new Dictionary(3); dictionary.Add(SPAlertFrequency.Immediate, "ShowImmediate"); dictionary.Add(SPAlertFrequency.Daily, "ShowDaily"); dictionary.Add(SPAlertFrequency.Weekly, "ShowWeekly"); foreach (SPAlertFrequency current in dictionary.Keys) { xmlAttribute = xmlNode.Attributes[dictionary[current]]; if (xmlAttribute != null) { if (string.Compare(xmlAttribute.Value, "true", true) == 0) { if (!this.AlertFrequencies.Contains(current)) { this.AlertFrequencies.Add(current); } } else { if (string.Compare(xmlAttribute.Value, "false", true) == 0 && this.AlertFrequencies.Contains(current)) { this.AlertFrequencies.Remove(current); } } } } } public static SPAlertTemplateConfiguration GetConfiguration(SPAlertTemplate alertTemplate, uint lcid) { if (alertTemplate == null) { throw new ArgumentNullException("alertTemplate"); } return SPAlertTemplateConfiguration.GetConfiguration(alertTemplate.GetLocalizedXml(lcid)); } public static SPAlertTemplateConfiguration GetConfiguration(string xml) { SPAlertTemplateConfiguration sPAlertTemplateConfiguration = new SPAlertTemplateConfiguration(); SPAlertTemplateConfiguration.InitializeAlertTemplateConfiguration(sPAlertTemplateConfiguration, xml); return sPAlertTemplateConfiguration; } private static void InitializeAlertTemplateConfiguration(SPAlertTemplateConfiguration alertTemplateConfiguration, string xml) { if (string.IsNullOrEmpty(xml)) { return; } XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xml); alertTemplateConfiguration.InitializeFromXml(xmlDocument); } } }