Exemple #1
0
        /// <summary>
        /// Gets the entity by it's location (item Id).
        /// </summary>
        /// <typeparam name="T">The entity type.</typeparam>
        /// <param name="item">The data item.</param>
        /// <param name="mappingRuleName">The mapping rule.</param>
        /// <returns>The entity instance.</returns>
        public virtual T GetEntity <T>(Item item, string mappingRuleName)
        {
            Assert.ArgumentNotNull(item, "Container item");
            IMappingRule <T> mappingRule = Context.Entity.SmartResolve <IMappingRule <T> >(mappingRuleName);

            Assert.IsNotNull(mappingRule, "Mapping rule was nor found");

            if (mappingRule.MappingObject.Equals(default(T)))
            {
                object entity = Context.Entity.HasRegistration(typeof(T)) ? Context.Entity.Resolve(typeof(T)) : ReflectionUtil.CreateObject(typeof(T));
                Assert.IsNotNull(entity, "The instance of the entity is null");

                mappingRule.MappingObject = (T)entity;
            }

            if (mappingRule.MappingObject is IEntity)
            {
                ((IEntity)mappingRule.MappingObject).Alias = item.ID.ToString();
            }

            mappingRule = this.GetEntity(item, mappingRule) as IMappingRule <T>;
            Assert.IsNotNull(mappingRule, "The instance of the mapping rule is null");

            return(mappingRule.MappingObject);
        }
 public void WithRule(IMappingRule rule)
 {
     if (rule == null)
     {
         throw new ArgumentNullException("rule");
     }
     mappingRule = rule;
 }
Exemple #3
0
        /// <summary>
        /// Saves the entity.
        /// </summary>
        /// <typeparam name="T">Save the entity to data storage.</typeparam>
        /// <param name="value">The value.</param>
        /// <param name="item">The data item.</param>
        /// <param name="mappingRuleName">The mapping rule name.</param>
        public virtual void SaveEntity <T>(T value, Item item, string mappingRuleName)
        {
            Assert.ArgumentNotNull(value, "Input container ");
            Assert.ArgumentNotNull(item, "Input item");

            IMappingRule <T> mappingRule = Context.Entity.SmartResolve <IMappingRule <T> >(mappingRuleName);

            Assert.IsNotNull(mappingRule, "Mapping rule was nor found");

            mappingRule.MappingObject = value;

            this.SaveEntity(mappingRule, item);
        }
Exemple #4
0
 public void AddRule(string propertyName, IMappingRule rule)
 {
     _rules.Add(propertyName, rule);
 }
Exemple #5
0
        /// <summary>
        /// Gets the property value from the object by the selected fieldName
        /// </summary>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="obj">The object.</param>
        /// <returns>The property value from the object by the selected fieldName</returns>
        private static string GetPropertyValue(string fieldName, object obj)
        {
            if (!string.IsNullOrEmpty(fieldName))
            {
                if (obj is Order)
                {
                    IMappingRule <Order> rule = Context.Entity.Resolve <IMappingRule <Order> >("OrderMappingRule");
                    rule.MappingObject = (Order)obj;
                    obj = rule;
                }

                // If obj is of type Item, then retrieve fieldname.
                if (obj is Item)
                {
                    var item = (Item)obj;
                    return(BusinessCatalogUtil.GetTitleFromReferencedItemOrValue(item[fieldName]));
                }

                EntityHelper entityHelper = Context.Entity.Resolve <EntityHelper>();
                IDictionary <string, object> fieldsCollection = entityHelper.GetPropertiesValues(obj);

                if (fieldsCollection.Count > 0)
                {
                    string value = fieldsCollection.FirstOrDefault(p => p.Key.EndsWith(fieldName)).Value as string;
                    if (DateUtil.IsIsoDate(value))
                    {
                        DateTime date = DateUtil.IsoDateToDateTime(value);
                        return(DateUtil.FormatShortDateTime(date, Sitecore.Context.Culture));
                    }

                    if (!string.IsNullOrEmpty(value) && ID.IsID(value))
                    {
                        Item valueItem = Sitecore.Context.Database.GetItem(value) ?? Sitecore.Context.ContentDatabase.GetItem(value);
                        if (valueItem != null)
                        {
                            value = valueItem.Name;
                        }
                    }

                    return(value);
                }

                // Else retrieve from Property
                if (obj != null)
                {
                    PropertyInfo[] propertyInfos = obj.GetType().GetProperties();
                    foreach (PropertyInfo info in propertyInfos)
                    {
                        if (info.Name.Equals(fieldName))
                        {
                            object o     = info.GetValue(obj, null);
                            string value = o + string.Empty;

                            return(BusinessCatalogUtil.GetTitleFromReferencedItemOrValue(value));
                        }
                    }
                }
            }

            return(string.Empty);
        }
 public static void WithRule(IMappingRule rule)
 {
     Instance.WithRule(rule);
 }
 public ObjectMapperInstance()
 {
     mappingRule = new MatchingNameAndType();
 }