Skip to content

Instantly share code, notes, and snippets.

@blacktambourine
Created November 10, 2017 06:00
Show Gist options
  • Save blacktambourine/bbc0cb0a00e37387c6e075bfaac8c7f7 to your computer and use it in GitHub Desktop.
Save blacktambourine/bbc0cb0a00e37387c6e075bfaac8c7f7 to your computer and use it in GitHub Desktop.
Custom Indexable item type for my non-sitecore data
using System;
using System.Collections.Generic;
using System.Globalization;
using MyCustomModels.Flights.Model;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Converters;
using Sitecore.Data;
using Sitecore.Diagnostics;
namespace Business.Search.Json
{
public class IndexableJsonField : IIndexableDataField
{
public string Name { get; set; }
public string TypeKey { get; set; }
public Type FieldType { get; set; }
public object Value { get; set; }
public object Id { get; set; }
}
public class JsonIndexableItem : IHierarchicalIndexable, IIndexable, IIndexableBuiltinFields
{
public virtual IIndexFieldStorageValueFormatter IndexFieldStorageValueFormatter { get; set; }
public JsonIndexableItem(KeyValuePair<string, FlightDetail> item)
{
Assert.ArgumentNotNull(item, nameof(item));
this.Item = item;
}
public virtual KeyValuePair<string, FlightDetail> Item { get; protected set; }
public static implicit operator JsonIndexableItem(KeyValuePair<string, FlightDetail> item)
{
return item.Value == null ? (JsonIndexableItem)null : new JsonIndexableItem(item);
}
public static implicit operator KeyValuePair<string, FlightDetail>(JsonIndexableItem indexable)
{
return indexable == null ? new KeyValuePair<string, FlightDetail>() : indexable.Item;
}
public virtual IIndexableId Id
{
get
{
return new IndexableId<string>(this.Item.Key.Replace(":", ""));
}
}
public virtual IIndexableUniqueId UniqueId
{
get
{
return new IndexableUniqueId<string>(this.Item.Key.Replace(":", ""));
}
}
public virtual string DataSource
{
get
{
return "Fids Json";
}
}
public virtual string AbsolutePath
{
get
{
return "/sitecore/content/Home/passengers/flights/departures-and-arrivals/" + this.Item.Key.Replace(":", "");
}
}
public virtual CultureInfo Culture
{
get
{
return CultureInfo.CurrentCulture;
}
}
public virtual IEnumerable<IIndexableDataField> Fields
{
get
{
var fieldList = new List<IIndexableDataField>();
var fObj = this.Item.Value;
var itemType = fObj.GetType();
foreach (var propertyInfo in itemType.GetProperties())
{
if (!propertyInfo.CanRead)
{
continue;
}
var fName = propertyInfo.Name;
var fValue = propertyInfo.GetValue(fObj);
Type fType = Type.GetType("System.String"); //map to a string to keep things simple
var newField = new IndexableJsonField{ Name = fName, Value = fValue, FieldType = fType, Id = fName, TypeKey = "System.String" };
fieldList.Add(newField);
}
return fieldList;
}
}
public virtual void LoadAllFields()
{
//not needed, specific to Sitecore items
}
public virtual IIndexableDataField GetFieldById(object fieldId)
{
var fObj = this.Item.Value;
var itemType = fObj.GetType();
foreach (var propertyInfo in itemType.GetProperties())
{
if (!propertyInfo.CanRead)
{
continue;
}
if (propertyInfo.Name != fieldId.ToString())
{
continue;
}
var fName = propertyInfo.Name;
var fValue = propertyInfo.GetValue(fObj);
var fType = propertyInfo.GetType();
return new IndexableJsonField { Name = fName, Value = fValue, FieldType = fType, Id = fName, TypeKey = fType.FullName };
}
return null;
}
public virtual IIndexableDataField GetFieldByName(string fieldName)
{
var fObj = this.Item.Value;
var itemType = fObj.GetType();
foreach (var propertyInfo in itemType.GetProperties())
{
if (!propertyInfo.CanRead)
{
continue;
}
if (propertyInfo.Name != fieldName)
{
continue;
}
var fName = propertyInfo.Name;
var fValue = propertyInfo.GetValue(fObj);
var fType = propertyInfo.GetType();
return new IndexableJsonField { Name = fName, Value = fValue, FieldType = fType, Id = fName, TypeKey = fType.FullName };
}
return null;
}
string IIndexableBuiltinFields.ID
{
get
{
return this.Item.Key;
}
}
string IIndexableBuiltinFields.Database
{
get
{
return "web";
}
}
string IIndexableBuiltinFields.Language
{
get
{
return "en-au";
}
}
object IIndexableBuiltinFields.TemplateId
{
get
{
//a dummy template id, not actually used
return (object)new ID("{9B0146DA-B70B-47F7-BF2B-63839858C068}");
}
}
bool IIndexableBuiltinFields.IsLatestVersion { get; set; }
int IIndexableBuiltinFields.Version
{
get
{
return 1;
}
}
object IIndexableBuiltinFields.Group
{
get
{
return (object)this.Item.Key;
}
}
bool IIndexableBuiltinFields.IsClone
{
get
{
return false;
}
}
string IIndexableBuiltinFields.FullPath
{
get
{
return "/sitecore/content/Home/passengers/flights/departures-and-arrivals/" + this.Item.Key.Replace(":", "").ToLower();
}
}
string IIndexableBuiltinFields.Name
{
get
{
return this.Item.Key;
}
}
string IIndexableBuiltinFields.DisplayName
{
get
{
return this.Item.Key;
}
}
object IIndexableBuiltinFields.Parent
{
get
{
return null;
}
}
string IIndexableBuiltinFields.CreatedBy
{
get
{
return "sitecore\admin";
}
}
string IIndexableBuiltinFields.UpdatedBy
{
get
{
return "sitecore\admin";
}
}
string IIndexableBuiltinFields.TemplateName
{
get
{
return "FlightInfo";
}
}
DateTime IIndexableBuiltinFields.CreatedDate
{
get
{
return DateTime.Now;
}
}
DateTime IIndexableBuiltinFields.UpdatedDate
{
get
{
return DateTime.Now;
}
}
IEnumerable<string> IIndexableBuiltinFields.Paths
{
get
{
return new List<string>(); //not used
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment