using Microsoft.SharePoint; using System; using System.Linq; using System.Globalization; using System.Threading; using Taloyhtio.CondoUpdate.Common; namespace CondoUpdate.ResponsiveLayout.ActivateLisapalvelut { public class UpdaterImpl : ICondoUpdater { private readonly Guid WEB_FEATURE_ID = new Guid("ecf4686d-7379-4b4e-abe1-6e8801fb983a"); 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) { this.warn("Condo site '{0}' doesn't exist. It will be ignored", url); return; } Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)condoWeb.Language); if (!condoWeb.Features.Cast().Any(f => f.DefinitionId.Equals(WEB_FEATURE_ID))) { condoWeb.Features.Add(WEB_FEATURE_ID, true); } else { this.warn("Feature already activated on Condo '{0}', ignore", url); } } } } #region notifier 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 }); } } #endregion } }