Skip to content

Commit

Permalink
The problematic Linq query used by Social Search Provder has been rew…
Browse files Browse the repository at this point in the history
…orked.
  • Loading branch information
IvanDomashchenko committed Feb 3, 2017
1 parent 260ccf9 commit b9de1fd
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Sitecore.Support.139772.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{B331DA70-FD0E-4667-939C-24001DBAA105}") = "Sitecore.Support.139772", "Sitecore.Support.139772\Sitecore.Support.139772.csproj", "{5B8B743C-C225-48D8-A388-3CE0BD48C438}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5B8B743C-C225-48D8-A388-3CE0BD48C438}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B8B743C-C225-48D8-A388-3CE0BD48C438}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B8B743C-C225-48D8-A388-3CE0BD48C438}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B8B743C-C225-48D8-A388-3CE0BD48C438}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<pipelines>
<social.initializeApi>
<processor type="Sitecore.Social.Infrastructure.IoC.IoCInitialization, Sitecore.Social.Infrastructure">
<assemblies>
<assembly patch:after="*[text()='Sitecore.Social.dll']" hint="support">Sitecore.Support.139772.dll</assembly>
</assemblies>
</processor>
</social.initializeApi>
</pipelines>
<social>
<indexConfigurations>
<socialCloudIndexConfiguration>
<fieldMap>
<fieldNames>
<fieldType fieldName="is_in_workflow" cloudFieldName="is_in_workflow" boost="1f" type="System.Boolean" settingType="Sitecore.ContentSearch.Azure.CloudSearchFieldConfiguration, Sitecore.ContentSearch.Azure"/>
</fieldNames>
</fieldMap>
<fields hint="raw:AddComputedIndexField">
<field fieldName="is_in_workflow">Sitecore.Support.Social.Client.MessagePosting.UI.CustomFields.IsInWorkflow,Sitecore.Support.139772</field>
</fields>
</socialCloudIndexConfiguration>
</indexConfigurations>
</social>
</sitecore>
</configuration>
14 changes: 14 additions & 0 deletions src/Sitecore.Support.139772/ExSearchItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

namespace Sitecore.Support.Social.Search
{
using System;
using ContentSearch;
using Sitecore.Social.Search;

[Serializable]
public class ExSearchItem : SearchItem
{
[IndexField("is_in_workflow")]
public bool IsInWorkflow { get; set; }
}
}
36 changes: 36 additions & 0 deletions src/Sitecore.Support.139772/IsInWorkflow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

namespace Sitecore.Support.Social.Client.MessagePosting.UI.CustomFields
{
using ContentSearch;
using ContentSearch.ComputedFields;
using Data;
using Data.Items;
using Workflows;

public class IsInWorkflow : IComputedIndexField
{
public object ComputeFieldValue(IIndexable indexable)
{
Item item = indexable as SitecoreIndexableItem;

Database database = item?.Database;

if (database == null)
{
return null;
}

IWorkflow workflow = database.WorkflowProvider?.GetWorkflow(item);

if (workflow == null)
{
return null;
}

return !workflow.IsApproved(item);
}

public string FieldName { get; set; }
public string ReturnType { get; set; }
}
}
6 changes: 6 additions & 0 deletions src/Sitecore.Support.139772/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Sitecore.Support.139772")]
[assembly: AssemblyProduct("Sitecore.Support.139772")]
[assembly: ComVisible(false)]
125 changes: 125 additions & 0 deletions src/Sitecore.Support.139772/SearchProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@

namespace Sitecore.Support.Social.Search
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ContentSearch.Security;
using Sitecore.Data;
using Sitecore.Social.Configuration.Model;
using Sitecore.Social.Domain.Model;
using Sitecore.Social.Search;
using Sitecore.Social.Infrastructure.Extensions;
using Sitecore.ContentSearch.Linq.Utilities;

