|
using System.Linq;
using System.Web.Mvc;
using System.Reflection;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Collections.Generic;
using CodeEffects.Rule.Common;
namespace CodeEffects.Rule.Demo.Filter.Mvc.Models
{
// The utility service that is used to get static data for drop downs
// and dynamic menu data sources (read more about them here:
// http://rule.codeeffects.com/Doc/Business-Rules-Dynamic-Menu-Data-Sources)
public class Utility
{
public static List<SelectListItem> GetEntities()
{
List<SelectListItem> entities = new List<SelectListItem>();
entities.Add(new SelectListItem { Text = "Customers", Value = "Customers" });
entities.Add(new SelectListItem { Text = "Products", Value = "Products" });
entities.Add(new SelectListItem { Text = "Orders", Value = "Orders" });
return entities;
}
public static List<SelectListItem> GetThemes()
{
List<SelectListItem> themes = new List<SelectListItem>();
themes.Add(new SelectListItem { Text = ThemeType.Gray.ToString(), Value = ThemeType.Gray.ToString() });
themes.Add(new SelectListItem { Text = ThemeType.Blue.ToString(), Value = ThemeType.Blue.ToString() });
themes.Add(new SelectListItem { Text = ThemeType.Green.ToString(), Value = ThemeType.Green.ToString() });
themes.Add(new SelectListItem { Text = ThemeType.Red.ToString(), Value = ThemeType.Red.ToString() });
themes.Add(new SelectListItem { Text = ThemeType.White.ToString(), Value = ThemeType.White.ToString() });
themes.Add(new SelectListItem { Text = ThemeType.Navy.ToString(), Value = ThemeType.Navy.ToString() });
themes.Add(new SelectListItem { Text = ThemeType.Black.ToString(), Value = ThemeType.Black.ToString() });
return themes;
}
public static List<DataSourceItem> GetCountries()
{
return GetLookupItems<Country>(new WebRuleLinqEntities().Countries, typeof(Country).GetProperty("Name"));
}
public static List<DataSourceItem> GetColors()
{
return GetLookupItems<Color>(new WebRuleLinqEntities().Colors, typeof(Color).GetProperty("Name"));
}
public static List<DataSourceItem> GetSizes()
{
return GetLookupItems<Size>(new WebRuleLinqEntities().Sizes, typeof(Size).GetProperty("Name"));
}
public static List<DataSourceItem> GetProductTypes()
{
return GetLookupItems<ProductType>(new WebRuleLinqEntities().ProductTypes, typeof(ProductType).GetProperty("Name"));
}
private static List<DataSourceItem> GetLookupItems<T> (ObjectSet<T> items, PropertyInfo pi) where T: EntityObject, new()
{
List<DataSourceItem> list = new List<DataSourceItem>();
var q = from c in items.OrderBy("it.Name")
select c;
foreach(var item in q)
list.Add(
new DataSourceItem(
(int)item.EntityKey.EntityKeyValues[0].Value,
pi.GetValue(item, null).ToString()));
return list;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.