2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Web.DynamicData / System.Web.DynamicData / DynamicDataManager.cs
1 //
2 // DynamicDataManager.cs
3 //
4 // Author:
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.ComponentModel;
36 using System.Drawing;
37 using System.Globalization;
38 using System.Security.Permissions;
39 using System.Security.Principal;
40 using System.Web.Caching;
41 using System.Web.UI;
42 using System.Web.UI.WebControls;
43 using System.Web.DynamicData.ModelProviders;
44
45 namespace System.Web.DynamicData
46 {
47         [ToolboxBitmap (typeof(DynamicDataManager), "DynamicDataManager.ico")]
48         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
49         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
50         [NonVisualControl]
51         [ParseChildren (true)]
52         [PersistChildren (false)]
53         public class DynamicDataManager : Control
54         {
55                 private class AutoFieldGenerator : IAutoFieldGenerator
56                 {
57                         MetaTable table;
58                         
59                         public AutoFieldGenerator (MetaTable table)
60                         {
61                                 this.table = table;
62                         }
63                         
64                         public ICollection GenerateFields (Control ctl)
65                         {
66                                 var ret = new List <DynamicField> ();
67                                 foreach (MetaColumn column in table.Columns) {
68                                         if (!column.Scaffold)
69                                                 continue;
70                                         
71                                         var field = new DynamicField ();
72                                         field.DataField = column.Name;
73                                         ret.Add (field);
74                                 }
75                                 
76                                 return ret;
77                         }
78                 }
79                 
80                 public DynamicDataManager ()
81                 {
82                 }
83
84                 public bool AutoLoadForeignKeys {
85                         get;
86                         set;
87                 }
88
89                 [Browsable (false)]
90                 public override bool Visible {
91                         get { return true; }
92
93                         // NOTE: it is supposed to throw the exception
94                         set { throw new NotImplementedException (); }
95                 }
96
97                 protected override void OnLoad (EventArgs e)
98                 {
99                         base.OnLoad (e);
100
101                         // Why override?
102                 }
103
104                 public void RegisterControl (Control control)
105                 {
106                         RegisterControl (control, false);
107                 }
108
109                 public void RegisterControl (Control control, bool setSelectionFromUrl)
110                 {
111                         // .NET doesn't check for null here, but since I don't like such code, we
112                         // will do the check and throw the same exception as .NET
113                         if (control == null)
114                                 throw new NullReferenceException ();
115
116                         if (!ControlIsValid (control))
117                                 throw new Exception ("Controls of type " + control.GetType () + " are not supported.");
118
119                         DataBoundControl dbc = control as DataBoundControl;
120                         if (dbc != null) {
121                                 IDynamicDataSource dds = dbc.InternalGetDataSource () as IDynamicDataSource;
122                                 if (dds == null)
123                                         return;
124
125                                 MetaTable table = dds.GetTable ();
126                                 if (table == null)
127                                         return;
128                         
129                                 GridView gv = control as GridView;
130                                 if (gv != null) {
131                                         gv.ColumnsGenerator = new AutoFieldGenerator (table);
132                                         return;
133                                 }
134                         }
135                 }
136
137                 bool ControlIsValid (Control control)
138                 {
139                         if (control is Repeater) {
140                                 if (control.NamingContainer == null)
141                                         throw new HttpException ("The Repeater control '" + control.ID + "' does not have a naming container.");
142                                 return true;
143                         }
144                         
145                         DataBoundControl dbc = control as DataBoundControl;
146                         if (dbc == null)
147                                 return false;
148                         
149                         return true;
150                 }
151         }
152 }