UmbracoPropertyConfiguration
Inheritance: Glass.Mapper.Configuration.FieldConfiguration
        /// <summary>
        /// Gets the property.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="config">The config.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override object GetProperty(Umbraco.Core.Models.Property property, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
        {
            if (property == null || property.Value == null)
                return null;

            var mediaService = new MediaService(new RepositoryFactory());
            int id;

            if (!int.TryParse(property.Value.ToString(), out id))
                return null;

            var file = mediaService.GetById(id);

            if (file != null)
            {
                int bytes;
                int.TryParse(file.Properties["umbracoBytes"].Value.ToString(), out bytes);

                var img = new File
                    {
                        Id = file.Id,
                        Name = file.Name,
                        Src = file.Properties["umbracoFile"].Value.ToString(),
                        Extension = file.Properties["umbracoExtension"].Value.ToString(),
                        Size = bytes
                    };
                return img;
            }

            return null;
        }
        /// <summary>
        /// Gets the property value.
        /// </summary>
        /// <param name="propertyValue">The property value.</param>
        /// <param name="config">The config.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override object GetPropertyValue(object propertyValue, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
        {
            Type type = config.PropertyInfo.PropertyType;
            //Get generic type
            Type pType = Utilities.GetGenericArgument(type);

            var list = Glass.Mapper.Utilities.CreateGenericType(typeof(List<>), new[] { pType }) as IList;

            if (propertyValue == null)
                return list;

            //The enumerator only works with piped lists
            IEnumerable<string> parts = propertyValue.ToString().Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            IEnumerable<object> items = parts.Select(x => Mapper.GetPropertyValue(x, Mapper.Configuration as UmbracoPropertyConfiguration, context)).ToArray();


            foreach (var item in items)
            {
                if (item != null)
                    if (list != null) list.Add(item);
            }

            return list;
        }
        /// <summary>
        /// Gets the property value.
        /// </summary>
        /// <param name="propertyValue">The value.</param>
        /// <param name="config">The config.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override object GetPropertyValue(object propertyValue, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
        {
            int id;
            if (!int.TryParse(propertyValue.ToString(), out id))
                return null;

            var item = context.Service.ContentService.GetById(id);
            return context.Service.CreateType(config.PropertyInfo.PropertyType, item, IsLazy, InferType);
        }
        /// <summary>
        /// Sets the property value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="config">The config.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        /// <exception cref="System.NullReferenceException">Could not find item to save value {0}</exception>
        public override object SetPropertyValue(object value, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
        {
            if (value == null)
                return string.Empty;
            
            var typeConfig = context.Service.GlassContext[value.GetType()] as UmbracoTypeConfiguration;

            var item = typeConfig.ResolveItem(value, context.Service.ContentService);
            if (item == null)
                throw new NullReferenceException("Could not find item to save value {0}");

            return item.Id;
        }
        /// <summary>
        /// Gets the property value.
        /// </summary>
        /// <param name="propertyValue">The property value.</param>
        /// <param name="config">The config.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override object GetPropertyValue(object propertyValue, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
        {
            Type enumType = config.PropertyInfo.PropertyType;

            int intValue;

            if (int.TryParse(propertyValue.ToString(), out intValue))
                return Enum.ToObject(enumType, intValue);
            
            if (Enum.IsDefined(enumType, propertyValue))
                return Enum.Parse(enumType, propertyValue.ToString(), true);
                
            throw new MapperException("Can not convert value {0} to enum type {1}".Formatted(propertyValue, enumType.FullName));
        }
        public void CanHandle_TypeIsHandledWithConfig_ReturnsTrue()
        {
            //Assign
            var config = new UmbracoPropertyConfiguration();
            var type1 = typeof (string);
            var mapper = new StubMapper(type1);

            config.PropertyInfo = typeof (Stub).GetProperty("Property");

            //Act
            var result = mapper.CanHandle(config, null);

            //Assert
            Assert.IsTrue(result);
        }
        public void SetProperty_PropertyString_ValueWrittenToProperty()
        {
            //Assign
            var expected = true;
            var content = _contentService.GetById(new Guid("{5928EFBB-6DF2-4BB6-A026-BF4938D7ED7A}"));
            var property = content.Properties[ContentTypeProperty];

            var mapper = new UmbracoPropertyBooleanMapper();
            var config = new UmbracoPropertyConfiguration();

            //Act
            mapper.SetProperty(property, expected, config, null);

            //Assert
            Assert.AreEqual(expected, property.Value);
        }
        /// <summary>
        /// Sets the property.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="value">The value.</param>
        /// <param name="config">The config.</param>
        /// <param name="context">The context.</param>
        public override void SetProperty(Umbraco.Core.Models.Property property, object value, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
        {
            throw new NotImplementedException();
           /* Image img = value as Image;
            var item = field.Item;

            if (field == null) return;

            ImageField scImg = new ImageField(field);

            if (img == null)
            {
                scImg.Clear();
                return;
            }

            if (scImg.MediaID.Guid != img.MediaId)
            {
                //this only handles empty guids, but do we need to remove the link before adding a new one?
                if (img.MediaId == Guid.Empty)
                {
                    ItemLink link = new ItemLink(item.Database.Name, item.ID, scImg.InnerField.ID, scImg.MediaItem.Database.Name, scImg.MediaID, scImg.MediaItem.Paths.Path);
                    scImg.RemoveLink(link);
                }
                else
                {
                    ID newId = new ID(img.MediaId);
                    Item target = item.Database.GetItem(newId);
                    if (target != null)
                    {
                        scImg.MediaID = newId;
                        ItemLink link = new ItemLink(item.Database.Name, item.ID, scImg.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath);
                        scImg.UpdateLink(link);
                    }
                    else throw new MapperException("No item with ID {0}. Can not update Media Item field".Formatted(newId));
                }
            }

            scImg.Height = img.Height.ToString();
            scImg.Width = img.Width.ToString();
            scImg.HSpace = img.HSpace.ToString();
            scImg.VSpace = img.VSpace.ToString();
            scImg.Alt = img.Alt;
            scImg.Border = img.Border;
            scImg.Class = img.Class;*/
        }
        public void SetProperty_PropertyString_ValueWrittenToProperty()
        {
            //Assign
            var contentTypeProperty = "TestProperty";
            var expected = "Test data";
            var content = _contentService.GetById(new Guid("{5F6D851E-46C0-40C7-A93A-EC3F6D7EBA3E}"));
            var property = content.Properties[contentTypeProperty];

            var mapper = new UmbracoPropertyStringMapper();
            var config = new UmbracoPropertyConfiguration();

            //Act
            mapper.SetProperty(property, expected, config, null);

            //Assert
            Assert.AreEqual(expected, property.Value);
        }
        public void GetProperty_PropertyContainsData_BooleanIsReturned()
        {
            //Assign
            var fieldValue = true;

            var content = _contentService.GetById(new Guid("{5928EFBB-6DF2-4BB6-A026-BF4938D7ED7A}"));
            var property = content.Properties[ContentTypeProperty];
            property.Value = fieldValue;

            var mapper = new UmbracoPropertyBooleanMapper();
            var config = new UmbracoPropertyConfiguration();
            
            //Act
            var result = (bool)mapper.GetProperty(property, config, null);

            //Assert
            Assert.AreEqual(fieldValue, result);
        }
        public void GetProperty_PropertyContainsData_StringIsReturned()
        {
            //Assign
            var fieldValue = "hello world";
            var contentTypeProperty = "TestProperty";
            
            var content = _contentService.GetById(new Guid("{5F6D851E-46C0-40C7-A93A-EC3F6D7EBA3E}"));
            var property = content.Properties[contentTypeProperty];
            property.Value = fieldValue;

            var mapper = new UmbracoPropertyStringMapper();
            var config = new UmbracoPropertyConfiguration();
            
            //Act
            var result = mapper.GetProperty(property, config, null) as string;

            //Assert
            Assert.AreEqual(fieldValue, result);
        }
        /// <summary>
        /// Called to map each property automatically
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        protected override AbstractPropertyConfiguration AutoMapProperty(System.Reflection.PropertyInfo property)
        {
            string          name = property.Name;
            UmbracoInfoType infoType;

            if (name.ToLowerInvariant() == "id")
            {
                UmbracoIdConfiguration idConfig = new UmbracoIdConfiguration();
                idConfig.PropertyInfo = property;
                return(idConfig);
            }

            if (name.ToLowerInvariant() == "parent")
            {
                UmbracoParentConfiguration parentConfig = new UmbracoParentConfiguration();
                parentConfig.PropertyInfo = property;
                return(parentConfig);
            }
            if (name.ToLowerInvariant() == "children")
            {
                UmbracoChildrenConfiguration childrenConfig = new UmbracoChildrenConfiguration();
                childrenConfig.PropertyInfo = property;
                return(childrenConfig);
            }

            if (Enum.TryParse(name, true, out infoType))
            {
                UmbracoInfoConfiguration infoConfig = new UmbracoInfoConfiguration();
                infoConfig.PropertyInfo = property;
                infoConfig.Type         = infoType;
                return(infoConfig);
            }

            UmbracoPropertyConfiguration fieldConfig = new UmbracoPropertyConfiguration();

            fieldConfig.PropertyAlias = name;
            fieldConfig.PropertyInfo  = property;
            return(fieldConfig);
        }
        /// <summary>
        /// Sets the Property value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="config">The config.</param>
        /// <param name="context">The context.</param>
        /// <returns>System.String.</returns>
        public override object SetPropertyValue(object value, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
        {
            var list = value as IEnumerable;

            if (list == null)
            {
                return string.Empty;
            }

            var sList = new List<string>();
            
            foreach (object obj in list)
            {
                string result = Mapper.SetPropertyValue(obj, config, context).ToString();
                if (!result.IsNullOrEmpty())
                    sList.Add(result);
            }

            if (sList.Any())
                return sList.Aggregate((x, y) => x + "," + y);
            
            return string.Empty;
        }
        /// <summary>
        /// Gets the property.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="config">The config.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override object GetProperty(Umbraco.Core.Models.Property property, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
        {
            if (property == null)
                return null;

            var mediaService = new MediaService(new RepositoryFactory());
            int id;

            if (!int.TryParse(property.Value.ToString(), out id))
                return null;

            var image = mediaService.GetById(id);

            if (image != null)
            {
                int width;
                int.TryParse(image.Properties["umbracoWidth"].Value.ToString(), out width);
                int height;
                int.TryParse(image.Properties["umbracoHeight"].Value.ToString(), out height);
                int bytes;
                int.TryParse(image.Properties["umbracoBytes"].Value.ToString(), out bytes);

                var img = new Image
                    {
                        Id = image.Id,
                        Alt = image.Name,
                        Src = image.Properties["umbracoFile"].Value.ToString(),
                        Width = width,
                        Height = height,
                        Extension = image.Properties["umbracoExtension"].Value.ToString(),
                        Size = bytes
                    };
                return img;
            }

            return null;
        }
 /// <summary>
 /// Gets the property value.
 /// </summary>
 /// <param name="propertyValue">The property value.</param>
 /// <param name="config">The config.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 public override object GetPropertyValue(object propertyValue, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
 {
     return propertyValue as string;
 }
        public void CanHandle_StringType_ReturnsTrue()
        {
            //Assign
            var mapper = new UmbracoPropertyStringMapper();
            var config = new UmbracoPropertyConfiguration();
            config.PropertyInfo = new FakePropertyInfo(typeof(String));

            //Act
            var result = mapper.CanHandle(config, null);

            //Assert
            Assert.IsTrue(result);
        }
 /// <summary>
 /// Gets the property value.
 /// </summary>
 /// <param name="propertyValue">The property value.</param>
 /// <param name="config">The config.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 public override object GetPropertyValue(object propertyValue, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
 {
     return propertyValue is decimal ? (decimal)propertyValue : 0;
 }
 /// <summary>
 /// Sets the property value.
 /// </summary>
 /// <param name="value">The value.</param>
 /// <param name="config">The config.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 public override object SetPropertyValue(object value, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
 {
     if (value is bool && (bool)value)
         return "1";
     return "0";
 }
 /// <summary>
 /// Gets the property value.
 /// </summary>
 /// <param name="propertyValue">The property value.</param>
 /// <param name="config">The config.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 public override object GetPropertyValue(object propertyValue, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
 {
     return propertyValue is DateTime ? (DateTime)propertyValue : new DateTime();
 }
 /// <summary>
 /// Gets the property value.
 /// </summary>
 /// <param name="propertyValue">The property value.</param>
 /// <param name="config">The config.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 public override object GetPropertyValue(object propertyValue, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
 {
     if (propertyValue != null && propertyValue.ToString() == "1")
         return true;
     return false;
 }
 public override void SetProperty(Property property, object value, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
 {
     property.Value = Value;
 }
        public void MapToCms_SetsValueByPropertyAlias_PropertyValueUpdated()
        {
            //Assign
            var propertyValue = "test value set";
            var propertyAlias = "Property";
            var content = _contentService.GetById(new Guid("{D2517065-2818-4AF7-B851-493E46EA79D5}"));

            var config = new UmbracoPropertyConfiguration();
            config.PropertyAlias = propertyAlias;
            config.PropertyInfo = typeof(Stub).GetProperty("Property");

            var mapper = new StubMapper(null);
            mapper.Setup(new DataMapperResolverArgs(null, config));
            mapper.Value = propertyValue;

            var context = new UmbracoDataMappingContext(new Stub(), content, null);

            content.Properties[propertyAlias].Value = string.Empty;

            //Act
            mapper.MapToCms(context);

            //Assert
            Assert.AreEqual(propertyValue, mapper.Value);
        }
 /// <summary>
 /// Sets the property value.
 /// </summary>
 /// <param name="value">The value.</param>
 /// <param name="config">The config.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 public override object SetPropertyValue(object value, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
 {
     return value;
 }
 /// <summary>
 /// Gets the property value.
 /// </summary>
 /// <param name="propertyValue">The property value.</param>
 /// <param name="config">The config.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 /// <exception cref="System.NotImplementedException"></exception>
 public override object GetPropertyValue(object propertyValue, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Gets the property value.
 /// </summary>
 /// <param name="propertyValue">The property value.</param>
 /// <param name="config">The config.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 public override object GetPropertyValue(object propertyValue, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
 {
     return propertyValue is Guid ? (Guid)propertyValue : new Guid();
 }
        public void MapToProperty_GetsValueByPropertyAlias_ReturnsPropertyValue()
        {
            //Assign
            var propertyValue = "test value set";
            var propertyAlias = "Property";
            var content = _contentService.GetById(new Guid("{D2517065-2818-4AF7-B851-493E46EA79D5}"));
           
            var config = new UmbracoPropertyConfiguration();
            config.PropertyAlias = propertyAlias;

            var mapper = new StubMapper(null);
            mapper.Setup(new DataMapperResolverArgs(null,config));
            mapper.Value = propertyValue;

            var context = new UmbracoDataMappingContext(null, content, null);

            content.Properties[propertyAlias].Value = propertyValue;

            //Act
            var result = mapper.MapToProperty(context);
            
            //Assert
            Assert.AreEqual(propertyValue, result);
        }
        /// <summary>
        /// Called to map each property automatically
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        protected override AbstractPropertyConfiguration AutoMapProperty(System.Reflection.PropertyInfo property)
        {
            string name = property.Name;
            UmbracoInfoType infoType;

            if (name.ToLowerInvariant() == "id")
            {
                UmbracoIdConfiguration idConfig = new UmbracoIdConfiguration();
                idConfig.PropertyInfo = property;
                return idConfig;
            }

            if (name.ToLowerInvariant() == "parent")
            {
                UmbracoParentConfiguration parentConfig = new UmbracoParentConfiguration();
                parentConfig.PropertyInfo = property;
                return parentConfig;
            }
            if (name.ToLowerInvariant() == "children")
            {
                UmbracoChildrenConfiguration childrenConfig = new UmbracoChildrenConfiguration();
                childrenConfig.PropertyInfo = property;
                return childrenConfig;
            }

            if (Enum.TryParse(name, true, out infoType))
            {
                UmbracoInfoConfiguration infoConfig = new UmbracoInfoConfiguration();
                infoConfig.PropertyInfo = property;
                infoConfig.Type = infoType;
                return infoConfig;
            }

            UmbracoPropertyConfiguration fieldConfig = new UmbracoPropertyConfiguration();
            fieldConfig.PropertyAlias = name;
            fieldConfig.PropertyInfo = property;
            return fieldConfig;
        }
 /// <summary>
 /// Sets the property value.
 /// </summary>
 /// <param name="value">The value.</param>
 /// <param name="config">The config.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 public override object SetPropertyValue(object value, UmbracoPropertyConfiguration config, UmbracoDataMappingContext context)
 {
     return Enum.GetName(config.PropertyInfo.PropertyType, value);
 }