Skip to content

Commit

Permalink
Merge branch 'hotfix/Publish_parent_page'
Browse files Browse the repository at this point in the history
  • Loading branch information
jstemerdink committed Sep 8, 2017
2 parents 0cc1b5f + f4b0c72 commit 258ba6c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 81 deletions.
Binary file modified GlobalSuppressions.cs
Binary file not shown.
153 changes: 72 additions & 81 deletions SearchInitialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,35 @@ namespace EPi.Libraries.BlockSearch
[ModuleDependency(typeof(FrameworkInitialization))]
public class SearchInitialization : IInitializableModule
{
private static readonly ILogger Logger = LogManager.GetLogger(typeof(SearchInitialization));
/// <summary>
/// Gets or sets the logger
/// </summary>
/// <value>The logger.</value>
protected ILogger Logger { get; set; }

/// <summary>
/// Gets or sets the content events.
/// </summary>
/// <value>The content events.</value>
protected Injected<IContentEvents> ContentEvents { get; set; }
protected IContentEvents ContentEvents { get; set; }

/// <summary>
/// Gets or sets the content repository.
/// </summary>
/// <value>The content repository.</value>
protected Injected<IContentRepository> ContentRepository { get; set; }
protected IContentRepository ContentRepository { get; set; }

/// <summary>
/// Gets or sets the content soft link repository.
/// </summary>
/// <value>The content soft link repository.</value>
protected Injected<IContentSoftLinkRepository> ContentSoftLinkRepository { get; set; }
protected IContentSoftLinkRepository ContentSoftLinkRepository { get; set; }

/// <summary>
/// Gets or sets the content type respository.
/// </summary>
/// <value>The content type respository.</value>
protected Injected<IContentTypeRepository> ContentTypeRepository { get; set; }
protected IContentTypeRepository ContentTypeRepository { get; set; }

/// <summary>
/// Initializes this instance.
Expand All @@ -82,12 +86,18 @@ public class SearchInitialization : IInitializableModule
/// only once per AppDomain, unless the method throws an exception. If an exception is thrown, the initialization
/// method will be called repeadetly for each request reaching the site until the method succeeds.
/// </remarks>
/// <exception cref="ActivationException">if there is are errors resolving the service instance.</exception>
public void Initialize(InitializationEngine context)
{
this.ContentEvents.Service.PublishingContent += this.OnPublishingContent;
this.ContentEvents.Service.PublishedContent += this.OnPublishedContent;
this.Logger = context.Locate.Advanced.GetInstance<ILogger>();
this.ContentEvents = context.Locate.Advanced.GetInstance<IContentEvents>();
this.ContentTypeRepository = context.Locate.Advanced.GetInstance<IContentTypeRepository>();
this.ContentSoftLinkRepository = context.Locate.Advanced.GetInstance<IContentSoftLinkRepository>();
this.ContentRepository = context.Locate.Advanced.GetInstance<IContentRepository>();

Logger.Information("[Blocksearch] Initialized.");
this.ContentEvents.PublishedContent += this.OnPublishedContent;

this.Logger.Information("[Blocksearch] Initialized.");
}

/// <summary>
Expand All @@ -113,7 +123,7 @@ public void OnPublishedContent(object sender, ContentEventArgs contentEventArgs)

// Get the references to this block
List<ContentReference> referencingContentLinks =
this.ContentSoftLinkRepository.Service.Load(contentEventArgs.ContentLink, true)
this.ContentSoftLinkRepository.Load(contentEventArgs.ContentLink, true)
.Where(
link =>
(link.SoftLinkType == ReferenceType.PageLinkReference)
Expand All @@ -125,34 +135,30 @@ public void OnPublishedContent(object sender, ContentEventArgs contentEventArgs)
foreach (ContentReference referencingContentLink in referencingContentLinks)
{
PageData parent;
this.ContentRepository.Service.TryGet(referencingContentLink, out parent);
this.ContentRepository.TryGet(referencingContentLink, out parent);

// If it is not pagedata, do nothing
if (parent == null)
{
Logger.Information("[Blocksearch] Referencing content is not a page. Skipping update.");
this.Logger.Information("[Blocksearch] Referencing content is not a page. Skipping update.");
continue;
}

// Check if the containing page is published.
if (!parent.CheckPublishedStatus(PagePublishedStatus.Published))
{
Logger.Information("[Blocksearch] page named '{0}' is not published. Skipping update.", parent.Name);
this.Logger.Information("[Blocksearch] page named '{0}' is not published. Skipping update.", parent.Name);
continue;
}

// Republish the containing page.
try
{
this.ContentRepository.Service.Save(
parent.CreateWritableClone(),
SaveAction.Publish | SaveAction.ForceCurrentVersion,
AccessLevel.NoAccess);
Logger.Information("[Blocksearch] Updated containing page named '{0}'.", parent.Name);
this.RepublishParent(parent);
}
catch (AccessDeniedException accessDeniedException)
{
Logger.Error(
this.Logger.Error(
string.Format(
CultureInfo.InvariantCulture,
"[Blocksearch] Not enough accessrights to republish containing pagetype named '{0}'.",
Expand All @@ -163,25 +169,37 @@ public void OnPublishedContent(object sender, ContentEventArgs contentEventArgs)
}

/// <summary>
/// Raises the page event.
/// Resets the module into an uninitialized state.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="contentEventArgs">Event information to send to registered event handlers.</param>
public void OnPublishingContent(object sender, ContentEventArgs contentEventArgs)
/// <param name="context">The context.</param>
/// <remarks>
/// <para>
/// This method is usually not called when running under a web application since the web app may be shut down very
/// abruptly, but your module should still implement it properly since it will make integration and unit testing
/// much simpler.
/// </para>
/// <para>
/// Any work done by
/// <see
/// cref="M:EPiServer.Framework.IInitializableModule.Initialize(EPiServer.Framework.Initialization.InitializationEngine)" />
/// as well as any code executing on
/// <see cref="E:EPiServer.Framework.Initialization.InitializationEngine.InitComplete" /> should be reversed.
/// </para>
/// </remarks>
public void Uninitialize(InitializationEngine context)
{
if (contentEventArgs == null)
{
return;
}
this.ContentEvents.PublishedContent -= this.OnPublishedContent;

PageData page = contentEventArgs.Content as PageData;

if (page == null)
{
return;
}
this.Logger.Information("[Blocksearch] Uninitialized.");
}

PropertyInfo addtionalSearchContentProperty = GetAddtionalSearchContentProperty(page);
/// <summary>
/// Republishes the parent.
/// </summary>
/// <param name="parent">The parent.</param>
private void RepublishParent(PageData parent)
{
PropertyInfo addtionalSearchContentProperty = this.GetAddtionalSearchContentProperty(parent);

if (addtionalSearchContentProperty == null)
{
Expand All @@ -195,14 +213,14 @@ public void OnPublishingContent(object sender, ContentEventArgs contentEventArgs

StringBuilder stringBuilder = new StringBuilder();

ContentType contentType = this.ContentTypeRepository.Service.Load(page.ContentTypeID);
ContentType contentType = this.ContentTypeRepository.Load(parent.ContentTypeID);

foreach (PropertyDefinition current in
from d in contentType.PropertyDefinitions
where typeof(PropertyContentArea).IsAssignableFrom(d.Type.DefinitionType)
select d)
{
PropertyData propertyData = page.Property[current.Name];
PropertyData propertyData = parent.Property[current.Name];

ContentArea contentArea = propertyData.Value as ContentArea;

Expand All @@ -214,7 +232,7 @@ where typeof(PropertyContentArea).IsAssignableFrom(d.Type.DefinitionType)
foreach (ContentAreaItem contentAreaItem in contentArea.Items)
{
IContent content;
if (!this.ContentRepository.Service.TryGet(contentAreaItem.ContentLink, out content))
if (!this.ContentRepository.TryGet(contentAreaItem.ContentLink, out content))
{
continue;
}
Expand All @@ -231,7 +249,7 @@ where typeof(PropertyContentArea).IsAssignableFrom(d.Type.DefinitionType)
// Content area is not a block, but probably a page used as a teaser.
if (blockData == null)
{
Logger.Information(
this.Logger.Information(
"[Blocksearch] Contentarea item is not block data. Skipping update.",
content.Name);
continue;
Expand All @@ -252,73 +270,46 @@ where typeof(PropertyContentArea).IsAssignableFrom(d.Type.DefinitionType)
string additionalSearchContent = TextIndexer.StripHtml(stringBuilder.ToString(), 0);

// When being "delayed published" the pagedata is readonly. Create a writable clone to be safe.
PageData editablePage = page.CreateWritableClone();
PageData editablePage = parent.CreateWritableClone();
editablePage[addtionalSearchContentProperty.Name] = additionalSearchContent;

// Save the writable pagedata, do not create a new version
this.ContentRepository.Service.Save(
this.ContentRepository.Save(
editablePage,
SaveAction.Save,
SaveAction.Publish | SaveAction.ForceCurrentVersion,
AccessLevel.NoAccess);
}
catch (EPiServerException ePiServerException)
catch (EPiServerException epiServerException)
{
Logger.Error(
this.Logger.Error(
string.Format(
CultureInfo.InvariantCulture,
"[Blocksearch] Property {0} dose not exist on {1} .",
"[Blocksearch] Property {0} does not exist on {1}.",
addtionalSearchContentProperty.Name,
page.Name),
ePiServerException);
parent.Name),
epiServerException);
}
}

/// <summary>
/// Resets the module into an uninitialized state.
/// </summary>
/// <param name="context">The context.</param>
/// <remarks>
/// <para>
/// This method is usually not called when running under a web application since the web app may be shut down very
/// abruptly, but your module should still implement it properly since it will make integration and unit testing
/// much simpler.
/// </para>
/// <para>
/// Any work done by
/// <see
/// cref="M:EPiServer.Framework.IInitializableModule.Initialize(EPiServer.Framework.Initialization.InitializationEngine)" />
/// as well as any code executing on
/// <see cref="E:EPiServer.Framework.Initialization.InitializationEngine.InitComplete" /> should be reversed.
/// </para>
/// </remarks>
public void Uninitialize(InitializationEngine context)
{
this.ContentEvents.Service.PublishingContent -= this.OnPublishingContent;
this.ContentEvents.Service.PublishedContent -= this.OnPublishedContent;

Logger.Information("[Blocksearch] Uninitialized.");
}

/// <summary>
/// Gets the name of the key word property.
/// </summary>
/// <param name="page">The page.</param>
/// <returns>System.String.</returns>
private static PropertyInfo GetAddtionalSearchContentProperty(PageData page)
/// <returns>The propertyinfo.</returns>
private PropertyInfo GetAddtionalSearchContentProperty(PageData page)
{
PropertyInfo keywordsMetatagProperty =
page.GetType().GetProperties().Where(HasAttribute<AdditionalSearchContentAttribute>).FirstOrDefault();
page.GetType().GetProperties().Where(this.HasAttribute<AdditionalSearchContentAttribute>).FirstOrDefault();

return keywordsMetatagProperty;
}

/// <summary>
/// Determines whether the specified self has attribute.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T">The type of the attribute.</typeparam>
/// <param name="propertyInfo">The propertyInfo.</param>
/// <returns><c>true</c> if the specified self has attribute; otherwise, <c>false</c>.</returns>
private static bool HasAttribute<T>(PropertyInfo propertyInfo) where T : Attribute
private bool HasAttribute<T>(PropertyInfo propertyInfo) where T : Attribute
{
T attr = default(T);

Expand All @@ -328,7 +319,7 @@ private static bool HasAttribute<T>(PropertyInfo propertyInfo) where T : Attribu
}
catch (Exception exception)
{
Logger.Error("[Blocksearch] Error getting custom attribute.", exception);
this.Logger.Error("[Blocksearch] Error getting custom attribute.", exception);
}

return attr != null;
Expand All @@ -339,7 +330,7 @@ private static bool HasAttribute<T>(PropertyInfo propertyInfo) where T : Attribu
/// </summary>
/// <param name="contentData">The content data.</param>
/// <param name="contentType">Type of the content.</param>
/// <returns>IEnumerable&lt;System.String&gt;.</returns>
/// <returns>A list of prperty values.</returns>
private IEnumerable<string> GetSearchablePropertyValues(IContentData contentData, ContentType contentType)
{
if (contentType == null)
Expand Down Expand Up @@ -376,10 +367,10 @@ from d in contentType.PropertyDefinitions
/// </summary>
/// <param name="contentData">The content data.</param>
/// <param name="contentTypeID">The content type identifier.</param>
/// <returns>IEnumerable&lt;System.String&gt;.</returns>
/// <returns>A list of searchable property values.</returns>
private IEnumerable<string> GetSearchablePropertyValues(IContentData contentData, int contentTypeID)
{
return this.GetSearchablePropertyValues(contentData, this.ContentTypeRepository.Service.Load(contentTypeID));
return this.GetSearchablePropertyValues(contentData, this.ContentTypeRepository.Load(contentTypeID));
}
}
}

0 comments on commit 258ba6c

Please sign in to comment.