Taylohtio/AlertManager/SPSolutions.SharePoint.Aler.../SPSolutions.SharePoint/SPSolutions.SharePoint.Alerts/SPAlertTemplateUtil.cs

238 lines
8.7 KiB
C#

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using SPSolutions.Serialization;
using SPSolutions.SharePoint.Logging;
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
namespace SPSolutions.SharePoint.Alerts
{
public static class SPAlertTemplateUtil
{
private const string c_customAlertTemplateIdentifier = "SPSolutionsAlertManager";
public static string CustomAlertTemplateIdentifier
{
get
{
return "SPSolutionsAlertManager";
}
}
public static void ApplyAlertTemplateToList(SPList list, SPAlertTemplate at)
{
list.ParentWeb.AllowUnsafeUpdates = (true);
list.AlertTemplate = (at);
list.Update();
list.ParentWeb.AllowUnsafeUpdates = (false);
}
public static void ApplyAlertTemplateToAlert(SPAlert a, SPAlertTemplate at)
{
a.List.ParentWeb.AllowUnsafeUpdates = (true);
a.AlertTemplate = (at);
a.Update();
if (a.User != null)
{
a.User.Update();
}
a.List.ParentWeb.AllowUnsafeUpdates = (false);
}
public static void ApplyFarmAlertTemplateToList(SPList list, string alertTemplateName, string alertTemplateXml)
{
SPAlertTemplateCollection sPAlertTemplatesInContext = SPAlertTemplateUtil.GetSPAlertTemplatesInContext();
SPAlertTemplate sPAlertTemplate = sPAlertTemplatesInContext.GetValue<SPAlertTemplate>(alertTemplateName);
if (null == sPAlertTemplate)
{
sPAlertTemplate = sPAlertTemplatesInContext.Add();
sPAlertTemplate.Name = (alertTemplateName);
sPAlertTemplate.Xml =(alertTemplateXml);
sPAlertTemplate.Properties["custom"] = "SPSolutionsAlertManager";
sPAlertTemplate.Update();
}
else
{
sPAlertTemplate.Xml = (alertTemplateXml);
sPAlertTemplate.Properties["custom"] = "SPSolutionsAlertManager";
sPAlertTemplate.Update();
}
list.AlertTemplate = (sPAlertTemplate);
list.Update();
}
public static void ApplyFarmAlertTemplateToAlert(SPAlert a, string alertTemplateName, string alertTemplateXml)
{
SPAlertTemplateCollection sPAlertTemplatesInContext = SPAlertTemplateUtil.GetSPAlertTemplatesInContext();
SPAlertTemplate sPAlertTemplate = sPAlertTemplatesInContext.GetValue<SPAlertTemplate>(alertTemplateName);
if (null == sPAlertTemplate)
{
sPAlertTemplate = sPAlertTemplatesInContext.Add();
sPAlertTemplate.Name = (alertTemplateName);
sPAlertTemplate.Xml = (alertTemplateXml);
sPAlertTemplate.Properties["custom"] = "SPSolutionsAlertManager";
sPAlertTemplate.Update();
}
else
{
sPAlertTemplate.Xml =(alertTemplateXml);
sPAlertTemplate.Properties["custom"] = "SPSolutionsAlertManager";
sPAlertTemplate.Update();
}
a.AlertTemplate = (sPAlertTemplate);
a.Update();
}
public static void InsertFarmAlertTemplate(string alertTemplateName, string alertTemplateXml)
{
SPAlertTemplateCollection sPAlertTemplatesInContext = SPAlertTemplateUtil.GetSPAlertTemplatesInContext();
SPAlertTemplate sPAlertTemplate = sPAlertTemplatesInContext.GetValue<SPAlertTemplate>(alertTemplateName);
if (null == sPAlertTemplate)
{
sPAlertTemplate = sPAlertTemplatesInContext.Add();
sPAlertTemplate.Name =(alertTemplateName);
sPAlertTemplate.Xml =(alertTemplateXml);
sPAlertTemplate.Update();
return;
}
sPAlertTemplate.Xml=(alertTemplateXml);
sPAlertTemplate.Update();
}
public static string GenerateAlertTemplateXml(string alertTemplateName)
{
string assemblyManifestResourceFileContent = SerializationUtil.GetAssemblyManifestResourceFileContent("Alerts.BaseAlertTemplate.xml");
Hashtable hashtable = new Hashtable();
hashtable["AlertTemplateName"] = alertTemplateName;
hashtable["NotificationHandlerAssembly"] = typeof(CustomAlertHandler).Assembly.FullName;
hashtable["NotificationHandlerClassName"] = typeof(CustomAlertHandler).FullName;
hashtable["NotificationHandlerProperties"] = string.Empty;
hashtable["UpdateHandlerAssembly"] = typeof(CustomAlertHandler).Assembly.FullName;
hashtable["UpdateHandlerClassName"] = typeof(CustomAlertHandler).FullName;
hashtable["UpdateHandlerProperties"] = string.Empty;
return StringUtil.ReplaceSquareBraceTokens(assemblyManifestResourceFileContent, hashtable);
}
public static string GenerateUniqueLocalizedAlertTemplateName()
{
return string.Format("{0}.{1}.{2}", "SPSolutionsAlertManager", Guid.NewGuid().ToString(), CultureInfo.CurrentCulture.LCID.ToString(CultureInfo.InvariantCulture));
}
public static string GetLocalizedAlertTemplateName(string templateName)
{
return string.Format("{0}.{1}", templateName, CultureInfo.CurrentCulture.LCID.ToString(CultureInfo.InvariantCulture));
}
public static SPAlertTemplateCollection GetSPAlertTemplatesInContext()
{
return new SPAlertTemplateCollection(SPWebService.ContentService);
}
public static SPAlertTemplateCollection GetSPAlertTemplatesInContext(SPSite site)
{
return null;
}
public static string TransformXmlToHtmlUsingXsl(string xml, string xsl, XsltArgumentList args)
{
StringReader stringReader = new StringReader(xml);
XmlReader input = XmlReader.Create(stringReader);
StringReader stringReader2 = new StringReader(xsl);
XmlTextReader xmlTextReader = new XmlTextReader(stringReader2);
XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
xslCompiledTransform.Load(xmlTextReader, null, null);
StringWriter stringWriter = new StringWriter();
xslCompiledTransform.Transform(input, args, stringWriter);
string text = stringWriter.ToString();
stringReader.Close();
stringReader2.Close();
xmlTextReader.Close();
stringWriter.Close();
return text.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");
}
public static Hashtable GetTokensForList(SPList list)
{
Hashtable hashtable = new Hashtable();
foreach (SPField sPField in list.Fields)
{
if (!sPField.Hidden && !sPField.InternalName.StartsWith("_") && !(sPField is SPFieldComputed))
{
hashtable[sPField.InternalName] = string.Empty;
}
}
return hashtable;
}
public static Hashtable GetTokensForListItem(SPListItem item)
{
Hashtable hashtable = new Hashtable();
foreach (SPField sPField in item.ParentList.Fields)
{
if (!sPField.Hidden && !(sPField is SPFieldComputed))
{
if (!(sPField is SPFieldDateTime))
{
string friendlyValue = SPFieldUtil.GetFriendlyValue(item, sPField.Id);
hashtable[sPField.InternalName] = (string.IsNullOrEmpty(friendlyValue) ? " " : friendlyValue);
}
else
{
string friendlyValue2 = SPFieldUtil.GetFriendlyValue(item, sPField.Id);
if (!string.IsNullOrEmpty(friendlyValue2))
{
DateTime dateTime = DateTime.MaxValue;
DateTime.TryParse(friendlyValue2, out dateTime);
if (dateTime != DateTime.MinValue)
{
try
{
try
{
dateTime = item.ParentList.ParentWeb.RegionalSettings.TimeZone.UTCToLocalTime(dateTime);
}
catch (Exception ex)
{
TraceProvider.WriteTrace(0u, TraceProvider.TraceSeverity.InformationEvent, Guid.Empty, "w3wp.exe", "Alert Manager", "CustomAlertHandler", "Error converting date: " + ex.Message);
}
continue;
}
finally
{
hashtable[sPField.InternalName] = dateTime.ToString();
}
}
hashtable[sPField.InternalName] = SPFieldUtil.GetFriendlyValue(item, sPField.Id);
}
else
{
hashtable[sPField.InternalName] = " ";
}
}
}
}
return hashtable;
}
public static Hashtable GetTokensForeventXml(string eventXml)
{
XmlDocument xmlDocument = new XmlDocument();
try
{
xmlDocument.LoadXml(eventXml);
}
catch (Exception ex)
{
TraceProvider.WriteTrace(0u, TraceProvider.TraceSeverity.Exception, Guid.Empty, "w3wp.exe", "Alert Manager", "CustomAlertHandler", "Error getting Tokens for eventXml - Xml.LoadXml(eventXml): " + ex.Message);
throw new Exception("Error Loading eventXml", ex.InnerException);
}
Hashtable hashtable = new Hashtable();
XmlNodeList elementsByTagName;
try
{
elementsByTagName = xmlDocument.GetElementsByTagName("Field");
}
catch (Exception ex2)
{
TraceProvider.WriteTrace(0u, TraceProvider.TraceSeverity.Exception, Guid.Empty, "w3wp.exe", "Alert Manager", "CustomAlertHandler", "Error getting xmlFields from eventXml: " + ex2.Message);
throw new Exception("Error getting EventXML Fields", ex2.InnerException);
}
for (int i = 0; i < elementsByTagName.Count; i++)
{
string value = elementsByTagName[i].Attributes["Name"].Value;
string value2 = (elementsByTagName[i].Attributes["LookupOldF"] == null) ? elementsByTagName[i].Attributes["Old"].Value : elementsByTagName[i].Attributes["LookupOldF"].Value;
hashtable[value] = value2;
}
return hashtable;
}
}
}