File: RowToParametersTransformer.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (356 lines) | stat: -rw-r--r-- 16,135 bytes parent folder | download | duplicates (7)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//------------------------------------------------------------------------------
// <copyright file="RowToParametersTransformer.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI.WebControls.WebParts {

    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Web.UI.WebControls;

    [WebPartTransformer(typeof(IWebPartRow), typeof(IWebPartParameters))]
    public sealed class RowToParametersTransformer : WebPartTransformer, IWebPartParameters {

        private IWebPartRow _provider;
        private string[] _consumerFieldNames;
        private string[] _providerFieldNames;
        private PropertyDescriptorCollection _consumerSchema;
        private ParametersCallback _callback;

        public override Control CreateConfigurationControl() {
            return new RowToParametersConfigurationWizard(this);
        }

        [
        TypeConverterAttribute(typeof(StringArrayConverter)),
        ]
        public string[] ConsumerFieldNames {
            get {
                return (_consumerFieldNames != null) ? (string[])_consumerFieldNames.Clone() : new string[0];
            }
            set {
                _consumerFieldNames = (value != null) ? (string[])value.Clone() : null;
            }
        }

        private PropertyDescriptorCollection ConsumerSchema {
            get {
                return _consumerSchema;
            }
        }

        [
        TypeConverterAttribute(typeof(StringArrayConverter)),
        ]
        public string[] ProviderFieldNames {
            get {
                return (_providerFieldNames != null) ? (string[])_providerFieldNames.Clone() : new string[0];
            }
            set {
                _providerFieldNames = (value != null) ? (string[])value.Clone() : null;
            }
        }

        private PropertyDescriptorCollection ProviderSchema {
            get {
                return (_provider != null) ? _provider.Schema : null;
            }
        }

        // Schema containing property descriptors associated with each name in ProviderFieldNames
        private PropertyDescriptorCollection SelectedProviderSchema {
            get {
                PropertyDescriptorCollection props = new PropertyDescriptorCollection(null);

                PropertyDescriptorCollection providerSchema = ProviderSchema;
                if (providerSchema != null && _providerFieldNames != null && _providerFieldNames.Length > 0) {
                    foreach (string fieldName in _providerFieldNames) {
                        PropertyDescriptor prop = providerSchema.Find(fieldName, /* ignoreCase */ true);
                        if (prop == null) {
                            // If any provider field name is not in the schema, return an empty schema
                            return new PropertyDescriptorCollection(null);
                        }
                        else {
                            props.Add(prop);
                        }
                    }
                }

                return props;
            }
        }

        // Throws an exception if the ConsumerFieldNames and ProviderFieldNames have different length
        private void CheckFieldNamesLength() {
            int consumerFieldNamesLength = (_consumerFieldNames != null) ? _consumerFieldNames.Length : 0;
            int providerFieldNamesLength = (_providerFieldNames != null) ? _providerFieldNames.Length : 0;

            if (consumerFieldNamesLength != providerFieldNamesLength) {
                throw new InvalidOperationException(SR.GetString(SR.RowToParametersTransformer_DifferentFieldNamesLength));
            }
        }

        private void GetRowData(object rowData) {
            Debug.Assert(_callback != null);

            // Only return null if rowData is null.  Else, return an empty dictionary. (VSWhidbey 381264)
            IDictionary parametersData = null;

            // For perf, check in decreasing order of likeliness that an object is null
            if (rowData != null) {
                PropertyDescriptorCollection consumerSchema = ((IWebPartParameters)this).Schema;
                parametersData = new HybridDictionary(consumerSchema.Count);
                if (consumerSchema.Count > 0) {
                    PropertyDescriptorCollection providerSchema = SelectedProviderSchema;
                    if (providerSchema != null && providerSchema.Count > 0) {
                        if (providerSchema.Count == consumerSchema.Count) {
                            for (int i=0; i < providerSchema.Count; i++) {
                                PropertyDescriptor providerProp = providerSchema[i];
                                PropertyDescriptor consumerProp = consumerSchema[i];
                                parametersData[consumerProp.Name] = providerProp.GetValue(rowData);
                            }
                        }
                    }
                }
            }

            _callback(parametersData);
        }

        protected internal override void LoadConfigurationState(object savedState) {
            if (savedState != null) {
                string[] fieldNames = (string[])savedState;
                int fieldNamesLength = fieldNames.Length;

                if (fieldNamesLength % 2 != 0) {
                    throw new InvalidOperationException(SR.GetString(SR.RowToParametersTransformer_DifferentFieldNamesLength));
                }

                int length = fieldNamesLength / 2;
                _consumerFieldNames = new string[length];
                _providerFieldNames = new string[length];
                for (int i=0; i < length; i++) {
                    _consumerFieldNames[i] = fieldNames[2*i];
                    _providerFieldNames[i] = fieldNames[2*i + 1];
                }
            }
        }

        protected internal override object SaveConfigurationState() {
            CheckFieldNamesLength();

            int consumerFieldNamesLength = (_consumerFieldNames != null) ? _consumerFieldNames.Length : 0;
            if (consumerFieldNamesLength > 0) {
                string[] fieldNames = new string[consumerFieldNamesLength * 2];
                for (int i=0; i < consumerFieldNamesLength; i++) {
                    fieldNames[2*i] = _consumerFieldNames[i];
                    fieldNames[2*i + 1] = _providerFieldNames[i];
                }
                return fieldNames;
            }

            return null;
        }

        public override object Transform(object providerData) {
            _provider = (IWebPartRow)providerData;
            return this;
        }

        #region Implementation of IWebPartParameters
        void IWebPartParameters.GetParametersData(ParametersCallback callback) {
            if (callback == null) {
                throw new ArgumentNullException("callback");
            }

            CheckFieldNamesLength();

            if (_provider != null) {
                _callback = callback;
                _provider.GetRowData(new RowCallback(GetRowData));
            }
            else {
                callback(null);
            }
        }

        PropertyDescriptorCollection IWebPartParameters.Schema {
            get {
                CheckFieldNamesLength();

                PropertyDescriptorCollection props = new PropertyDescriptorCollection(null);

                if (_consumerSchema != null && _consumerFieldNames != null && _consumerFieldNames.Length > 0) {
                    foreach (string fieldName in _consumerFieldNames) {
                        PropertyDescriptor prop = _consumerSchema.Find(fieldName, true);
                        if (prop == null) {
                            // If any consumer field name is not in the schema, return an empty schema
                            return new PropertyDescriptorCollection(null);
                        }
                        else {
                            props.Add(prop);
                        }
                    }
                }

                return props;
            }
        }

        void IWebPartParameters.SetConsumerSchema(PropertyDescriptorCollection schema) {
            _consumerSchema = schema;
        }
        #endregion

        private sealed class RowToParametersConfigurationWizard : TransformerConfigurationWizardBase {
            private DropDownList[] _consumerFieldNames;
            private RowToParametersTransformer _owner;

            private const string consumerFieldNameID = "ConsumerFieldName";

            public RowToParametersConfigurationWizard(RowToParametersTransformer owner) {
                Debug.Assert(owner != null);
                _owner = owner;
            }

            protected override PropertyDescriptorCollection ConsumerSchema {
                get {
                    return _owner.ConsumerSchema;
                }
            }

            protected override PropertyDescriptorCollection ProviderSchema {
                get {
                    return _owner.ProviderSchema;
                }
            }

            protected override void CreateWizardSteps() {
                // The WizardSteps should be empty when this is called
                Debug.Assert(WizardSteps.Count == 0);

                int oldProviderNamesLength = (OldProviderNames != null) ? OldProviderNames.Length : 0;
                if (oldProviderNamesLength > 0) {
                    _consumerFieldNames = new DropDownList[oldProviderNamesLength / 2];

                    ListItem[] consumerItems = null;
                    int oldConsumerNamesLength = (OldConsumerNames != null) ? OldConsumerNames.Length : 0;
                    if (oldConsumerNamesLength > 0) {
                        consumerItems = new ListItem[oldConsumerNamesLength / 2];
                        for (int i=0; i < oldConsumerNamesLength / 2; i++) {
                            consumerItems[i] = new ListItem(OldConsumerNames[2*i], OldConsumerNames[2*i + 1]);
                        }
                    }

                    for (int i=0; i < oldProviderNamesLength / 2; i++) {
                        WizardStep s = new WizardStep();

                        s.Controls.Add(new LiteralControl(
                            SR.GetString(SR.RowToParametersTransformer_ProviderFieldName) + " "));
                        Label label = new Label();

                        // HtmlEncode the string, since it comes from the provider schema and it may contain
                        // unsafe characters.
                        label.Text = HttpUtility.HtmlEncode(OldProviderNames[2*i]);

                        label.Font.Bold = true;
                        s.Controls.Add(label);

                        s.Controls.Add(new LiteralControl("<br />"));

                        DropDownList consumerFieldName = new DropDownList();
                        consumerFieldName.ID = consumerFieldNameID + i;
                        if (consumerItems != null) {
                            consumerFieldName.Items.Add(new ListItem());

                            // Calculate consumerFieldValue based on the current providerFieldValue,
                            // and the ProviderFieldNames and ConsumerFieldNames on the Transformer
                            string[] providerFieldNames = _owner._providerFieldNames;
                            string[] consumerFieldNames = _owner._consumerFieldNames;
                            string providerFieldValue = OldProviderNames[2*i + 1];
                            string consumerFieldValue = null;
                            if (providerFieldNames != null) {
                                for (int j=0; j < providerFieldNames.Length; j++) {
                                    // Ignore case when getting the value, since we ignore case when
                                    // returing the connection data. (VSWhidbey 434566)
                                    if (String.Equals(providerFieldNames[j], providerFieldValue,
                                                      StringComparison.OrdinalIgnoreCase) &&
                                        consumerFieldNames != null && consumerFieldNames.Length > j) {
                                        consumerFieldValue = consumerFieldNames[j];
                                        break;
                                    }
                                }
                            }

                            foreach (ListItem consumerItem in consumerItems) {
                                ListItem item = new ListItem(consumerItem.Text, consumerItem.Value);
                                // Ignore case when setting selected value, since we ignore case when
                                // returing the connection data. (VSWhidbey 434566)
                                if (String.Equals(item.Value, consumerFieldValue, StringComparison.OrdinalIgnoreCase)) {
                                    item.Selected = true;
                                }
                                consumerFieldName.Items.Add(item);
                            }
                        }
                        else {
                            consumerFieldName.Items.Add(new ListItem(
                                SR.GetString(SR.RowToParametersTransformer_NoConsumerSchema)));
                            consumerFieldName.Enabled = false;
                        }
                        _consumerFieldNames[i] = consumerFieldName;

                        Label consumerFieldNameLabel = new Label();
                        consumerFieldNameLabel.Text = SR.GetString(SR.RowToParametersTransformer_ConsumerFieldName);
                        consumerFieldNameLabel.AssociatedControlID = consumerFieldName.ID;

                        s.Controls.Add(consumerFieldNameLabel);
                        s.Controls.Add(new LiteralControl(" "));
                        s.Controls.Add(consumerFieldName);

                        WizardSteps.Add(s);
                    }
                }
                else {
                    WizardStep s = new WizardStep();
                    s.Controls.Add(new LiteralControl(SR.GetString(SR.RowToParametersTransformer_NoProviderSchema)));
                    WizardSteps.Add(s);
                }

                // We should always have at least 1 WizardStep when we return
                Debug.Assert(WizardSteps.Count > 0);
            }

            protected override void OnFinishButtonClick(WizardNavigationEventArgs e) {
                ArrayList providerFieldNames = new ArrayList();
                ArrayList consumerFieldNames = new ArrayList();

                int oldProviderNamesLength = (OldProviderNames != null) ? OldProviderNames.Length : 0;
                if (oldProviderNamesLength > 0) {
                    Debug.Assert(_consumerFieldNames != null);
                    Debug.Assert(oldProviderNamesLength == 2 * _consumerFieldNames.Length);
                    for (int i=0; i < _consumerFieldNames.Length; i++) {
                        DropDownList consumerFieldName = _consumerFieldNames[i];
                        if (consumerFieldName.Enabled) {
                            string selectedConsumerFieldName = consumerFieldName.SelectedValue;
                            if (!String.IsNullOrEmpty(selectedConsumerFieldName)) {
                                providerFieldNames.Add(OldProviderNames[2*i + 1]);
                                consumerFieldNames.Add(selectedConsumerFieldName);
                            }
                        }
                    }
                }

                _owner.ConsumerFieldNames = (string[])consumerFieldNames.ToArray(typeof(string));
                _owner.ProviderFieldNames = (string[])providerFieldNames.ToArray(typeof(string));

                base.OnFinishButtonClick(e);
            }
        }
    }
}