New test.
[mono.git] / mcs / class / System.Data / System.Data.Common / DataTableMappingCollection.cs
1 //
2 // System.Data.Common.DataTableMappingCollection.cs
3 //
4 // Author:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Tim Coleman (tim@timcoleman.com)
7 //
8 // (C) Ximian, Inc
9 // Copyright (C) Tim Coleman, 2002-2003
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Collections;
37 using System.ComponentModel;
38
39 namespace System.Data.Common {
40         [ListBindable (false)]
41         [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DataTableMappingCollectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
42         public sealed class DataTableMappingCollection : MarshalByRefObject, ITableMappingCollection, IList, ICollection, IEnumerable
43         {
44                 #region Fields
45
46                 ArrayList mappings;
47                 Hashtable sourceTables;
48                 Hashtable dataSetTables;
49
50                 #endregion
51
52                 #region Constructors 
53
54                 public DataTableMappingCollection() 
55                 {
56                         mappings = new ArrayList ();
57                         sourceTables = new Hashtable ();
58                         dataSetTables = new Hashtable ();
59                 }
60
61                 #endregion // Constructors
62
63                 #region Properties
64
65                 [Browsable (false)]
66 #if !NET_2_0
67                 [DataSysDescription ("The number of items in the collection")]
68 #endif
69                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
70                 public int Count {
71                         get { return mappings.Count; }
72                 }
73
74                 [Browsable (false)]
75 #if !NET_2_0
76                 [DataSysDescription ("The specified DataTableMapping object")]
77 #endif
78                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
79                 public DataTableMapping this [int index] {
80                         get { return (DataTableMapping)(mappings[index]); }
81                         set { 
82                                 DataTableMapping mapping = (DataTableMapping) mappings[index];
83                                 sourceTables [mapping.SourceTable] = value;
84                                 dataSetTables [mapping.DataSetTable] = value;
85                                 mappings [index] = value; 
86                         }
87                 }
88
89                 [Browsable (false)]
90 #if !NET_2_0
91                 [DataSysDescription ("The specified DataTableMapping object")]
92 #endif
93                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
94                 public DataTableMapping this [string sourceTable] {
95                         get { return (DataTableMapping) sourceTables[sourceTable]; }
96                         set { this [mappings.IndexOf (sourceTables[sourceTable])] = value; }
97                 }
98                         
99                 object IList.this [int index] {
100                         get { return (object)(this[index]); }
101                         set { 
102                                 if (!(value is DataTableMapping))
103                                         throw new ArgumentException (); 
104                                 this[index] = (DataTableMapping)value;
105                          } 
106                 }
107
108                 bool ICollection.IsSynchronized {
109                         get { return mappings.IsSynchronized; }
110                 }
111
112                 object ICollection.SyncRoot {
113                         get { return mappings.SyncRoot; }
114                 }
115
116                 bool IList.IsFixedSize {
117                         get { return false; }
118                 }
119
120                 bool IList.IsReadOnly {
121                         get { return false; }
122                 }
123
124                 object ITableMappingCollection.this [string sourceTable] {
125                         get { return this [sourceTable]; }
126                         set { 
127                                 if (!(value is DataTableMapping))
128                                         throw new ArgumentException ();
129                                 this [sourceTable] = (DataTableMapping) value;
130                         }
131                 }
132
133                 #endregion // Properties
134
135                 #region Methods
136
137                 public int Add (object value) 
138                 {
139                         if (!(value is System.Data.Common.DataTableMapping))
140                                 throw new InvalidCastException ("The object passed in was not a DataTableMapping object.");
141
142                         sourceTables[((DataTableMapping)value).SourceTable] = value;    
143                         dataSetTables[((DataTableMapping)value).DataSetTable] = value;  
144                         return mappings.Add (value);
145                 }
146
147                 public DataTableMapping Add (string sourceTable, string dataSetTable) 
148                 {
149                         DataTableMapping mapping = new DataTableMapping (sourceTable, dataSetTable);
150                         Add (mapping);
151                         return mapping;
152                 }
153
154 #if NET_2_0
155                 [MonoTODO]
156                 public void AddRange (Array values)
157                 {
158                         throw new NotImplementedException ();
159                 }
160 #endif
161
162                 public void AddRange (DataTableMapping[] values) 
163                 {
164                         foreach (DataTableMapping dataTableMapping in values)
165                                 this.Add (dataTableMapping);
166                 }
167
168                 public void Clear () 
169                 {
170                         sourceTables.Clear ();
171                         dataSetTables.Clear ();
172                         mappings.Clear ();
173                 }
174
175                 public bool Contains (object value) 
176                 {
177                         return mappings.Contains (value);
178                 }
179
180                 public bool Contains (string value) 
181                 {
182                         return sourceTables.Contains (value);
183                 }
184
185                 public void CopyTo (Array array, int index) 
186                 {
187                         mappings.CopyTo (array, index);
188                 }
189
190 #if NET_2_0
191                 public void CopyTo (DataTableMapping[] array, int index) 
192                 {
193                         mappings.CopyTo (array, index);
194                 }
195 #endif
196
197                 public DataTableMapping GetByDataSetTable (string dataSetTable) 
198                 {
199                         
200                         // this should work case-insenstive.
201                         if (!(dataSetTables[dataSetTable] == null)) 
202                                  return (DataTableMapping)(dataSetTables[dataSetTable]);
203                         else {
204                                 string lowcasevalue = dataSetTable.ToLower();
205                                 object [] keyarray = new object[dataSetTables.Count];
206                                 dataSetTables.Keys.CopyTo(keyarray,0);
207                                 for (int i=0; i<keyarray.Length; i++) {
208                                         string temp = (string) keyarray[i];
209                                         if (lowcasevalue.Equals(temp.ToLower()))                                                                return (DataTableMapping)(dataSetTables[keyarray[i]]);
210                                 }
211                                 return null;
212                                                                                 
213                         }
214
215                 }
216
217                 [EditorBrowsable (EditorBrowsableState.Advanced)]
218                 public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction) 
219                 {
220                         if (tableMappings.Contains (sourceTable))
221                                 return tableMappings[sourceTable];
222                         if (mappingAction == MissingMappingAction.Error)
223                                 throw new InvalidOperationException (String.Format ("Missing source table mapping: '{0}'",
224                                                                                     sourceTable));
225                         if (mappingAction == MissingMappingAction.Ignore)
226                                 return null;
227                         return new DataTableMapping (sourceTable, dataSetTable);
228                 }
229
230                 public IEnumerator GetEnumerator ()
231                 {
232                         return mappings.GetEnumerator ();
233                 }
234
235                 public int IndexOf (object value) 
236                 {
237                         return mappings.IndexOf (value);
238                 }
239
240                 public int IndexOf (string sourceTable) 
241                 {
242                         return IndexOf (sourceTables[sourceTable]);
243                 }
244
245                 public int IndexOfDataSetTable (string dataSetTable) 
246                 {
247                           // this should work case-insensitive
248                                                                                              
249                          if (!(dataSetTables[dataSetTable] == null)) 
250                                 return IndexOf ((DataTableMapping)(dataSetTables[dataSetTable]));
251                          else {
252                                 string lowcasevalue = dataSetTable.ToLower();
253                                 object [] keyarray = new object[dataSetTables.Count];
254                                 dataSetTables.Keys.CopyTo(keyarray,0);
255                                 for (int i=0; i<keyarray.Length; i++) {
256                                         string temp = (string) keyarray[i];
257                                         if (lowcasevalue.Equals(temp.ToLower()))
258                                                 return IndexOf ((DataTableMapping)(dataSetTables[keyarray[i]]));
259                                           
260                                 }
261                                 return -1;
262                                                                                                     
263                         }
264
265                 }
266
267                 public void Insert (int index, object value) 
268                 {
269                         mappings.Insert (index, value);
270                         sourceTables[((DataTableMapping)value).SourceTable] = value;
271                         dataSetTables[((DataTableMapping)value).DataSetTable] = value;
272                 }
273
274 #if NET_2_0
275                 public void Insert (int index, DataTableMapping value) 
276                 {
277                         mappings.Insert (index, value);
278                         sourceTables[value.SourceTable] = value;
279                         dataSetTables[value.DataSetTable] = value;
280                 }
281 #endif
282
283                 ITableMapping ITableMappingCollection.Add (string sourceTableName, string dataSetTableName)
284                 {
285                         ITableMapping tableMapping = new DataTableMapping (sourceTableName, dataSetTableName);
286                         Add (tableMapping);
287                         return tableMapping;
288                 }
289
290                 ITableMapping ITableMappingCollection.GetByDataSetTable (string dataSetTableName)
291                 {
292                         return this [mappings.IndexOf (dataSetTables [dataSetTableName])];
293                 }
294
295                 public void Remove (object value) 
296                 {
297                         if (!(value is DataTableMapping))
298                                  throw new InvalidCastException ();
299                         int index = mappings.IndexOf (value);
300                         if (( index < 0 ) || (index >=mappings.Count))
301                                     throw new ArgumentException("There is no such element in collection.");
302                         mappings.Remove ((DataTableMapping) value);
303                 }
304
305 #if NET_2_0
306                 public void Remove (DataTableMapping value) 
307                 {
308                         int index = mappings.IndexOf (value);
309                         if (( index < 0 ) || (index >=mappings.Count))
310                                     throw new ArgumentException("There is no such element in collection."); 
311                         mappings.Remove ((DataTableMapping) value);
312                 }
313 #endif
314
315                 public void RemoveAt (int index) 
316                 {
317                          if (( index < 0 ) || (index >=mappings.Count))
318                                     throw new IndexOutOfRangeException("There is no element in collection.");
319
320                         mappings.RemoveAt (index);
321                 }
322
323                 public void RemoveAt (string sourceTable) 
324                 {
325                         RemoveAt (mappings.IndexOf (sourceTables[sourceTable]));
326                 }
327
328                 #endregion // Methods
329         }
330 }