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