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