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

209 lines
8.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Taloyhtio.CondoUpdate.Common;
using Taloyhtio.WhatsNewWebpart;
namespace CondoUpdate.ResponsiveLayout.CompanySiteConfigureWebParts
{
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)
{
configureWebParts(url);
}
private void configureWebParts(string url)
{
PublishingWeb pweb = null;
try
{
SPSecurity.RunWithElevatedPrivileges(() =>
{
using (var site = new SPSite(url))
{
using (var web = site.OpenWeb())
{
if (web == null)
{
return;
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int) web.Language);
bool condoAllowUnsafeUpdates = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
pweb = PublishingWeb.GetPublishingWeb(web);
if (pweb == null || pweb.DefaultPage == null ||
string.IsNullOrEmpty(pweb.DefaultPage.ServerRelativeUrl))
{
this.warn("Publishing web of '{0}' is null. It will be ignored", url);
return;
}
var file = pweb.DefaultPage;
if (file == null)
{
this.error("Start page is null. Site won't be updated");
return;
}
if (file.CheckOutType == SPFile.SPCheckOutType.None)
{
file.CheckOut();
}
else
{
file.UndoCheckOut();
file.CheckOut();
}
using (var manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
if (manager == null)
{
file.UndoCheckOut();
this.error("Web part manager is null. Site won't be updated");
return;
}
if (manager.WebParts != null)
{
// if (!manager.WebParts.Cast<WebPart>().Any(wp => wp.Title.ToLower().Equals("uusimmat päivitykset")) &&
// !manager.WebParts.Cast<WebPart>().Any(wp => wp.Title.ToLower().Equals("tilitoimisto tiedottaa - asiakastiedotteet")) &&
// !manager.WebParts.Cast<WebPart>().Any(wp => wp.Title.ToLower().Equals("yhteystiedot")))
// {
// file.UndoCheckOut();
// this.error("Web parts are already configured. Site won't be updated");
// return;
// }
var webPartsToDelete = new List<WebPart>();
foreach (WebPart wp in manager.WebParts)
{
if (wp.Title.ToLower().Equals("uusimmat päivitykset"))
{
wp.Title = "Uutta palvelussa";
var whatsNew = wp as WhatsNewWebpart;
if (whatsNew != null)
{
whatsNew.MaxItems = 15;
whatsNew.ShowTime = false;
}
manager.SaveChanges(wp);
}
if (wp.Title.ToLower().Equals("ilmoitustaulu"))
{
manager.MoveWebPart(wp, "LeftColumnZone", 1);
manager.SaveChanges(wp);
}
if (wp.Title.ToLower().Equals("keskustelu") ||
wp.Title.ToLower().Equals("keskustelu tilitoimistosi kanssa") || // for tevilex
wp.Title.ToLower().Equals("huomioitavaa ja kommentteja")) // for tilitoimistoverso
{
manager.MoveWebPart(wp, "RightColumnZone", 2);
manager.SaveChanges(wp);
}
if (wp.Title.ToLower().Equals("tilitoimisto tiedottaa - asiakastiedotteet"))
{
wp.Title = "Kirjanpitäjä tiedottaa";
manager.MoveWebPart(wp, "RightColumnZone", 3);
//manager.MoveWebPart(wp, "LeftColumnZone", 0); // for tevilex
manager.SaveChanges(wp);
}
if (wp.Title.ToLower().Equals("yhteystiedot"))
{
webPartsToDelete.Add(wp);
}
}
foreach (var wp in webPartsToDelete)
{
manager.DeleteWebPart(wp);
}
}
file.Update();
file.CheckIn("Updated company site webparts");
if (file.Item.ParentList.EnableMinorVersions)
{
file.Publish("Updated company site webparts");
}
if (file.Item.ParentList.EnableModeration)
{
file.Approve("Updated company site webparts");
}
}
web.AllowUnsafeUpdates = condoAllowUnsafeUpdates;
}
}
});
}
catch (Exception ex)
{
if (pweb != null)
{
this.error("Error occured when update web part on '{0}' site:\n{1}\n{2}", pweb.Web.Url, ex.Message,
ex.StackTrace);
var file = pweb.DefaultPage;
if (file.CheckOutType != SPFile.SPCheckOutType.None)
{
file.UndoCheckOut();
}
}
}
}
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 });
}
}
}
}