Locale.cs removed
[mono.git] / mcs / class / System.Data / System.Data / DataRelationCollection.cs
1 //
2 // System.Data.DataRelationCollection.cs
3 //
4 // Author:
5 //   Christopher Podurgiel (cpodurgiel@msn.com)
6 //   Daniel Morgan <danmorg@sc.rr.com>
7 //   Tim Coleman (tim@timcoleman.com)
8 //   Alan Tam Siu Lung <Tam@SiuLung.com>
9 //
10 // (C) Chris Podurgiel
11 // (C) 2002 Daniel Morgan
12 // Copyright (C) Tim Coleman, 2002
13 //
14
15 using System;
16 using System.Collections;
17 using System.ComponentModel;
18
19 namespace System.Data {
20         /// <summary>
21         /// Represents the collection of DataRelation objects for this DataSet.
22         /// </summary>
23         [Editor]
24         [DefaultEvent ("CollectionChanged")]
25         [Serializable]
26         public abstract class DataRelationCollection : InternalDataCollectionBase
27         {
28                 /// <summary>
29                 /// Summary description for DataTableRelationCollection.
30                 /// </summary>
31                 internal class DataSetRelationCollection : DataRelationCollection
32                 {
33                         private DataSet dataSet;
34                         
35                         /// <summary>
36                         /// Initializes a new instance of the DataSetRelationCollection class.
37                         /// </summary>
38                         internal DataSetRelationCollection (DataSet dataSet)
39                         {
40                                 this.dataSet = dataSet;
41                         }
42
43                         /// <summary>
44                         /// Gets the DataRelation object specified by name.
45                         /// </summary>
46                         public override DataRelation this [string name]
47                         {
48                                 get {
49                                         foreach (DataRelation dataRelation in List)
50                                                 if (dataRelation.RelationName == name) return dataRelation;
51                                         return null;
52                                 }
53                         }
54
55                         /// <summary>
56                         /// Gets the DataRelation object at the specified index.
57                         /// </summary>
58                         public override DataRelation this [int index]
59                         {
60                                 get {
61                                         return List [index] as DataRelation;
62                                 }
63                         }
64
65                         protected override DataSet GetDataSet()
66                         {
67                                 return dataSet;
68                         }
69
70                         /// <summary>
71                         /// Performs verification on the table.
72                         /// </summary>
73                         /// <param name="relation">The relation to check.</param>
74                         protected override void AddCore (DataRelation relation)
75                         {
76                                 base.AddCore (relation);
77                                 if (relation.ChildTable.DataSet != this.dataSet || relation.ParentTable.DataSet != this.dataSet)
78                                         throw new DataException ();
79                                 List.Add (relation);
80                                 relation.SetDataSet (dataSet);
81                                 relation.ParentTable.ChildRelations.Add (relation);
82                                 relation.ChildTable.ParentRelations.Add (relation);
83                                 ForeignKeyConstraint foreignKeyConstraint = null;
84                                 if (relation.createConstraints) {
85                                         foreignKeyConstraint = new ForeignKeyConstraint (relation.ParentColumns, relation.ChildColumns);
86                                         relation.ChildTable.Constraints.Add (foreignKeyConstraint);
87                                 }
88                                 UniqueConstraint uniqueConstraint = null;
89                                 foreach (object o in List) {
90                                         if (o is UniqueConstraint) {
91                                                 UniqueConstraint uc = (UniqueConstraint) o;
92                                                 if (uc.Columns.Length == relation.ParentColumns.Length) {
93                                                         bool allColumnsEqual = true;
94                                                         for (int columnCnt = 0; columnCnt < uc.Columns.Length; ++columnCnt) {
95                                                                 if (uc.Columns[columnCnt] != relation.ParentColumns[columnCnt]) {
96                                                                         allColumnsEqual = false;
97                                                                         break;
98                                                                 }
99                                                         }
100                                                         if (allColumnsEqual) {
101                                                                 uniqueConstraint = uc;
102                                                                 break;
103                                                         }
104                                                 }
105                                         }
106                                 }
107                                 relation.SetParentKeyConstraint (uniqueConstraint);
108                                 relation.SetChildKeyConstraint (foreignKeyConstraint);
109                         }
110
111                         public override void AddRange (DataRelation[] relations)
112                         {
113                                 base.AddRange (relations);
114                         }
115
116                         public override void Clear ()
117                         {
118                                 base.Clear ();
119                         }
120
121                         protected override void RemoveCore (DataRelation relation)
122                         {
123                                 base.RemoveCore (relation);
124                                 relation.SetDataSet (null);
125                                 relation.ParentTable.ChildRelations.Remove (relation);
126                                 relation.ChildTable.ParentRelations.Remove (relation);
127                                 ForeignKeyConstraint foreignKeyConstraint = null;
128                                 relation.SetParentKeyConstraint (null);
129                                 relation.SetChildKeyConstraint (null);
130                         }
131
132                         protected override ArrayList List {
133                                 get {
134                                         return base.List;
135                                 }
136                         }
137                 }
138
139                 /// <summary>
140                 /// Summary description for DataTableRelationCollection.
141                 /// </summary>
142                 internal class DataTableRelationCollection : DataRelationCollection
143                 {
144                         private DataTable dataTable;
145                         
146                         /// <summary>
147                         /// Initializes a new instance of the DataTableRelationCollection class.
148                         /// </summary>
149                         internal DataTableRelationCollection (DataTable dataTable)
150                         {
151                                 this.dataTable = dataTable;
152                         }
153
154                         /// <summary>
155                         /// Gets the DataRelation object specified by name.
156                         /// </summary>
157                         public override DataRelation this [string name]
158                         {
159                                 get {
160                                         foreach (DataRelation dataRelation in List)
161                                                 if (dataRelation.RelationName == name) return dataRelation;
162                                         return null;
163                                 }
164                         }
165
166                         /// <summary>
167                         /// Gets the DataRelation object at the specified index.
168                         /// </summary>
169                         public override DataRelation this [int index]
170                         {
171                                 get {
172                                         return List [index] as DataRelation;
173                                 }
174                         }
175
176                         protected override DataSet GetDataSet()
177                         {
178                                 return dataTable.DataSet;
179                         }
180
181                         protected override void AddCore (DataRelation relation)
182                         {
183                                 base.AddCore (relation);
184                         }
185
186                         protected override void RemoveCore (DataRelation relation)
187                         {
188                                 base.RemoveCore (relation);
189                         }
190
191                         protected override ArrayList List {
192                                 get {
193                                         return base.List;
194                                 }
195                         }
196                 }
197
198                 private int defaultNameIndex;
199                 private bool inTransition;
200                 
201                 /// <summary>
202                 /// Initializes a new instance of the DataRelationCollection class.
203                 /// </summary>
204                 protected DataRelationCollection () 
205                         : base ()
206                 {
207                         defaultNameIndex = 1;
208                         inTransition = false;
209                 }
210
211                 /// <summary>
212                 /// Gets the DataRelation object specified by name.
213                 /// </summary>
214                 public abstract DataRelation this[string name]{get;}
215
216                 /// <summary>
217                 /// Gets the DataRelation object at the specified index.
218                 /// </summary>
219                 public abstract DataRelation this[int index]{get;}
220
221                 
222                 #region Add Methods
223                 private string GetNextDefaultRelationName ()
224                 {
225                         string defRelationName = "Relation";
226                         for (int index = 1; Contains (defRelationName); ++index) {
227                                 defRelationName = "Relation" + index;
228                         }
229                         return defRelationName;
230                 }
231
232                 /// <summary>
233                 /// Adds a DataRelation to the DataRelationCollection.
234                 /// </summary>
235                 /// <param name="relation">The DataRelation to add to the collection.</param>
236                 [MonoTODO]
237                 public void Add(DataRelation relation)
238                 {
239                         this.AddCore (relation);
240                         CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Add, this);
241                         List.Add(relation);
242                         OnCollectionChanged(e);
243                 }
244
245                 /// <summary>
246                 /// Creates a relation given the parameters and adds it to the collection. The name is defaulted.
247                 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
248                 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
249                 /// The CollectionChanged event is fired if it succeeds.
250                 /// </summary>
251                 /// <param name="parentColumn">parent column of relation.</param>
252                 /// <param name="childColumn">child column of relation.</param>
253                 /// <returns>The created DataRelation.</returns>
254                 public virtual DataRelation Add(DataColumn parentColumn, DataColumn childColumn)
255                 {       
256                         DataRelation dataRelation = new DataRelation(GetNextDefaultRelationName (), parentColumn, childColumn);
257                         Add(dataRelation);
258                         return dataRelation;
259                 }
260
261                 /// <summary>
262                 /// Creates a relation given the parameters and adds it to the collection. The name is defaulted.
263                 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
264                 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
265                 /// The CollectionChanged event is raised if it succeeds.
266                 /// </summary>
267                 /// <param name="parentColumns">An array of parent DataColumn objects.</param>
268                 /// <param name="childColumns">An array of child DataColumn objects.</param>
269                 /// <returns>The created DataRelation.</returns>
270                 public virtual DataRelation Add(DataColumn[] parentColumns, DataColumn[] childColumns)
271                 {
272                         DataRelation dataRelation = new DataRelation(GetNextDefaultRelationName (), parentColumns, childColumns);
273                         Add(dataRelation);
274                         return dataRelation;
275                 }
276
277                 /// <summary>
278                 /// Creates a relation given the parameters and adds it to the collection.
279                 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
280                 /// A DuplicateNameException is generated if this collection already has a relation with the same name (case insensitive).
281                 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
282                 /// The CollectionChanged event is raised if it succeeds.
283                 /// </summary>
284                 /// <param name="name">The name of the relation.</param>
285                 /// <param name="parentColumn">parent column of relation.</param>
286                 /// <returns>The created DataRelation.</returns>
287                 /// <returns></returns>
288                 public virtual DataRelation Add(string name, DataColumn parentColumn, DataColumn childColumn)
289                 {
290                         //If no name was supplied, give it a default name.
291                         if (name == null || name == "") name = GetNextDefaultRelationName ();
292
293                         DataRelation dataRelation = new DataRelation(name, parentColumn, childColumn);
294                         Add(dataRelation);
295                         return dataRelation;
296                 }
297
298                 /// <summary>
299                 /// Creates a DataRelation with the specified name, and arrays of parent and child columns, and adds it to the collection.
300                 /// </summary>
301                 /// <param name="name">The name of the DataRelation to create.</param>
302                 /// <param name="parentColumns">An array of parent DataColumn objects.</param>
303                 /// <param name="childColumns">An array of child DataColumn objects.</param>
304                 /// <returns>The created DataRelation.</returns>
305                 public virtual DataRelation Add(string name, DataColumn[] parentColumns, DataColumn[] childColumns)
306                 {
307                         //If no name was supplied, give it a default name.
308                         if (name == null || name == "") name = GetNextDefaultRelationName ();
309
310                         DataRelation dataRelation = new DataRelation(name, parentColumns, childColumns);
311                         Add(dataRelation);
312                         return dataRelation;
313                 }
314
315                 /// <summary>
316                 /// Creates a relation given the parameters and adds it to the collection.
317                 /// An ArgumentException is generated if this relation already belongs to this collection or belongs to another collection.
318                 /// A DuplicateNameException is generated if this collection already has a relation with the same name (case insensitive).
319                 /// An InvalidConstraintException is generated if the relation can't be created based on the parameters.
320                 /// The CollectionChanged event is raised if it succeeds.
321                 /// </summary>
322                 /// <param name="name">The name of the relation.</param>
323                 /// <param name="parentColumn">parent column of relation.</param>
324                 /// <param name="childColumn">child column of relation.</param>
325                 /// <param name="createConstraints">true to create constraints; otherwise false. (default is true)</param>
326                 /// <returns>The created DataRelation.</returns>
327                 public virtual DataRelation Add(string name, DataColumn parentColumn, DataColumn childColumn, bool createConstraints)
328                 {
329                         //If no name was supplied, give it a default name.
330                         if (name == null || name == "") name = GetNextDefaultRelationName ();
331
332                         DataRelation dataRelation = new DataRelation(name, parentColumn, childColumn, createConstraints);
333                         Add(dataRelation);
334                         return dataRelation;
335                 }
336
337                 /// <summary>
338                 /// Creates a DataRelation with the specified name, arrays of parent and child columns, 
339                 /// and value specifying whether to create a constraint, and adds it to the collection.
340                 /// </summary>
341                 /// <param name="name">The name of the DataRelation to create.</param>
342                 /// <param name="parentColumns">An array of parent DataColumn objects.</param>
343                 /// <param name="childColumns">An array of child DataColumn objects.</param>
344                 /// <param name="createConstraints">true to create a constraint; otherwise false.</param>
345                 /// <returns>The created DataRelation.</returns>
346                 public virtual DataRelation Add(string name, DataColumn[] parentColumns, DataColumn[] childColumns, bool createConstraints)
347                 {
348                         //If no name was supplied, give it a default name.
349                         if (name == null || name == "") name = GetNextDefaultRelationName ();
350
351                         DataRelation dataRelation = new DataRelation(name, parentColumns, childColumns, createConstraints);
352                         Add(dataRelation);
353                         return dataRelation;
354                 }
355                 #endregion
356         
357                 /// <summary>
358                 /// Performs verification on the table.
359                 /// </summary>
360                 /// <param name="relation">The relation to check.</param>
361                 [MonoTODO]
362                 protected virtual void AddCore(DataRelation relation)
363                 {
364                         if (relation == null)
365                         {
366                                 //TODO: Issue a good exception message.
367                                 throw new ArgumentNullException();
368                         }
369                         else if(List.IndexOf(relation) != -1)
370                         {
371                                 //TODO: Issue a good exception message.
372                                 throw new ArgumentException();
373                         }
374                         else if(List.Contains(relation.RelationName))
375                         {
376                                 //TODO: Issue a good exception message.
377                                 throw new DuplicateNameException("A Relation named " + relation.RelationName + " already belongs to this DataSet.");
378                         }
379                 }
380
381                 /// <summary>
382                 /// Copies the elements of the specified DataRelation array to the end of the collection.
383                 /// </summary>
384                 /// <param name="relations">The array of DataRelation objects to add to the collection.</param>
385                 public virtual void AddRange(DataRelation[] relations)
386                 {
387                         foreach (DataRelation relation in relations) Add(relation);
388                 }
389
390                 [MonoTODO]
391                 public virtual bool CanRemove(DataRelation relation)
392                 {
393                         //TODO: Implement.
394                         return false;
395                 }
396
397                 public virtual void Clear()
398                 {
399                         List.Clear();
400                 }
401
402                 public virtual bool Contains(string name)
403                 {
404                         return IndexOf(name) != -1;
405                 }
406
407                 private CollectionChangeEventArgs CreateCollectionChangeEvent (CollectionChangeAction action)
408                 {
409                         return new CollectionChangeEventArgs (action, this);
410                 }
411
412                 protected abstract DataSet GetDataSet();
413
414                 public virtual int IndexOf(DataRelation relation)
415                 {
416                         return List.IndexOf(relation);
417                 }
418
419                 public virtual int IndexOf(string relationName)
420                 {
421                         return List.IndexOf(this[relationName]);
422                 }
423
424                 protected virtual void OnCollectionChanged (CollectionChangeEventArgs ccevent)
425                 {
426                         if (CollectionChanged != null)
427                                 CollectionChanged (this, ccevent);
428                 }
429
430                 [MonoTODO]
431                 protected internal virtual void OnCollectionChanging (CollectionChangeEventArgs ccevent)
432                 {
433                         throw new NotImplementedException ();
434                 }
435
436                 public void Remove (DataRelation relation)
437                 {
438                         RemoveCore (relation);
439                         List.Remove (relation);
440                         OnCollectionChanged (CreateCollectionChangeEvent (CollectionChangeAction.Remove));
441                 }
442
443                 public void Remove (string name)
444                 {
445                         Remove ((DataRelation) List[IndexOf (name)]);
446                 }
447
448                 public void RemoveAt (int index)
449                 {
450                         List.RemoveAt (index);
451                 }
452
453                 [MonoTODO]
454                 protected virtual void RemoveCore(DataRelation relation)
455                 {
456                         // TODO: What have to be done?
457                 }
458
459                 [ResDescriptionAttribute ("Occurs whenever this collection's membership changes.")]
460                 public event CollectionChangeEventHandler CollectionChanged;
461         }
462 }