using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using PeterBlum.DES.DAO.Attributes;
using PeterBlum.DES.DAO.EntityDAO;
using DESDA = PeterBlum.DES.DataAnnotations;
namespace PeterBlum.WithDataAnnotations
{
[EntityDAOType(typeof(OrderDAO))]
[MetadataType(typeof(OrderMetadata))]
public partial class Order : DESDA.ICustomizeDataField
{
public ValidationResult CheckOrderLimit(Order pOrder, ValidationContext pValidationContext)
{
if (!AllowedOrderPriceTotal())
return new DESDA.EntityValidationResult("CheckOrderLimit",
String.Format("The order exceeds the limit of {0:C}.", OrderPriceLimit));
return ValidationResult.Success;
}
public bool AllowedOrderPriceTotal()
{
return ProductsTotal <= OrderPriceLimit;
}
[Browsable(false)]
[DESDA.ScaffoldColumn(false)]
public decimal OrderPriceLimit
{
get { return 1000000.0M; }
}
[DESDA.CurrencyDataType(ShowColumnTotal=true)]
[DESDA.DisplayName("Products total", ShortDisplayName="Total")]
public decimal ProductsTotal
{
get
{
decimal total = 0.0M;
foreach (Order_Detail orderDetail in this.Order_Details)
total = total + orderDetail.OrderPrice;
return total;
}
}
[DESDA.CurrencyDataType(ShowColumnTotal=true)]
[DESDA.DisplayName("Products total with shipping charges", ShortDisplayName="Total w/ship")]
public decimal ProductsTotalWithShipping
{
get
{
return ProductsTotal + (Freight.HasValue ? Freight.Value : 0.0M);
}
}
#region ICustomizeDataField Members
public void CustomizeDataField(DESDA.ActiveDataField activeDataField)
{
if (activeDataField.DataEntryMode != DESDA.DataEntryMode.ReadOnly)
switch (activeDataField.DataField)
{
case "OrderDate":
if (activeDataField.DataEntryMode == DESDA.DataEntryMode.Insert)
{
DESDA.CompareToValueAttribute vCtoVAtt =
activeDataField.GetEditableValidationAttribute<DESDA.CompareToValueAttribute>(true);
vCtoVAtt.ValueToCompare = DateTime.Today;
vCtoVAtt.Operator = DES.ConditionOperator.GreaterThanEqual;
activeDataField.DefaultValue = DateTime.Today;
}
break;
}
}
#endregion
}
[DESDA.CustomValidation(MethodName="CheckOrderLimit")]
[DESDA.TableRestriction("Admin", DESDA.DenyAccess.None)]
[DESDA.TableRestriction("Customer", DESDA.DenyAccess.Edit | DESDA.DenyAccess.Delete | DESDA.DenyAccess.Insert)]
public class OrderMetadata
{
[DESDA.Required()]
[DESDA.CharacterSet(LettersUppercase=true, LettersLowercase=false, ErrorMessage="Must be uppercase letters.")]
[DESDA.StringLength(MinimumLength=5, MaximumLength=5, ErrorMessage="Exactly 5 letters.")]
[DESDA.Filter(DESDA.AutoGeneratePriority.Always)]
[DESDA.DisplayName("Customer ID", ShortDisplayName="Customer")]
[DESDA.ScaffoldColumn(ScaffoldEdit=false, ScaffoldInsert=false)]
public object CustomerID { get; set; }
[DESDA.ScaffoldColumn(Position = 0)]
public object Customer { get; set; }
[DESDA.DateDataType()]
[DESDA.Required()]
[DESDA.DisplayName("Order date", ShortDisplayName="Ordered")]
public object OrderDate { get; set; }
[DESDA.DateDataType()]
[DESDA.DisplayName("Required by")]
[DESDA.CompareTwoColumns("OrderDate", DES.ConditionOperator.GreaterThanEqual)]
public object RequiredDate { get; set; }
[DESDA.DateDataType()]
[DESDA.DisplayName("Date shipped", ShortDisplayName="Shipped")]
[DESDA.CompareTwoColumns("OrderDate", DES.ConditionOperator.GreaterThanEqual)]
public object ShippedDate { get; set; }
[DESDA.Enumerated(ValueNameList="1=UPS Standard|2=UPS Overnight|3=UPS 2 day|10=FedEx Standard|11=FedEx Overnight")]
[DefaultValue(1)]
public object ShipVia { get; set; }
[DESDA.CurrencyDataType(AllowNegatives=false)]
[DESDA.DisplayName("Shipping charges")]
public object Freight { get; set; }
[DESDA.DisplayName("Recipient name")]
[DESDA.Required()]
public object ShipName { get; set; }
[DESDA.Required()]
[DESDA.RegularExpression(@"\S\s+\S", CaseInsensitive=true,
ErrorMessage="Enter a building number and street name.")]
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Advanced, InMultiFieldSearch=true)]
[DESDA.DisplayName("Shipping Street", ShortDisplayName="Street")]
public object ShipAddress { get; set; }
[DESDA.Required()]
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Advanced, InMultiFieldSearch=true)]
[DESDA.DisplayName("Shipping City", ShortDisplayName="City")]
public object ShipCity { get; set; }
[DESDA.DisplayName("Shipping Region", ShortDisplayName="Region")]
public object ShipRegion { get; set; }
[DESDA.Required()]
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Standard, InMultiFieldSearch=true)]
[DESDA.DisplayName("Shipping Postal code", ShortDisplayName="Postal code")]
public object ShipPostalCode { get; set; }
[DESDA.Required()]
[DESDA.EnumeratedString(ValueNameList="Brazil|Canada|France|Germany|Great Britain|Mexico|United States", DefaultValue="United States")]
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Advanced, InMultiFieldSearch=true)]
[DESDA.DisplayName("Shipping Country", ShortDisplayName="Country")]
public object ShipCountry { get; set; }
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Standard)]
[DESDA.ForeignKeyQuery(DisplayDataFields="FullName")]
public object Employee { get; set; }
}
[TableName("Orders")]
public class OrderDAO : LINQtoSQLEntityDAO<Order>
{
public OrderDAO() : base(typeof(NorthWindDataContext)) { }
public OrderDAO(object pDataContext) : base(pDataContext) { }
protected override void Update(LINQtoSQLEntityDAO<Order>.L2SChangeEntityActionWrapper pWrapper)
{
Order order = pWrapper.GetEntity();
if (order.Customer != null)
order.CustomerID = order.Customer.CustomerID;
base.Update(pWrapper);
}
protected override void Insert(LINQtoSQLEntityDAO<Order>.L2SChangeEntityActionWrapper pWrapper)
{
Order order = pWrapper.GetEntity();
if (order.Customer != null)
order.CustomerID = order.Customer.CustomerID;
base.Insert(pWrapper);
}
}
}