예제 #1
0
 public static void SetItemData(object target, Type targetType, Item item, bool isLazyLoad)
 {
     foreach (var property in targetType.GetProperties())
     {
         if (property == null || string.IsNullOrEmpty(property.Name) || !property.CanWrite)
         {
             continue;
         }
         SitecoreItemFieldAttribute attribute = (SitecoreItemFieldAttribute)property.GetCustomAttribute(_types[DataTypes.SitecoreItemFieldAttribute]);
         if (attribute == null)
         {
             SitecoreItemPropertyAttribute propAttribute = (SitecoreItemPropertyAttribute)property.GetCustomAttribute(_types[DataTypes.SitecoreItemPropertyAttribute]);
             if (propAttribute == null)
             {
                 SitecoreItemAttribute itemAttribute = (SitecoreItemAttribute)property.GetCustomAttribute(_types[DataTypes.SitecoreItemAttribute]);
                 if (itemAttribute != null)
                 {
                     property.SetValue(target, item);
                 }
             }
             else
             {
                 property.SetValue(target, GetSitecoreItemPropertyValue(property.PropertyType, item, propAttribute));
             }
         }
         else
         {
             property.SetValue(target, GetSitecoreItemFieldValue(property.PropertyType, item, attribute, isLazyLoad));
         }
     }
 }
예제 #2
0
        public static void SetFieldValueFromDatabase(object target, bool isLazyLoad)
        {
            List <PropertyInfo> properties       = target.GetType().GetProperties().ToList();
            PropertyInfo        uniqueIdProperty = properties.FirstOrDefault(property =>
            {
                IndexFieldAttribute attribute = property.GetCustomAttributes <IndexFieldAttribute>().FirstOrDefault(attr => attr.IndexFieldName == BuiltinFields.UniqueId);
                return(attribute != null);
            });

            if (uniqueIdProperty != null)
            {
                object uniqueIdValue = uniqueIdProperty.GetValue(target);
                if (uniqueIdValue.GetType() == typeof(ItemUri))
                {
                    ItemUri uri         = (ItemUri)uniqueIdValue;
                    Item    contextItem = null;
                    if (uri != null && (contextItem = Database.GetItem(uri)) != null)
                    {
                        properties.ForEach(property =>
                        {
                            if (property != null && property.GetValue(target) == null && !string.IsNullOrEmpty(property.Name) && property.CustomAttributes.Any() && property.CanWrite)
                            {
                                SitecoreItemFieldAttribute attribute        = property.GetCustomAttribute <SitecoreItemFieldAttribute>();
                                SitecoreItemPropertyAttribute propAttribute = property.GetCustomAttribute <SitecoreItemPropertyAttribute>();
                                SitecoreItemAttribute itemAttribute         = property.GetCustomAttribute <SitecoreItemAttribute>();
                                if (attribute != null)
                                {
                                    property.SetValue(target, GetSitecoreItemFieldValue(property.PropertyType, contextItem, attribute, isLazyLoad));
                                }
                                else if (propAttribute != null)
                                {
                                    property.SetValue(target, GetSitecoreItemPropertyValue(property.PropertyType, contextItem, propAttribute));
                                }
                                else if (itemAttribute != null)
                                {
                                    property.SetValue(target, contextItem);
                                }
                            }
                        });
                    }
                }
            }
        }
        public void GetValue_GetsClassUsingId()
        {
            //Assign
            SitecoreItemAttribute attr = new SitecoreItemAttribute();

            attr.Id     = "{66E62701-3FF2-492D-81A4-BD3E55428837}";
            attr.IsLazy = true;
            SitecoreProperty prop = new SitecoreProperty();

            prop.Attribute = attr;
            prop.Property  = new FakePropertyInfo(typeof(EmptyTemplate1));
            _handler.ConfigureDataHandler(prop);

            //Act
            EmptyTemplate1 result = _handler.GetValue(null, _service) as EmptyTemplate1;


            //Assert
            Assert.AreEqual(new Guid(attr.Id), result.Id);
        }
        public void ConfigureDataHandler_CorrectlyConfiguresWithPath()
        {
            //Assign
            SitecoreItemAttribute attr = new SitecoreItemAttribute();

            attr.Path   = "/stecore/content";
            attr.IsLazy = true;
            SitecoreProperty prop = new SitecoreProperty();

            prop.Attribute = attr;

            //Act
            _handler.ConfigureDataHandler(prop);

            //Assert
            Assert.AreEqual(Guid.Empty, _handler.Id);
            Assert.AreEqual(attr.IsLazy, _handler.IsLazy);
            Assert.AreEqual(prop, _handler.Property);
            Assert.AreEqual(attr.Path, _handler.Path);
        }
        public void ConfigureDataHandler_CorrectlyConfiguresWithID()
        {
            //Assign
            SitecoreItemAttribute attr = new SitecoreItemAttribute();

            attr.Id     = "{51C00CB9-E82F-4445-8B3A-F2E9A29B2876}";
            attr.IsLazy = true;
            SitecoreProperty prop = new SitecoreProperty();

            prop.Attribute = attr;

            //Act
            _handler.ConfigureDataHandler(prop);

            //Assert
            Assert.AreEqual(new Guid(attr.Id), _handler.Id);
            Assert.AreEqual(attr.IsLazy, _handler.IsLazy);
            Assert.AreEqual(prop, _handler.Property);
            Assert.IsTrue(_handler.Path.IsNullOrEmpty());
        }
        public void GetValue_GetsClassUsingPath()
        {
            //Assign
            SitecoreItemAttribute attr = new SitecoreItemAttribute();

            attr.Path   = "/sitecore/content/Data/SitecoreItemHandler/Item1";
            attr.IsLazy = true;
            SitecoreProperty prop = new SitecoreProperty();

            prop.Attribute = attr;
            prop.Property  = new FakePropertyInfo(typeof(EmptyTemplate1));


            _handler.ConfigureDataHandler(prop);

            //Act
            EmptyTemplate1 result = _handler.GetValue(null, _service) as EmptyTemplate1;


            //Assert
            Assert.AreEqual(attr.Path, result.Path);
        }