2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Web.DynamicData / System.Web.DynamicData / DynamicDataRoute.cs
1 //
2 // DynamicDataRoute.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.Routing;
40
41 namespace System.Web.DynamicData
42 {
43         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         public class DynamicDataRoute : Route
46         {
47                 static readonly object initLock = new object ();
48                 bool initDone;
49                 
50                 public DynamicDataRoute (string url)
51                         : base (url, null)
52                 {
53                         Model = MetaModel.Default;
54                         RouteHandler = new DynamicDataRouteHandler ();
55                 }
56
57                 public string Action { get; set; }
58
59                 public MetaModel Model { get; set; }
60
61                 public DynamicDataRouteHandler RouteHandler { 
62                         get { return base.RouteHandler as DynamicDataRouteHandler; }
63                         set { base.RouteHandler = value; }
64                 }
65
66                 public string Table { get; set; }
67
68                 public string ViewName { get; set; }
69
70                 void EnsureInitialized ()
71                 {
72                         if (initDone)
73                                 return;
74                         
75                         // We need to lock since we might be stored in the RouteTable.Routes
76                         // collection which might be accessed from many concurrent requests.
77                         lock (initLock) {
78                                 if (initDone)
79                                         return;
80                                 
81                                 initDone = true;
82
83                                 DynamicDataRouteHandler rh = RouteHandler;
84                                 if (rh != null)
85                                         rh.Model = Model;
86                                 
87                                 string action = Action, table = Table;
88                                 if (action == null && table == null)
89                                         return;
90
91                                 RouteValueDictionary defaults = Defaults;
92                                 if (defaults == null)
93                                         Defaults = defaults = new RouteValueDictionary ();
94
95                                 if (table != null) {
96                                         // Force check for table existence
97                                         MetaModel model = Model ?? MetaModel.Default;
98                                         if (model != null)
99                                                 Model.GetTable (table);
100                                         
101                                         if (defaults.ContainsKey ("Table"))
102                                                 defaults ["Table"] = table;
103                                         else
104                                                 defaults.Add ("Table", table);
105                                 }
106                                 
107                                 if (action != null) {
108                                         if (defaults.ContainsKey ("Action"))
109                                                 defaults ["Action"] = action;
110                                         else
111                                                 defaults.Add ("Action", action);
112                                 }
113                         }
114                 }
115                 
116                 public string GetActionFromRouteData (RouteData routeData)
117                 {
118                         if (routeData == null)
119                                 throw new ArgumentNullException ("routeData");
120                         return routeData.GetRequiredString ("Action");
121                 }
122
123                 public override RouteData GetRouteData (HttpContextBase httpContext)
124                 {
125                         EnsureInitialized ();
126                         RouteData rd = base.GetRouteData (httpContext);
127
128                         if (rd == null)
129                                 return null;
130
131                         MetaModel model = Model ?? MetaModel.Default;
132                         MetaTable table;
133                         if (model == null || !model.TryGetTable (rd.GetRequiredString ("Table"), out table))
134                                 return null;
135                         
136                         return rd;
137                 }
138
139                 public MetaTable GetTableFromRouteData (RouteData routeData)
140                 {
141                         if (routeData == null)
142                                 throw new ArgumentNullException ("routeData");
143                         var t = routeData.GetRequiredString ("Table");
144                         if (Model == null)
145                                 throw new InvalidOperationException ("MetaModel must be set to the DynamicDataRoute before retrieving MetaTable");
146                         MetaTable mt;
147                         return Model.GetTable (t);
148                 }
149
150                 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
151                 {
152                         EnsureInitialized ();
153                         return base.GetVirtualPath (requestContext, values);
154                 }
155         }
156 }