Taylohtio/CondoUpdate/CondoUpdate.ResponsiveLayou.../UpdaterImpl.cs

108 lines
3.5 KiB
C#

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Taloyhtio.CondoUpdate.Common;
namespace CondoUpdate.ResponsiveLayout.AsumisenOppaat
{
public class UpdaterImpl : ICondoUpdater
{
private readonly Guid OLD_FEATURE_ID = new Guid("8d788e09-59b6-4d7b-b31d-fd6855e0b15c");
private readonly Guid WEB_FEATURE_ID = new Guid("2b106f86-7017-4641-b8b9-85b7ea131e24");
private readonly string GUIDE_PAGE = "Energiajakoti.aspx";
public event EventHandler<LogEventArgs> 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 web = site.OpenWeb())
{
if (!web.Exists)
{
this.warn("Condo site '{0}' doesn't exist. It will be ignored", url);
return;
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
var pages = SPUtility.GetLocalizedString("$Resources:List_Pages_UrlName", "cmscore", web.RegionalSettings.LocaleId);
var pageExists = web.GetFolder(pages).Files.Cast<SPFile>().Any(f => string.Compare(f.Name, GUIDE_PAGE, true) == 0);
if (!pageExists)
{
this.warn("guide pages not found on condo site '{0}'. skip", url);
return;
//this.warn("guide pages not found on condo site '{0}'. activating feature", url);
//web.Features.Add(OLD_FEATURE_ID, true);
}
if (!web.Features.Cast<SPFeature>().Any(f => f.DefinitionId.Equals(WEB_FEATURE_ID)))
{
web.Features.Add(WEB_FEATURE_ID, true);
}
else
{
this.warn("Feature already activated on condo site '{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
}
}