1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
using System.Security.Permissions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web.DynamicData.ModelProviders;
using System.Collections;
using System.Web.Routing;
namespace System.Web.DynamicData {
/// <summary>
/// A special column representing 1-many relationships
/// </summary>
public class MetaChildrenColumn : MetaColumn, IMetaChildrenColumn {
public MetaChildrenColumn(MetaTable table, ColumnProvider entityMember)
: base(table, entityMember) {
}
/// <summary>
/// Perform initialization logic for this column
/// </summary>
internal protected override void Initialize() {
base.Initialize();
AssociationProvider a = this.Provider.Association;
ChildTable = Model.GetTable(a.ToTable.Name, Table.DataContextType);
if (a.ToColumn != null) {
ColumnInOtherTable = ChildTable.GetColumn(a.ToColumn.Name);
}
}
/// <summary>
/// Returns whether this entity set column is in a Many To Many relationship
/// </summary>
public bool IsManyToMany {
get {
return Provider.Association != null &&
Provider.Association.Direction == AssociationDirection.ManyToMany;
}
}
/// <summary>
/// The child table (e.g. Products in Categories<-Products)
/// </summary>
public MetaTable ChildTable { get; private set; }
/// <summary>
/// A pointer to the MetaColumn in the other table
/// </summary>
public MetaColumn ColumnInOtherTable { get; private set; }
/// <summary>
/// Override disabling sorting
/// </summary>
internal override string SortExpressionInternal {
get {
// children columns are not sortable
return String.Empty;
}
}
/*protected*/ internal override bool ScaffoldNoCache {
get {
// always display 1-many associations
return true;
}
}
/// <summary>
/// Shortcut for getting the path to the list action for all entities in the child table that have the given row as a parent.
/// </summary>
/// <param name="row"></param>
/// <returns></returns>
public string GetChildrenListPath(object row) {
return GetChildrenPath(PageAction.List, row);
}
public string GetChildrenPath(string action, object row) {
// If there is no row, we can't get a path
if (row == null)
return String.Empty;
return ChildTable.GetActionPath(action, GetRouteValues(row));
}
public string GetChildrenPath(string action, object row, string path) {
// If there is no row, we can't get a path
if (row == null)
return String.Empty;
if (String.IsNullOrEmpty(path)) {
return GetChildrenPath(action, row);
}
// Build a query string param with our primary key
RouteValueDictionary routeValues = GetRouteValues(row);
// Add it to the path
return QueryStringHandler.AddFiltersToPath(path, routeValues);
}
private RouteValueDictionary GetRouteValues(object row) {
var routeValues = new RouteValueDictionary();
IList<object> pkValues = Table.GetPrimaryKeyValues(row);
var fkColumn = ColumnInOtherTable as MetaForeignKeyColumn;
if (fkColumn != null) {
Debug.Assert(fkColumn.ForeignKeyNames.Count == pkValues.Count);
for (int i = 0; i < fkColumn.ForeignKeyNames.Count; i++) {
routeValues.Add(fkColumn.ForeignKeyNames[i], Misc.SanitizeQueryStringValue(pkValues[i]));
}
}
return routeValues;
}
IMetaTable IMetaChildrenColumn.ChildTable {
get { return ChildTable; }
}
IMetaColumn IMetaChildrenColumn.ColumnInOtherTable {
get { return ColumnInOtherTable; }
}
}
}
|