Skip to content

Instantly share code, notes, and snippets.

@jstemerdink
Last active November 9, 2017 06:47
Show Gist options
  • Save jstemerdink/c1e866ff791012b154907783c0f2e6f0 to your computer and use it in GitHub Desktop.
Save jstemerdink/c1e866ff791012b154907783c0f2e6f0 to your computer and use it in GitHub Desktop.
using System;
using System.Web.Mvc;
using EPiServer.Shell.ObjectEditing;
using Mediachase.BusinessFoundation.Data.Meta.Management;
/// <summary>
/// The meta enum selection attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class MetaEnumSelectionAttribute : SelectManyAttribute, IMetadataAware
{
/// <summary>
/// Gets the name of the <see cref="McDataType" />.
/// </summary>
/// <value>The name of the mc data type.</value>
public string McDataTypeName { get; }
/// <summary>
/// Initializes a new instance of the <see cref="MetaEnumSelectionAttribute"/> class.
/// </summary>
/// <param name="mcDataTypeName">Name of the <see cref="McDataType" />.</param>
public MetaEnumSelectionAttribute(string mcDataTypeName)
{
this.McDataTypeName = mcDataTypeName;
}
/// <summary>
/// Called when [metadata created].
/// </summary>
/// <param name="metadata">
/// The metadata.
/// </param>
public new void OnMetadataCreated(ModelMetadata metadata)
{
if (metadata == null)
{
return;
}
this.SelectionFactoryType = typeof(MetaEnumSelectionFactory);
base.OnMetadataCreated(metadata);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer.Logging;
using EPiServer.Shell.ObjectEditing;
using Mediachase.BusinessFoundation.Data;
using Mediachase.BusinessFoundation.Data.Meta;
using Mediachase.BusinessFoundation.Data.Meta.Management;
/// <summary>
/// Class MetaEnumSelectionFactory.
/// </summary>
/// <seealso cref="EPiServer.Shell.ObjectEditing.ISelectionFactory" />
public class MetaEnumSelectionFactory : ISelectionFactory
{
/// <summary>
/// The log.
/// </summary>
private static readonly ILogger Log = LogManager.GetLogger();
/// <summary>
/// Creates a list of selection items for a specific property.
/// </summary>
/// <param name="metadata">
/// The metadata for a property.
/// </param>
/// <returns>
/// A list of selection items for a specific property.
/// </returns>
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
try
{
MetaEnumSelectionAttribute metaEnumSelectionAttribute =
metadata.Attributes.OfType<MetaEnumSelectionAttribute>().SingleOrDefault();
MetaFieldType currentType = null;
if (metaEnumSelectionAttribute != null
&& !string.IsNullOrEmpty(metaEnumSelectionAttribute.McDataTypeName))
{
currentType =
DataContext.Current.MetaModel.RegisteredTypes.Cast<MetaFieldType>()
.FirstOrDefault(
type =>
type.McDataType == McDataType.Enum
&& type.Name.Equals(
metaEnumSelectionAttribute.McDataTypeName,
StringComparison.OrdinalIgnoreCase));
}
if (currentType == null)
{
return new List<ISelectItem>();
}
MetaEnumItem[] values = MetaEnum.GetItems(currentType);
return
values.Select(
metaEnumItem =>
new SelectItem
{
Text = MetaEnum.GetFriendlyName(currentType, metaEnumItem.Handle),
Value = metaEnumItem.Handle.ToString()
}).Cast<ISelectItem>().ToList();
}
catch (ArgumentNullException argumentNullException)
{
Log.Error(argumentNullException.Message, argumentNullException);
}
catch (ArgumentException argumentException)
{
Log.Error(argumentException.Message, argumentException);
}
catch (InvalidOperationException invalidOperationException)
{
Log.Error(invalidOperationException.Message, invalidOperationException);
}
catch (InvalidCastException invalidCastException)
{
Log.Error(invalidCastException.Message, invalidCastException);
}
return new List<ISelectItem>();
}
}
[MetaEnumSelection("ContactGroup")]
public virtual string Customergroups { get; set; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment