2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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                         throw new NotImplementedException ();
76                 }
77
78                 [MonoTODO]
79                 public static IDynamicDataSource FindDataSourceControl (this Control current)
80                 {
81                         throw new NotImplementedException ();
82                 }
83
84                 [MonoTODO]
85                 public static Control FindFieldTemplate (this Control control, string columnName)
86                 {
87                         throw new NotImplementedException ();
88                 }
89
90                 public static MetaTable FindMetaTable (this Control current)
91                 {
92                         // .NET doesn't perform the check, we will
93                         if (current == null)
94                                 throw new NullReferenceException ();
95
96                         while (current != null) {
97                                 DataBoundControl dbc = current as DataBoundControl;
98                                 if (dbc != null) {
99                                         IDynamicDataSource dds = dbc.DataSourceObject as IDynamicDataSource;
100                                         if (dds != null)
101                                                 return dds.GetTable ();
102                                 }
103
104                                 current = current.NamingContainer;
105                         }
106
107                         return null;
108                 }
109
110                 [MonoTODO]
111                 public static string FormatEditValue (this IFieldFormattingOptions formattingOptions, object fieldValue)
112                 {
113                         throw new NotImplementedException ();
114                 }
115
116                 [MonoTODO]
117                 public static string FormatValue (this IFieldFormattingOptions formattingOptions, object fieldValue)
118                 {
119                         throw new NotImplementedException ();
120                 }
121
122                 static string GetDataSourceId (IDynamicDataSource dataSource)
123                 {
124                         Control c = dataSource as Control;
125                         if (c == null)
126                                 return String.Empty;
127                         
128                         return c.ID;
129                 }
130                 
131                 public static MetaTable GetTable (this IDynamicDataSource dataSource)
132                 {
133                         if (dataSource == null)
134                                 return null;
135
136                         string entitySetName = dataSource.EntitySetName;
137                         if (String.IsNullOrEmpty (entitySetName)) {
138                                 // LAMESPEC: MSDN says we should throw in this case, but .NET calls
139                                 // DynamicDataRouteHandler.GetRequestMetaTable(HttpContext
140                                 // httpContext) instead (eventually)
141                                 MetaTable ret = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
142                                 if (ret == null)
143                                         throw new InvalidOperationException ("The control '" + GetDataSourceId (dataSource) +
144                                                                              "' does not have a TableName property and a table name cannot be inferred from the URL.");
145                         }
146                         
147                         Type contextType = dataSource.ContextType;
148                         if (contextType == null)
149                                 throw new InvalidOperationException ("The ContextType property of control '" + GetDataSourceId (dataSource) + "' must specify a data context");
150                         
151                         return MetaModel.GetModel (contextType).GetTable (entitySetName);
152                 }
153
154                 [MonoTODO]
155                 public static void LoadWithForeignKeys (this LinqDataSource dataSource, Type rowType)
156                 {
157                         throw new NotImplementedException ();
158                 }
159         }
160 }