using System; using System.Globalization; using System.IO; using System.Linq; using System.Threading; using System.Xml; using Microsoft.SharePoint; using Taloyhtio.CondoUpdate.Common; namespace CondoUpdate.ActivateCustomAlertHandler { public class UpdaterImpl : ICondoUpdater { private readonly Guid SPSOLUTIONS_ALERT_HANDLER_SITE_FEATURE_ID = new Guid("230FADC0-9081-40f9-B2AA-C589D3D3E0E8"); private readonly Guid TALOYHTIO_ALERT_HANDLER_SITE_FEATURE_ID = new Guid("8cc68f6e-e53a-4f16-baba-307b82d607fb"); public event EventHandler OnNotify; public void Update(object args) { string url = args as string; if (string.IsNullOrEmpty(url)) { this.warn("Url is empty"); return; } try { this.updateImpl(url); } catch(Exception x) { this.error("Error occured during updating of Condo '{0}':\n{1}\n{2}", url, x.Message, x.StackTrace); } } private void updateImpl(string url) { using (var site = new SPSite(url)) { using (var condoWeb = site.OpenWeb()) { if (!condoWeb.Exists || string.Compare(condoWeb.Url, url, true) != 0) { return; } Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)condoWeb.Language); this.ensureSiteFeatureActivated(site); } } } private void ensureSiteFeatureActivated(SPSite site) { if (!site.Features.Any(f => f.DefinitionId == SPSOLUTIONS_ALERT_HANDLER_SITE_FEATURE_ID)) { site.Features.Add(SPSOLUTIONS_ALERT_HANDLER_SITE_FEATURE_ID, false); } if (!site.Features.Any(f => f.DefinitionId == TALOYHTIO_ALERT_HANDLER_SITE_FEATURE_ID)) { site.Features.Add(TALOYHTIO_ALERT_HANDLER_SITE_FEATURE_ID, false); } } private void info(string msg, params object[] args) { this.notify(LogLevel.Info, msg, args); } private void warn(string msg, params object[] args) { this.notify(LogLevel.Warn, msg, args); } private void error(string msg, params object[] args) { this.notify(LogLevel.Error, msg, args); } private void notify(LogLevel level, string msg, params object[] args) { this.notify(level, string.Format(msg, args)); } private void notify(LogLevel level, string msg) { if (this.OnNotify != null) { this.OnNotify(this, new LogEventArgs { LogLevel = level, Message = msg }); } } } }