484 lines
15 KiB
C#
484 lines
15 KiB
C#
using Microsoft.SharePoint;
|
|
using SPSolutions.Serialization;
|
|
using SPSolutions.SharePoint.Alerts.Tokenization;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Xml;
|
|
namespace SPSolutions.SharePoint.Alerts
|
|
{
|
|
public class AlertTemplateManager
|
|
{
|
|
private static string c_notificationMessageKey = "NotificationMessage";
|
|
private SPAlertTemplate m_innerSPAlertTemplate;
|
|
private string m_messageHtml;
|
|
private bool m_innerSPAlertTemplateIsDirty;
|
|
private NotificationMessage m_notificationMesssage;
|
|
private Hashtable m_globalTokens;
|
|
private SPAlertTemplateConfiguration m_alertTemplateConfiguration;
|
|
public bool IsCurrentUserOwnerOrAdmin
|
|
{
|
|
get
|
|
{
|
|
return SPContext.Current.Web.CurrentUser.IsSiteAdmin || string.Compare(SPContext.Current.Web.CurrentUser.LoginName, this.OwnerLoginName, false) == 0;
|
|
}
|
|
}
|
|
public bool IsPublic
|
|
{
|
|
get
|
|
{
|
|
return !this.IsCustom || (bool)SerializationUtil.DeserializeObject(this.InnerSPAlertTemplate.Properties["Public"] as string, typeof(bool));
|
|
}
|
|
set
|
|
{
|
|
if (!this.IsCustom)
|
|
{
|
|
return;
|
|
}
|
|
this.InnerSPAlertTemplate.Properties["Public"] = SerializationUtil.SerializeObject(value, typeof(bool));
|
|
this.m_innerSPAlertTemplateIsDirty = true;
|
|
}
|
|
}
|
|
public string OwnerLoginName
|
|
{
|
|
get
|
|
{
|
|
return this.m_innerSPAlertTemplate.Properties["OwnerLoginName"] as string;
|
|
}
|
|
set
|
|
{
|
|
this.m_innerSPAlertTemplate.Properties["OwnerLoginName"] = value;
|
|
this.m_innerSPAlertTemplateIsDirty = true;
|
|
}
|
|
}
|
|
public string FriendlyName
|
|
{
|
|
get
|
|
{
|
|
if (!this.IsCustom)
|
|
{
|
|
return this.Name;
|
|
}
|
|
string text = this.InnerSPAlertTemplate.Properties["FriendlyName"] as string;
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
return this.Name;
|
|
}
|
|
return text;
|
|
}
|
|
set
|
|
{
|
|
this.InnerSPAlertTemplate.Properties["FriendlyName"] = value;
|
|
this.m_innerSPAlertTemplateIsDirty = true;
|
|
}
|
|
}
|
|
public SPAlertTemplateConfiguration InnerSPAlertTemplateConfiguration
|
|
{
|
|
get
|
|
{
|
|
return this.m_alertTemplateConfiguration;
|
|
}
|
|
set
|
|
{
|
|
this.m_alertTemplateConfiguration = value;
|
|
}
|
|
}
|
|
public Hashtable GlobalTokens
|
|
{
|
|
get
|
|
{
|
|
return this.m_globalTokens;
|
|
}
|
|
set
|
|
{
|
|
this.m_globalTokens = value;
|
|
}
|
|
}
|
|
public SPAlertTemplate InnerSPAlertTemplate
|
|
{
|
|
get
|
|
{
|
|
return this.m_innerSPAlertTemplate;
|
|
}
|
|
set
|
|
{
|
|
this.m_innerSPAlertTemplate = value;
|
|
}
|
|
}
|
|
public string MessageHtml
|
|
{
|
|
get
|
|
{
|
|
return this.m_messageHtml;
|
|
}
|
|
}
|
|
public string Name
|
|
{
|
|
get
|
|
{
|
|
return this.m_innerSPAlertTemplate.Name;
|
|
}
|
|
set
|
|
{
|
|
this.m_innerSPAlertTemplate.Name = (value);
|
|
this.m_innerSPAlertTemplateIsDirty = true;
|
|
}
|
|
}
|
|
public string DisplayName
|
|
{
|
|
get
|
|
{
|
|
return this.m_innerSPAlertTemplate.DisplayName;
|
|
}
|
|
}
|
|
public string Description
|
|
{
|
|
get
|
|
{
|
|
return this.m_innerSPAlertTemplate.Properties["Description"] as string;
|
|
}
|
|
set
|
|
{
|
|
this.m_innerSPAlertTemplate.Properties["Description"] = value;
|
|
this.m_innerSPAlertTemplateIsDirty = true;
|
|
}
|
|
}
|
|
public bool IsCustom
|
|
{
|
|
get
|
|
{
|
|
return this.InnerSPAlertTemplate.Properties["Custom"] != null && this.InnerSPAlertTemplate.Properties["Custom"] is string && (string)this.InnerSPAlertTemplate.Properties["Custom"] == SPAlertTemplateUtil.CustomAlertTemplateIdentifier;
|
|
}
|
|
}
|
|
public Guid Id
|
|
{
|
|
get
|
|
{
|
|
return this.m_innerSPAlertTemplate.Id;
|
|
}
|
|
set
|
|
{
|
|
this.m_innerSPAlertTemplate.Id = (value);
|
|
this.m_innerSPAlertTemplateIsDirty = true;
|
|
}
|
|
}
|
|
public NotificationMessage NotificationMessage
|
|
{
|
|
get
|
|
{
|
|
return this.m_notificationMesssage;
|
|
}
|
|
set
|
|
{
|
|
this.m_notificationMesssage = value;
|
|
}
|
|
}
|
|
public string NotificationMessageSubject
|
|
{
|
|
get
|
|
{
|
|
return this.NotificationMessage.GetFinalizedSubject(this.GlobalTokens);
|
|
}
|
|
}
|
|
public string NotificationMessageBody
|
|
{
|
|
get
|
|
{
|
|
return this.NotificationMessage.GetFinalizedMessage(this.GlobalTokens);
|
|
}
|
|
}
|
|
public bool AllowFrequencyImmediate
|
|
{
|
|
get
|
|
{
|
|
return this.InnerSPAlertTemplateConfiguration.ShowAlertFrequencyImmediate;
|
|
}
|
|
set
|
|
{
|
|
if (value)
|
|
{
|
|
if (!this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Contains(0))
|
|
{
|
|
this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Add(0);
|
|
this.InnerSPAlertTemplateConfiguration.IsDirty = true;
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Contains(0) && this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Count > 1)
|
|
{
|
|
this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Remove(0);
|
|
this.InnerSPAlertTemplateConfiguration.IsDirty = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public bool AllowFrequencyDaily
|
|
{
|
|
get
|
|
{
|
|
return this.InnerSPAlertTemplateConfiguration.ShowAlertFrequencyDaily;
|
|
}
|
|
set
|
|
{
|
|
if (value)
|
|
{
|
|
if (!this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Contains(SPAlertFrequency.Daily))
|
|
{
|
|
this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Add(SPAlertFrequency.Daily);
|
|
this.InnerSPAlertTemplateConfiguration.IsDirty = true;
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Contains(SPAlertFrequency.Daily) && this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Count > 1)
|
|
{
|
|
this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Remove(SPAlertFrequency.Daily);
|
|
this.InnerSPAlertTemplateConfiguration.IsDirty = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public bool AllowFrequencyWeekly
|
|
{
|
|
get
|
|
{
|
|
return this.InnerSPAlertTemplateConfiguration.ShowAlertFrequencyWeekly;
|
|
}
|
|
set
|
|
{
|
|
if (value)
|
|
{
|
|
if (!this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Contains(SPAlertFrequency.Weekly))
|
|
{
|
|
this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Add(SPAlertFrequency.Weekly);
|
|
this.InnerSPAlertTemplateConfiguration.IsDirty = true;
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Contains(SPAlertFrequency.Weekly) && this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Count > 1)
|
|
{
|
|
this.InnerSPAlertTemplateConfiguration.AlertFrequencies.Remove(SPAlertFrequency.Weekly);
|
|
this.InnerSPAlertTemplateConfiguration.IsDirty = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public AlertTemplateManager()
|
|
{
|
|
this.GlobalTokens = new Hashtable();
|
|
this.GlobalTokens["EventDescription"] = string.Empty;
|
|
this.GlobalTokens["WebTitle"] = string.Empty;
|
|
this.GlobalTokens["ListTitle"] = string.Empty;
|
|
this.GlobalTokens["ListId"] = string.Empty;
|
|
this.GlobalTokens["WebAppAbsolutePublicUrl"] = string.Empty;
|
|
this.GlobalTokens["SiteAbsoluteUrl"] = string.Empty;
|
|
this.GlobalTokens["SiteAbsolutePublicUrl"] = string.Empty;
|
|
this.GlobalTokens["WebAbsoluteUrl"] = string.Empty;
|
|
this.GlobalTokens["WebAbsolutePublicUrl"] = string.Empty;
|
|
this.GlobalTokens["ListAbsoluteUrl"] = string.Empty;
|
|
this.GlobalTokens["ItemDisplayFormAbsoluteUrl"] = string.Empty;
|
|
this.GlobalTokens["ItemLink"] = string.Empty;
|
|
this.GlobalTokens["ItemTitle"] = string.Empty;
|
|
this.GlobalTokens["ItemId"] = string.Empty;
|
|
this.GlobalTokens["WebLink"] = string.Empty;
|
|
this.GlobalTokens["ListLink"] = string.Empty;
|
|
this.GlobalTokens["AlertDetailsTable"] = string.Empty;
|
|
this.GlobalTokens["AlertName"] = string.Empty;
|
|
this.GlobalTokens["href"] = "href";
|
|
this.GlobalTokens["ParentDiscussionTitle"] = string.Empty;
|
|
}
|
|
public AlertTemplateManager(SPAlertTemplate innerAlertTemplate) : this()
|
|
{
|
|
this.m_innerSPAlertTemplate = innerAlertTemplate;
|
|
this.EnsureInnerAlertTemplate();
|
|
}
|
|
|
|
public AlertTemplateManager(string name) : this(SPAlertTemplateUtil.GetSPAlertTemplatesInContext().Add())
|
|
{
|
|
Guid id = Guid.NewGuid();
|
|
this.m_innerSPAlertTemplate.Id = (id);
|
|
this.Name = name;
|
|
this.LoadDefaults();
|
|
}
|
|
private void LoadDefaults()
|
|
{
|
|
this.InnerSPAlertTemplate.Xml = (SPAlertTemplateUtil.GenerateAlertTemplateXml(this.Name));
|
|
this.InnerSPAlertTemplateConfiguration = SPAlertTemplateConfiguration.GetConfiguration(this.InnerSPAlertTemplate.Xml);
|
|
this.NotificationMessage.LoadDefaults();
|
|
this.InnerSPAlertTemplate.Properties["Custom"] = SPAlertTemplateUtil.CustomAlertTemplateIdentifier;
|
|
this.OwnerLoginName = SPContext.Current.Web.CurrentUser.LoginName;
|
|
this.FriendlyName = string.Format("{0}.{1}", SPAlertTemplateUtil.CustomAlertTemplateIdentifier, this.m_innerSPAlertTemplate.Id);
|
|
this.IsPublic = true;
|
|
this.Description = SerializationUtil.GetAssemblyManifestResourceFileContent("Alerts.DefaultCustomAlertTemplateDescription.htm");
|
|
}
|
|
public void EnsureInnerAlertTemplate()
|
|
{
|
|
if (this.InnerSPAlertTemplate == null)
|
|
{
|
|
throw new NullReferenceException("InnerAlertTemplate cannot be null.");
|
|
}
|
|
this.EnsureNotificationMessageObject();
|
|
this.m_alertTemplateConfiguration = SPAlertTemplateConfiguration.GetConfiguration(this.InnerSPAlertTemplate.Xml);
|
|
bool arg_40_0 = this.m_alertTemplateConfiguration.ShowAlertFilters;
|
|
}
|
|
public void LoadXml()
|
|
{
|
|
this.InnerSPAlertTemplate.Xml = (SPAlertTemplateUtil.GenerateAlertTemplateXml(SPAlertTemplateUtil.GetLocalizedAlertTemplateName("YATTContinue")));
|
|
this.InnerSPAlertTemplateConfiguration.Xml = new XmlDocument();
|
|
this.InnerSPAlertTemplateConfiguration.Xml.LoadXml(this.InnerSPAlertTemplate.Xml);
|
|
}
|
|
private void EnsureNotificationMessageObject()
|
|
{
|
|
if (this.InnerSPAlertTemplate.Properties.ContainsKey(AlertTemplateManager.c_notificationMessageKey))
|
|
{
|
|
this.m_notificationMesssage = (SerializationUtil.DeserializeObject((string)this.InnerSPAlertTemplate.Properties[AlertTemplateManager.c_notificationMessageKey], typeof(NotificationMessage)) as NotificationMessage);
|
|
}
|
|
if (this.m_notificationMesssage == null)
|
|
{
|
|
this.m_notificationMesssage = new NotificationMessage(true);
|
|
this.InnerSPAlertTemplate.Properties[AlertTemplateManager.c_notificationMessageKey] = SerializationUtil.SerializeObject(this.m_notificationMesssage, typeof(NotificationMessage));
|
|
this.m_innerSPAlertTemplateIsDirty = true;
|
|
}
|
|
}
|
|
public void SetDefaultFrequency(string val)
|
|
{
|
|
if (string.IsNullOrEmpty(val))
|
|
{
|
|
this.InnerSPAlertTemplateConfiguration.DefaultAlertFrequency = SPAlertFrequency.Immediate;
|
|
return;
|
|
}
|
|
if (string.Compare(val, "immediate", true) == 0)
|
|
{
|
|
this.InnerSPAlertTemplateConfiguration.DefaultAlertFrequency = SPAlertFrequency.Immediate;
|
|
return;
|
|
}
|
|
if (string.Compare(val, "daily", true) == 0)
|
|
{
|
|
this.InnerSPAlertTemplateConfiguration.DefaultAlertFrequency = SPAlertFrequency.Daily;
|
|
return;
|
|
}
|
|
if (string.Compare(val, "weekly", true) == 0)
|
|
{
|
|
this.InnerSPAlertTemplateConfiguration.DefaultAlertFrequency = SPAlertFrequency.Weekly;
|
|
return;
|
|
}
|
|
this.InnerSPAlertTemplateConfiguration.DefaultAlertFrequency = SPAlertFrequency.Immediate;
|
|
}
|
|
public void Save()
|
|
{
|
|
if (this.m_innerSPAlertTemplateIsDirty || this.NotificationMessage.IsDirty || this.InnerSPAlertTemplateConfiguration.IsDirty)
|
|
{
|
|
this.ForceSave();
|
|
}
|
|
}
|
|
public void ForceSave()
|
|
{
|
|
if (!this.IsCustom)
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
this.InnerSPAlertTemplate.Properties["Custom"] = SPAlertTemplateUtil.CustomAlertTemplateIdentifier;
|
|
this.InnerSPAlertTemplate.Properties[AlertTemplateManager.c_notificationMessageKey] = SerializationUtil.SerializeObject(this.m_notificationMesssage, typeof(NotificationMessage));
|
|
this.InnerSPAlertTemplateConfiguration.UpdateXml();
|
|
this.InnerSPAlertTemplate.Xml = (this.InnerSPAlertTemplateConfiguration.Xml.InnerXml);
|
|
this.InnerSPAlertTemplate.Update();
|
|
this.m_innerSPAlertTemplateIsDirty = false;
|
|
this.NotificationMessage.IsDirty = false;
|
|
this.InnerSPAlertTemplateConfiguration.IsDirty = false;
|
|
}
|
|
internal void ProcessNotification(SPAlertHandlerParams alertHandlerParams)
|
|
{
|
|
string eventXml = string.Empty;
|
|
if (alertHandlerParams.eventData[0].eventType == 4)
|
|
{
|
|
string itemFullUrl = alertHandlerParams.eventData[0].itemFullUrl;
|
|
string itemName = itemFullUrl.Substring(itemFullUrl.LastIndexOf("/") + 1, itemFullUrl.Length - itemFullUrl.LastIndexOf("/") - 1);
|
|
eventXml = this.BuildEventXml(alertHandlerParams.eventData[0].itemId, itemName);
|
|
}
|
|
AlertParamsTokenizer alertParamsTokenizer;
|
|
try
|
|
{
|
|
alertParamsTokenizer = new AlertParamsTokenizer(alertHandlerParams, this.NotificationMessage.AlertDetailXslt);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(string.Format("Error creating AlertParamsTokenizer - new message. {0} - {1}", ex, ex.InnerException));
|
|
}
|
|
ListItemTokenizer listItemTokenizer;
|
|
try
|
|
{
|
|
if (alertParamsTokenizer.Items.Count > 0)
|
|
{
|
|
listItemTokenizer = new ListItemTokenizer(alertParamsTokenizer.Items[0]);
|
|
}
|
|
else
|
|
{
|
|
listItemTokenizer = new ListItemTokenizer(eventXml);
|
|
}
|
|
listItemTokenizer.SetTokenizer(alertParamsTokenizer);
|
|
}
|
|
catch (Exception innerException)
|
|
{
|
|
throw new Exception("Error creating ListItemTokenizer.", innerException);
|
|
}
|
|
ListTokenizer listTokenizer;
|
|
try
|
|
{
|
|
if (alertParamsTokenizer.Items.Count > 0)
|
|
{
|
|
listTokenizer = new ListTokenizer(alertParamsTokenizer.List, alertParamsTokenizer.Items[0]);
|
|
}
|
|
else
|
|
{
|
|
listTokenizer = new ListTokenizer(eventXml);
|
|
}
|
|
listTokenizer.SetTokenizer(listItemTokenizer);
|
|
}
|
|
catch (Exception innerException2)
|
|
{
|
|
throw new Exception("Error creating ListTokenizer.", innerException2);
|
|
}
|
|
try
|
|
{
|
|
listTokenizer.PopulateTokenValues();
|
|
}
|
|
catch (Exception innerException3)
|
|
{
|
|
throw new Exception("Error spListTokenizer.PopulateTokenValues().", innerException3);
|
|
}
|
|
try
|
|
{
|
|
this.GlobalTokens = listTokenizer.AddTokens(this.GlobalTokens);
|
|
}
|
|
catch (Exception innerException4)
|
|
{
|
|
throw new Exception("Error GlobalTokens = spListTokenizer.AddTokens(GlobalTokens);", innerException4);
|
|
}
|
|
}
|
|
internal string BuildEventXml(int itemID, string itemName)
|
|
{
|
|
XmlDocument xmlDocument = new XmlDocument();
|
|
XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", null, null);
|
|
xmlDocument.AppendChild(newChild);
|
|
XmlElement xmlElement = xmlDocument.CreateElement("Fields");
|
|
xmlDocument.AppendChild(xmlElement);
|
|
XmlElement xmlElement2 = xmlDocument.CreateElement("Field");
|
|
xmlElement2.SetAttribute("Name", "ID");
|
|
xmlElement2.SetAttribute("Type", "Integer");
|
|
xmlElement2.SetAttribute("Old", itemID.ToString());
|
|
xmlElement.AppendChild(xmlElement2);
|
|
XmlElement xmlElement3 = xmlDocument.CreateElement("Field");
|
|
xmlElement3.SetAttribute("Name", "Title");
|
|
xmlElement3.SetAttribute("Type", "string");
|
|
xmlElement3.SetAttribute("Old", itemName);
|
|
xmlElement3.SetAttribute("New", itemName);
|
|
xmlElement.AppendChild(xmlElement3);
|
|
return xmlDocument.OuterXml;
|
|
}
|
|
}
|
|
}
|