-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathDefaultModelBuilder.cs
More file actions
executable file
·1195 lines (1073 loc) · 52.6 KB
/
DefaultModelBuilder.cs
File metadata and controls
executable file
·1195 lines (1073 loc) · 52.6 KB
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using DD4T.ContentModel;
using Sdl.Web.Common;
using Sdl.Web.Common.Configuration;
using Sdl.Web.Common.Extensions;
using Sdl.Web.Common.Interfaces;
using Sdl.Web.Common.Logging;
using Sdl.Web.Common.Mapping;
using Sdl.Web.Common.Models;
using Sdl.Web.Tridion.Extensions;
using IPage = DD4T.ContentModel.IPage;
namespace Sdl.Web.Tridion.Mapping
{
/// <summary>
/// Default Model Builder implementation (DD4T-based).
/// </summary>
/// <remarks>
/// Typically this model builder is the only one in the <see cref="ModelBuilderPipeline"/>, but advanced modules (like the SmartTarget module)
/// may add their own model builder to the pipeline (to post-process the resulting Strongly Typed View Models).
/// Note that the default model building creates the View Models and ignores any existing ones so should normally be the first in the pipeline.
/// </remarks>
public class DefaultModelBuilder : BaseModelBuilder, IModelBuilder
{
// TODO: while it works perfectly well, this class is in need of some refactoring to make its behaviour a bit more understandable and maintainable,
// as its currently very easy to get lost in the semantic mapping logic
private const string StandardMetadataXmlFieldName = "standardMeta";
private const string StandardMetadataTitleXmlFieldName = "name";
private const string StandardMetadataDescriptionXmlFieldName = "description";
private const string RegionForPageTitleComponent = "Main";
private const string ComponentXmlFieldNameForPageTitle = "headline";
#region IModelBuilder members
public virtual void BuildPageModel(ref PageModel pageModel, IPage page, IEnumerable<IPage> includes, Localization localization)
{
using (new Tracer(pageModel, page, includes, localization))
{
pageModel = CreatePageModel(page, localization);
RegionModelSet regions = pageModel.Regions;
// Create predefined Regions from Page Template Metadata
CreatePredefinedRegions(regions, page.PageTemplate);
// Create Regions/Entities from Component Presentations
IConditionalEntityEvaluator conditionalEntityEvaluator = SiteConfiguration.ConditionalEntityEvaluator;
foreach (IComponentPresentation cp in page.ComponentPresentations)
{
MvcData cpRegionMvcData = GetRegionMvcData(cp);
string regionName = cpRegionMvcData.RegionName ?? cpRegionMvcData.ViewName;
RegionModel region;
if (regions.TryGetValue(regionName, out region))
{
// Region already exists in Page Model; MVC data should match.
if (!region.MvcData.Equals(cpRegionMvcData))
{
Log.Warn("Region '{0}' is defined with conflicting MVC data: [{1}] and [{2}]. Using the former.", region.Name, region.MvcData, cpRegionMvcData);
}
}
else
{
// Region does not exist in Page Model yet; create Region Model and add it.
region = CreateRegionModel(cpRegionMvcData);
regions.Add(region);
}
try
{
EntityModel entity = ModelBuilderPipeline.CreateEntityModel(cp, localization);
if (conditionalEntityEvaluator == null || conditionalEntityEvaluator.IncludeEntity(entity))
{
region.Entities.Add(entity);
}
}
catch (Exception ex)
{
// If there is a problem mapping an Entity, we replace it with an ExceptionEntity which holds the error details and carry on.
Log.Error(ex);
region.Entities.Add(new ExceptionEntity(ex));
}
}
// Create Regions from Include Pages
if (includes != null)
{
foreach (IPage includePage in includes)
{
PageModel includePageModel = ModelBuilderPipeline.CreatePageModel(includePage, null, localization);
// Model Include Page as Region:
RegionModel includePageRegion = GetRegionFromIncludePage(includePage);
RegionModel existingRegion;
if (regions.TryGetValue(includePageRegion.Name, out existingRegion))
{
// Region with same name already exists; merge include Page Region.
existingRegion.Regions.UnionWith(includePageModel.Regions);
if (existingRegion.XpmMetadata != null)
{
existingRegion.XpmMetadata.Remove(RegionModel.IncludedFromPageIdXpmMetadataKey);
existingRegion.XpmMetadata.Remove(RegionModel.IncludedFromPageTitleXpmMetadataKey);
existingRegion.XpmMetadata.Remove(RegionModel.IncludedFromPageFileNameXpmMetadataKey);
}
Log.Info("Merged Include Page [{0}] into Region [{1}]. Note that merged Regions can't be edited properly in XPM (yet).",
includePageModel, existingRegion);
}
else
{
includePageRegion.Regions.UnionWith(includePageModel.Regions);
regions.Add(includePageRegion);
}
#pragma warning disable 618
// Legacy WebPage.Includes support:
pageModel.Includes.Add(includePage.Title, includePageModel);
#pragma warning restore 618
}
if (pageModel.MvcData.ViewName != "IncludePage")
{
pageModel.Title = ProcessPageMetadata(page, pageModel.Meta, localization);
}
}
}
}
public virtual void BuildEntityModel(ref EntityModel entityModel, IComponentPresentation cp, Localization localization)
{
using (new Tracer(entityModel, cp, localization))
{
MvcData mvcData = GetMvcData(cp);
Type modelType = ModelTypeRegistry.GetViewModelType(mvcData);
// NOTE: not using ModelBuilderPipeline here, but directly calling our own implementation.
BuildEntityModel(ref entityModel, cp.Component, modelType, localization);
entityModel.XpmMetadata = GetXpmMetadata(cp.Component);
entityModel.XpmMetadata.Add("ComponentTemplateID", cp.ComponentTemplate.Id);
entityModel.XpmMetadata.Add("ComponentTemplateModified", cp.ComponentTemplate.RevisionDate.ToString("yyyy-MM-ddTHH:mm:ss"));
entityModel.XpmMetadata.Add("IsRepositoryPublished", cp.IsDynamic);
entityModel.MvcData = mvcData;
// add html classes to model from metadata
// TODO: move to CreateViewModel so it can be merged with the same code for a Page/PageTemplate
IComponentTemplate template = cp.ComponentTemplate;
if (template.MetadataFields != null && template.MetadataFields.ContainsKey("htmlClasses"))
{
// strip illegal characters to ensure valid html in the view (allow spaces for multiple classes)
entityModel.HtmlClasses = template.MetadataFields["htmlClasses"].Value.StripIllegalCharacters(@"[^\w\-\ ]");
}
if (cp.IsDynamic)
{
// update Entity Identifier to that of a DCP
entityModel.Id = GetDxaIdentifierFromTcmUri(cp.Component.Id, cp.ComponentTemplate.Id);
}
}
}
public virtual void BuildEntityModel(ref EntityModel entityModel, IComponent component, Type baseModelType, Localization localization)
{
using (new Tracer(entityModel, component, baseModelType, localization))
{
string[] schemaTcmUriParts = component.Schema.Id.Split('-');
SemanticSchema semanticSchema = SemanticMapping.GetSchema(schemaTcmUriParts[1], localization);
// The semantic mapping may resolve to a more specific model type than specified by the View Model itself (e.g. Image instead of just MediaItem for Teaser.Media)
Type modelType = semanticSchema.GetModelTypeFromSemanticMapping(baseModelType);
MappingData mappingData = new MappingData
{
SemanticSchema = semanticSchema,
EntityNames = semanticSchema.GetEntityNames(),
TargetEntitiesByPrefix = GetEntityDataFromType(modelType),
Content = component.Fields,
Meta = component.MetadataFields,
TargetType = modelType,
SourceEntity = component,
Localization = localization
};
entityModel = (EntityModel)CreateViewModel(mappingData);
entityModel.Id = GetDxaIdentifierFromTcmUri(component.Id);
if (entityModel is MediaItem && component.Multimedia != null && component.Multimedia.Url != null)
{
MediaItem mediaItem = (MediaItem)entityModel;
mediaItem.Url = component.Multimedia.Url;
mediaItem.FileName = component.Multimedia.FileName;
mediaItem.FileSize = component.Multimedia.Size;
mediaItem.MimeType = component.Multimedia.MimeType;
}
if (entityModel is EclItem)
{
MapEclItem((EclItem) entityModel, component);
}
if (entityModel is Link)
{
Link link = (Link)entityModel;
if (String.IsNullOrEmpty(link.Url))
{
link.Url = SiteConfiguration.LinkResolver.ResolveLink(component.Id);
}
}
// Set the Entity Model's default View (if any) after it has been fully initialized.
entityModel.MvcData = entityModel.GetDefaultView(localization);
}
}
#endregion
private static void MapEclItem(EclItem eclItem, IComponent component)
{
eclItem.EclUri = component.EclId;
IFieldSet eclExtensionDataFields;
if (component.ExtensionData == null || !component.ExtensionData.TryGetValue("ECL", out eclExtensionDataFields))
{
Log.Warn("Encountered ECL Stub Component without ECL Extension Data: {0}", component.Id);
return;
}
eclItem.EclDisplayTypeId = GetFieldValue("DisplayTypeId", eclExtensionDataFields);
eclItem.EclTemplateFragment = GetFieldValue("TemplateFragment", eclExtensionDataFields);
string eclFileName = GetFieldValue("FileName", eclExtensionDataFields);
if (!string.IsNullOrEmpty(eclFileName))
{
eclItem.FileName = eclFileName;
}
string eclMimeType = GetFieldValue("MimeType", eclExtensionDataFields);
if (!string.IsNullOrEmpty(eclMimeType))
{
eclItem.MimeType = eclMimeType;
}
IFieldSet eclExternalMetadataFields;
if (component.ExtensionData.TryGetValue("ECL-ExternalMetadata", out eclExternalMetadataFields))
{
eclItem.EclExternalMetadata = MapEclExternalMetadata(eclExternalMetadataFields);
}
}
private static IDictionary<string, object> MapEclExternalMetadata(IFieldSet fields)
{
IDictionary<string, object> result = new Dictionary<string, object>();
foreach (IField field in fields.Values)
{
object mappedValue;
switch (field.FieldType)
{
case FieldType.Number:
mappedValue = GetFieldValue(field.NumericValues);
break;
case FieldType.Date:
mappedValue = GetFieldValue(field.DateTimeValues);
break;
case FieldType.Embedded:
if (field.EmbeddedValues.Count == 1)
{
mappedValue = MapEclExternalMetadata(field.EmbeddedValues[0]);
}
else
{
mappedValue = field.EmbeddedValues.Select(MapEclExternalMetadata).ToArray();
}
break;
default:
mappedValue = GetFieldValue(field.Values);
break;
}
result.Add(field.Name, mappedValue);
}
return result;
}
private static object GetFieldValue<T>(IList<T> fieldValues)
{
switch (fieldValues.Count)
{
case 0:
return null;
case 1:
return fieldValues[0];
default:
return fieldValues;
}
}
private static string GetFieldValue(string fieldName, IFieldSet fields)
{
IField field;
if (!fields.TryGetValue(fieldName, out field))
{
return null;
}
return field.Value;
}
private PageModel CreatePageModel(IPage page, Localization localization)
{
MvcData pageMvcData = GetMvcData(page);
Type pageModelType = ModelTypeRegistry.GetViewModelType(pageMvcData);
string pageId = GetDxaIdentifierFromTcmUri(page.Id);
ISchema pageMetadataSchema = page.Schema;
PageModel pageModel;
if (pageModelType == typeof(PageModel))
{
// Standard Page Model
pageModel = new PageModel(pageId);
}
else if (pageMetadataSchema == null)
{
// Custom Page Model but no Page metadata that can be mapped; simply create a Page Model instance of the right type.
pageModel = (PageModel)Activator.CreateInstance(pageModelType, pageId);
}
else
{
// Custom Page Model and Page metadata is present; do full-blown model mapping.
string[] schemaTcmUriParts = pageMetadataSchema.Id.Split('-');
SemanticSchema semanticSchema = SemanticMapping.GetSchema(schemaTcmUriParts[1], localization);
MappingData mappingData = new MappingData
{
TargetType = pageModelType,
SemanticSchema = semanticSchema,
EntityNames = semanticSchema.GetEntityNames(),
TargetEntitiesByPrefix = GetEntityDataFromType(pageModelType),
Meta = page.MetadataFields,
ModelId = pageId,
Localization = localization
};
pageModel = (PageModel) CreateViewModel(mappingData);
}
pageModel.MvcData = pageMvcData;
pageModel.XpmMetadata = GetXpmMetadata(page);
pageModel.Title = page.Title;
// add html classes to model from metadata
// TODO: move to CreateViewModel so it can be merged with the same code for a Component/ComponentTemplate
IPageTemplate template = page.PageTemplate;
if (template.MetadataFields != null && template.MetadataFields.ContainsKey("htmlClasses"))
{
// strip illegal characters to ensure valid html in the view (allow spaces for multiple classes)
pageModel.HtmlClasses = template.MetadataFields["htmlClasses"].Value.StripIllegalCharacters(@"[^\w\-\ ]");
}
return pageModel;
}
protected virtual ViewModel CreateViewModel(MappingData mappingData)
{
Type modelType = mappingData.TargetType; // TODO: why is this not a separate parameter?
ViewModel model;
if (string.IsNullOrEmpty(mappingData.ModelId))
{
// Use parameterless constructor
model = (ViewModel)Activator.CreateInstance(modelType);
}
else
{
// Pass model Identifier in constructor.
model = (ViewModel)Activator.CreateInstance(modelType, mappingData.ModelId);
}
Dictionary<string, string> xpmPropertyMetadata = new Dictionary<string, string>();
Dictionary<string, List<SemanticProperty>> propertySemantics = LoadPropertySemantics(modelType);
propertySemantics = FilterPropertySemanticsByEntity(propertySemantics, mappingData);
foreach (PropertyInfo pi in modelType.GetProperties())
{
bool multival = pi.PropertyType.IsGenericType && (pi.PropertyType.GetGenericTypeDefinition() == typeof(List<>));
Type propertyType = multival ? pi.PropertyType.GetGenericArguments()[0] : pi.PropertyType;
if (propertySemantics.ContainsKey(pi.Name))
{
foreach (SemanticProperty info in propertySemantics[pi.Name])
{
IField field = GetFieldFromSemantics(mappingData, info);
if (field != null)
{
pi.SetValue(model, MapFieldValues(field, propertyType, multival, mappingData));
xpmPropertyMetadata.Add(pi.Name, GetFieldXPath(field));
break;
}
// Special mapping cases require SourceEntity to be set
if (mappingData.SourceEntity == null)
{
continue;
}
bool processed = false;
if (info.PropertyName == "_self")
{
//Map the whole entity to an image property, or a resolved link to the entity to a Url field
if (typeof(MediaItem).IsAssignableFrom(propertyType) || typeof(Link).IsAssignableFrom(propertyType) || propertyType == typeof(String))
{
object mappedSelf = MapComponent(mappingData.SourceEntity, propertyType, mappingData.Localization);
if (multival)
{
IList genericList = CreateGenericList(propertyType);
genericList.Add(mappedSelf);
pi.SetValue(model, genericList);
}
else
{
pi.SetValue(model, mappedSelf);
}
processed = true;
}
}
else if (info.PropertyName == "_all" && pi.PropertyType == typeof(Dictionary<string, string>))
{
//Map all fields into a single (Dictionary) property
pi.SetValue(model, GetAllFieldsAsDictionary(mappingData.SourceEntity));
processed = true;
}
if (processed)
{
break;
}
}
}
}
EntityModel entityModel = model as EntityModel;
if (entityModel != null)
{
entityModel.XpmPropertyMetadata = xpmPropertyMetadata;
}
return model;
}
protected virtual Dictionary<string, List<SemanticProperty>> FilterPropertySemanticsByEntity(Dictionary<string, List<SemanticProperty>> propertySemantics, MappingData mapData)
{
Dictionary<string, List<SemanticProperty>> filtered = new Dictionary<string, List<SemanticProperty>>();
foreach (KeyValuePair<string, List<SemanticProperty>> property in propertySemantics)
{
filtered.Add(property.Key, new List<SemanticProperty>());
List<SemanticProperty> defaultProperties = new List<SemanticProperty>();
foreach (SemanticProperty semanticProperty in property.Value)
{
//Default prefix is always OK, but should be added last
if (string.IsNullOrEmpty(semanticProperty.Prefix))
{
defaultProperties.Add(semanticProperty);
}
else
{
//Filter out any properties belonging to other entities than the source entity
KeyValuePair<string, string>? entityData = GetEntityData(semanticProperty.Prefix, mapData.TargetEntitiesByPrefix, mapData.ParentDefaultPrefix);
if (entityData != null && mapData.EntityNames!=null && mapData.EntityNames.Contains(entityData.Value.Key))
{
if (mapData.EntityNames[entityData.Value.Key].First() == entityData.Value.Value)
{
filtered[property.Key].Add(semanticProperty);
}
}
}
}
filtered[property.Key].AddRange(defaultProperties);
}
return filtered;
}
private static IField GetFieldFromSemantics(MappingData mapData, SemanticProperty info)
{
KeyValuePair<string, string>? entityData = GetEntityData(info.Prefix, mapData.TargetEntitiesByPrefix, mapData.ParentDefaultPrefix);
if (entityData != null)
{
// determine field semantics
string vocab = entityData.Value.Key;
string prefix = SemanticMapping.GetPrefix(vocab, mapData.Localization);
if (prefix != null && mapData.EntityNames!=null)
{
string property = info.PropertyName;
string entity = mapData.EntityNames[vocab].FirstOrDefault();
if (entity != null && mapData.SemanticSchema!=null)
{
FieldSemantics fieldSemantics = new FieldSemantics(prefix, entity, property);
// locate semantic schema field
SemanticSchemaField matchingField = mapData.SemanticSchema.FindFieldBySemantics(fieldSemantics);
if (matchingField != null)
{
return ExtractMatchedField(matchingField, (matchingField.IsMetadata && mapData.Meta!=null) ? mapData.Meta : mapData.Content, mapData.EmbedLevel);
}
}
}
}
return null;
}
private static KeyValuePair<string, string>? GetEntityData(string prefix, Dictionary<string, KeyValuePair<string, string>> entityData, string defaultPrefix)
{
if (defaultPrefix != null && String.IsNullOrEmpty(prefix))
{
prefix = defaultPrefix;
}
if (entityData.ContainsKey(prefix))
{
return entityData[prefix];
}
return null;
}
private static IField ExtractMatchedField(SemanticSchemaField matchingField, IFieldSet fields, int embedLevel, string path = null)
{
if (path==null)
{
path = matchingField.Path;
while (embedLevel >= -1 && path.Contains("/"))
{
int pos = path.IndexOf("/", StringComparison.Ordinal);
path = path.Substring(pos+1);
embedLevel--;
}
}
string[] bits = path.Split('/');
if (fields.ContainsKey(bits[0]))
{
if (bits.Length > 1)
{
int pos = path.IndexOf("/", StringComparison.Ordinal);
return ExtractMatchedField(matchingField, fields[bits[0]].EmbeddedValues[0], embedLevel, path.Substring(pos + 1));
}
return fields[bits[0]];
}
return null;
}
private static IList CreateGenericList(Type listItemType)
{
ConstructorInfo genericListConstructor = typeof(List<>).MakeGenericType(listItemType).GetConstructor(Type.EmptyTypes);
if (genericListConstructor == null)
{
// This should never happen.
throw new DxaException(String.Format("Unable get constructor for generic list of '{0}'.", listItemType.FullName));
}
return (IList)genericListConstructor.Invoke(null);
}
private object MapFieldValues(IField field, Type modelType, bool multival, MappingData mapData)
{
try
{
// Convert.ChangeType cannot convert non-nullable types to nullable types, so don't try that.
Type bareModelType = modelType;
if (modelType.IsGenericType && modelType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
bareModelType = modelType.GenericTypeArguments[0];
}
IList mappedValues = CreateGenericList(modelType);
switch (field.FieldType)
{
case FieldType.Date:
foreach (DateTime value in field.DateTimeValues)
{
mappedValues.Add(Convert.ChangeType(value, bareModelType));
}
break;
case FieldType.Number:
foreach (Double value in field.NumericValues)
{
mappedValues.Add(Convert.ChangeType(value, bareModelType));
}
break;
case FieldType.MultiMediaLink:
case FieldType.ComponentLink:
foreach (IComponent value in field.LinkedComponentValues)
{
mappedValues.Add(MapComponent(value, modelType, mapData.Localization));
}
break;
case FieldType.Embedded:
foreach (IFieldSet value in field.EmbeddedValues)
{
mappedValues.Add(MapEmbeddedFields(value, modelType, mapData));
}
break;
case FieldType.Keyword:
foreach (IKeyword value in field.Keywords)
{
mappedValues.Add(MapKeyword(value, modelType));
}
break;
case FieldType.Xhtml:
IRichTextProcessor richTextProcessor = SiteConfiguration.RichTextProcessor;
foreach (string value in field.Values)
{
RichText richText = richTextProcessor.ProcessRichText(value, mapData.Localization);
if (modelType == typeof(string))
{
mappedValues.Add(richText.ToString());
}
else
{
mappedValues.Add(richText);
}
}
break;
default:
foreach (string value in field.Values)
{
object mappedValue = (modelType == typeof(RichText)) ? new RichText(value) : Convert.ChangeType(value, bareModelType);
mappedValues.Add(mappedValue);
}
break;
}
if (multival)
{
return mappedValues;
}
return mappedValues.Count == 0 ? null : mappedValues[0];
}
catch (Exception ex)
{
throw new DxaException(String.Format("Unable to map field '{0}' to property of type '{1}'.", field.Name, modelType.FullName), ex);
}
}
private static string GetFieldXPath(IField field)
{
return field.XPath;
}
protected virtual IDictionary<string, object> GetXpmMetadata(IComponent comp)
{
IDictionary<string, object> result = new Dictionary<string, object>();
if (comp != null)
{
result.Add("ComponentID", comp.Id);
result.Add("ComponentModified", comp.RevisionDate.ToString("yyyy-MM-ddTHH:mm:ss"));
}
return result;
}
protected virtual IDictionary<string, object> GetXpmMetadata(IPage page)
{
IDictionary<string, object> result = new Dictionary<string, object>();
if (page != null)
{
result.Add("PageID", page.Id);
result.Add("PageModified", page.RevisionDate.ToString("yyyy-MM-ddTHH:mm:ss"));
result.Add("PageTemplateID", page.PageTemplate.Id);
result.Add("PageTemplateModified", page.PageTemplate.RevisionDate.ToString("yyyy-MM-ddTHH:mm:ss"));
}
return result;
}
protected virtual object MapKeyword(IKeyword keyword, Type modelType)
{
// TODO TSI-811: Keyword mapping should also be generic rather than hard-coded like below
string displayText = String.IsNullOrEmpty(keyword.Description) ? keyword.Title : keyword.Description;
if (modelType == typeof(Tag))
{
return new Tag
{
DisplayText = displayText,
Key = String.IsNullOrEmpty(keyword.Key) ? keyword.Id : keyword.Key,
TagCategory = keyword.TaxonomyId
};
}
if (modelType == typeof(bool))
{
//For booleans we assume the keyword key or value can be converted to bool
return Boolean.Parse(String.IsNullOrEmpty(keyword.Key) ? keyword.Title : keyword.Key);
}
if (modelType == typeof(string))
{
return displayText;
}
throw new DxaException(String.Format("Cannot map Keyword to type '{0}'. The type must be Tag, bool or string.", modelType));
}
protected virtual object MapComponent(IComponent component, Type modelType, Localization localization)
{
if (modelType == typeof(string))
{
return SiteConfiguration.LinkResolver.ResolveLink(component.Id);
}
if (!modelType.IsSubclassOf(typeof(EntityModel)))
{
throw new DxaException(String.Format("Cannot map a Component to type '{0}'. The type must be String or a subclass of EntityModel.", modelType));
}
return ModelBuilderPipeline.CreateEntityModel(component, modelType, localization);
}
private ViewModel MapEmbeddedFields(IFieldSet embeddedFields, Type modelType, MappingData mapData)
{
MappingData embeddedMappingData = new MappingData
{
TargetType = modelType,
Content = embeddedFields,
Meta = null,
EntityNames = mapData.EntityNames, // TODO: should this not be re-determined for the embedded model type?
ParentDefaultPrefix = mapData.ParentDefaultPrefix,
TargetEntitiesByPrefix = mapData.TargetEntitiesByPrefix, // TODO: should this not be re-determined for the embedded model type?
SemanticSchema = mapData.SemanticSchema, // TODO: should this not be re-determined for the embedded model type?
EmbedLevel = mapData.EmbedLevel + 1,
Localization = mapData.Localization
};
return CreateViewModel(embeddedMappingData);
}
protected Dictionary<string, string> GetAllFieldsAsDictionary(IComponent component)
{
Dictionary<string, string> values = new Dictionary<string, string>();
foreach (string fieldname in component.Fields.Keys)
{
if (!values.ContainsKey(fieldname))
{
//special case for multival embedded name/value pair fields
if (fieldname == "settings" && component.Fields[fieldname].FieldType == FieldType.Embedded)
{
foreach (IFieldSet embedFieldset in component.Fields[fieldname].EmbeddedValues)
{
string key = embedFieldset.ContainsKey("name") ? embedFieldset["name"].Value : null;
if (key != null)
{
string value = embedFieldset.ContainsKey("value") ? embedFieldset["value"].Value : null;
if (!values.ContainsKey(key))
{
values.Add(key, value);
}
}
}
}
//Default is to add the value as plain text
else
{
string val = GetFieldValuesAsStrings(component.Fields[fieldname]).FirstOrDefault();
if (val != null)
{
values.Add(fieldname, val);
}
}
}
}
foreach (string fieldname in component.MetadataFields.Keys)
{
if (!values.ContainsKey(fieldname))
{
string val = GetFieldValuesAsStrings(component.MetadataFields[fieldname]).FirstOrDefault();
if (val != null)
{
values.Add(fieldname, val);
}
}
}
return values;
}
private static string[] GetFieldValuesAsStrings(IField field)
{
switch (field.FieldType)
{
case FieldType.Number:
return field.NumericValues.Select(v => v.ToString(CultureInfo.InvariantCulture)).ToArray();
case FieldType.Date:
return field.DateTimeValues.Select(v => v.ToString("s")).ToArray();
case FieldType.ComponentLink:
case FieldType.MultiMediaLink:
return field.LinkedComponentValues.Select(v => v.Id).ToArray();
case FieldType.Keyword:
return field.KeywordValues.Select(v => v.Id).ToArray();
default:
return field.Values.ToArray();
}
}
protected virtual string ProcessPageMetadata(IPage page, IDictionary<string, string> meta, Localization localization)
{
//First grab metadata from the page
if (page.MetadataFields != null)
{
foreach (IField field in page.MetadataFields.Values)
{
ProcessMetadataField(field, meta);
}
}
string description = meta.ContainsKey("description") ? meta["description"] : null;
string title = meta.ContainsKey("title") ? meta["title"] : null;
string image = meta.ContainsKey("image") ? meta["image"] : null;
//If we don't have a title or description - go hunting for a title and/or description from the first component in the main region on the page
if (title == null || description == null)
{
bool first = true;
foreach (IComponentPresentation cp in page.ComponentPresentations)
{
MvcData regionMvcData = GetRegionMvcData(cp);
// determine title and description from first component in 'main' region
if (first && regionMvcData.ViewName.Equals(RegionForPageTitleComponent))
{
first = false;
IFieldSet metadata = cp.Component.MetadataFields;
IFieldSet fields = cp.Component.Fields;
if (metadata.ContainsKey(StandardMetadataXmlFieldName) && metadata[StandardMetadataXmlFieldName].EmbeddedValues.Count > 0)
{
IFieldSet standardMeta = metadata[StandardMetadataXmlFieldName].EmbeddedValues[0];
if (title == null && standardMeta.ContainsKey(StandardMetadataTitleXmlFieldName))
{
title = standardMeta[StandardMetadataTitleXmlFieldName].Value;
}
if (description == null && standardMeta.ContainsKey(StandardMetadataDescriptionXmlFieldName))
{
description = standardMeta[StandardMetadataDescriptionXmlFieldName].Value;
}
}
if (title == null && fields.ContainsKey(ComponentXmlFieldNameForPageTitle))
{
title = fields[ComponentXmlFieldNameForPageTitle].Value;
}
//Try to find an image
if (image == null && fields.ContainsKey("image"))
{
image = fields["image"].LinkedComponentValues[0].Multimedia.Url;
}
}
}
}
IDictionary coreResources = localization.GetResources("core");
string titlePostfix = coreResources["core.pageTitleSeparator"].ToString() + coreResources["core.pageTitlePostfix"].ToString();
//if we still dont have a title, use the page title
if (title == null)
{
title = Regex.Replace(page.Title, @"^\d{3}\s", String.Empty);
// Index and Default are not a proper titles for an HTML page
if (title.ToLowerInvariant().Equals("index") || title.ToLowerInvariant().Equals("default"))
{
title = coreResources["core.defaultPageTitle"].ToString();
}
}
meta.Add("twitter:card", "summary");
meta.Add("og:title", title);
// TODO: if the URL is really needed, it should be added higher up (e.g. in the View code): meta.Add("og:url", WebRequestContext.RequestUrl);
// TODO: is this always article?
meta.Add("og:type", "article");
meta.Add("og:locale", localization.Culture);
if (description != null)
{
meta.Add("og:description", description);
}
if (image != null)
{
image = localization.GetBaseUrl() + image;
meta.Add("og:image", image);
}
if (!meta.ContainsKey("description"))
{
meta.Add("description", description ?? title);
}
return title + titlePostfix;
}
protected virtual void ProcessMetadataField(IField field, IDictionary<string, string> meta)
{
if (field.FieldType==FieldType.Embedded)
{
if (field.EmbeddedValues!=null && field.EmbeddedValues.Count>0)
{
IFieldSet subfields = field.EmbeddedValues[0];
foreach (IField subfield in subfields.Values)
{
ProcessMetadataField(subfield, meta);
}
}
}
else
{
string value;
switch (field.Name)
{
case "internalLink":
value = SiteConfiguration.LinkResolver.ResolveLink(field.Value);
break;
case "image":
value = field.LinkedComponentValues[0].Multimedia.Url;
break;
default:
value = String.Join(",", field.Values);
break;
}
if (value != null && !meta.ContainsKey(field.Name))
{
meta.Add(field.Name, value);
}
}
}
private static void InitializeRegionMvcData(MvcData regionMvcData)
{
if (String.IsNullOrEmpty(regionMvcData.ControllerName))
{
regionMvcData.ControllerName = SiteConfiguration.GetRegionController();
regionMvcData.ControllerAreaName = SiteConfiguration.GetDefaultModuleName();
}
else if (String.IsNullOrEmpty(regionMvcData.ControllerAreaName))
{
regionMvcData.ControllerAreaName = regionMvcData.AreaName;
}
regionMvcData.ActionName = SiteConfiguration.GetRegionAction();
}
private static MvcData GetRegionMvcData(IComponentPresentation cp)
{
IComponentTemplate ct = cp.ComponentTemplate;
string regionViewName = null;
IField regionViewNameField;
if (ct.MetadataFields != null && ct.MetadataFields.TryGetValue("regionView", out regionViewNameField))
{
regionViewName = regionViewNameField.Value;
}
else
{
// Fallback if no CT metadata found: try to extract Region View name from CT title
if (ct.Title == null)
{
// If a DCP has been published with DDT4 1.31 TBBs, it won't be read properly using the DD4T 2.0 ComponentPresentationFactory
// resulting in almost all CT properties being null.
throw new DxaException(
string.Format("No Component Template data available for DCP '{0}/{1}'. Republish the DCP to ensure it uses the new DD4T 2.0 DCP format.",
cp.Component.Id, cp.ComponentTemplate.Id)
);
}
Match match = Regex.Match(ct.Title, @".*?\[(.+?)\]");
if (match.Success)
{
regionViewName = match.Groups[1].Value;
}
}
if (String.IsNullOrEmpty(regionViewName))
{
regionViewName = "Main";
}
MvcData regionMvcData = new MvcData(regionViewName);
InitializeRegionMvcData(regionMvcData);
IField regionNameField;
if (ct.MetadataFields != null && ct.MetadataFields.TryGetValue("regionName", out regionNameField))
{
if (!String.IsNullOrEmpty(regionNameField.Value))
{
regionMvcData.RegionName = regionNameField.Value;
}
}
regionMvcData.RouteValues = GetRouteValues(ct,"regionRouteValues");
return regionMvcData;
}
private static RegionModel GetRegionFromIncludePage(IPage page)
{
// Page Title can be a qualified View Name; we use the unqualified View Name as Region Name.
MvcData regionMvcData = new MvcData(page.Title);