using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using PeterBlum.DES.DAO;
using PeterBlum.DES.DAO.Attributes;
using PeterBlum.DES.DAO.EntityDAO;
using PeterBlum.DES.DataAnnotations;
using DESDA = PeterBlum.DES.DataAnnotations;
namespace PeterBlum.WithDataAnnotations
{
[EntityDAOType(typeof(CategoryDAO))]
[MetadataType(typeof(CategoryMetadata))]
public partial class Category
{
public ValidationResult CheckForDuplicateCategoryNames(
string pNewName, ValidationContext pValidationContext)
{
BaseDAOChangeEntityActionArgs vArgs = (BaseDAOChangeEntityActionArgs)pValidationContext.GetChangeEntityActionArgs();
ChangeEntityAction vAction = vArgs != null ? vArgs.Action : ChangeEntityAction.Update;
if ((vAction == ChangeEntityAction.Insert) || (vAction == ChangeEntityAction.Update))
{
Category vCategory = (Category)vArgs.Entity;
int vThisCategoryID = (vAction == ChangeEntityAction.Insert) ? -1 : vCategory.CategoryID;
NorthWindDataContext vDataContext = new NorthWindDataContext();
System.Data.Linq.Table<Category> vTable = vDataContext.Categories;
if (vTable.FirstOrDefault<Category>(category =>
(String.Compare(category.CategoryName, pNewName, StringComparison.CurrentCultureIgnoreCase) == 0)
&& (category.CategoryID != vThisCategoryID)) != null)
{
DESDA.EntityValidationResult vResult = new DESDA.EntityValidationResult(
"CheckForDuplicateCategoryNames",
"This name already exists. Choose another.",
typeof(Category), "CategoryName", pNewName);
vResult.SummaryErrorMessage = "{LABEL} already exists. Choose another.";
return vResult;
}
}
return ValidationResult.Success;
}
}
[DESDA.InjectionSecurity(DetectScriptInjection=true, DetectSQLInjection=false)]
[DESDA.TableRestriction("Admin", DESDA.DenyAccess.None)]
[DESDA.TableRestriction("Customer", DESDA.DenyAccess.Edit | DESDA.DenyAccess.Delete | DESDA.DenyAccess.Insert)]
public class CategoryMetadata
{
[DESDA.Required()]
[DESDA.CustomValidation(MethodName="CheckForDuplicateCategoryNames")]
[DESDA.DisplayName("Name")]
[DESDA.Filter(AutoGeneratePriority=DESDA.AutoGeneratePriority.Always, InMultiFieldSearch=true)]
public object CategoryName { get; set; }
[DESDA.DataType(DataType.MultilineText)]
[DESDA.Filter(InMultiFieldSearch=true)]
[DESDA.InjectionSecurity(DetectScriptInjection=true, DetectSQLInjection=true,
SQLDetectionLevel=PeterBlum.DES.Web.SQLDetectionLevel.MediumLow,
HTMLTagMode=PeterBlum.DES.Web.HTMLTagMode.IllegalExceptTags,
HTMLTags="br|img|span|div|a")]
public object Description { get; set; }
[DESDA.DbImageDataType(BadFormatErrorMessage="Bad format", ErrorMessage="Must be {EXTENSION}", SupportedFileTypes="jpg")]
public object Picture { get; set; }
}
[TableName("Categories")]
public class CategoryDAO : LINQtoSQLEntityDAO<Category>
{
public CategoryDAO() : base(typeof(NorthWindDataContext)) { }
public CategoryDAO(object pDataContext) : base(pDataContext) { }
}
}