* SiteMapNode.cs (GetExplicitResourceString): implement.
[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                 }
53
54                 public virtual void Delete (IDictionary keys, IDictionary values,
55                                                 DataSourceViewOperationCallback callBack)
56                 {
57                         if (callBack == null)
58                                 throw new ArgumentNullException ("callBack");
59
60                         int rowAffected = 0;
61                         try {
62                                 rowAffected = ExecuteDelete (keys, values);
63                         }
64                         catch (Exception e) {
65                                 if (!callBack (rowAffected, e))
66                                         throw;
67                                 return;
68                         }
69                         callBack (rowAffected, null);
70                 }
71
72                 protected virtual int ExecuteDelete(IDictionary keys, IDictionary values)
73                 {
74                         throw new NotSupportedException ();
75                 }
76
77                 protected virtual int ExecuteInsert (IDictionary keys)
78                 {
79                         throw new NotSupportedException();
80                 }
81
82                 protected internal abstract IEnumerable ExecuteSelect (
83                                         DataSourceSelectArguments arguments);
84
85                 protected virtual int ExecuteUpdate (IDictionary keys, IDictionary values, 
86                                                                 IDictionary oldValues )
87                 {
88                         throw new NotSupportedException ();
89                 }
90
91                 public virtual void Insert (IDictionary values, 
92                                         DataSourceViewOperationCallback callBack)
93                 {
94                         if (callBack == null)
95                                 throw new ArgumentNullException("callBack");
96
97                         int rowAffected = 0;
98                         Exception passOn = null;
99                         try {
100                                 rowAffected = ExecuteInsert (values);
101                         } catch (Exception e) {
102                                 passOn = e;
103                         }
104
105                         if (!callBack (rowAffected, passOn) && passOn != null)
106                                 throw passOn;
107                 }
108
109                 protected virtual void OnDataSourceViewChanged (EventArgs eventArgs)
110                 {
111                         if (eventsList != null) {
112                                 EventHandler evtHandler = eventsList [EventDataSourceViewChanged] as EventHandler;
113                                 if (evtHandler != null)
114                                         evtHandler(this, eventArgs);
115                         }
116                 }
117                 
118                 protected internal virtual void RaiseUnsupportedCapabilityError (
119                                                 DataSourceCapabilities capability)
120                 {
121                         if ((capability & DataSourceCapabilities.Sort) != 0)
122                                 if (!CanSort)
123                                         throw new NotSupportedException ("Sort Capabilites");
124
125                         if ((capability & DataSourceCapabilities.Page) != 0)
126                                 if (!CanPage)
127                                         throw new NotSupportedException("Page Capabilites");
128
129                         if ((capability & DataSourceCapabilities.RetrieveTotalRowCount) != 0)
130                                 if (!CanRetrieveTotalRowCount)
131                                         throw new NotSupportedException("RetrieveTotalRowCount Capabilites");
132
133                         return;
134                 }
135
136                 public virtual void Select (DataSourceSelectArguments selectArgs,
137                                                 DataSourceViewSelectCallback callBack)
138                 {
139                         if (callBack == null)
140                                 throw new ArgumentNullException("callBack");
141
142                         selectArgs.RaiseUnsupportedCapabilitiesError (this);
143                         
144                         IEnumerable selectList = ExecuteSelect (selectArgs);
145                         callBack (selectList);
146                 }
147
148                 public virtual void Update(IDictionary keys, IDictionary values,
149                         IDictionary oldValues, DataSourceViewOperationCallback callBack)
150                 {
151                         if (callBack == null)
152                                 throw new ArgumentNullException ("callBack");
153
154                         int rowAffected = 0;
155                         Exception passOn = null;
156                         try {
157                                 rowAffected = ExecuteUpdate (keys, values, oldValues);
158                         } catch (Exception e) {
159                                 passOn = e;
160                         }
161
162                         if (!callBack (rowAffected, passOn) && passOn != null)
163                                 throw passOn;
164                 } 
165                 
166                 public virtual bool CanDelete { get { return false; } }
167                 public virtual bool CanInsert { get { return false; } }
168                 public virtual bool CanPage { get { return false; } }
169                 public virtual bool CanRetrieveTotalRowCount { get { return false; } }
170                 public virtual bool CanSort { get { return false; } }
171                 public virtual bool CanUpdate { get { return false; } }
172
173                 EventHandlerList eventsList;
174                 protected EventHandlerList Events {
175                         get {
176                                 if (eventsList == null)
177                                         eventsList = new EventHandlerList();
178
179                                 return eventsList;
180                         }
181                 }
182                 
183                 internal bool HasEvents ()
184                 {
185                         return eventsList != null;
186                 }
187
188                 public string Name { 
189                         get { return viewName; } 
190                 }
191
192                 static readonly object EventDataSourceViewChanged = new object ();
193                                 
194                 public event EventHandler DataSourceViewChanged
195                 {
196                         add { Events.AddHandler (EventDataSourceViewChanged, value); }
197                         remove { Events.RemoveHandler (EventDataSourceViewChanged, value); }
198                 }
199                 
200         }
201         
202 }
203 #endif
204