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