using System; using System.Globalization; using System.Linq; using System.Threading; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint.WebPartPages; using Taloyhtio.CondoAutomation.Resources; using Taloyhtio.CondoAutomation.Utils; using Taloyhtio.CondoUpdate.Common; using WebPart = Microsoft.SharePoint.WebPartPages.WebPart; namespace CondoUpdate.RetargetNewsWebParts { public class UpdaterImpl : ICondoUpdater { public event EventHandler OnNotify; public void Update(object args) { string url = args as string; if (string.IsNullOrEmpty(url)) { this.warn("Url is empty"); return; } try { this.updateImpl(SPUrlUtility.CombineUrl(url, ""), () => TaloyhtioPropertyManagersSite.List_TenantsReleases_Title, Constants.Retargeting.CONDO_SITE_OLD_TARGET_LIST_ID_ON_START_PAGE); this.updateImpl(SPUrlUtility.CombineUrl(url, "Osakkaille"), () => TaloyhtioPropertyManagersSite.List_LandlordsReleases_Title, Constants.Retargeting.LANDLORDS_OLD_TARGET_LIST_ID_ON_START_PAGE); this.updateImpl(SPUrlUtility.CombineUrl(url, "Hallitukselle"), () => TaloyhtioPropertyManagersSite.List_BoardMembersReleases_Title, Constants.Retargeting.BOARD_MEMBERS_OLD_TARGET_LIST_ID_ON_START_PAGE); } 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, Func getListTitle, string oldListId) { using (var site = new SPSite(url)) { using (var web = site.OpenWeb()) { if (!web.Exists) { return; } Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language); // 2020-04-21: need to get list title only after setup of current thread language string listTitle = getListTitle(); if (!PublishingWeb.IsPublishingWeb(web)) { return; } var pweb = PublishingWeb.GetPublishingWeb(web); WebPartsHelper.SetupDataFormWebPartOnStartPage(pweb, listTitle, oldListId, Constants.Retargeting.OLD_TARGET_WEB_URL); WebPartsHelper.SetupWebPartsOnNewsPage(pweb, "Uutinen", listTitle, Constants.Retargeting.OLD_TARGET_LIST_ID_ON_NEWS_PAGE, Constants.Retargeting.OLD_TARGET_WEB_URL); } } } 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 }); } } } }