Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / DataAnnotationsModelMetadataProvider.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.ComponentModel;\r
17     using System.ComponentModel.DataAnnotations;\r
18     using System.Linq;\r
19 \r
20     public class DataAnnotationsModelMetadataProvider : AssociatedMetadataProvider {\r
21         protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) {\r
22             List<Attribute> attributeList = new List<Attribute>(attributes);\r
23             DisplayColumnAttribute displayColumnAttribute = attributeList.OfType<DisplayColumnAttribute>().FirstOrDefault();\r
24             DataAnnotationsModelMetadata result = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);\r
25 \r
26             // Do [HiddenInput] before [UIHint], so you can override the template hint\r
27             HiddenInputAttribute hiddenInputAttribute = attributeList.OfType<HiddenInputAttribute>().FirstOrDefault();\r
28             if (hiddenInputAttribute != null) {\r
29                 result.TemplateHint = "HiddenInput";\r
30                 result.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue;\r
31             }\r
32 \r
33             // We prefer [UIHint("...", PresentationLayer = "MVC")] but will fall back to [UIHint("...")]\r
34             IEnumerable<UIHintAttribute> uiHintAttributes = attributeList.OfType<UIHintAttribute>();\r
35             UIHintAttribute uiHintAttribute = uiHintAttributes.FirstOrDefault(a => String.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase))\r
36                                            ?? uiHintAttributes.FirstOrDefault(a => String.IsNullOrEmpty(a.PresentationLayer));\r
37             if (uiHintAttribute != null) {\r
38                 result.TemplateHint = uiHintAttribute.UIHint;\r
39             }\r
40 \r
41             DataTypeAttribute dataTypeAttribute = attributeList.OfType<DataTypeAttribute>().FirstOrDefault();\r
42             if (dataTypeAttribute != null) {\r
43                 result.DataTypeName = dataTypeAttribute.GetDataTypeName();\r
44             }\r
45 \r
46             ReadOnlyAttribute readOnlyAttribute = attributeList.OfType<ReadOnlyAttribute>().FirstOrDefault();\r
47             if (readOnlyAttribute != null) {\r
48                 result.IsReadOnly = readOnlyAttribute.IsReadOnly;\r
49             }\r
50 \r
51             DisplayFormatAttribute displayFormatAttribute = attributeList.OfType<DisplayFormatAttribute>().FirstOrDefault();\r
52             if (displayFormatAttribute == null && dataTypeAttribute != null) {\r
53                 displayFormatAttribute = dataTypeAttribute.DisplayFormat;\r
54             }\r
55             if (displayFormatAttribute != null) {\r
56                 result.NullDisplayText = displayFormatAttribute.NullDisplayText;\r
57                 result.DisplayFormatString = displayFormatAttribute.DataFormatString;\r
58                 result.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull;\r
59 \r
60                 if (displayFormatAttribute.ApplyFormatInEditMode) {\r
61                     result.EditFormatString = displayFormatAttribute.DataFormatString;\r
62                 }\r
63             }\r
64 \r
65             ScaffoldColumnAttribute scaffoldColumnAttribute = attributeList.OfType<ScaffoldColumnAttribute>().FirstOrDefault();\r
66             if (scaffoldColumnAttribute != null) {\r
67                 result.ShowForDisplay = result.ShowForEdit = scaffoldColumnAttribute.Scaffold;\r
68             }\r
69 \r
70             DisplayNameAttribute displayNameAttribute = attributeList.OfType<DisplayNameAttribute>().FirstOrDefault();\r
71             if (displayNameAttribute != null) {\r
72                 result.DisplayName = displayNameAttribute.DisplayName;\r
73             }\r
74 \r
75             RequiredAttribute requiredAttribute = attributeList.OfType<RequiredAttribute>().FirstOrDefault();\r
76             if (requiredAttribute != null) {\r
77                 result.IsRequired = true;\r
78             }\r
79 \r
80             return result;\r
81         }\r
82     }\r
83 }\r