Skip to content

Commit

Permalink
Added PropertyExtractResolver.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbreuer committed Sep 30, 2016
1 parent cd5834f commit ad624ef
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sources/Umbraco.Extensions/Events/UmbracoEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace Umbraco.Extensions.Events
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
using Umbraco.Extensions.ContentFinders;
using Umbraco.Extensions.Extract;
using Umbraco.Extensions.Models.Custom;
using Umbraco.Extensions.UrlProviders;
using Umbraco.Web;
using Umbraco.Web.Cache;
Expand Down Expand Up @@ -79,6 +81,21 @@ protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplica
externalIndexer.GatheringNodeData += this.ExternalIndexerGatheringContentData;
}

/// <summary>
/// The application initialized event
/// </summary>
/// <param name="umbracoApplication">
/// The umbraco application.
/// </param>
/// <param name="applicationContext">
/// The application context.
/// </param>
protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
// Resolve all property extract classes.
PropertyExtractResolver.Current = new PropertyExtractResolver(PluginManager.Current.ResolveTypes<PropertyExtractBase>());
}

private void PageCacheRefresherCacheUpdated(PageCacheRefresher sender, Core.Cache.CacheRefresherEventArgs e)
{
// After content has been updated clear content finder cache.
Expand Down
46 changes: 46 additions & 0 deletions Sources/Umbraco.Extensions/Extract/PropertyExtractResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PropertyExtractResolver.cs" company="Colours B.V.">
// © Colours B.V. 2015
// </copyright>
// <summary>
// The property extract resolver.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Umbraco.Extensions.Extract
{
using System;
using System.Collections.Generic;
using System.Linq;

using Umbraco.Core.ObjectResolution;
using Umbraco.Extensions.Models.Custom;

/// <summary>
/// The property extract resolver.
/// </summary>
public class PropertyExtractResolver : ManyObjectsResolverBase<PropertyExtractResolver, PropertyExtractBase>
{
/// <summary>
/// Initializes a new instance of the <see cref="PropertyExtractResolver"/> class.
/// </summary>
/// <param name="extractItems">
/// The extract items.
/// </param>
public PropertyExtractResolver(IEnumerable<Type> extractItems)
: base(extractItems, ObjectLifetimeScope.Application)
{
}

/// <summary>
/// Gets the property extract items.
/// </summary>
public List<PropertyExtractBase> PropertyExtractItems
{
get
{
return this.Values.ToList();
}
}
}
}
71 changes: 71 additions & 0 deletions Sources/Umbraco.Extensions/Models/Custom/PropertyExtractBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PropertyExtractBase.cs" company="Colours B.V.">
// © Colours B.V. 2015
// </copyright>
// <summary>
// The property extract base.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Umbraco.Extensions.Models.Custom
{
using System.Linq;
using System.Text;

using Umbraco.Core.Models;
using Umbraco.Web;

/// <summary>
/// The property extract base.
/// </summary>
public abstract class PropertyExtractBase
{
/// <summary>
/// Gets the allowed types.
/// </summary>
public abstract string[] AllowedTypes { get; }

/// <summary>
/// Checks if the extract is for this property type.
/// </summary>
/// <param name="propertyTypeAlias">
/// The property type alias.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public bool IsExtractForPropertyType(string propertyTypeAlias)
{
return this.AllowedTypes.Contains(propertyTypeAlias);
}

/// <summary>
/// Converts the content into a string for Examine.
/// </summary>
/// ///
/// <param name="extractedContent">
/// The extracted content.
/// </param>
/// <param name="content">
/// The content.
/// </param>
/// <param name="alias">
/// The alias.
/// </param>
/// <param name="language">
/// The language.
/// </param>
public virtual void Examine(
StringBuilder extractedContent,
IPublishedContent content,
string alias,
string language = null)
{
var text = content.GetPropertyValue<string>(alias);
if (!string.IsNullOrEmpty(text))
{
extractedContent.Append(" " + content.GetPropertyValue<string>(alias));
}
}
}
}
2 changes: 2 additions & 0 deletions Sources/Umbraco.Extensions/Umbraco.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@
<Compile Include="Controllers\UrlPreviewApiController.cs" />
<Compile Include="CustomBootManager.cs" />
<Compile Include="Events\UmbracoEvents.cs" />
<Compile Include="Extract\PropertyExtractResolver.cs" />
<Compile Include="Global.cs" />
<Compile Include="Models\Builder.cs">
<Generator>UmbracoModelsBuilder</Generator>
Expand All @@ -297,6 +298,7 @@
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
<Compile Include="Models\Custom\PropertyExtractBase.cs" />
<Compile Include="Models\Image.cs" />
<Compile Include="Models\Detached.generated.cs">
<DependentUpon>Builder.cs</DependentUpon>
Expand Down

0 comments on commit ad624ef

Please sign in to comment.