-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAttributes.cs
528 lines (492 loc) · 20.9 KB
/
Attributes.cs
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DVM4T.Contracts;
using System.Reflection;
using System.Web;
using DVM4T.Reflection;
using DVM4T.Exceptions;
using System.Collections;
using DVM4T.Core;
namespace DVM4T.Attributes
{
/// <summary>
/// A Base class for an Attribute identifying a Property that represents some part of a View Model
/// </summary>
public abstract class ModelPropertyAttributeBase : Attribute, IPropertyAttribute
{
public abstract IEnumerable GetPropertyValues(IViewModelData modelData, IModelProperty property, IViewModelFactory factory);
/// <summary>
/// When overriden in a derived class, this property returns the expected return type of the View Model property.
/// </summary>
/// <remarks>Primarily used for debugging purposes. This property is used to throw an accurate exception at run time if
/// the property return type does not match with the expected type.</remarks>
public abstract Type ExpectedReturnType { get; }
public Core.Binding.IModelMapping ComplexTypeMapping
{
get;
set;
}
}
/// <summary>
/// A Base class for an Attribute identifying a Property that represents a Field
/// </summary>
/// <remarks>
/// The field can be content, metadata, or the metadata of a template
/// </remarks>
public abstract class FieldAttributeBase : ModelPropertyAttributeBase, IFieldAttribute
{
protected bool allowMultipleValues = false;
protected bool inlineEditable = false;
protected bool mandatory = false; //probably don't need this one
protected bool isMetadata = false;
/// <summary>
/// Creates a new Field Attribute
/// </summary>
public FieldAttributeBase()
{ }
public override IEnumerable GetPropertyValues(IViewModelData modelData, IModelProperty property, IViewModelFactory factory)
{
IEnumerable result = null;
if (modelData != null)
{
//need null checks on Template
IFieldsData fields = null;
if (IsTemplateMetadata && modelData is ITemplatedViewModelData)
{
var templateData = modelData as ITemplatedViewModelData;
fields = templateData.Template != null ? templateData.Template.Metadata : null;
}
else if (IsMetadata)
{
fields = modelData.Metadata;
}
else if (modelData is IContentPresentationData)
{
fields = (modelData as IContentPresentationData).Content;
}
else
{
fields = modelData.Metadata;
}
if (String.IsNullOrEmpty(FieldName)) FieldName = GetFieldName(property.Name); //Convention over configuration by default -- Field name = Property name
if (fields != null && fields.ContainsKey(FieldName))
{
var template = modelData is ITemplatedViewModelData ? (modelData as ITemplatedViewModelData).Template
: null;
result = this.GetFieldValues(fields[FieldName], property, template, factory);
}
}
return result;
}
private string GetFieldName(string propertyName)
{
return propertyName.Substring(0, 1).ToLowerInvariant() + propertyName.Substring(1); //lowercase the first letter
}
/// <summary>
/// When overriden in a derived class, this method should return the value of the View Model property from a Field object
/// </summary>
/// <param name="field">The Field</param>
/// <param name="propertyType">The concrete type of the view model property for this attribute</param>
/// <param name="template">The Component Template to use</param>
/// <param name="factory">The View Model Builder</param>
/// <returns></returns>
public abstract IEnumerable GetFieldValues(IFieldData field, IModelProperty property, ITemplateData template,IViewModelFactory factory = null);
/// <summary>
/// The Tridion schema field name for this property. If not used, the property name with pascal casing is used
/// e.g. public string SubTitle {get;set;} will use field name "subTitle" if this value is not specified.
/// </summary>
public string FieldName
{
get;
set;
}
/// <summary>
/// Semantic only - Is a multi value field. Actualy determination of multi versus single value is done by
/// inspection the Type of the property.
/// </summary>
public bool AllowMultipleValues
{
get
{
return allowMultipleValues;
}
set { allowMultipleValues = value; }
}
/// <summary>
/// Semantic only - Is inline editable.
/// </summary>
public bool InlineEditable
{
get
{
return inlineEditable;
}
set
{
inlineEditable = value;
}
}
/// <summary>
/// Is a mandatory field. For semantic use only.
/// </summary>
public bool Mandatory
{
get
{
return mandatory;
}
set
{
mandatory = value;
}
}
/// <summary>
/// Is a metadata field. False indicates this is a content field.
/// </summary>
public bool IsMetadata
{
get { return isMetadata; }
set { isMetadata = value; }
}
/// <summary>
/// Is a template metadata field (if template exists in the context of the property).
/// </summary>
public bool IsTemplateMetadata { get; set; }
}
/// <summary>
/// A Base class for an Attribute identifying a Property that represents some part of a Component
/// </summary>
public abstract class ComponentAttributeBase : ModelPropertyAttributeBase, IComponentAttribute
{
public override IEnumerable GetPropertyValues(IViewModelData modelData, IModelProperty property, IViewModelFactory factory)
{
IEnumerable result = null;
if (modelData != null)
{
if (modelData is IComponentPresentationData)
{
var cpData = modelData as IComponentPresentationData;
if (cpData != null)
{
result = GetPropertyValues(cpData.Component, property,
cpData.Template, factory);
}
}
else if (modelData is IComponentData) //Not all components come with Templates
{
result = GetPropertyValues(modelData as IComponentData, property, null, factory);
}
}
return result;
}
/// <summary>
/// When overriden in a derived class, this gets the value of the Property for a given Component
/// </summary>
/// <param name="component">Component for the View Model</param>
/// <param name="propertyType">Actual return type for the Property</param>
/// <param name="template">Component Template</param>
/// <param name="factory">View Model factory</param>
/// <returns>The Property value</returns>
public abstract IEnumerable GetPropertyValues(IComponentData component, IModelProperty property, ITemplateData template, IViewModelFactory factory);
}
/// <summary>
/// A Base class for an Attribute identifying a Property that represents some part of a Component Template
/// </summary>
public abstract class ComponentTemplateAttributeBase : ModelPropertyAttributeBase, ITemplateAttribute
{
/// <summary>
/// When overriden in a derived class, this gets the value of the Property for a given Component Template
/// </summary>
/// <param name="template">Component Template for the View Model</param>
/// <param name="propertyType">Actual return type for the Property</param>
/// <param name="factory">View Model factory</param>
/// <returns>The Property value</returns>
public abstract object GetPropertyValues(ITemplateData template, Type propertyType, IViewModelFactory factory);
public override IEnumerable GetPropertyValues(IViewModelData modelData, IModelProperty property, IViewModelFactory factory)
{
IEnumerable result = null;
if (modelData is IContentPresentationData
&& (modelData as IContentPresentationData).Template is ITemplateData)
{
var templateData = (modelData as IContentPresentationData).Template as ITemplateData;
result = this.GetPropertyValues(templateData, property, factory);
}
return result;
}
}
/// <summary>
/// A Base class for an Attribute identifying a Property that represents some part of a Page
/// </summary>
public abstract class PageAttributeBase : ModelPropertyAttributeBase, IPageAttribute
{
/// <summary>
/// When overriden in a derived class, this gets the value of the Property for a given Page
/// </summary>
/// <param name="page">Page for the View Model</param>
/// <param name="propertyType">Actual return type for the Property</param>
/// <param name="factory">View Model factory</param>
/// <returns>The Property value</returns>
public abstract object GetPropertyValues(IPageData page, Type propertyType, IViewModelFactory factory);
public override IEnumerable GetPropertyValues(IViewModelData modelData, IModelProperty property, IViewModelFactory factory)
{
IEnumerable result = null;
if (modelData is IPageData)
{
var pageModel = (modelData as IPageData);
result = this.GetPropertyValues(pageModel, property, factory);
}
return result;
}
}
/// <summary>
/// A Base class for an Attribute identifying a Property that represents a set of Component Presentations
/// </summary>
/// <remarks>The View Model must be a Page</remarks>
public abstract class ComponentPresentationsAttributeBase : ModelPropertyAttributeBase //For use in a PageModel
{
//Really leaving the bulk of the work to implementer -- they must both find out if the CP matches this attribute and then construct an object with it
/// <summary>
/// When overriden in a derived class, this gets a set of values representing Component Presentations of a Page
/// </summary>
/// <param name="cps">Component Presentations for the Page Model</param>
/// <param name="propertyType">Actual return type of the Property</param>
/// <param name="factory">A View Model factory</param>
/// <returns>The Property value</returns>
public abstract IEnumerable GetPresentationValues(IList<IComponentPresentationData> cps, IModelProperty property, IViewModelFactory factory);
public override IEnumerable GetPropertyValues(IViewModelData modelData, IModelProperty property, IViewModelFactory factory)
{
IEnumerable result = null;
if (modelData is IPageData)
{
var cpModels = (modelData as IPageData).ComponentPresentations;
result = GetPresentationValues(cpModels, property, factory);
}
return result;
}
}
/// <summary>
/// An Attribute for identifying a Content View Model
/// </summary>
public class ViewModelAttribute : Attribute, IDefinedModelAttribute //Should be re-named ContentViewModelAttribute
{
//TODO: De-couple this from the Schema name specifically? What would make sense?
//TOOD: Possibly change this to use purely ViewModelKey and make that an object, leave it to the key provider to assign objects with logical equals overrides
private string schemaName;
private bool inlineEditable = false;
private bool isDefault = false;
private string[] viewModelKeys;
/// <summary>
/// View Model
/// </summary>
/// <param name="schemaName">Tridion schema name for component type for this View Model</param>
/// <param name="isDefault">Is this the default View Model for this schema. If true, Components
/// with this schema will use this class if no other View Models' Keys match.</param>
public ViewModelAttribute(string schemaName, bool isDefault)
{
this.schemaName = schemaName;
this.isDefault = isDefault;
}
//Using Schema Name ties each View Model to a single Tridion Schema -- this is probably ok in 99% of cases
//Using schema name doesn't allow us to de-couple the Model itself from Tridion however (neither does requiring
//inheritance of IViewModel!)
//Possible failure: if the same model was meant to represent similar parts of multiple schemas (should however
//be covered by decent Schema design i.e. use of Embedded Schemas and Linked Components. Same fields shouldn't
//occur repeatedly)
public string SchemaName
{
get
{
return schemaName;
}
}
/// <summary>
/// Identifiers for further specifying which View Model to use for different presentations.
/// </summary>
public string[] ViewModelKeys
{
get { return viewModelKeys; }
set { viewModelKeys = value; }
}
/// <summary>
/// Is inline editable. Only for semantic use.
/// </summary>
public bool InlineEditable
{
get
{
return inlineEditable;
}
set
{
inlineEditable = value;
}
}
/// <summary>
/// Is the default View Model for the schema. If set to true, this will be the View Model to use for a given schema if no View Model ID is specified.
/// </summary>
public bool IsDefault { get { return isDefault; } }
public override int GetHashCode()
{
return base.GetHashCode(); //no need to override the hash code
}
public override bool Equals(object obj)
{
if (obj != null && obj is ViewModelAttribute)
{
ViewModelAttribute key = (ViewModelAttribute)obj;
if (this.ViewModelKeys != null && key.ViewModelKeys != null)
{
//if both have a ViewModelKey set, use both ViewModelKey and schema
//Check for a match anywhere in both lists
var match = from i in this.ViewModelKeys
join j in key.ViewModelKeys
on i equals j
select i;
//Schema names match and there is a matching view model ID
if (this.SchemaName == key.SchemaName && match.Count() > 0)
return true;
}
//Note: if the parent of a linked component is using a View Model Key, the View Model
//for that linked component must either be Default with no View Model Keys, or it must
//have the View Model Key of the parent View Model
if (((this.ViewModelKeys == null || this.ViewModelKeys.Length == 0) && key.IsDefault) //this set of IDs is empty and the input is default
|| ((key.ViewModelKeys == null || key.ViewModelKeys.Length == 0) && this.IsDefault)) //input set of IDs is empty and this is default
//if (key.IsDefault || this.IsDefault) //Fall back to default if the view model key isn't found -- useful for linked components
{
//Just compare the schema names
return this.SchemaName == key.SchemaName;
}
}
return false;
}
public bool IsMatch(IViewModelData data, IViewModelKeyProvider provider)
{
var key = provider.GetViewModelKey(data);
return IsMatch(data, key);
}
public bool IsMatch(IViewModelData data, string key)
{
bool result = false;
if (data is IDefinedData)
{
var definedData = data as IDefinedData;
var compare = new ViewModelAttribute(definedData.Schema.Title, false)
{
ViewModelKeys = key == null ? null : new string[] { key }
};
result = this.Equals(compare);
}
return result;
}
}
/// <summary>
/// An Attribute for identifying a Page View Model
/// </summary>
public class PageViewModelAttribute : Attribute, IPageModelAttribute
{
public PageViewModelAttribute(string[] viewModelKeys)
{
ViewModelKeys = viewModelKeys;
}
public string[] ViewModelKeys
{
get;
set;
}
public bool IsMatch(IViewModelData data, IViewModelKeyProvider provider)
{
string key = provider.GetViewModelKey(data);
return IsMatch(data, key);
}
public bool IsMatch(IViewModelData data, string key)
{
bool result = false;
if (data is IPageData)
{
var contentData = data as IPageData;
return ViewModelKeys.Any(x => x.Equals(key));
}
return result;
}
}
/// <summary>
/// An Attribute for identifying a Keyword View Model
/// </summary>
public class KeywordViewModelAttribute : Attribute, IKeywordModelAttribute
{
public KeywordViewModelAttribute(string[] viewModelKeys)
{
ViewModelKeys = viewModelKeys;
}
/// <summary>
/// View Model Keys for this Keyword
/// </summary>
/// <remarks>Common View Model Keys for Keywords are Metadata Schema Title or Category Title.</remarks>
public string[] ViewModelKeys
{
get;
set;
}
public bool IsMatch(IViewModelData data, IViewModelKeyProvider provider)
{
string key = provider.GetViewModelKey(data);
return IsMatch(data, key);
}
public bool IsMatch(IViewModelData data, string key)
{
bool result = false;
if (data is IKeywordData)
{
return ViewModelKeys.Any(x => x.Equals(key));
}
return result;
}
}
public abstract class NestedModelFieldAttributeBase : FieldAttributeBase
{
public override IEnumerable GetFieldValues(IFieldData field, IModelProperty property, ITemplateData template,IViewModelFactory factory = null)
{
IEnumerable fieldValue = null;
var values = GetRawValues(field);
if (values != null)
{
if (ComplexTypeMapping == null && ReturnRawData)
fieldValue = values;
else
fieldValue = values.Cast<object>()
.Select(value => BuildModel(factory, BuildModelData(value, field, template)))
.Where(value => value != null);
}
return fieldValue;
}
protected virtual object BuildModel(IViewModelFactory factory, IViewModelData data)
{
object result = null;
if (ComplexTypeMapping != null)
{
result = factory.BuildMappedModel(data, ComplexTypeMapping);
}
else
{
var modelType = GetModelType(data, factory);
result = modelType != null ? factory.BuildViewModel(modelType, data) : null;
}
return result;
}
public abstract IEnumerable GetRawValues(IFieldData field);
protected abstract IViewModelData BuildModelData(object value, IFieldData field, ITemplateData template);
protected abstract Type GetModelType(IViewModelData data, IViewModelFactory factory);
protected abstract bool ReturnRawData { get; }
public override Type ExpectedReturnType
{
get
{
if (ComplexTypeMapping != null)
return AllowMultipleValues ? typeof(ICollection<>) : typeof(object);
else return AllowMultipleValues ? typeof(ICollection<IViewModel>) : typeof(IViewModel);
}
}
}
}