public class SearchProvider : Sitecore.Social.Search.SearchProvider, ISearchProvider
{
IEnumerable<Identifier> ISearchProvider.GetMessagesReadyToPostAutomatically(IEnumerable<Identifier> accountIds)
{
Expression<Func<ExSearchItem, bool>> messageFilterExpression = searchItem =>
(searchItem.MessagePostedDate == DateTime.MinValue.Date) &&
//(searchItem.FinalWorkflowState || (searchItem.WorkflowStateId == ID.Null));
!searchItem.IsInWorkflow;

var messagesByMessageFilter = this.SearchExItems(messageFilterExpression.And<ExSearchItem>(this.TemplateIsMessageExpression())).ToList();

if (!messagesByMessageFilter.Any())
{
return Enumerable.Empty<Identifier>();
}

// search messages by Posting Configuration fields
var contentPostingConfigurationTemplateId = this.ConfigurationFactory
.Get<PostingConfigurationsConfiguration>()
.PostingConfigurations
.Where(postingConfiguration => string.Equals(postingConfiguration.Name, "ContentPosting", StringComparison.Ordinal))
.Select(postingConfiguration => ID.Parse(postingConfiguration.TemplateId))
.First();

Expression<Func<SearchItem, bool>> postingConfigurationFilterExpression = searchItem =>
(searchItem.TemplateId == contentPostingConfigurationTemplateId) &&
searchItem.PostAutomatically && searchItem.ItemPublished;

Expression<Func<SearchItem, bool>> accountFilterExpression = null;

foreach (var accountId in accountIds)
{
var normalizedAccountId = accountId.GetID();

if (accountFilterExpression == null)
{
accountFilterExpression = searchItem => searchItem.AccountId == normalizedAccountId;
}
else
{
accountFilterExpression = accountFilterExpression.Or(searchItem => searchItem.AccountId == normalizedAccountId);
}
}

if (accountFilterExpression != null)
{
postingConfigurationFilterExpression = postingConfigurationFilterExpression.And(accountFilterExpression);
}

var messagesByPostingConfigurationFilter = this.SearchItems(postingConfigurationFilterExpression, searchItem => searchItem.Parent.GetIdentifier()).ToList();

// return messages that exists in both search results
return !messagesByPostingConfigurationFilter.Any()
? Enumerable.Empty<Identifier>() :
messagesByMessageFilter.Intersect(messagesByPostingConfigurationFilter);
}

protected IEnumerable<Identifier> SearchExItems(Expression<Func<ExSearchItem, bool>> whereExpression)
{
if (this.SearchIndex == null)
{
return new List<Identifier>();
}

using (var searchContext = this.SearchIndex.CreateSearchContext(SearchSecurityOptions.DisableSecurityCheck))
{
return searchContext.GetQueryable<ExSearchItem>()
.Where(whereExpression)
.ToList()
.Select(searchItem => searchItem.ItemId.GetIdentifier());
}
}

private Expression<Func<ExSearchItem, bool>> TemplateIsMessageExpression()
{
return this.OrTemplateExpression(
this.ConfigurationFactory
.Get<NetworksConfiguration>()
.Networks
.SelectMany(networkSettings => networkSettings.Items.Select(messageSettings => ID.Parse(messageSettings.MessageTemplateId))));
}

private Expression<Func<ExSearchItem, bool>> OrTemplateExpression(IEnumerable<ID> templateIds)
{
var templateIdList = templateIds as IList<ID> ?? templateIds.ToList();

if (!templateIdList.Any())
{
return item => false;
}

Expression<Func<ExSearchItem, bool>> whereExpression = null;
foreach (var templateId in templateIdList)
{
var filterTemplateId = templateId;

if (whereExpression == null)
{
whereExpression = searchItem => searchItem.TemplateId == filterTemplateId;
}
else
{
whereExpression = whereExpression.Or(searchItem => searchItem.TemplateId == filterTemplateId);
}
}

return whereExpression;
}
}
}
122 changes: 122 additions & 0 deletions src/Sitecore.Support.139772/Sitecore.Support.139772.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{5B8B743C-C225-48D8-A388-3CE0BD48C438}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Sitecore.Support</RootNamespace>
<AssemblyName>Sitecore.Support.139772</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<UseIISExpress>true</UseIISExpress>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
<UseGlobalApplicationHostFile />
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Ninject, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
<HintPath>..\packages\SC.Ninject.8.2.2\lib\Ninject.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sitecore.ContentSearch, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SC.Sitecore.ContentSearch.8.2.2\lib\Sitecore.ContentSearch.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sitecore.ContentSearch.Linq, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SC.Sitecore.ContentSearch.Linq.8.2.2\lib\Sitecore.ContentSearch.Linq.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sitecore.Kernel">
<HintPath>..\packages\SC.Sitecore.Kernel.8.2.2\lib\Sitecore.Kernel.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sitecore.Social, Version=10.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SC.Sitecore.Social.8.2.2\lib\Sitecore.Social.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sitecore.Social.Configuration, Version=10.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SC.Sitecore.Social.Configuration.8.2.2\lib\Sitecore.Social.Configuration.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Sitecore.Social.Domain, Version=10.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SC.Sitecore.Social.Domain.8.2.2\lib\Sitecore.Social.Domain.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sitecore.Social.Infrastructure, Version=10.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SC.Sitecore.Social.Infrastructure.8.2.2\lib\Sitecore.Social.Infrastructure.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
</ItemGroup>
<ItemGroup>
<Compile Include="ExSearchItem.cs" />
<Compile Include="IsInWorkflow.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SearchProvider.cs" />
<Compile Include="SupportModule139772.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="App_Config\Include\zzz\Sitecore.Support.139772.config">
<SubType>Designer</SubType>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="web.config" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<UseIIS>True</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>0</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost:63273/</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
<CustomServerUrl>
</CustomServerUrl>
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties>
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
</Project>
14 changes: 14 additions & 0 deletions src/Sitecore.Support.139772/SupportModule139772.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

namespace Sitecore.Support.Social.IoC.Modules
{
using ISearchProvider = Sitecore.Social.Search.ISearchProvider;
using Ninject.Modules;

public class SupportModule139772 : NinjectModule
{
public override void Load()
{
this.Rebind<ISearchProvider>().To<Sitecore.Support.Social.Search.SearchProvider>();
}
}
}
11 changes: 11 additions & 0 deletions src/Sitecore.Support.139772/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SC.Ninject" version="8.2.2" targetFramework="net452" />
<package id="SC.Sitecore.ContentSearch" version="8.2.2" targetFramework="net452" />
<package id="SC.Sitecore.ContentSearch.Linq" version="8.2.2" targetFramework="net452" />
<package id="SC.Sitecore.Kernel" version="8.2.2" targetFramework="net452" />
<package id="SC.Sitecore.Social" version="8.2.2" targetFramework="net452" />
<package id="SC.Sitecore.Social.Configuration" version="8.2.2" targetFramework="net452" />
<package id="SC.Sitecore.Social.Domain" version="8.2.2" targetFramework="net452" />
<package id="SC.Sitecore.Social.Infrastructure" version="8.2.2" targetFramework="net452" />
</packages>
4 changes: 4 additions & 0 deletions src/Sitecore.Support.139772/web.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- Do not change this file - it must be empty, see https://github.com/SitecoreSupport/PatchCreator/issues/13 -->
</configuration>

0 comments on commit b9de1fd

Please sign in to comment.