Taylohtio/CondoUpdate/CondoUpdate.ActivateAlerts/UpdaterImpl.cs

232 lines
7.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using SPSolutions.SharePoint.Alerts;
using Taloyhtio.CondoAutomation.CodeFiles;
using Taloyhtio.CondoAutomation.Utils;
using Taloyhtio.CondoUpdate.Common;
using Taloyhtio.CustomAlertHandler.CodeFiles;
namespace CondoUpdate.ActivateAlerts
{
public class SiteInfo
{
public string Url { get; set; }
public string Prefix { get; set; }
}
public class UpdaterImpl : ICondoUpdater
{
private const string SITE_INFO_FILE = "siteinfo.txt";
public event EventHandler<LogEventArgs> OnNotify;
private static List<SiteInfo> SiteInfo = null;
public void Update(object args)
{
string url = args as string;
if (string.IsNullOrEmpty(url))
{
this.warn("Url is empty");
return;
}
if (SiteInfo == null)
{
SiteInfo = this.getSiteInfo();
if (SiteInfo == null)
{
this.error("SiteInfo from file is null. Can't continue");
return;
}
this.info("Site Info: size {0}", SiteInfo.Count);
foreach (var si in SiteInfo)
{
this.info("{0}: {1}", si.Url, si.Prefix);
}
}
try
{
this.updateImpl(url, SiteInfo);
}
catch (Exception x)
{
this.error("Error occured during updating of Condo '{0}':\n{1}\n{2}", url, x.Message, x.StackTrace);
}
}
private List<SiteInfo> getSiteInfo()
{
try
{
if (!File.Exists(SITE_INFO_FILE))
{
this.error("File not found");
return null;
}
var lines = File.ReadAllLines(SITE_INFO_FILE);
if (lines.Length == 0)
{
this.error("File is empty");
return null;
}
var result = new List<SiteInfo>();
foreach (string line in lines)
{
if (string.IsNullOrEmpty(line))
{
continue;
}
if (!line.Contains(";"))
{
continue;
}
var parts = line.Split(new[] {";"}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
{
continue;
}
if (string.IsNullOrEmpty(parts[0]) || string.IsNullOrEmpty(parts[1]))
{
continue;
}
result.Add(new SiteInfo{ Url = parts[0], Prefix = parts[1] });
}
return result;
}
catch (Exception x)
{
this.error("Error occured when read site info: {0}\n{1}", x.Message, x.StackTrace);
return null;
}
}
private void updateImpl(string url, List<SiteInfo> siteInfos)
{
using (var site = new SPSite(url))
{
using (var web = site.OpenWeb())
{
if (!web.Exists)
{
this.warn("Web site '{0}' doesn't exist. It will be ignored", url);
return;
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int) web.Language);
// check on 1st alerts-enabled list are alerts already activated
bool? alreadyActivated = this.isAlreadyActivated(web);
if (alreadyActivated == null || alreadyActivated.Value)
{
info("Alerts already activated");
return;
}
var si = this.getSiteInfoByPMC(site, siteInfos);
AlertsManager am = null;
if (si == null)
{
this.info("Basic template will be used");
am = new AlertsManager(new AlertsActivationParams {IsCustom = false, ShortName = ""});
}
else
{
this.info("Custom template will be used: {0}", si.Prefix);
am = new AlertsManager(new AlertsActivationParams { IsCustom = true, ShortName = si.Prefix });
}
var msgs = new List<string>();
bool result = am.SubScribeCondoGroups(web, msgs);
if (!result)
{
this.error("Error occured:\n{0}", string.Join("\n", msgs.ToArray()));
}
}
}
}
private SiteInfo getSiteInfoByPMC(SPSite site, List<SiteInfo> siteInfos)
{
if (siteInfos == null || siteInfos.Count == 0)
{
return null;
}
var rootWeb = site.RootWeb;
if (!rootWeb.Url.Contains("/"))
{
return null;
}
string lastUrlPart = rootWeb.Url.Substring(rootWeb.Url.LastIndexOf("/") + 1);
if (string.IsNullOrEmpty(lastUrlPart))
{
return null;
}
return siteInfos.FirstOrDefault(s => string.Compare(s.Url, lastUrlPart, true) == 0);
}
private bool? isAlreadyActivated(SPWeb web)
{
SPList list = null;
try
{
string listUrl = SPUrlUtility.CombineUrl(web.Url, "/Lists/Ilmoitustaulu");
list = web.GetList(listUrl);
}
catch
{
}
if (list == null)
{
return null;
}
var alert = this.getFirstListAlert(web, list);
return (alert != null);
}
private SPAlert getFirstListAlert(SPWeb web, SPList list)
{
var alerts = web.Alerts.Cast<SPAlert>();
return alerts.FirstOrDefault(a => a.ListID == list.ID);
}
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 });
}
}
}
}