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

254 lines
9.2 KiB
C#

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Taloyhtio.CondoUpdate.Common;
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
namespace CondoUpdate.ResponsiveLayout.ToimistonSisaisetWebPartTallier
{
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)
{
using (var site = new SPSite(url))
{
using (var acWeb = site.RootWeb)
{
if (!acWeb.Exists)
{
return;
}
if (!PublishingWeb.IsPublishingWeb(acWeb))
{
return;
}
var pweb = PublishingWeb.GetPublishingWeb(acWeb);
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)acWeb.Language);
this.addWebPart(pweb);
this.removeWebPart(acWeb);
}
}
}
private void removeWebPart(SPWeb pmcWeb)
{
bool pageExists = false;
try
{
pageExists = pmcWeb.GetFolder((int)pmcWeb.Language == 1035 ? "Sivut" : "Pages").Files.Cast<SPFile>().Any(f => f.Name.ToLower() == "sisäisetasiat.aspx");
if (!pageExists)
{
this.error("Unable to find guide pages. Site won't be updated");
return;
}
var file =
pmcWeb.GetFolder((int)pmcWeb.Language == 1035 ? "Sivut" : "Pages")
.Files.Cast<SPFile>()
.First(f => f.Name.ToLower() == "sisäisetasiat.aspx");
if (file.CheckOutType == SPFile.SPCheckOutType.None)
{
file.CheckOut();
}
else
{
// if file was checked out by another user, need undo check out and check in again in order to avoid errors
// when add web part
file.UndoCheckOut();
file.CheckOut();
}
using (var webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
if (webPartManager == null)
{
this.error("Web part manager is null. Site won't be updated");
file.UndoCheckOut();
return;
}
var webParts = webPartManager.WebParts;
if (webParts != null)
{
if (webParts.Cast<WebPart>().Any(wp => wp.Title == "Sisäiset tiedotteet"))
{
var webPart = webParts.Cast<WebPart>().First(wp => wp.Title == "Sisäiset tiedotteet");
webPartManager.DeleteWebPart(webPart);
}
}
file.Update();
file.CheckIn("Sisäiset tiedotteet is removed");
if (file.Item.ParentList.EnableMinorVersions)
{
file.Publish("Sisäiset tiedotteet is removed");
}
if (file.Item.ParentList.EnableModeration)
{
file.Approve("Sisäiset tiedotteet is removed");
}
}
}
catch (Exception ex)
{
this.error("Error occured when remove web part from the '{0}' site:\n{1}\n{2}", pmcWeb.Url, ex.Message, ex.StackTrace);
if (pageExists)
{
var file = pmcWeb.GetFolder((int)pmcWeb.Language == 1035 ? "Sivut" : "Pages")
.Files.Cast<SPFile>()
.First(f => f.Name.ToLower() == "sisäisetasiat.aspx");
if (file.CheckOutType != SPFile.SPCheckOutType.None)
{
file.UndoCheckOut();
}
}
}
}
private void addWebPart(PublishingWeb pweb)
{
try
{
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
{
// if file was checked out by another user, need undo check out and check in again in order to avoid errors
// when add web part
file.UndoCheckOut();
file.CheckOut();
}
using (var webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
if (webPartManager == null)
{
this.error("Web part manager is null. Site won't be updated");
file.UndoCheckOut();
return;
}
bool alreadyExists = false;
var webParts = webPartManager.WebParts;
if (webParts != null)
{
if (webParts.Cast<WebPart>().Any(wp => wp.GetType().FullName == "Taloyhtio.CompanyAutomation.IntraOfficeWebPart"))
{
this.warn("IntraOfficeWebPart already exist on start page. Site won't be updated");
alreadyExists = true;
}
}
if (alreadyExists)
{
file.UndoCheckOut();
return;
}
using (var fs = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText("IntraOfficeWebPart.webpart"))))
{
using (var reader = new XmlTextReader(fs))
{
string error;
WebPart wp = webPartManager.ImportWebPart(reader, out error);
if (wp == null)
{
this.error("Imported web part is null. Site '{0}' won't be updated. Error message: {1}", pweb.Web.Url, error);
file.UndoCheckOut();
return;
}
webPartManager.AddWebPart(wp, "LeftColumnZone", 2);
webPartManager.SaveChanges(wp);
}
}
file.Update();
file.CheckIn("IntraOfficeWebPart is added");
if (file.Item.ParentList.EnableMinorVersions)
{
file.Publish("IntraOfficeWebPart is added");
}
if (file.Item.ParentList.EnableModeration)
{
file.Approve("IntraOfficeWebPart is added");
}
}
}
catch (Exception x)
{
this.error("Error occured when add web part to the '{0}' site:\n{1}\n{2}", pweb.Web.Url, x.Message, x.StackTrace);
var file = pweb.DefaultPage;
if (file.CheckOutType != SPFile.SPCheckOutType.None)
{
file.UndoCheckOut();
}
}
}
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 });
}
}
}
}