Created
October 24, 2014 18:30
-
-
Save herskinduk/ee45e2e9fc1917eb6f30 to your computer and use it in GitHub Desktop.
Fix for fortis lucene content search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using Fortis; | |
using Fortis.Model; | |
using Sitecore.ContentSearch; | |
using Sitecore.ContentSearch.Linq.Methods; | |
using Sitecore.ContentSearch.Security; | |
using Sitecore.Data; | |
using Sitecore.Web.UI.WebControls; | |
namespace FortisExtension | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Fortis.Model; | |
using Lucene.Net.Documents; | |
using Sitecore.ContentSearch.Linq.Common; | |
using Sitecore.ContentSearch.LuceneProvider; | |
using Sitecore.Diagnostics; | |
public class LuceneDocumentTypeMapper : DefaultLuceneDocumentTypeMapper | |
{ | |
protected override void MapFieldValuesToType<TElement>(IDictionary<string, object> fieldValues, TElement result, DocumentTypeMapInfo documentTypeMapInfo) | |
{ | |
base.MapFieldValuesToType<TElement>(fieldValues, result, documentTypeMapInfo); | |
} | |
public override TElement MapToType<TElement>(Document document, SelectMethod selectMethod, | |
IEnumerable<IFieldQueryTranslator> virtualFieldProcessors, | |
IEnumerable<IExecutionContext> executionContexts, | |
SearchSecurityOptions securityOptions) | |
{ | |
var typeOfTElement = typeof (TElement); | |
if (!typeof (IItemWrapper).IsAssignableFrom(typeOfTElement)) | |
{ | |
return base.MapToType<TElement>(document, selectMethod, virtualFieldProcessors, executionContexts, | |
securityOptions); | |
} | |
Guid itemId; | |
Guid templateId; | |
var fields = ExtractFieldsFromDocument(document, virtualFieldProcessors); | |
if (fields.ContainsKey("_group") && | |
fields.ContainsKey("_template") && | |
Guid.TryParse(fields["_group"].ToString(), out itemId) && | |
Guid.TryParse(fields["_template"].ToString(), out templateId)) | |
{ | |
var item = Global.SpawnProvider.FromItem(itemId, templateId, typeOfTElement, fields); | |
if (item is TElement) | |
{ | |
return (TElement) item; | |
} | |
} | |
if (fields.ContainsKey("_uniqueid")) | |
{ | |
var id = fields["_uniqueid"].ToString(); | |
var uri = ItemUri.Parse(id); | |
var item = Sitecore.Context.Database.GetItem(uri.ToDataUri()); | |
if (item != null) | |
{ | |
var mappedItem = Global.SpawnProvider.FromItem(item); | |
if (mappedItem is TElement) | |
{ | |
return (TElement) mappedItem; | |
} | |
} | |
} | |
return default(TElement); | |
} | |
public override TElement MapToType<TElement>(Document document, Sitecore.ContentSearch.Linq.Methods.SelectMethod selectMethod, | |
IEnumerable<IFieldQueryTranslator> virtualFieldProcessors, | |
Sitecore.ContentSearch.Security.SearchSecurityOptions securityOptions) | |
{ | |
var typeOfTElement = typeof(TElement); | |
if (!typeof(IItemWrapper).IsAssignableFrom(typeOfTElement)) | |
{ | |
return base.MapToType<TElement>(document, selectMethod, virtualFieldProcessors, securityOptions); | |
} | |
Guid itemId; | |
Guid templateId; | |
var fields = ExtractFieldsFromDocument(document, virtualFieldProcessors); | |
if (fields.ContainsKey("_group") && | |
fields.ContainsKey("_template") && | |
Guid.TryParse(fields["_group"].ToString(), out itemId) && | |
Guid.TryParse(fields["_template"].ToString(), out templateId)) | |
{ | |
var item = Global.SpawnProvider.FromItem(itemId, templateId, typeOfTElement, fields); | |
if (item is TElement) | |
{ | |
return (TElement)item; | |
} | |
} | |
if (fields.ContainsKey("_uniqueid")) | |
{ | |
var id = fields["_uniqueid"].ToString(); | |
var uri = ItemUri.Parse(id); | |
var item = Sitecore.Context.Database.GetItem(uri.ToDataUri()); | |
if (item != null) | |
{ | |
var mappedItem = Global.SpawnProvider.FromItem(item); | |
if (mappedItem is TElement) | |
{ | |
return (TElement) mappedItem; | |
} | |
} | |
} | |
return default(TElement); | |
} | |
private Dictionary<string, object> ExtractFieldsFromDocument(Document document, IEnumerable<IFieldQueryTranslator> virtualFieldProcessors) | |
{ | |
Assert.ArgumentNotNull(document, "document"); | |
IDictionary<string, object> dictionary = new Dictionary<string, object>(); | |
foreach (var grouping in document.GetFields().GroupBy(f => f.Name)) | |
{ | |
if (grouping.Count() > 1) | |
{ | |
dictionary[grouping.Key] = string.Join("|", grouping.Select(x => x.StringValue)); | |
} | |
else | |
{ | |
dictionary[grouping.Key] = grouping.First().StringValue; | |
} | |
} | |
if (virtualFieldProcessors != null) | |
{ | |
dictionary = virtualFieldProcessors.Aggregate(dictionary, (current, processor) => processor.TranslateFieldResult(current, index.FieldNameTranslator)); | |
} | |
return dictionary.ToDictionary(x => x.Key, x => x.Value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment