/// <summary> /// Sets up the data mapper for a particular property /// </summary> /// <param name="args"></param> public override void Setup(DataMapperResolverArgs args) { var config = args.PropertyConfiguration as UmbracoPropertyConfiguration; IsLazy = (config.Setting & UmbracoPropertySettings.DontLoadLazily) != UmbracoPropertySettings.DontLoadLazily; InferType = (config.Setting & UmbracoPropertySettings.InferType) == UmbracoPropertySettings.InferType; base.Setup(args); }
public override void Execute(DataMapperResolverArgs args) { if (args.Result != null) { var fieldMapper = args.Result as AbstractSitecoreFieldMapper; if (fieldMapper != null) { var scConfig = args.Result.Configuration as SitecoreFieldConfiguration; scConfig.FieldName = GetFieldName(scConfig.FieldName); } } base.Execute(args); }
public void Execute_NoDataMapperAttribute_NoMatchingMapper_ThrowsMapperException() { //Assign var configuration = Substitute.For<AbstractPropertyConfiguration>(); configuration.PropertyInfo = typeof (StubClass).GetProperty("NoAttributeProperty"); var args = new DataMapperResolverArgs(null, configuration); args.DataMappers = Enumerable.Empty<AbstractDataMapper>(); //Act Assert.Throws<MapperException>(() => _task.Execute(args)); //Assert }
public override void Setup(DataMapperResolverArgs args) { if (args.PropertyConfiguration.PropertyInfo.PropertyType == typeof(Guid)) _getValue = (item) => item.ID.Guid; else if (args.PropertyConfiguration.PropertyInfo.PropertyType == typeof(ID)) _getValue = (item) => item.ID; else { throw new NotSupportedException("The type {0} on {0}.{1} is not supported by SitecoreIdMapper".Formatted (args.PropertyConfiguration.PropertyInfo.ReflectedType.FullName, args.PropertyConfiguration.PropertyInfo.Name)); } base.Setup(args); }
public void Execute_DataMapperAttribute_SetsResultToSpecifiedMapper() { //Assign var task = new DataMapperAttributeResolverTask(); var configuration = Substitute.For<AbstractPropertyConfiguration>(); configuration.PropertyInfo = typeof(StubClass).GetProperty("StubMapperProperty"); var args = new DataMapperResolverArgs(null, configuration); args.DataMappers = Enumerable.Empty<AbstractDataMapper>(); //Act task.Execute(args); //Assert Assert.IsNotNull(args.Result); Assert.IsTrue(args.Result.GetType() == typeof(StubMapper)); }
public void Execute_InvokesMapperSetup() { //Assign var configuration = Substitute.For<AbstractPropertyConfiguration>(); configuration.PropertyInfo = typeof (StubClass).GetProperty("NoAttributeProperty"); var mapper = Substitute.For<AbstractDataMapper>(); mapper.CanHandle(configuration, null).Returns(true); var args = new DataMapperResolverArgs(null, configuration); args.DataMappers = new List<AbstractDataMapper> { mapper }; //Act _task.Execute(args); //Assert mapper.Received().Setup(args); }
public void Execute_NoDataMapperAttribute_SetsResultToMatchingRegisteredMapper() { //Assign var configuration = Substitute.For<AbstractPropertyConfiguration>(); configuration.PropertyInfo = typeof (StubClass).GetProperty("NoAttributeProperty"); var mapper = Substitute.For<AbstractDataMapper>(); mapper.CanHandle(configuration, null).Returns(true); var args = new DataMapperResolverArgs(null, configuration); args.DataMappers = new List<AbstractDataMapper> { mapper }; //Act _task.Execute(args); //Assert Assert.IsNotNull(args.Result); Assert.AreEqual(mapper, args.Result); }
public void Execute_DataMapperAttributeMapperMissingConstructor_SetsResultToSpecifiedMapper() { //Assign var task = new DataMapperAttributeResolverTask(); var configuration = Substitute.For<AbstractPropertyConfiguration>(); configuration.PropertyInfo = typeof(StubClass).GetProperty("MissingConstructorStubMapperProperty"); var args = new DataMapperResolverArgs(null, configuration); args.DataMappers = Enumerable.Empty<AbstractDataMapper>(); //Act Assert.Throws<NotSupportedException>(() => { task.Execute(args); }); //Assert }
public override void Setup(DataMapperResolverArgs args) { base.Setup(args); var property = args.PropertyConfiguration.PropertyInfo; InnerType = Glass.Mapper.Utilities.GetGenericArgument(property.PropertyType); var configCopy = Configuration.Copy(); configCopy.PropertyInfo = new FakePropertyInfo(InnerType, property.Name, property.DeclaringType); Mapper = args.DataMappers.FirstOrDefault(x => x.CanHandle(configCopy, args.Context) ); if (Mapper == null) throw new MapperException( "No mapper to handle type {0} on property {1} class {2}".Formatted(InnerType.FullName, property.Name, property.ReflectedType.FullName)); Mapper.Setup(new DataMapperResolverArgs(args.Context, configCopy)); }
public void Execute_DataMapperAttributeLoadedFromMapperCollection_SetsResultToSpecifiedMapper() { //Assign var task = new DataMapperAttributeResolverTask(); var configuration = Substitute.For<AbstractPropertyConfiguration>(); configuration.PropertyInfo = typeof(StubClass).GetProperty("StubMapperProperty"); var mapper=new StubMapper(); var args = new DataMapperResolverArgs(null, configuration); args.DataMappers = new AbstractDataMapper[] {mapper}; //Act task.Execute(args); //Assert Assert.IsNotNull(args.Result); Assert.AreEqual(mapper, args.Result); }
/// <summary> /// Sets up the data mapper for a particular property /// </summary> /// <param name="args"></param> public override void Setup(DataMapperResolverArgs args) { var config = args.PropertyConfiguration as UmbracoInfoConfiguration; this.ReadOnly = config.Type != UmbracoInfoType.Name; base.Setup(args); }
/// <summary> /// Sets up the data mapper for a particular property /// </summary> /// <param name="args">The args.</param> /// <exception cref="Glass.Mapper.MapperException">No mapper to handle type {0} on property {1} class {2}.Formatted(type.FullName, property.Name, /// property.ReflectedType.FullName)</exception> public override void Setup(DataMapperResolverArgs args) { base.Setup(args); var fieldConfiguration = this.Configuration as SitecoreFieldConfiguration; PropertyInfo propertyInfo = args.PropertyConfiguration.PropertyInfo; Type[] genericArguments = propertyInfo.PropertyType.GetGenericArguments(); this.KeyMapper = this.GetMapper(genericArguments[0], fieldConfiguration, propertyInfo, args); this.ValueMapper = this.GetMapper(genericArguments[1], fieldConfiguration, propertyInfo, args); }
private AbstractSitecoreFieldMapper GetMapper(Type genericArgument, SitecoreFieldConfiguration fieldConfiguration, PropertyInfo propertyInfo, DataMapperResolverArgs args) { AbstractPropertyConfiguration configCopy = fieldConfiguration.Copy(); configCopy.PropertyInfo = new FakePropertyInfo(genericArgument, propertyInfo.Name, propertyInfo.DeclaringType); var mapper = args.DataMappers.FirstOrDefault( x => x.CanHandle(configCopy, args.Context) && x is AbstractSitecoreFieldMapper) as AbstractSitecoreFieldMapper; if (mapper == null) { throw new MapperException(Glass.Mapper.ExtensionMethods.Formatted( "No mapper to handle type {0} on property {1} class {2}", (object)genericArgument.FullName, (object)propertyInfo.Name, (object)propertyInfo.ReflectedType.FullName)); } else { mapper.Setup(new DataMapperResolverArgs(args.Context, configCopy)); } return mapper; }
public void SetField_ListContainsNoValues_SetsEmptyField() { //Assign var expected = string.Empty; var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldIEnumerableMapper/SetField"); var field = item.Fields[FieldName]; var value = new List<double>(); var config = new SitecoreFieldConfiguration(); config.PropertyInfo = typeof(StubClass).GetProperty("IList"); var mapper = new SitecoreFieldIEnumerableMapper(); var subMapper = new SitecoreFieldDoubleMapper(); var args = new DataMapperResolverArgs(null, config); args.DataMappers = new[] { subMapper }; mapper.Setup(args); using (new ItemEditing(item, true)) { field.Value = string.Empty; } //Act using (new ItemEditing(item, true)) { mapper.SetField(field, value, config, null); } //Assert Assert.AreEqual(expected, field.Value); }
public void Setup_SubMapperIsAssigned() { //Assign var config = new SitecoreFieldConfiguration(); config.PropertyInfo = typeof(StubClass).GetProperty("IList"); var mapper = new SitecoreFieldIEnumerableMapper(); var subMapper = new SitecoreFieldDoubleMapper(); var args = new DataMapperResolverArgs(null, config); args.DataMappers = new[] {subMapper}; //Act mapper.Setup(args); //Assert Assert.AreEqual(subMapper, mapper.Mapper); }
public override void Setup(DataMapperResolverArgs args) { base.Setup(args); GenericType = Mapper.Utilities.GetGenericArgument(Configuration.PropertyInfo.PropertyType); }
public void GetField_EmptyField_ReturnsEmptyList() { //Assign var fieldValue = ""; var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldIEnumerableMapper/GetField"); var field = item.Fields[FieldName]; var config = new SitecoreFieldConfiguration(); config.PropertyInfo = typeof(StubClass).GetProperty("IList"); var mapper = new SitecoreFieldIEnumerableMapper(); var subMapper = new SitecoreFieldDoubleMapper(); var args = new DataMapperResolverArgs(null, config); args.DataMappers = new[] { subMapper }; mapper.Setup(args); using (new ItemEditing(item, true)) { field.Value = fieldValue; } //Act var result = mapper.GetField(field, config, null) as List<double>; //Assert Assert.AreEqual(0, result.Count); }
public override void Setup(DataMapperResolverArgs args) { throw new NotImplementedException(); }
/// <summary> /// Sets up the data mapper for a particular property /// </summary> /// <param name="args">The args.</param> /// <exception cref="Glass.Mapper.MapperException">No mapper to handle type {0} on property {1} class {2}.Formatted(type.FullName, property.Name, /// property.ReflectedType.FullName)</exception> public override void Setup(DataMapperResolverArgs args) { base.Setup(args); var scConfig = Configuration as SitecoreFieldConfiguration; var property = args.PropertyConfiguration.PropertyInfo; var type = Utilities.GetGenericArgument(property.PropertyType); var configCopy = scConfig.Copy(); configCopy.PropertyInfo = new FakePropertyInfo(type, property.Name); Mapper = args.DataMappers.FirstOrDefault( x => x.CanHandle(configCopy, args.Context) && x is AbstractSitecoreFieldMapper) as AbstractSitecoreFieldMapper; if (Mapper == null) throw new MapperException( "No mapper to handle type {0} on property {1} class {2}".Formatted(type.FullName, property.Name, property.ReflectedType.FullName)); Mapper.Setup(new DataMapperResolverArgs(args.Context, configCopy)); }
/// <summary> /// Sets up the data mapper for a particular property /// </summary> /// <param name="args">The args.</param> public override void Setup(DataMapperResolverArgs args) { var scConfig = args.PropertyConfiguration as SitecoreInfoConfiguration; if (scConfig == null) { throw new NullReferenceException("Configuration has not been set."); } ReadOnly = scConfig.Type != SitecoreInfoType.DisplayName && scConfig.Type != SitecoreInfoType.Name; base.Setup(args); }
/// <summary> /// Sets up the data mapper for a particular property /// </summary> /// <param name="args">The args.</param> public override void Setup(DataMapperResolverArgs args) { var scConfig = args.PropertyConfiguration as SitecoreInfoConfiguration; if (scConfig == null) { throw new NullReferenceException("Configuration has not been set."); } ReadOnly = scConfig.Type != SitecoreInfoType.DisplayName && scConfig.Type != SitecoreInfoType.Name; switch (scConfig.Type) { case SitecoreInfoType.ContentPath: _getValue = item => item.Paths.ContentPath; break; case SitecoreInfoType.DisplayName: _getValue = item => item[Global.Fields.DisplayName]; break; case SitecoreInfoType.FullPath: _getValue = item => item.Paths.FullPath; break; case SitecoreInfoType.Name: _getValue = item => item.Name; break; case SitecoreInfoType.Key: _getValue = item => item.Key; break; case SitecoreInfoType.MediaUrl: _getValue = item => { var mediaUrlOptions = _mediaUrlOptionsResolver.GetMediaUrlOptions(scConfig.MediaUrlOptions); var media = new MediaItem(item); return MediaManager.GetMediaUrl(media, mediaUrlOptions); }; break; case SitecoreInfoType.Path: _getValue = item => item.Paths.Path; break; case SitecoreInfoType.TemplateId: _getValue = item => { if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(ID)) return item.TemplateID; return item.TemplateID.Guid; }; break; case SitecoreInfoType.TemplateName: _getValue = item => item.TemplateName; break; case SitecoreInfoType.Url: _getValue = item => { var urlOptions = _urlOptionsResolver.CreateUrlOptions(scConfig.UrlOptions); if (scConfig.UrlOptions == SitecoreInfoUrlOptions.UseItemLanguage) { urlOptions.Language = item.Language; } else { urlOptions.Language = null; } return LinkManager.GetItemUrl(item, urlOptions); }; break; case SitecoreInfoType.Version: _getValue = item => { if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(string)) { return item.Version.Number.ToString(); } return item.Version.Number; }; break; case SitecoreInfoType.Language: _getValue = item => { if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(string)) { return item.Language.Name; } return item.Language; }; break; case SitecoreInfoType.BaseTemplateIds: _getValue = item => { Template template = TemplateManager.GetTemplate(item.TemplateID, item.Database); if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(IEnumerable<ID>)) return template.GetBaseTemplates().Select(x => x.ID); return template.GetBaseTemplates().Select(x => x.ID.Guid); }; break; case SitecoreInfoType.ItemUri: _getValue = item => new ItemUri(item.ID, item.Language, item.Version, item.Database); break; #if (SC81|| SC82) case SitecoreInfoType.OriginalLanguage: _getValue = item => item.OriginalLanguage; break; case SitecoreInfoType.OriginatorId: _getValue = item => item.OriginatorId; break; #endif default: throw new MapperException("SitecoreInfoType {0} not supported".Formatted(scConfig.Type)); } base.Setup(args); }
/// <summary> /// Processes the properties. /// </summary> /// <param name="properties">The properties.</param> /// <exception cref="System.NullReferenceException">Could not find data mapper for property {0} on type {1} /// .Formatted(property.PropertyInfo.Name,property.PropertyInfo.ReflectedType.FullName)</exception> private void ProcessProperties(IEnumerable<AbstractPropertyConfiguration> properties ) { DataMapperResolver runner = new DataMapperResolver(DependencyResolver.ResolveAll<IDataMapperResolverTask>()); foreach(var property in properties) { DataMapperResolverArgs args = new DataMapperResolverArgs(this, property); args.PropertyConfiguration = property; args.DataMappers = DependencyResolver.ResolveAll<AbstractDataMapper>(); runner.Run(args); if(args.Result == null) { throw new NullReferenceException( "Could not find data mapper for property {0} on type {1}" .Formatted(property.PropertyInfo.Name,property.PropertyInfo.ReflectedType.FullName)); } property.Mapper = args.Result; } }
/// <summary> /// Sets up the data mapper for a particular property /// </summary> /// <param name="args">The args.</param> public override void Setup(DataMapperResolverArgs args) { var scConfig = args.PropertyConfiguration as SitecoreFieldConfiguration; IsLazy = (scConfig.Setting & SitecoreFieldSettings.DontLoadLazily) != SitecoreFieldSettings.DontLoadLazily; InferType = (scConfig.Setting & SitecoreFieldSettings.InferType) == SitecoreFieldSettings.InferType; base.Setup(args); }
public void SetField_ListContainsNoValues_SetsEmptyField() { //Assign var templateId = ID.NewID; var fieldId = ID.NewID; var targetId = ID.NewID; using (Db database = new Db { new DbTemplate(templateId) { {"Field", ""} }, new Sitecore.FakeDb.DbItem("Target", targetId, templateId), }) { var expected = string.Empty; var item = database.GetItem("/sitecore/content/Target"); var field = item.Fields["Field"]; var value = new List<double>(); var config = new SitecoreFieldConfiguration(); config.PropertyInfo = typeof(StubClass).GetProperty("IList"); var mapper = new SitecoreFieldIEnumerableMapper(); var subMapper = new SitecoreFieldDoubleMapper(); var args = new DataMapperResolverArgs(null, config); args.DataMappers = new[] {subMapper}; mapper.Setup(args); using (new ItemEditing(item, true)) { field.Value = string.Empty; } //Act using (new ItemEditing(item, true)) { mapper.SetField(field, value, config, null); } //Assert Assert.AreEqual(expected, field.Value); } }
public void GetField_EmptyField_ReturnsEmptyList() { //Assign var templateId = ID.NewID; var fieldId = ID.NewID; var targetId = ID.NewID; using (Db database = new Db { new DbTemplate(templateId) { {"Field", ""} }, new Sitecore.FakeDb.DbItem("Target", targetId, templateId), }) { var fieldValue = ""; var item = database.GetItem("/sitecore/content/Target"); var field = item.Fields["Field"]; var config = new SitecoreFieldConfiguration(); config.PropertyInfo = typeof(StubClass).GetProperty("IList"); var mapper = new SitecoreFieldIEnumerableMapper(); var subMapper = new SitecoreFieldDoubleMapper(); var args = new DataMapperResolverArgs(null, config); args.DataMappers = new[] {subMapper}; mapper.Setup(args); using (new ItemEditing(item, true)) { field.Value = fieldValue; } //Act var result = mapper.GetField(field, config, null) as List<double>; //Assert Assert.AreEqual(0, result.Count); } }
public void Setup_SubMapperMissing_ExceptionThrown() { //Assign var config = new SitecoreFieldConfiguration(); config.PropertyInfo = typeof(StubClass).GetProperty("IEnumerable"); var mapper = new SitecoreFieldIEnumerableMapper(); var subMapper = new SitecoreFieldDoubleMapper(); var args = new DataMapperResolverArgs(null, config); args.DataMappers = new[] { subMapper }; //Act Assert.Throws<MapperException>(() => { mapper.Setup(args); }); //Assert }
/// <summary> /// Sets up the data mapper for a particular property /// </summary> /// <param name="args">The args.</param> public override void Setup(DataMapperResolverArgs args) { var scConfig = args.PropertyConfiguration as SitecoreInfoConfiguration; this.ReadOnly = scConfig.Type != SitecoreInfoType.DisplayName && scConfig.Type != SitecoreInfoType.Name; base.Setup(args); }
/// <summary> /// Sets up the data mapper for a particular property /// </summary> /// <param name="args">The args.</param> public virtual void Setup(DataMapperResolverArgs args) { Configuration = args.PropertyConfiguration; }