This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Data / System.Data.Common / DataColumnMappingCollection.cs
1 //
2 // System.Data.Common.DataColumnMappingCollection
3 //
4 // Authors:
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 using System.Data;
39
40 namespace System.Data.Common {
41         public sealed class DataColumnMappingCollection : MarshalByRefObject, IColumnMappingCollection , IList, ICollection, IEnumerable
42         {
43                 #region Fields
44
45                 ArrayList list;
46                 Hashtable sourceColumns;
47                 Hashtable dataSetColumns;
48
49                 #endregion // Fields
50
51                 #region Constructors 
52
53                 public DataColumnMappingCollection () 
54                 {
55                         list = new ArrayList ();
56                         sourceColumns = new Hashtable ();
57                         dataSetColumns = new Hashtable ();
58                 }
59
60                 #endregion // Constructors
61
62                 #region Properties
63
64                 [Browsable (false)]
65                 [DataSysDescription ("The number of items in the collection")]
66                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
67                 public int Count {
68                         get { return list.Count; }
69                 }
70
71                 [Browsable (false)]
72                 [DataSysDescription ("The specified DataColumnMapping object.")]
73                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
74                 public DataColumnMapping this [int index] {
75                         get { return (DataColumnMapping)(list[index]); }
76                         set { 
77                                 DataColumnMapping mapping = (DataColumnMapping)(list[index]);
78                                 sourceColumns[mapping] = value;
79                                 dataSetColumns[mapping] = value;
80                                 list[index] = value;
81                         }
82                 }
83
84                 [Browsable (false)]
85                 [DataSysDescription ("DataTableMappings_Item")]
86                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
87                 public DataColumnMapping this [string sourceColumn] {
88                         get {
89                                 if (!Contains(sourceColumn)) {
90                                         throw new IndexOutOfRangeException("DataColumnMappingCollection doesn't contain DataColumnMapping with SourceColumn '" + sourceColumn + "'.");
91                                 }
92                                 return (DataColumnMapping) sourceColumns[sourceColumn]; }
93                         set { this [list.IndexOf (sourceColumns[sourceColumn])] = value; }
94                 }
95
96                 object ICollection.SyncRoot {
97                         get { return list.SyncRoot; }
98                 }
99
100                 bool ICollection.IsSynchronized {
101                         get { return list.IsSynchronized; }
102                 }
103
104                 object IColumnMappingCollection.this [string sourceColumn] {
105                         get { return this [sourceColumn]; }
106                         set {
107                                 if (!(value is DataColumnMapping))
108                                         throw new ArgumentException ();
109                                 this [sourceColumn] = (DataColumnMapping) value;
110                         }
111                 }
112
113                 object IList.this [int index] {
114                         get { return this[index]; }
115                         set {
116                                 if (!(value is DataColumnMapping))
117                                         throw new ArgumentException ();
118                                 this [index] = (DataColumnMapping) value;
119                          }
120                 }
121
122                 bool IList.IsReadOnly {
123                         get { return false; }
124                 }
125
126                 bool IList.IsFixedSize {
127                         get { return false; }
128                 }
129                 
130                 #endregion // Properties
131
132                 #region Methods
133
134                 public int Add (object value)
135                 {
136                         if (!(value is DataColumnMapping))
137                                 throw new InvalidCastException ();
138
139                         list.Add (value);
140                         sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
141                         dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
142                         return list.IndexOf (value);
143                 }
144
145                 public DataColumnMapping Add (string sourceColumn, string dataSetColumn)
146                 {
147                         DataColumnMapping mapping = new DataColumnMapping (sourceColumn, dataSetColumn);
148                         Add (mapping);
149                         return mapping;
150                 }
151
152 #if NET_2_0
153                 [MonoTODO]
154                 public void AddRange (Array values)
155                 {
156                         throw new NotImplementedException ();
157                 }
158 #endif
159
160                 public void AddRange (DataColumnMapping[] values) 
161                 {
162                         foreach (DataColumnMapping mapping in values)
163                                 Add (mapping);
164                 }
165
166                 public void Clear () 
167                 {
168                         list.Clear ();
169                 }
170
171                 public bool Contains (object value) 
172                 {
173                         if  (!(value is DataColumnMapping))
174                                 throw new InvalidCastException("Object is not of type DataColumnMapping");
175                         return (list.Contains (value));
176                 }
177
178                 public bool Contains (string value)
179                 {
180                         return (sourceColumns.Contains (value));
181                 }
182
183                 public void CopyTo (Array array, int index) 
184                 {
185                         (list.ToArray()).CopyTo(array,index);
186                 }
187
188                 public DataColumnMapping GetByDataSetColumn (string value) 
189                 {
190                         // this should work case-insenstive.
191                         if (!(dataSetColumns[value] == null))
192                                 return (DataColumnMapping)(dataSetColumns[value]);
193                         else {
194                                 string lowcasevalue = value.ToLower();
195                                 object [] keyarray = new object[dataSetColumns.Count];
196                                 dataSetColumns.Keys.CopyTo(keyarray,0);
197                                 for (int i=0; i<keyarray.Length; i++) {
198                                         string temp = (string) keyarray[i];
199                                         if (lowcasevalue.Equals(temp.ToLower()))
200                                                 return (DataColumnMapping)(dataSetColumns[keyarray[i]]);
201                                 }
202                                 return null;
203                         
204                         }       
205                 }
206
207                 [EditorBrowsable (EditorBrowsableState.Advanced)]
208                 public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction) 
209                 {
210                         if (columnMappings.Contains (sourceColumn))
211                                 return columnMappings[sourceColumn];
212                         if (mappingAction == MissingMappingAction.Ignore)
213                                 return null;
214                         if (mappingAction == MissingMappingAction.Error)
215                                 throw new InvalidOperationException (String.Format ("Missing SourceColumn mapping for '{0}'", sourceColumn));
216                         return new DataColumnMapping (sourceColumn, sourceColumn);
217                 }
218
219 #if NET_2_0
220                 [MonoTODO]
221                 public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
222                 {
223                         throw new NotImplementedException ();
224                 }
225 #endif
226
227                 public IEnumerator GetEnumerator ()
228                 {
229                         return list.GetEnumerator ();
230                 }
231
232                 IColumnMapping IColumnMappingCollection.Add (string sourceColumnName, string dataSetColumnName)
233                 {
234                         return Add (sourceColumnName, dataSetColumnName);
235                 }
236
237                 IColumnMapping IColumnMappingCollection.GetByDataSetColumn (string dataSetColumnName)
238                 {
239                         return GetByDataSetColumn (dataSetColumnName);
240                 }
241
242                 public int IndexOf (object value) 
243                 {
244                         return list.IndexOf (value);
245                 }
246
247                 public int IndexOf (string sourceColumn)
248                 {
249                         return list.IndexOf (sourceColumns[sourceColumn]);
250                 }
251
252                 public int IndexOfDataSetColumn (string value) 
253                 {
254                         // this should work case-insensitive
255                                         
256                          if (!(dataSetColumns[value] == null))
257                                 return list.IndexOf (dataSetColumns[value]);
258                         else {
259                                 string lowcasevalue = value.ToLower();
260                                 object [] keyarray = new object[dataSetColumns.Count];
261                                 dataSetColumns.Keys.CopyTo(keyarray,0);
262                                 for (int i=0; i<keyarray.Length; i++) {
263                                         string temp = (string) keyarray[i];
264                                         if (lowcasevalue.Equals(temp.ToLower()))
265                                                 return list.IndexOf (dataSetColumns[keyarray[i]]);
266
267                                 }
268                                 return -1;
269                                                                                                     
270                         }
271         
272                 }
273
274                 public void Insert (int index, object value) 
275                 {
276                         list.Insert (index, value);
277                         sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
278                         dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
279                 }
280
281                 public void Remove (object value) 
282                 {
283                         int index = list.IndexOf (value);
284                         sourceColumns.Remove(((DataColumnMapping)value).SourceColumn);
285                         dataSetColumns.Remove(((DataColumnMapping)value).DataSetColumn);
286                         if (( index < 0 ) || (index >=list.Count))
287                                     throw new ArgumentException("There is no such element in collection.");
288
289                         list.Remove (value);
290                 }
291
292                 public void RemoveAt (int index) 
293                 {
294                         if (( index < 0 ) || (index >=list.Count))
295                                     throw new IndexOutOfRangeException("There is no element in collection.");
296
297                         Remove (list[index]);
298                 }
299
300                 public void RemoveAt (string sourceColumn)
301                 {
302                         RemoveAt (list.IndexOf (sourceColumns[sourceColumn]));
303                 }
304
305                 #endregion // Methods
306         }
307 }