185 lines
6.6 KiB
C#
185 lines
6.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Xml.Serialization;
|
|
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Utilities;
|
|
using SPSolutions.SharePoint.Alerts;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
using Taloyhtio.CustomAlertHandler.CodeFiles;
|
|
|
|
namespace CondoUpdate.UpdateCondoAlerts
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
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)
|
|
{
|
|
ensureMappingsListExists(url);
|
|
// condo
|
|
fixList(url, "/Lists/Ilmoitustaulu");
|
|
fixList(url + "/asukkaille", "/Documents");
|
|
fixList(url + "/osakkaille", "/Documents");
|
|
fixList(url + "/Osakkaille", "/Lists/Osakastiedotteet");
|
|
fixList(url + "/hallitukselle", "/Documents");
|
|
fixList(url + "/Hallitukselle", "/Lists/Hallituksen%20tiedotteet");
|
|
fixList(url + "/Hallitukselle", "/Lists/Kalenteri");
|
|
fixList(url + "/Hallitukselle", "/Lists/Keskustelupalsta");
|
|
// company
|
|
// fixList(url, "/Lists/Ilmoitustaulu");
|
|
// fixList(url, "/Documents");
|
|
// fixList(url, "/Lists/Asiakirjojen%20lhetys%20tilitoimistolle");
|
|
// fixList(url, "/Lists/Keskustelu");
|
|
// fixList(url, "/Tuntilistat%20ja%20verokortit", false); // was added manually on company sites under /rp AC
|
|
}
|
|
|
|
private void ensureMappingsListExists(string webUrl)
|
|
{
|
|
using (var site = new SPSite(webUrl))
|
|
{
|
|
var web = site.RootWeb;
|
|
var mappingList = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == Constants.CUSTOM_ALERT_HANDLER_MAPPINGS_LIST);
|
|
if (mappingList != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
web.Features.Add(new Guid("8cc68f6e-e53a-4f16-baba-307b82d607fb"), true);
|
|
}
|
|
}
|
|
|
|
private void fixList(string webUrl, string listUrl, bool reportErrorIfListNotExists = true)
|
|
{
|
|
//log("Check list '{0}' in web '{1}'", listUrl, webUrl);
|
|
using (var site = new SPSite(webUrl))
|
|
{
|
|
using (var web = site.OpenWeb())
|
|
{
|
|
SPList list = null;
|
|
try
|
|
{
|
|
string url = SPUrlUtility.CombineUrl(webUrl, listUrl);
|
|
list = web.GetList(url);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
if (list == null)
|
|
{
|
|
if (reportErrorIfListNotExists)
|
|
{
|
|
error("List '{0}' can't be found", listUrl);
|
|
}
|
|
return;
|
|
}
|
|
|
|
string msg;
|
|
// bool? alreadyActivated = AlertTemplateConfigurator.HasEnabledFromField(list, out msg);
|
|
// if (alreadyActivated != null && alreadyActivated.Value)
|
|
// {
|
|
// //log("Custom alert handler is already activated");
|
|
// return;
|
|
// }
|
|
|
|
// get first alert from list (normally in all lists affected by alert creation tool there is only 1 alert)
|
|
var alert = getFirstListAlert(web, list);
|
|
if (alert == null)
|
|
{
|
|
//log("There are no alerts in the list");
|
|
return;
|
|
}
|
|
|
|
// get alert template from this list
|
|
var alertTemplate = alert.AlertTemplate;
|
|
if (alertTemplate == null)
|
|
{
|
|
error("Alert's alert template is null (list '{0}')", listUrl);
|
|
return;
|
|
}
|
|
|
|
if (!isCustom(alertTemplate))
|
|
{
|
|
error("Alert template '{0}' is not custom (list '{1}')", alertTemplate.DisplayName, listUrl);
|
|
return;
|
|
}
|
|
|
|
// assign alert template
|
|
list.AlertTemplate = alertTemplate;
|
|
list.Update();
|
|
//log("List's alert template was set to '{0}'", alertTemplate.DisplayName);
|
|
|
|
if (!AlertTemplateConfigurator.ActivateCustomAlertHandlerOnList(list, true, out msg))
|
|
{
|
|
error("There was problem when activating custom alert handler: '{0}' (list '{1}')", msg, listUrl);
|
|
return;
|
|
}
|
|
|
|
//log("Custom alert handler was successfilly activated");
|
|
}
|
|
}
|
|
}
|
|
|
|
private static SPAlert getFirstListAlert(SPWeb web, SPList list)
|
|
{
|
|
var alerts = web.Alerts.Cast<SPAlert>();
|
|
return alerts.FirstOrDefault(a => a.ListID == list.ID);
|
|
}
|
|
|
|
// This is copy of Alert template's AlertTemplateManager.IsCustom property implementation
|
|
private static bool isCustom(SPAlertTemplate t)
|
|
{
|
|
return (((t.Properties["Custom"] != null) && (t.Properties["Custom"] is string)) && (((string)t.Properties["Custom"]) == SPAlertTemplateUtil.CustomAlertTemplateIdentifier));
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|