2013-06-13 10:20:33 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2013-05-01 09:43:14 +03:00
|
|
|
using NzbDrone.Common.Reflection;
|
2013-05-03 08:24:52 +03:00
|
|
|
using NzbDrone.Core.Annotations;
|
2013-05-01 09:43:14 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Api.ClientSchema
|
|
|
|
{
|
|
|
|
public static class SchemaBuilder
|
|
|
|
{
|
|
|
|
public static List<Field> GenerateSchema(object model)
|
|
|
|
{
|
|
|
|
var properties = model.GetType().GetSimpleProperties();
|
|
|
|
|
|
|
|
var result = new List<Field>(properties.Count);
|
|
|
|
|
|
|
|
foreach (var propertyInfo in properties)
|
|
|
|
{
|
2013-05-03 08:24:52 +03:00
|
|
|
var fieldAttribute = propertyInfo.GetAttribute<FieldDefinitionAttribute>(false);
|
2013-05-01 09:43:14 +03:00
|
|
|
|
2013-05-03 08:24:52 +03:00
|
|
|
if (fieldAttribute != null)
|
|
|
|
{
|
2013-05-01 09:43:14 +03:00
|
|
|
|
2013-05-03 08:24:52 +03:00
|
|
|
var field = new Field()
|
|
|
|
{
|
|
|
|
Name = propertyInfo.Name,
|
|
|
|
Label = fieldAttribute.Label,
|
|
|
|
HelpText = fieldAttribute.HelpText,
|
2013-07-27 23:57:41 +03:00
|
|
|
HelpLink = fieldAttribute.HelpLink,
|
2013-05-03 08:24:52 +03:00
|
|
|
Order = fieldAttribute.Order,
|
2013-05-20 07:19:54 +03:00
|
|
|
Type = fieldAttribute.Type.ToString().ToLowerInvariant()
|
2013-05-03 08:24:52 +03:00
|
|
|
};
|
2013-05-01 09:43:14 +03:00
|
|
|
|
2013-05-03 08:24:52 +03:00
|
|
|
var value = propertyInfo.GetValue(model, null);
|
|
|
|
if (value != null)
|
|
|
|
{
|
2013-05-21 10:55:44 +03:00
|
|
|
field.Value = value;
|
2013-05-03 08:24:52 +03:00
|
|
|
}
|
|
|
|
|
2013-06-13 10:20:33 +03:00
|
|
|
if (fieldAttribute.Type == FieldType.Select)
|
|
|
|
{
|
|
|
|
field.SelectOptions = GetSelectOptions(fieldAttribute.SelectOptions);
|
|
|
|
}
|
|
|
|
|
2013-05-03 08:24:52 +03:00
|
|
|
result.Add(field);
|
|
|
|
}
|
2013-05-01 09:43:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
2013-06-13 10:20:33 +03:00
|
|
|
|
|
|
|
private static List<SelectOption> GetSelectOptions(Type selectOptions)
|
|
|
|
{
|
|
|
|
var options = from Enum e in Enum.GetValues(selectOptions)
|
|
|
|
select new SelectOption { Value = Convert.ToInt32(e), Name = e.ToString() };
|
|
|
|
|
|
|
|
return options.OrderBy(o => o.Value).ToList();
|
|
|
|
}
|
2013-05-01 09:43:14 +03:00
|
|
|
}
|
|
|
|
}
|