System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System.Web.DynamicData / System.Web.DynamicData / DynamicDataExtensions.cs
1 //
2 // DynamicDataExtensions.cs
3 //
4 // Authors:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //      Marek Habersack <mhabersack@novell.com>
7 //
8 // Copyright (C) 2008-2009 Novell Inc. http://novell.com
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 using System;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Collections.Specialized;
35 using System.Globalization;
36 using System.Security.Permissions;
37 using System.Security.Principal;
38 using System.Web.Caching;
39 using System.Web.UI;
40 using System.Web.UI.WebControls;
41 using System.Web.DynamicData.ModelProviders;
42
43 namespace System.Web.DynamicData
44 {
45         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46         public static class DynamicDataExtensions
47         {
48                 public static object ConvertEditedValue (this IFieldFormattingOptions formattingOptions, string value)
49                 {
50                         // Not a surprise anymore...
51                         if (formattingOptions == null)
52                                 throw new NullReferenceException ();
53
54                         if (String.IsNullOrEmpty (value)) {
55                                 if (formattingOptions.ConvertEmptyStringToNull)
56                                         return null;
57                         } else {
58                                 string nullDisplayText = formattingOptions.NullDisplayText;
59                                 if (!String.IsNullOrEmpty (nullDisplayText) && String.Compare (value, nullDisplayText, StringComparison.Ordinal) == 0)
60                                         return null;
61                         }
62
63                         return value;
64                 }
65
66                 [MonoTODO]
67                 public static void EnablePersistedSelection (this BaseDataBoundControl dataBoundControl)
68                 {
69                         throw new NotImplementedException ();
70                 }
71
72                 [MonoTODO]
73                 public static void ExpandDynamicWhereParameters (this IDynamicDataSource dataSource)
74                 {
75                         // http://forums.asp.net/p/1396453/3005197.aspx#3005197
76                         throw new NotImplementedException ();
77                 }
78
79                 [MonoTODO]
80                 public static IDynamicDataSource FindDataSourceControl (this Control current)
81                 {
82                         var control = current as BaseDataBoundControl;
83                         if (control == null)
84                                 return null;
85
86                         string dataSourceID = control.DataSourceID;
87                         if (!String.IsNullOrEmpty (dataSourceID))
88                                 return control.DataSource as IDynamicDataSource;
89                         
90                         Control namingContainer = control.NamingContainer;
91                         IDynamicDataSource dds;
92                         while (namingContainer != null) {
93                                 dds = namingContainer.FindControl (dataSourceID) as IDynamicDataSource;
94                                 if (dds != null)
95                                         return dds;
96
97                                 namingContainer = namingContainer.NamingContainer;
98                         }
99
100                         return null;
101                 }
102
103                 [MonoTODO]
104                 public static Control FindFieldTemplate (this Control control, string columnName)
105                 {
106                         throw new NotImplementedException ();
107                 }
108
109                 public static MetaTable FindMetaTable (this Control current)
110                 {
111                         // .NET doesn't perform the check, we will
112                         if (current == null)
113                                 throw new NullReferenceException ();
114
115                         while (current != null) {
116                                 DataBoundControl dbc = current as DataBoundControl;
117                                 if (dbc != null) {
118                                         IDynamicDataSource dds = dbc.DataSourceObject as IDynamicDataSource;
119                                         if (dds != null)
120                                                 return dds.GetTable ();
121                                 }
122
123                                 current = current.NamingContainer;
124                         }
125
126                         return null;
127                 }
128
129                 [MonoTODO]
130                 public static string FormatEditValue (this IFieldFormattingOptions formattingOptions, object fieldValue)
131                 {
132                         throw new NotImplementedException ();
133                 }
134
135                 [MonoTODO]
136                 public static string FormatValue (this IFieldFormattingOptions formattingOptions, object fieldValue)
137                 {
138                         throw new NotImplementedException ();
139                 }
140
141                 static string GetDataSourceId (IDynamicDataSource dataSource)
142                 {
143                         Control c = dataSource as Control;
144                         if (c == null)
145                                 return String.Empty;
146                         
147                         return c.ID;
148                 }
149                 
150                 public static MetaTable GetTable (this IDynamicDataSource dataSource)
151                 {
152                         if (dataSource == null)
153                                 return null;
154
155                         string entitySetName = dataSource.EntitySetName;
156                         if (String.IsNullOrEmpty (entitySetName)) {
157                                 // LAMESPEC: MSDN says we should throw in this case, but .NET calls
158                                 // DynamicDataRouteHandler.GetRequestMetaTable(HttpContext
159                                 // httpContext) instead (eventually)
160                                 MetaTable ret = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
161                                 if (ret == null)
162                                         throw new InvalidOperationException ("The control '" + GetDataSourceId (dataSource) +
163                                                                              "' does not have a TableName property and a table name cannot be inferred from the URL.");
164                         }
165                         
166                         Type contextType = dataSource.ContextType;
167                         if (contextType == null)
168                                 throw new InvalidOperationException ("The ContextType property of control '" + GetDataSourceId (dataSource) + "' must specify a data context");
169                         
170                         return MetaModel.GetModel (contextType).GetTable (entitySetName);
171                 }
172
173                 [MonoTODO]
174                 public static void LoadWithForeignKeys (this LinqDataSource dataSource, Type rowType)
175                 {
176                         throw new NotImplementedException ();
177                 }
178         }
179 }