Esempio n. 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));
         }
     }
 }
Esempio n. 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);
                                }
                            }
                        });
                    }
                }
            }
        }
Esempio n. 3
0
        private static object GetSitecoreItemFieldValue(Type propertyType, Item item, SitecoreItemFieldAttribute attribute, bool isLazyLoad)
        {
            if (propertyType == _types[DataTypes.String])
            {
                return(item.GetString(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.DateTime])
            {
                return(item.GetDateTime(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.Int])
            {
                return(item.GetInt(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.Double])
            {
                return(item.GetDouble(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.Bool])
            {
                return(item.GetBool(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.Item])
            {
                return(item.GetItem(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.ItemEnumerable])
            {
                return(item.GetItems(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.CustomField] || propertyType.IsSubclassOf(_types[DataTypes.CustomField]))
            {
                Field field = item.Fields[attribute.FieldId];
                if (field != null)
                {
                    return(ReflectionUtil.CreateObject(propertyType, new object[] { field }));
                }
            }
            // got to figure out a better way to do this
            if (propertyType == _types[DataTypes.SitecoreItem] || propertyType.IsSubclassOf(_types[DataTypes.SitecoreItem]))
            {
                Item fieldValueItem = item.GetItem(attribute.FieldId);
                if (fieldValueItem != null && ItemExtensions.IsItemValidForType(propertyType, fieldValueItem))
                {
                    return(TypeMapper.CreateObject(item.GetItem(attribute.FieldId), propertyType, isLazyLoad));
                }
            }
            // got to figure out a better way to do this

            if (_types[DataTypes.SitecoreItemEnumerable].IsAssignableFrom(propertyType))
            {
                Type[] types = propertyType.GetGenericArguments();
                if (!types.Any())
                {
                    throw new Exception("XCore needs at least one type argument");
                }
                if (types.Count() > 1)
                {
                    throw new Exception("XCore only supports one type argument");
                }
                Type  classType = types[0];
                Type  listType  = typeof(List <>).MakeGenericType(classType);
                IList list      = Activator.CreateInstance(listType) as IList;
                if (list == null)
                {
                    throw new Exception("XCore could not create a list of object type" + listType);
                }
                foreach (Item itm in item.GetItems(attribute.FieldId))
                {
                    if (itm == null || !ItemExtensions.IsItemValidForType(classType, itm))
                    {
                        continue;
                    }
                    object typeObject = TypeMapper.CreateObject(itm, classType, isLazyLoad);
                    if (typeObject != null)
                    {
                        list.Add(typeObject);
                    }
                }
                return(list);
            }
            return(null);
        }