updating to the latest module.
[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                 [DataSysDescription ("The number of items in the collection")]
67                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
68                 public int Count {
69                         get { return mappings.Count; }
70                 }
71
72                 [Browsable (false)]
73                 [DataSysDescription ("The specified DataTableMapping object")]
74                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
75                 public DataTableMapping this [int index] {
76                         get { return (DataTableMapping)(mappings[index]); }
77                         set { 
78                                 DataTableMapping mapping = (DataTableMapping) mappings[index];
79                                 sourceTables [mapping.SourceTable] = value;
80                                 dataSetTables [mapping.DataSetTable] = value;
81                                 mappings [index] = value; 
82                         }
83                 }
84
85                 [Browsable (false)]
86                 [DataSysDescription ("The specified DataTableMapping object")]
87                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
88                 public DataTableMapping this [string sourceTable] {
89                         get { return (DataTableMapping) sourceTables[sourceTable]; }
90                         set { this [mappings.IndexOf (sourceTables[sourceTable])] = value; }
91                 }
92                         
93                 object IList.this [int index] {
94                         get { return (object)(this[index]); }
95                         set { 
96                                 if (!(value is DataTableMapping))
97                                         throw new ArgumentException (); 
98                                 this[index] = (DataTableMapping)value;
99                          } 
100                 }
101
102                 bool ICollection.IsSynchronized {
103                         get { return mappings.IsSynchronized; }
104                 }
105
106                 object ICollection.SyncRoot {
107                         get { return mappings.SyncRoot; }
108                 }
109
110                 bool IList.IsFixedSize {
111                         get { return false; }
112                 }
113
114                 bool IList.IsReadOnly {
115                         get { return false; }
116                 }
117
118                 object ITableMappingCollection.this [string sourceTable] {
119                         get { return this [sourceTable]; }
120                         set { 
121                                 if (!(value is DataTableMapping))
122                                         throw new ArgumentException ();
123                                 this [sourceTable] = (DataTableMapping) value;
124                         }
125                 }
126
127                 #endregion // Properties
128
129                 #region Methods
130
131                 public int Add (object value) 
132                 {
133                         if (!(value is System.Data.Common.DataTableMapping))
134                                 throw new InvalidCastException ("The object passed in was not a DataTableMapping object.");
135
136                         sourceTables[((DataTableMapping)value).SourceTable] = value;    
137                         dataSetTables[((DataTableMapping)value).DataSetTable] = value;  
138                         return mappings.Add (value);
139                 }
140
141                 public DataTableMapping Add (string sourceTable, string dataSetTable) 
142                 {
143                         DataTableMapping mapping = new DataTableMapping (sourceTable, dataSetTable);
144                         Add (mapping);
145                         return mapping;
146                 }
147
148 #if NET_2_0
149                 [MonoTODO]
150                 public void AddRange (Array values)
151                 {
152                         throw new NotImplementedException ();
153                 }
154 #endif
155
156                 public void AddRange (DataTableMapping[] values) 
157                 {
158                         foreach (DataTableMapping dataTableMapping in values)
159                                 this.Add (dataTableMapping);
160                 }
161
162                 public void Clear () 
163                 {
164                         sourceTables.Clear ();
165                         dataSetTables.Clear ();
166                         mappings.Clear ();
167                 }
168
169                 public bool Contains (object value) 
170                 {
171                         return mappings.Contains (value);
172                 }
173
174                 public bool Contains (string value) 
175                 {
176                         return sourceTables.Contains (value);
177                 }
178
179                 public void CopyTo (Array array, int index) 
180                 {
181                         mappings.CopyTo (array, index);
182                 }
183
184                 public DataTableMapping GetByDataSetTable (string dataSetTable) 
185                 {
186                         
187                         // this should work case-insenstive.
188                         if (!(dataSetTables[dataSetTable] == null)) 
189                                  return (DataTableMapping)(dataSetTables[dataSetTable]);
190                         else {
191                                 string lowcasevalue = dataSetTable.ToLower();
192                                 object [] keyarray = new object[dataSetTables.Count];
193                                 dataSetTables.Keys.CopyTo(keyarray,0);
194                                 for (int i=0; i<keyarray.Length; i++) {
195                                         string temp = (string) keyarray[i];
196                                         if (lowcasevalue.Equals(temp.ToLower()))                                                                return (DataTableMapping)(dataSetTables[keyarray[i]]);
197                                 }
198                                 return null;
199                                                                                 
200                         }
201
202                 }
203
204                 [EditorBrowsable (EditorBrowsableState.Advanced)]
205                 public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction) 
206                 {
207                         if (tableMappings.Contains (sourceTable))
208                                 return tableMappings[sourceTable];
209                         if (mappingAction == MissingMappingAction.Error)
210                                 throw new InvalidOperationException ();
211                         if (mappingAction == MissingMappingAction.Ignore)
212                                 return null;
213                         return new DataTableMapping (sourceTable, dataSetTable);
214                 }
215
216                 public IEnumerator GetEnumerator ()
217                 {
218                         return mappings.GetEnumerator ();
219                 }
220
221                 public int IndexOf (object value) 
222                 {
223                         return mappings.IndexOf (value);
224                 }
225
226                 public int IndexOf (string sourceTable) 
227                 {
228                         return IndexOf (sourceTables[sourceTable]);
229                 }
230
231                 public int IndexOfDataSetTable (string dataSetTable) 
232                 {
233                           // this should work case-insensitive
234                                                                                              
235                          if (!(dataSetTables[dataSetTable] == null)) 
236                                 return IndexOf ((DataTableMapping)(dataSetTables[dataSetTable]));
237                          else {
238                                 string lowcasevalue = dataSetTable.ToLower();
239                                 object [] keyarray = new object[dataSetTables.Count];
240                                 dataSetTables.Keys.CopyTo(keyarray,0);
241                                 for (int i=0; i<keyarray.Length; i++) {
242                                         string temp = (string) keyarray[i];
243                                         if (lowcasevalue.Equals(temp.ToLower()))
244                                                 return IndexOf ((DataTableMapping)(dataSetTables[keyarray[i]]));
245                                           
246                                 }
247                                 return -1;
248                                                                                                     
249                         }
250
251                 }
252
253                 public void Insert (int index, object value) 
254                 {
255                         mappings.Insert (index, value);
256                         sourceTables[((DataTableMapping)value).SourceTable] = value;
257                         dataSetTables[((DataTableMapping)value).DataSetTable] = value;
258                 }
259
260                 ITableMapping ITableMappingCollection.Add (string sourceTableName, string dataSetTableName)
261                 {
262                         ITableMapping tableMapping = new DataTableMapping (sourceTableName, dataSetTableName);
263                         Add (tableMapping);
264                         return tableMapping;
265                 }
266
267                 ITableMapping ITableMappingCollection.GetByDataSetTable (string dataSetTableName)
268                 {
269                         return this [mappings.IndexOf (dataSetTables [dataSetTableName])];
270                 }
271
272                 public void Remove (object value) 
273                 {
274                         if (!(value is DataTableMapping))
275                                  throw new InvalidCastException ();
276                         int index = mappings.IndexOf (value);
277                         if (( index < 0 ) || (index >=mappings.Count))
278                                     throw new ArgumentException("There is no such element in collection.");                                                                                 
279
280                         mappings.Remove ((DataTableMapping) value);
281                 }
282
283                 public void RemoveAt (int index) 
284                 {
285                          if (( index < 0 ) || (index >=mappings.Count))
286                                     throw new IndexOutOfRangeException("There is no element in collection.");
287
288                         mappings.RemoveAt (index);
289                 }
290
291                 public void RemoveAt (string sourceTable) 
292                 {
293                         RemoveAt (mappings.IndexOf (sourceTables[sourceTable]));
294                 }
295
296                 #endregion // Methods
297         }
298 }