2004-03-09 Umadevi S (sumadevi@novell.com)
[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 using System;
13 using System.Collections;
14 using System.ComponentModel;
15
16 namespace System.Data.Common {
17         [ListBindable (false)]
18         [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DataTableMappingCollectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
19         public sealed class DataTableMappingCollection : MarshalByRefObject, ITableMappingCollection, IList, ICollection, IEnumerable
20         {
21                 #region Fields
22
23                 ArrayList mappings;
24                 Hashtable sourceTables;
25                 Hashtable dataSetTables;
26
27                 #endregion
28
29                 #region Constructors 
30
31                 public DataTableMappingCollection() 
32                 {
33                         mappings = new ArrayList ();
34                         sourceTables = new Hashtable ();
35                         dataSetTables = new Hashtable ();
36                 }
37
38                 #endregion // Constructors
39
40                 #region Properties
41
42                 [Browsable (false)]
43                 [DataSysDescription ("The number of items in the collection")]
44                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
45                 public int Count {
46                         get { return mappings.Count; }
47                 }
48
49                 [Browsable (false)]
50                 [DataSysDescription ("The specified DataTableMapping object")]
51                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
52                 public DataTableMapping this [int index] {
53                         get { return (DataTableMapping)(mappings[index]); }
54                         set { 
55                                 DataTableMapping mapping = (DataTableMapping) mappings[index];
56                                 sourceTables [mapping.SourceTable] = value;
57                                 dataSetTables [mapping.DataSetTable] = value;
58                                 mappings [index] = value; 
59                         }
60                 }
61
62                 [Browsable (false)]
63                 [DataSysDescription ("The specified DataTableMapping object")]
64                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
65                 public DataTableMapping this [string sourceTable] {
66                         get { return (DataTableMapping) sourceTables[sourceTable]; }
67                         set { this [mappings.IndexOf (sourceTables[sourceTable])] = value; }
68                 }
69                         
70                 object IList.this [int index] {
71                         get { return (object)(this[index]); }
72                         set { 
73                                 if (!(value is DataTableMapping))
74                                         throw new ArgumentException (); 
75                                 this[index] = (DataTableMapping)value;
76                          } 
77                 }
78
79                 bool ICollection.IsSynchronized {
80                         get { return mappings.IsSynchronized; }
81                 }
82
83                 object ICollection.SyncRoot {
84                         get { return mappings.SyncRoot; }
85                 }
86
87                 bool IList.IsFixedSize {
88                         get { return false; }
89                 }
90
91                 bool IList.IsReadOnly {
92                         get { return false; }
93                 }
94
95                 object ITableMappingCollection.this [string sourceTable] {
96                         get { return this [sourceTable]; }
97                         set { 
98                                 if (!(value is DataTableMapping))
99                                         throw new ArgumentException ();
100                                 this [sourceTable] = (DataTableMapping) value;
101                         }
102                 }
103
104                 #endregion // Properties
105
106                 #region Methods
107
108                 public int Add (object value) 
109                 {
110                         if (!(value is System.Data.Common.DataTableMapping))
111                                 throw new SystemException ("The object passed in was not a DataTableMapping object.");
112
113                         sourceTables[((DataTableMapping)value).SourceTable] = value;    
114                         dataSetTables[((DataTableMapping)value).DataSetTable] = value;  
115                         return mappings.Add (value);
116                 }
117
118                 public DataTableMapping Add (string sourceTable, string dataSetTable) 
119                 {
120                         DataTableMapping mapping = new DataTableMapping (sourceTable, dataSetTable);
121                         Add (mapping);
122                         return mapping;
123                 }
124
125 #if NET_1_2
126                 [MonoTODO]
127                 public void AddRange (Array values)
128                 {
129                         throw new NotImplementedException ();
130                 }
131 #endif
132
133                 public void AddRange (DataTableMapping[] values) 
134                 {
135                         foreach (DataTableMapping dataTableMapping in values)
136                                 this.Add (dataTableMapping);
137                 }
138
139                 public void Clear () 
140                 {
141                         sourceTables.Clear ();
142                         dataSetTables.Clear ();
143                         mappings.Clear ();
144                 }
145
146                 public bool Contains (object value) 
147                 {
148                         return mappings.Contains (value);
149                 }
150
151                 public bool Contains (string value) 
152                 {
153                         return sourceTables.Contains (value);
154                 }
155
156                 public void CopyTo (Array array, int index) 
157                 {
158                         mappings.CopyTo (array, index);
159                 }
160
161                 public DataTableMapping GetByDataSetTable (string dataSetTable) 
162                 {
163                         return (DataTableMapping)(dataSetTables[dataSetTable]);
164                 }
165
166                 [EditorBrowsable (EditorBrowsableState.Advanced)]
167                 public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction) 
168                 {
169                         if (tableMappings.Contains (sourceTable))
170                                 return tableMappings[sourceTable];
171                         if (mappingAction == MissingMappingAction.Error)
172                                 throw new InvalidOperationException ();
173                         if (mappingAction == MissingMappingAction.Ignore)
174                                 return null;
175                         return new DataTableMapping (sourceTable, dataSetTable);
176                 }
177
178                 public IEnumerator GetEnumerator ()
179                 {
180                         return mappings.GetEnumerator ();
181                 }
182
183                 public int IndexOf (object value) 
184                 {
185                         return mappings.IndexOf (value);
186                 }
187
188                 public int IndexOf (string sourceTable) 
189                 {
190                         return IndexOf (sourceTables[sourceTable]);
191                 }
192
193                 public int IndexOfDataSetTable (string dataSetTable) 
194                 {
195                         return IndexOf ((DataTableMapping)(dataSetTables[dataSetTable]));
196                 }
197
198                 public void Insert (int index, object value) 
199                 {
200                         mappings.Insert (index, value);
201                 }
202
203                 ITableMapping ITableMappingCollection.Add (string sourceTableName, string dataSetTableName)
204                 {
205                         ITableMapping tableMapping = new DataTableMapping (sourceTableName, dataSetTableName);
206                         Add (tableMapping);
207                         return tableMapping;
208                 }
209
210                 ITableMapping ITableMappingCollection.GetByDataSetTable (string dataSetTableName)
211                 {
212                         return this [mappings.IndexOf (dataSetTables [dataSetTableName])];
213                 }
214
215                 public void Remove (object value) 
216                 {
217                         mappings.Remove ((DataTableMapping) value);
218                 }
219
220                 public void RemoveAt (int index) 
221                 {
222                         mappings.RemoveAt (index);
223                 }
224
225                 public void RemoveAt (string sourceTable) 
226                 {
227                         RemoveAt (mappings.IndexOf (sourceTables[sourceTable]));
228                 }
229
230                 #endregion // Methods
231         }
232 }