From a29706b5ce0186ec213cec75b16d985cedfad540 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 20 Aug 2013 11:33:41 +1000 Subject: [PATCH] Updated the model mappers to include the pre-value value which is now wired up to the display, now just need to get the data type's persisting. --- src/Umbraco.Core/Models/DataTypeDefinition.cs | 2 + .../datatype/datatype.edit.controller.js | 1 + .../views/prevalueeditors/requiredfield.html | 1 + src/Umbraco.Web/Editors/DataTypeController.cs | 7 +++ .../ContentEditing/PreValueFieldDisplay.cs | 6 +++ .../ContentPropertyDisplayConverter.cs | 14 +++--- .../Mapping/ContentPropertyDtoConverter.cs | 14 ++++-- .../Mapping/ContentPropertyModelMapper.cs | 10 ++-- .../Models/Mapping/DataTypeModelMapper.cs | 9 +++- .../Models/Mapping/PreValueDisplayResolver.cs | 46 ++++++++++++++++++- 10 files changed, 92 insertions(+), 18 deletions(-) diff --git a/src/Umbraco.Core/Models/DataTypeDefinition.cs b/src/Umbraco.Core/Models/DataTypeDefinition.cs index b4b3f42422..f879510c00 100644 --- a/src/Umbraco.Core/Models/DataTypeDefinition.cs +++ b/src/Umbraco.Core/Models/DataTypeDefinition.cs @@ -146,6 +146,8 @@ namespace Umbraco.Core.Models } } + //NOTE: SD: Why do we have this ?? + /// /// Boolean indicating whether this entity is Trashed or not. /// diff --git a/src/Umbraco.Web.UI.Client/src/views/datatype/datatype.edit.controller.js b/src/Umbraco.Web.UI.Client/src/views/datatype/datatype.edit.controller.js index caaa332b10..4496d24fc7 100644 --- a/src/Umbraco.Web.UI.Client/src/views/datatype/datatype.edit.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/datatype/datatype.edit.controller.js @@ -33,6 +33,7 @@ function DataTypeEditController($scope, $routeParams, $location, dataTypeResourc description: preVals[i].description, label: preVals[i].label, view: preVals[i].view, + value: preVals[i].value }); } } diff --git a/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/requiredfield.html b/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/requiredfield.html index 621a0cb9d7..79b44ced00 100644 --- a/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/requiredfield.html +++ b/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/requiredfield.html @@ -4,4 +4,5 @@ required /> Required + diff --git a/src/Umbraco.Web/Editors/DataTypeController.cs b/src/Umbraco.Web/Editors/DataTypeController.cs index 41dbed0c26..60a48b3934 100644 --- a/src/Umbraco.Web/Editors/DataTypeController.cs +++ b/src/Umbraco.Web/Editors/DataTypeController.cs @@ -55,5 +55,12 @@ namespace Umbraco.Web.Editors return propEd.PreValueEditor.Fields.Select(Mapper.Map); } + + //TODO: Generally there probably won't be file uploads for pre-values but we should allow them just like we do for the content editor + + public DataTypeDisplay PostSave() + { + return null; + } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Models/ContentEditing/PreValueFieldDisplay.cs b/src/Umbraco.Web/Models/ContentEditing/PreValueFieldDisplay.cs index 9f38f6c331..772beeb727 100644 --- a/src/Umbraco.Web/Models/ContentEditing/PreValueFieldDisplay.cs +++ b/src/Umbraco.Web/Models/ContentEditing/PreValueFieldDisplay.cs @@ -37,5 +37,11 @@ namespace Umbraco.Web.Models.ContentEditing /// [DataMember(Name = "view", IsRequired = true)] public string View { get; set; } + + /// + /// The value stored for the pre-value field + /// + [DataMember(Name = "value", IsRequired = true)] + public string Value { get; set; } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Models/Mapping/ContentPropertyDisplayConverter.cs b/src/Umbraco.Web/Models/Mapping/ContentPropertyDisplayConverter.cs index 8214d08262..1eb883e1eb 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentPropertyDisplayConverter.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentPropertyDisplayConverter.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.Models; @@ -12,11 +13,11 @@ namespace Umbraco.Web.Models.Mapping /// internal class ContentPropertyDisplayConverter : ContentPropertyBasicConverter { - private readonly ApplicationContext _applicationContext; + private readonly Lazy _dataTypeService; - public ContentPropertyDisplayConverter(ApplicationContext applicationContext) + public ContentPropertyDisplayConverter(Lazy dataTypeService) { - _applicationContext = applicationContext; + _dataTypeService = dataTypeService; } protected override ContentPropertyDisplay ConvertCore(Property originalProp) @@ -27,12 +28,11 @@ namespace Umbraco.Web.Models.Mapping display.Alias = originalProp.Alias; display.Description = originalProp.PropertyType.Description; display.Label = originalProp.PropertyType.Name; - var dataTypeService = (DataTypeService) _applicationContext.Services.DataTypeService; + + var dataTypeService = (DataTypeService)_dataTypeService.Value; var preVals = dataTypeService.GetPreValuesCollectionByDataTypeId(originalProp.PropertyType.DataTypeDefinitionId); - - if (display.PropertyEditor == null) { display.Config = PreValueCollection.AsDictionary(preVals); diff --git a/src/Umbraco.Web/Models/Mapping/ContentPropertyDtoConverter.cs b/src/Umbraco.Web/Models/Mapping/ContentPropertyDtoConverter.cs index 297406e10d..7c2310375b 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentPropertyDtoConverter.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentPropertyDtoConverter.cs @@ -1,6 +1,8 @@ -using Umbraco.Core; +using System; +using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.PropertyEditors; +using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; namespace Umbraco.Web.Models.Mapping @@ -10,22 +12,24 @@ namespace Umbraco.Web.Models.Mapping /// internal class ContentPropertyDtoConverter : ContentPropertyBasicConverter { - private readonly ApplicationContext _applicationContext; + private readonly Lazy _dataTypeService; - public ContentPropertyDtoConverter(ApplicationContext applicationContext) + public ContentPropertyDtoConverter(Lazy dataTypeService) { - _applicationContext = applicationContext; + _dataTypeService = dataTypeService; } protected override ContentPropertyDto ConvertCore(Property originalProperty) { var propertyDto = base.ConvertCore(originalProperty); + var dataTypeService = (DataTypeService)_dataTypeService.Value; + propertyDto.IsRequired = originalProperty.PropertyType.Mandatory; propertyDto.ValidationRegExp = originalProperty.PropertyType.ValidationRegExp; propertyDto.Description = originalProperty.PropertyType.Description; propertyDto.Label = originalProperty.PropertyType.Name; - propertyDto.DataType = _applicationContext.Services.DataTypeService.GetDataTypeDefinitionById(originalProperty.PropertyType.DataTypeDefinitionId); + propertyDto.DataType = dataTypeService.GetDataTypeDefinitionById(originalProperty.PropertyType.DataTypeDefinitionId); return propertyDto; } diff --git a/src/Umbraco.Web/Models/Mapping/ContentPropertyModelMapper.cs b/src/Umbraco.Web/Models/Mapping/ContentPropertyModelMapper.cs index f567010f73..9ebb27098c 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentPropertyModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentPropertyModelMapper.cs @@ -1,7 +1,9 @@ -using AutoMapper; +using System; +using AutoMapper; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Models.Mapping; +using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; namespace Umbraco.Web.Models.Mapping @@ -14,6 +16,8 @@ namespace Umbraco.Web.Models.Mapping { public override void ConfigureMappings(IConfiguration config, ApplicationContext applicationContext) { + var lazyDataTypeService = new Lazy(() => applicationContext.Services.DataTypeService); + //FROM Property TO ContentPropertyBasic config.CreateMap>() .ForMember(tab => tab.Label, expression => expression.MapFrom(@group => @group.Name)) @@ -26,11 +30,11 @@ namespace Umbraco.Web.Models.Mapping //FROM Property TO ContentPropertyDto config.CreateMap() - .ConvertUsing(new ContentPropertyDtoConverter(applicationContext)); + .ConvertUsing(new ContentPropertyDtoConverter(lazyDataTypeService)); //FROM Property TO ContentPropertyDisplay config.CreateMap() - .ConvertUsing(new ContentPropertyDisplayConverter(applicationContext)); + .ConvertUsing(new ContentPropertyDisplayConverter(lazyDataTypeService)); } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Models/Mapping/DataTypeModelMapper.cs b/src/Umbraco.Web/Models/Mapping/DataTypeModelMapper.cs index e3eb42897b..bee8624739 100644 --- a/src/Umbraco.Web/Models/Mapping/DataTypeModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/DataTypeModelMapper.cs @@ -1,8 +1,10 @@ -using AutoMapper; +using System; +using AutoMapper; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Models.Mapping; using Umbraco.Core.PropertyEditors; +using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; namespace Umbraco.Web.Models.Mapping @@ -11,6 +13,8 @@ namespace Umbraco.Web.Models.Mapping { public override void ConfigureMappings(IConfiguration config, ApplicationContext applicationContext) { + var lazyDataTypeService = new Lazy(() => applicationContext.Services.DataTypeService); + config.CreateMap() .ForMember(basic => basic.EditorId, expression => expression.MapFrom(editor => editor.Id)); @@ -18,7 +22,8 @@ namespace Umbraco.Web.Models.Mapping config.CreateMap() .ForMember(display => display.AvailableEditors, expression => expression.ResolveUsing()) - .ForMember(display => display.PreValues, expression => expression.ResolveUsing()) + .ForMember(display => display.PreValues, expression => expression.ResolveUsing( + new PreValueDisplayResolver(lazyDataTypeService))) .ForMember(display => display.SelectedEditor, expression => expression.MapFrom(definition => definition.ControlId)); } } diff --git a/src/Umbraco.Web/Models/Mapping/PreValueDisplayResolver.cs b/src/Umbraco.Web/Models/Mapping/PreValueDisplayResolver.cs index 5bef6c75bd..a52822fed7 100644 --- a/src/Umbraco.Web/Models/Mapping/PreValueDisplayResolver.cs +++ b/src/Umbraco.Web/Models/Mapping/PreValueDisplayResolver.cs @@ -2,14 +2,24 @@ using System.Collections.Generic; using System.Linq; using AutoMapper; +using Umbraco.Core; +using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.PropertyEditors; +using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; namespace Umbraco.Web.Models.Mapping { internal class PreValueDisplayResolver : ValueResolver> { + private readonly Lazy _dataTypeService; + + public PreValueDisplayResolver(Lazy dataTypeService) + { + _dataTypeService = dataTypeService; + } + protected override IEnumerable ResolveCore(IDataTypeDefinition source) { var propEd = PropertyEditorResolver.Current.GetById(source.ControlId); @@ -18,7 +28,41 @@ namespace Umbraco.Web.Models.Mapping throw new InvalidOperationException("Could not find property editor with id " + source.ControlId); } - return propEd.PreValueEditor.Fields.Select(Mapper.Map); + var dataTypeService = (DataTypeService) _dataTypeService.Value; + var preVals = dataTypeService.GetPreValuesCollectionByDataTypeId(source.Id); + var dictionaryVals = PreValueCollection.AsDictionary(preVals); + + var result = propEd.PreValueEditor.Fields.Select(Mapper.Map).ToArray(); + var currentIndex = 0; //used if the collection is non-dictionary based. + foreach (var field in result) + { + if (preVals.IsDictionaryBased == false) + { + //we'll just need to wire up the values based on the order that the pre-values are stored + var found = dictionaryVals.Any(x => x.Key.InvariantEquals(currentIndex.ToInvariantString())); + if (found == false) + { + LogHelper.Warn("Could not find persisted pre-value for index " + currentIndex); + continue; + } + field.Value = (string) dictionaryVals.Single(x => x.Key.InvariantEquals(currentIndex.ToInvariantString())).Value; + currentIndex++; + } + else + { + var found = dictionaryVals.Any(x => x.Key.InvariantEquals(field.Key)); + if (found == false) + { + LogHelper.Warn("Could not find persisted pre-value for field " + field.Key); + continue; + } + field.Value = (string)dictionaryVals.Single(x => x.Key.InvariantEquals(field.Key)).Value; + } + + + } + + return result; } } } \ No newline at end of file