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