summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/src/System.Web.Helpers/WebGrid/WebGridRow.cs
blob: 8061dae898e1adabcf5e472bcfa12e5ead1a5f58 (plain)
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
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Web.Helpers.Resources;
using Microsoft.Internal.Web.Utils;

namespace System.Web.Helpers
{
    [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "Collection is not an appropriate suffix for this class")]
    public class WebGridRow : DynamicObject, IEnumerable<object>
    {
        private const string RowIndexMemberName = "ROW";
        private const BindingFlags BindFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.IgnoreCase;

        private WebGrid _grid;
        private IDynamicMetaObjectProvider _dynamic;
        private int _rowIndex;
        private object _value;
        private IEnumerable<dynamic> _values;

        public WebGridRow(WebGrid webGrid, object value, int rowIndex)
        {
            _grid = webGrid;
            _value = value;
            _rowIndex = rowIndex;
            _dynamic = value as IDynamicMetaObjectProvider;
        }

        public dynamic Value
        {
            get { return _value; }
        }

        public WebGrid WebGrid
        {
            get { return _grid; }
        }

        public object this[string name]
        {
            get
            {
                if (String.IsNullOrEmpty(name))
                {
                    throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "name");
                }
                object value = null;
                if (!TryGetMember(name, out value))
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                      HelpersResources.WebGrid_ColumnNotFound, name));
                }
                return value;
            }
        }

        public object this[int index]
        {
            get
            {
                if ((index < 0) || (index >= _grid.ColumnNames.Count()))
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                return this.Skip(index).First();
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public IEnumerator<object> GetEnumerator()
        {
            if (_values == null)
            {
                _values = _grid.ColumnNames.Select(c => WebGrid.GetMember(this, c));
            }
            return _values.GetEnumerator();
        }

        public IHtmlString GetSelectLink(string text = null)
        {
            if (String.IsNullOrEmpty(text))
            {
                text = HelpersResources.WebGrid_SelectLinkText;
            }
            return WebGridRenderer.GridLink(_grid, GetSelectUrl(), text);
        }

        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "review: I think a method is more appropriate here")]
        [SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings", Justification = "Strings are easier for Plan9 developer to work with")]
        public string GetSelectUrl()
        {
            NameValueCollection queryString = new NameValueCollection(1);
            queryString[WebGrid.SelectionFieldName] = (_rowIndex + 1L).ToString(CultureInfo.CurrentCulture);
            return WebGrid.GetPath(queryString);
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;
            // Try to get the row index
            if (TryGetRowIndex(binder.Name, out result))
            {
                return true;
            }

            // Try to evaluate the dynamic member based on the binder
            if (_dynamic != null && DynamicHelper.TryGetMemberValue(_dynamic, binder, out result))
            {
                return true;
            }

            return TryGetComplexMember(_value, binder.Name, out result);
        }

        internal bool TryGetMember(string memberName, out object result)
        {
            result = null;

            // Try to get the row index
            if (TryGetRowIndex(memberName, out result))
            {
                return true;
            }

            // Try to evaluate the dynamic member based on the name
            if (_dynamic != null && DynamicHelper.TryGetMemberValue(_dynamic, memberName, out result))
            {
                return true;
            }

            // Support '.' for navigation properties
            return TryGetComplexMember(_value, memberName, out result);
        }

        public override string ToString()
        {
            return _value.ToString();
        }

        private bool TryGetRowIndex(string memberName, out object result)
        {
            result = null;
            if (String.IsNullOrEmpty(memberName))
            {
                return false;
            }

            if (memberName == RowIndexMemberName)
            {
                result = _rowIndex;
                return true;
            }

            return false;
        }

        private static bool TryGetComplexMember(object obj, string name, out object result)
        {
            result = null;

            string[] names = name.Split('.');
            for (int i = 0; i < names.Length; i++)
            {
                if ((obj == null) || !TryGetMember(obj, names[i], out result))
                {
                    result = null;
                    return false;
                }
                obj = result;
            }
            return true;
        }

        private static bool TryGetMember(object obj, string name, out object result)
        {
            PropertyInfo property = obj.GetType().GetProperty(name, BindFlags);
            if ((property != null) && (property.GetIndexParameters().Length == 0))
            {
                result = property.GetValue(obj, null);
                return true;
            }
            result = null;
            return false;
        }
    }
}