180 lines
5.8 KiB
C#
180 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Xml.Serialization;
|
|
using Microsoft.SharePoint;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
|
|
namespace CondoUpdate.SetAlertsToResponsibleUsers
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private const string KESKUSTELU = "Keskustelu";
|
|
private const string KESKUSTELU_OTHER = "Huomioitavaa ja kommentteja";
|
|
private const string DOCUMENTS = "Asiakirjojen lähetys tilitoimistolle";
|
|
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())
|
|
{
|
|
var discussionGroups = new List<string>();
|
|
var documentsGroups = new List<string>();
|
|
|
|
string visitorsGroup = getVisitorsGroupName(web);
|
|
if (isGroupExist(web, visitorsGroup))
|
|
{
|
|
discussionGroups.Add(visitorsGroup);
|
|
}
|
|
|
|
string membersGroup = getMembersGroupName(web);
|
|
if (isGroupExist(web, membersGroup))
|
|
{
|
|
discussionGroups.Add(membersGroup);
|
|
}
|
|
|
|
string responsibleAccountantGroup = getResponsibleAccountantsGroupName(web);
|
|
if (isGroupExist(web, responsibleAccountantGroup))
|
|
{
|
|
discussionGroups.Add(responsibleAccountantGroup);
|
|
documentsGroups.Add(responsibleAccountantGroup);
|
|
}
|
|
|
|
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => string.Compare(l.Title, KESKUSTELU, true) == 0);
|
|
if (list != null)
|
|
{
|
|
fixAlerts(web, list, KESKUSTELU, discussionGroups);
|
|
}
|
|
|
|
list = web.Lists.Cast<SPList>().FirstOrDefault(l => string.Compare(l.Title, KESKUSTELU_OTHER, true) == 0);
|
|
if (list != null)
|
|
{
|
|
fixAlerts(web, list, KESKUSTELU_OTHER, discussionGroups);
|
|
}
|
|
|
|
list = web.Lists.Cast<SPList>().FirstOrDefault(l => string.Compare(l.Title, DOCUMENTS, true) == 0);
|
|
if (list != null)
|
|
{
|
|
fixAlerts(web, list, DOCUMENTS, documentsGroups);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void fixAlerts(SPWeb web, SPList list, string customAlertName, List<string> groups)
|
|
{
|
|
var alerts = web.Alerts;
|
|
foreach (SPAlert a in alerts)
|
|
{
|
|
if (a.ListID != list.ID)
|
|
{
|
|
continue;
|
|
}
|
|
if (!shouldFix(a, customAlertName))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
string groupsString = Serialize(groups);
|
|
a.Properties["alertgroups"] = groupsString;
|
|
a.Update();
|
|
}
|
|
}
|
|
|
|
private bool isGroupExist(SPWeb web, string groupName)
|
|
{
|
|
return web.SiteGroups.Cast<SPGroup>().Any(g => string.Compare(g.Name, groupName, true) == 0);
|
|
}
|
|
|
|
private string Serialize<T>(T objToSerialize) where T : class
|
|
{
|
|
if (objToSerialize == null)
|
|
{
|
|
throw new ArgumentNullException("objToSerialize");
|
|
}
|
|
var serializer = new XmlSerializer(typeof(T));
|
|
var writer = new StringWriter();
|
|
serializer.Serialize(writer, objToSerialize);
|
|
return writer.ToString();
|
|
}
|
|
|
|
private static string getMembersGroupName(SPWeb web)
|
|
{
|
|
return string.Format("{0} - Jäsenet", web.Title);
|
|
}
|
|
|
|
private static string getVisitorsGroupName(SPWeb web)
|
|
{
|
|
return string.Format("{0} - Työntekijät", web.Title);
|
|
}
|
|
|
|
private static string getResponsibleAccountantsGroupName(SPWeb web)
|
|
{
|
|
return string.Format("{0} - Responsible accountants", web.Title);
|
|
}
|
|
|
|
private bool shouldFix(SPAlert a, string customAlertName)
|
|
{
|
|
if (string.Compare(a.Title, customAlertName, true) == 0)
|
|
{
|
|
return true;
|
|
}
|
|
if (a.User == null)
|
|
{
|
|
return false;
|
|
}
|
|
return (string.Compare(a.User.LoginName, "tallier:company alerts owner", true) == 0);
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|