File: DynamicHyperLink.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (282 lines) | stat: -rw-r--r-- 11,105 bytes parent folder | download | duplicates (9)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Globalization;
using System.Security.Permissions;
using System.Web.Compilation;
using System.Web.Resources;
using System.Web.Routing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.DynamicData.Util;

namespace System.Web.DynamicData {
    /// <summary>
    /// <para>A control that displays links to table actions based on routing rules. It will not generate links for actions that are not
    /// allowed by the routing rules. It can work in 3 modes: explicit, databinding to MetaTable, or databinding to a data row.</para>
    /// <para>Databinding to MetaTable allows for creating links to actions for a collection of MetaTable objects (such as in the Default.aspx
    /// page in the project templates)</para>
    /// <para>Databinding to a data row allows for creating links to actions for data rows retrieved from a database. These are usually used with
    /// Edit and Details actions.</para>
    /// <para>Explicit mode allows for links to non-item-specific actions (like List and Insert) and is achieved by properly setting
    /// ContextTypeName, Table, and Action properties. This is done in the PreRender phase if the NavigateUrl property is null (i.e. it has not
    /// been set explicitly or did not get set in one of the databinding scenarios.)</para>
    /// <para>Extra route parameters can be provided by declaring expando attributes on the controls markup.</para>
    /// </summary>
    [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "HyperLink", Justification="It's an extension of the HyperLink class")]
    [DefaultProperty("Action")]
    [ToolboxBitmap(typeof(DynamicHyperLink), "DynamicHyperLink.bmp")]
    public class DynamicHyperLink : HyperLink, IAttributeAccessor {
        private HttpContextBase _context;
        private bool _dataBound;
        private object _dataItem;
        private Dictionary<string, string> _extraRouteParams = new Dictionary<string, string>();

        /// <summary>
        /// The name of the action
        /// </summary>
        [TypeConverter(typeof(ActionConverter))]
        [DefaultValue("")]
        [Category("Navigation")]
        [ResourceDescription("DynamicHyperLink_Action")]
        public string Action {
            get {
                object o = ViewState["Action"];
                return (o == null ? String.Empty: (string)o);
            }
            set {
                ViewState["Action"] = value;
            }
        }

        internal new HttpContextBase Context {
            get {
                return _context ?? new HttpContextWrapper(base.Context);
            }
            set {
                _context = value;
            }
        }

        /// <summary>
        /// The name of the context type
        /// </summary>
        [DefaultValue("")]
        [Category("Navigation")]
        [ResourceDescription("DynamicHyperLink_ContextTypeName")]
        public string ContextTypeName {
            get {
                object o = ViewState["ContextTypeName"];
                return ((o == null) ? String.Empty : (string)o);
            }
            set {
                ViewState["ContextTypeName"] = value;
            }
        }

        /// <summary>
        /// The name of the column whose value will be used to populate the Text
        /// property if it is not already set in data binding scenarios.
        /// </summary>
        [DefaultValue("")]
        [Category("Navigation")]
        [ResourceDescription("DynamicHyperLink_DataField")]
        public string DataField {
            get {
                object o = ViewState["DataField"];
                return ((o == null) ? String.Empty : (string)o);
            }
            set {
                ViewState["DataField"] = value;
            }
        }

        // for unit testing purposes
        internal object Page_DataItem {
            get {
                return _dataItem ?? Page.GetDataItem();
            }
            set {
                _dataItem = value;
            }
        }

        /// <summary>
        /// The name of the table
        /// </summary>
        [DefaultValue("")]
        [Category("Navigation")]
        [ResourceDescription("DynamicHyperLink_TableName")]
        public string TableName {
            get {
                object o = ViewState["TableName"];
                return ((o == null) ? String.Empty : (string)o);
            }
            set {
                ViewState["TableName"] = value;
            }
        }

        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers", MessageId = "0#")]
        protected override void OnDataBinding(EventArgs e) {
            base.OnDataBinding(e);

            if (DesignMode) {
                return;
            }

            if (!String.IsNullOrEmpty(NavigateUrl)) {
                // stop processing if there already is a URL
                return;
            }

            if (!String.IsNullOrEmpty(TableName) || !String.IsNullOrEmpty(ContextTypeName)) {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                    DynamicDataResources.DynamicHyperLink_CannotSetTableAndContextWhenDatabinding, this.ID));
            }

            object dataItem = Page_DataItem;
            if (dataItem == null) {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                    DynamicDataResources.DynamicHyperLink_CannotBindToNull, this.ID));
            }

            MetaTable table = dataItem as MetaTable;
            if (table != null) {
                BindToMetaTable(table);
            } else {
                BindToDataItem(dataItem);
            }

            _dataBound = true;
        }

        private void BindToMetaTable(MetaTable table) {
            string action = GetActionOrDefaultTo(PageAction.List);
            NavigateUrl = table.GetActionPath(action, GetRouteValues());
            if (String.IsNullOrEmpty(Text)) {
                Text = table.DisplayName;
            }
        }

        private void BindToDataItem(object dataItem) {
            dataItem = Misc.GetRealDataItem(dataItem);
            Debug.Assert(dataItem != null, "DataItem is null");
            // Try to get the MetaTable from the type and if we can't find it then ---- up.
            MetaTable table = Misc.GetTableFromTypeHierarchy(dataItem.GetType());
            if (table == null) {
                throw new InvalidOperationException(String.Format(
                    CultureInfo.CurrentCulture,
                    DynamicDataResources.MetaModel_EntityTypeDoesNotBelongToModel,
                    dataItem.GetType().FullName));
            }

            string action = GetActionOrDefaultTo(PageAction.Details);
            NavigateUrl = table.GetActionPath(action, GetRouteValues(table, dataItem));

            if (String.IsNullOrEmpty(Text)) {
                if (!String.IsNullOrEmpty(DataField)) {
                    Text = DataBinder.GetPropertyValue(dataItem, DataField).ToString();
                } else {
                    Text = table.GetDisplayString(dataItem);
                }
            }
        }

        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers", MessageId = "0#")]
        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);

            if (DesignMode) {
                if (!String.IsNullOrEmpty(NavigateUrl)) {
                    NavigateUrl = "DesignTimeUrl";
                }
                return;
            }

            // check both _dataBound and NavigateUrl cause NavigateUrl might be empty if routing/scaffolding
            // does not allow a particular action
            if (!_dataBound && String.IsNullOrEmpty(NavigateUrl)) {
                MetaTable table;
                try {
                    table = GetTable();
                } catch (Exception exception) {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, DynamicDataResources.DynamicHyperLink_CannotDetermineTable, this.ID), exception);
                }

                if (table == null) {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, DynamicDataResources.DynamicHyperLink_CannotDetermineTable, this.ID));
                }

                var action = GetActionOrDefaultTo(PageAction.List);
                NavigateUrl = table.GetActionPath(action, GetRouteValues());
            }
        }

        private RouteValueDictionary GetRouteValues() {
            var routeValues = new RouteValueDictionary();
            foreach (var entry in _extraRouteParams) {
                string key = entry.Key;
                routeValues[key] = entry.Value;
            }
            return routeValues;
        }

        private RouteValueDictionary GetRouteValues(MetaTable table, object row) {
            RouteValueDictionary routeValues = GetRouteValues();
            foreach (var pk in table.GetPrimaryKeyDictionary(row)) {
                routeValues[pk.Key] = pk.Value;
            }
            return routeValues;
        }

        private string GetActionOrDefaultTo(string defaultAction) {
            return String.IsNullOrEmpty(Action) ? defaultAction : Action;
        }

        // internal for unit testing
        internal virtual MetaTable GetTable() {
            MetaTable table;
            if (!String.IsNullOrEmpty(TableName)) {
                table = GetTableFromTableName();
            } else {
                table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
            }
            return table;
        }

        private MetaTable GetTableFromTableName() {
            var tableName = TableName;
            var contextTypeName = ContextTypeName;
            Debug.Assert(!String.IsNullOrEmpty(tableName));

            if (!String.IsNullOrEmpty(contextTypeName)) {
                // context type allows to disambiguate table names
                Type contextType = BuildManager.GetType(contextTypeName, /* throwOnError */ true, /* ignoreCase */ true);
                MetaModel model = MetaModel.GetModel(contextType);
                MetaTable table = model.GetTable(tableName, contextType);
                return table;
            } else {
                var table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
                if (table == null) {
                    return null;
                }
                return table.Model.GetTable(tableName);
            }
        }

        #region IAttributeAccessor Members

        string IAttributeAccessor.GetAttribute(string key) {
            return (string)_extraRouteParams[key];
        }

        void IAttributeAccessor.SetAttribute(string key, string value) {
            _extraRouteParams[key] = value;
        }

        #endregion
    }
}