Forked from jstemerdink/MetaEnumSelectionAttribute.cs
Last active
November 30, 2017 03:32
-
-
Save ChauThan/9a9651f4b93e26cd9f998aeda1c99953 to your computer and use it in GitHub Desktop.
[MetaEnumSelectionAttribute] #episerver
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.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); | |
} | |
} |
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 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>(); | |
} | |
} |
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
[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