2009-06-12 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web.DynamicData / Test / ModelProviders / DynamicDataContainerColumnProvider.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.DynamicData;
6 using System.Web.DynamicData.ModelProviders;
7
8 using MonoTests.DataSource;
9
10 namespace MonoTests.ModelProviders
11 {
12         public class DynamicDataContainerColumnProvider : ColumnProvider
13         {
14                 DynamicDataColumn column;
15                 bool associationResolved;
16
17                 public override AssociationProvider Association {
18                         get {
19                                 ResolveAssociations ();
20                                 return base.Association;
21                         }
22
23                         protected set {
24                                 base.Association = value;
25                         }
26                 }
27
28                 public DynamicDataContainerColumnProvider (DynamicDataContainerTableProvider owner, DynamicDataColumn column)
29                         : base (owner)
30                 {
31                         if (column == null)
32                                 throw new ArgumentNullException ("column");
33
34                         this.column = column;
35
36                         Type columnType = column.DataType;
37                         if (columnType == null)
38                                 throw new InvalidOperationException ("column.DataType must not be null for column '" + column.Name + "'");
39
40                         Name = column.Name;
41                         ColumnType = columnType;
42                         Nullable = columnType.IsGenericType && typeof (Nullable<>).IsAssignableFrom (columnType.GetGenericTypeDefinition ());
43                         IsPrimaryKey = column.PrimaryKey;
44                 }
45
46                 public void ResolveAssociations ()
47                 {
48                         if (associationResolved)
49                                 return;
50
51                         associationResolved = true;
52                         string associated = column.AssociatedTo;
53                         if (String.IsNullOrEmpty (associated))
54                                 return;
55
56                         string[] names = associated.Split (new char[] { '.' });
57                         if (names.Length != 2)
58                                 throw new ApplicationException ("Only associations of type Table.Column are supported");
59                         string tableName = names[0];
60                         string columnName = names[1];
61                         
62                         TableProvider tableProvider = null;
63                         try {
64                                 tableProvider = Table.DataModel.Tables.First<TableProvider> ((TableProvider tp) => {
65                                         if (tp.Name == tableName)
66                                                 return true;
67                                         return false;
68                                 });
69                         } catch {
70                                 return;
71                         }
72
73                         if (tableProvider == null)
74                                 return;
75
76                         ColumnProvider toColumn = null;
77
78                         try {
79                                 toColumn = tableProvider.Columns.First<ColumnProvider> ((ColumnProvider cp) => {
80                                         if (cp.Name == columnName)
81                                                 return true;
82                                         return false;
83                                 });
84                         } catch {
85                                 return;
86                         }
87
88                         if (toColumn == null)
89                                 return;
90
91                         IsForeignKeyComponent = true;
92                         Association = new DynamicDataAssociationProvider (column.AssociationDirection, this, toColumn);
93                 }
94         }
95 }