* MasterPage.cs: Include relative URL of MasterPage in exception
[mono.git] / mcs / class / System.Web / System.Web.UI / DataSourceView.cs
1 //
2 // System.Web.UI.DataSourceView
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Sanjay Gupta (gsanjay@novell.com)
7 //
8 // (C) 2003 Ben Maurer
9 // (C) 2004 Novell, Inc. (http://www.novell.com)
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 #if NET_2_0
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Text;
37 using System.ComponentModel;
38
39 namespace System.Web.UI {
40         public abstract class DataSourceView
41         {
42                 //IDataSource dataSourceOwner;
43                 string viewName = String.Empty;
44
45                 protected DataSourceView (IDataSource owner, string viewName)
46                 {
47                         if (owner == null)
48                                 throw new ArgumentNullException ("owner");
49
50                         //this.dataSourceOwner = owner;
51                         this.viewName = viewName;
52                         owner.DataSourceChanged += new EventHandler (OnDataSourceChanged);
53                 }
54
55                 void OnDataSourceChanged (object sender, EventArgs e) {
56                         OnDataSourceViewChanged (EventArgs.Empty);
57                 }
58
59                 public virtual void Delete (IDictionary keys, IDictionary values,
60                                                 DataSourceViewOperationCallback callBack)
61                 {
62                         if (callBack == null)
63                                 throw new ArgumentNullException ("callBack");
64
65                         int rowAffected;
66                         try {
67                                 rowAffected = ExecuteDelete (keys, values);
68                         }
69                         catch (Exception e) {
70                                 if (!callBack (0, e))
71                                         throw;
72                                 return;
73                         }
74                         callBack (rowAffected, null);
75                 }
76
77                 protected virtual int ExecuteDelete(IDictionary keys, IDictionary values)
78                 {
79                         throw new NotSupportedException ();
80                 }
81
82                 protected virtual int ExecuteInsert (IDictionary keys)
83                 {
84                         throw new NotSupportedException();
85                 }
86
87                 protected internal abstract IEnumerable ExecuteSelect (
88                                         DataSourceSelectArguments arguments);
89
90                 protected virtual int ExecuteUpdate (IDictionary keys, IDictionary values, 
91                                                                 IDictionary oldValues )
92                 {
93                         throw new NotSupportedException ();
94                 }
95
96                 public virtual void Insert (IDictionary values, 
97                                         DataSourceViewOperationCallback callBack)
98                 {
99                         if (callBack == null)
100                                 throw new ArgumentNullException("callBack");
101
102                         int rowAffected;
103                         try {
104                                 rowAffected = ExecuteInsert (values);
105                         } catch (Exception e) {
106                                 if (!callBack (0, e))
107                                         throw;
108                                 return;
109                         }
110
111                         callBack (rowAffected, null);
112                 }
113
114                 protected virtual void OnDataSourceViewChanged (EventArgs eventArgs)
115                 {
116                         if (eventsList != null) {
117                                 EventHandler evtHandler = eventsList [EventDataSourceViewChanged] as EventHandler;
118                                 if (evtHandler != null)
119                                         evtHandler(this, eventArgs);
120                         }
121                 }
122                 
123                 protected internal virtual void RaiseUnsupportedCapabilityError (
124                                                 DataSourceCapabilities capability)
125                 {
126                         if ((capability & DataSourceCapabilities.Sort) != 0)
127                                 if (!CanSort)
128                                         throw new NotSupportedException ("Sort Capabilites");
129
130                         if ((capability & DataSourceCapabilities.Page) != 0)
131                                 if (!CanPage)
132                                         throw new NotSupportedException("Page Capabilites");
133
134                         if ((capability & DataSourceCapabilities.RetrieveTotalRowCount) != 0)
135                                 if (!CanRetrieveTotalRowCount)
136                                         throw new NotSupportedException("RetrieveTotalRowCount Capabilites");
137
138                         return;
139                 }
140
141                 public virtual void Select (DataSourceSelectArguments selectArgs,
142                                                 DataSourceViewSelectCallback callBack)
143                 {
144                         if (callBack == null)
145                                 throw new ArgumentNullException("callBack");
146
147                         selectArgs.RaiseUnsupportedCapabilitiesError (this);
148                         
149                         IEnumerable selectList = ExecuteSelect (selectArgs);
150                         callBack (selectList);
151                 }
152
153                 public virtual void Update(IDictionary keys, IDictionary values,
154                         IDictionary oldValues, DataSourceViewOperationCallback callBack)
155                 {
156                         if (callBack == null)
157                                 throw new ArgumentNullException ("callBack");
158
159                         int rowAffected;
160                         try {
161                                 rowAffected = ExecuteUpdate (keys, values, oldValues);
162                         } catch (Exception e) {
163                                 if (!callBack (0, e))
164                                         throw;
165                                 return;
166                         }
167
168                         callBack (rowAffected, null);
169                 } 
170                 
171                 public virtual bool CanDelete { get { return false; } }
172                 public virtual bool CanInsert { get { return false; } }
173                 public virtual bool CanPage { get { return false; } }
174                 public virtual bool CanRetrieveTotalRowCount { get { return false; } }
175                 public virtual bool CanSort { get { return false; } }
176                 public virtual bool CanUpdate { get { return false; } }
177
178                 EventHandlerList eventsList;
179                 protected EventHandlerList Events {
180                         get {
181                                 if (eventsList == null)
182                                         eventsList = new EventHandlerList();
183
184                                 return eventsList;
185                         }
186                 }
187                 
188                 internal bool HasEvents ()
189                 {
190                         return eventsList != null;
191                 }
192
193                 public string Name { 
194                         get { return viewName; } 
195                 }
196
197                 static readonly object EventDataSourceViewChanged = new object ();
198                                 
199                 public event EventHandler DataSourceViewChanged
200                 {
201                         add { Events.AddHandler (EventDataSourceViewChanged, value); }
202                         remove { Events.RemoveHandler (EventDataSourceViewChanged, value); }
203                 }
204                 
205         }
206         
207 }
208 #endif
209