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