using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using PeterBlum.DES.DAO.Attributes;
using PeterBlum.DES.DAO.EntityDAO;
using PeterBlum.DES.DAO.EntityFilters;
using PeterBlum.DES.DAO;
using DESDA = PeterBlum.DES.DataAnnotations;
namespace PeterBlum.WithDataAnnotations
{
[EntityDAOType(typeof(ProductDAO))]
[MetadataType(typeof(ProductMetadata))]
public partial class Product : DESDA.ICustomizeDataField
{
[DESDA.CurrencyDataType()]
[DESDA.DisplayName("Value of Stock")]
[DESDA.ScaffoldColumn(100, true)]
[DESDA.CalculationDescriptor("if (Not Discontinued) then {UnitPrice * UnitsInStock} else novalue")]
[DESDA.ColumnRestriction("Customer", DESDA.DenyAccess.All)]
[DESDA.SortableColumn(SortExpression="UnitPrice * UnitsInStock", Sortable=true)]
public decimal ValueOfStock
{
get
{
if (!Discontinued)
if (UnitPrice.HasValue && UnitsInStock.HasValue)
return UnitPrice.Value * UnitsInStock.Value;
return 0.0M;
}
}
[DESDA.CurrencyDataType()]
[DESDA.CalculationDescriptor("if (!Discontinued) then {UnitPrice * (UnitsInStock + UnitsOnOrder)} else novalue")]
[DESDA.ScaffoldColumn(101, true)]
[DESDA.DisplayName("Value of Available Units", ShortDisplayName="Val avail.")]
[DESDA.ColumnRestriction("Customer", DESDA.DenyAccess.All)]
public decimal ValueOfAvailableUnits
{
get
{
if (!Discontinued)
{
int vStock = 0;
if (UnitsInStock.HasValue)
vStock = UnitsInStock.Value;
if (UnitsOnOrder.HasValue)
vStock = vStock + UnitsOnOrder.Value;
if (UnitPrice.HasValue)
return UnitPrice.Value * vStock;
}
return 0.0M;
}
}
[AttributeSource(typeof(Supplier), "HomePage")]
[DESDA.ScaffoldColumn(AfterThisDataField="Supplier")]
public string SupplierHomePage
{
get
{
if (Supplier != null)
return Supplier.HomePage;
return String.Empty;
}
}
#region ICustomizeDataField Members
public void CustomizeDataField(DESDA.ActiveDataField activeDataField)
{
switch (activeDataField.DataField)
{
case "Category":
activeDataField.SelectForeignKeyQueryAttribute("Desc", false);
break;
}
}
#endregion
}
[DESDA.TableRestriction("Admin", DESDA.DenyAccess.None)]
[DESDA.TableRestriction("Customer", DESDA.DenyAccess.Edit | DESDA.DenyAccess.Delete | DESDA.DenyAccess.Insert)]
public class ProductMetadata
{
[DESDA.DisplayName("Product ID", ShortDisplayName="ID")]
public object ProductID { get; set; }
[DESDA.CharacterSet(LettersLowercase=true, LettersUppercase=true, Digits=true,
Space=true, DiacriticLetters=true, OtherCharacters="'",
ErrorMessage="Illegal character", SummaryErrorMessage="{LABEL} has an illegal character.")]
[DESDA.DisplayName("Product Name", ShortDisplayName="Name")]
[DESDA.Description("The product name.", Prompt="Enter the product name:")]
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Always)]
public object ProductName { get; set; }
[DESDA.DisplayName("Quantity Per Unit")]
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Advanced)]
[DESDA.ScaffoldColumn("UnitPrice")]
[DESDA.SortableColumn(false)]
public object QuantityPerUnit { get; set; }
[DESDA.CurrencyDataType(MaxWholeDigits=4, UseCurrencySymbol=true, AllowNegatives=false)]
[DESDA.Description("The price of one unit", Prompt="Enter the price for one unit")]
[DESDA.DisplayName("Unit Price")]
[DESDA.Filter(DataFormatString="c")]
[DESDA.RangeEntityFilterPicker(true, 0, 10, 20, 100)]
[DESDA.CompareBooleanDependency(DataField="Discontinued", ValueToCompare=false)]
public object UnitPrice { get; set; }
[DESDA.IntegerDataType(MaxWholeDigits=3, AllowNegatives=false)]
[DESDA.CompareToValue(500, PeterBlum.DES.ConditionOperator.LessThan)]
[DefaultValue(10)]
[DESDA.SortableColumn(false)]
[DESDA.DisplayName("Units In Stock")]
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Advanced)]
[DESDA.RangeEntityFilterPicker(false, 0, 9, 10, 49, 50, 99, 100)]
[DESDA.ColumnRestriction("Customer", DESDA.DenyAccess.View)]
public object UnitsInStock { get; set; }
[DESDA.IntegerDataType(MaxWholeDigits=3, AllowNegatives=false)]
[DESDA.DisplayName("Units On Order")]
[DESDA.Range(0, 199)]
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Advanced)]
[DESDA.RangeEntityFilterPicker(false, 0, 9, 10, 49, 50, 99, 100)]
[DESDA.ColumnRestriction("Customer", DESDA.DenyAccess.View)]
public object UnitsOnOrder { get; set; }
[DESDA.Enumerated(typeof(ReorderLevelEnum), ValueNameList="10=Medium Low|50=Medium High")]
[DESDA.DisplayName("Reorder Level")]
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Advanced)]
[DESDA.ColumnRestriction("Customer", DESDA.DenyAccess.View)]
public object ReorderLevel { get; set; }
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Advanced)]
[DESDA.ForeignKeyQuery(QueryName="", DisplayDataFields="CompanyName|ContactName", OrderBy="CompanyName")]
public object Supplier { get; set; }
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Advanced)]
[DefaultValue(1)]
[DESDA.ForeignKeyQuery(QueryName="", Where="", OrderBy="CategoryName")]
[DESDA.ForeignKeyQuery(QueryName="Desc", Where="", OrderBy="CategoryName desc")]
[DESDA.ForeignKeyQuery(QueryName="Condiments and Beverages",
Where="(CategoryName == \"Condiments\") or (CategoryName == \"Beverages\")", OrderBy="CategoryName")]
public object Category { get; set; }
[DESDA.CompareToValueDependency(Name="ZeroUnitsInStock", DataField="UnitsInStock",
Operator=PeterBlum.DES.ConditionOperator.Equal, ValueToCompareAsNative=0)]
[DESDA.RequiredDependency(Name="QtyPerUnitEmpty", DataFields="QuantityPerUnit", NotCondition=true )]
[DESDA.MultiConditionDependency(Name="Strict",
Expression="ZeroUnitsInStock OR QtyPerUnitEmpty")]
[DESDA.Required(DependencyName="ZeroUnitsInStock")]
[DefaultValue(false)]
public object Discontinued { get; set; }
[DESDA.ColumnRestriction("Customer", DESDA.DenyAccess.All)]
public object Order_Details { get; set; }
}
public enum ReorderLevelEnum
{
None = 0,
Low = 5,
MediumLow = 10,
Medium = 30,
MediumHigh = 50,
High = 100
}
[TableName("Products")]
public class ProductDAO : LINQtoSQLEntityDAO<Product>
{
public ProductDAO() : base(typeof(NorthWindDataContext)) { }
public ProductDAO(object pDataContext) : base(pDataContext) { }
[DataObjectMethod(DataObjectMethodType.Select, false)]
[SelectMethodParameter(ActualName="startPrice", AliasNames="StartPrice")]
[SelectMethodParameter(ActualName="endPrice", AliasNames="EndPrice")]
[Description("All Products whose UnitPrice is within these parameters.")]
[SelectArgs(Filters=true, SortExpression=true, Paging=true)]
public IEnumerable<Product> SelectPriceRange(
decimal startPrice, decimal endPrice, SelectArgs selectArgs)
{
if (endPrice == 0)
endPrice = 999999999.99M;
selectArgs.EntityFilters.Add(
new RangeEntityFilter("UnitPrice", startPrice, endPrice));
return ExecuteSelectCommand(selectArgs);
}
[DataObjectMethod(DataObjectMethodType.Select, false)]
[SelectMethodParameter(ActualName="startPrice", AliasNames="StartPrice")]
[SelectMethodParameter(ActualName="endPrice", AliasNames="EndPrice")]
[Description("All Products whose UnitPrice is within these parameters.")]
[SelectArgs(Filters=true, SortExpression=true, Paging=true)]
public IEnumerable<Product> TopTen(SelectArgs selectArgs)
{
NorthWindDataContext vDataContext = new NorthWindDataContext();
var vProducts = vDataContext.Ten_Most_Expensive_Products();
return ExecuteSelectCommand(vProducts, selectArgs, "TenMostExpensive");
}
}
}