Пример #1
0
        /// <summary>
        /// Maps a region to the given data entity.
        /// </summary>
        /// <typeparam name="T">The model type</typeparam>
        /// <param name="model">The model</param>
        /// <param name="page">The data entity</param>
        /// <param name="region">The region to map</param>
        /// <param name="regionType">The region type</param>
        /// <param name="regionId">The region id</param>
        /// <param name="sortOrder">The optional sort order</param>
        private void MapRegion <T>(T model, Data.Page page, object region, Extend.RegionType regionType, string regionId, int sortOrder = 0) where T : Models.Page <T>
        {
            // Now map all of the fields
            for (var n = 0; n < regionType.Fields.Count; n++)
            {
                var fieldDef  = regionType.Fields[n];
                var fieldType = App.Fields.GetByShorthand(fieldDef.Type);
                if (fieldType == null)
                {
                    fieldType = App.Fields.GetByType(fieldDef.Type);
                }

                if (fieldType != null)
                {
                    object fieldValue = null;
                    if (regionType.Fields.Count == 1)
                    {
                        // Get the field value for simple region
                        fieldValue = region; //GetSimpleValue(model, regionId);
                    }
                    else
                    {
                        // Get the field value for complex region
                        fieldValue = GetComplexValue(region, fieldDef.Id); // GetComplexValue(model, regionId, fieldDef.Id);
                    }

                    // Check that the returned value matches the type specified
                    // for the page type, otherwise deserialization won't work
                    // when the model is retrieved from the database.
                    if (fieldValue.GetType() != fieldType.Type)
                    {
                        throw new ArgumentException("Given page field value does not match the configured page type");
                    }

                    // Check if we have the current field in the database already
                    var field = page.Fields
                                .SingleOrDefault(f => f.RegionId == regionId && f.FieldId == fieldDef.Id && f.SortOrder == sortOrder);

                    // If not, create a new field
                    if (field == null)
                    {
                        field = new Data.PageField()
                        {
                            PageId   = page.Id,
                            RegionId = regionId,
                            FieldId  = fieldDef.Id
                        };
                        db.PageFields.Add(field);
                    }

                    // Update field info & value
                    field.CLRType   = fieldType.CLRType;
                    field.SortOrder = sortOrder;
                    field.Value     = Module.Serializer.Serialize(fieldValue);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Creates a region value.
        /// </summary>
        /// <param name="regionType">The region type</param>
        /// <param name="region">The region</param>
        /// <returns>The created value</returns>
        private object CreateRegion(Type regionType, Extend.RegionType region)
        {
            if (region.Fields.Count == 1)
            {
                var type = App.Fields.GetByShorthand(region.Fields[0].Type);
                if (type == null)
                {
                    type = App.Fields.GetByType(region.Fields[0].Type);
                }

                if (type != null && type.Type == regionType)
                {
                    return(Activator.CreateInstance(type.Type));
                }
                return(null);
            }
            else
            {
                var reg      = Activator.CreateInstance(regionType);
                var typeInfo = reg.GetType().GetTypeInfo();

                foreach (var field in region.Fields)
                {
                    var type = App.Fields.GetByShorthand(field.Type);
                    if (type == null)
                    {
                        type = App.Fields.GetByType(field.Type);
                    }

                    if (type != null)
                    {
                        var prop = typeInfo.GetProperty(field.Id, App.PropertyBindings);

                        if (prop != null && type.Type == prop.PropertyType)
                        {
                            prop.SetValue(reg, Activator.CreateInstance(type.Type));
                        }
                    }
                }
                return(reg);
            }
        }
Пример #3
0
        /// <summary>
        /// Creates a region value.
        /// </summary>
        /// <param name="region">The region type</param>
        /// <returns>The created value</returns>
        private object CreateRegion(Extend.RegionType region)
        {
            if (region.Fields.Count == 1)
            {
                var type = App.Fields.GetByShorthand(region.Fields[0].Type);
                if (type == null)
                {
                    type = App.Fields.GetByType(region.Fields[0].Type);
                }

                if (type != null)
                {
                    return(Activator.CreateInstance(type.Type));
                }
            }
            else
            {
                var reg = new ExpandoObject();

                foreach (var field in region.Fields)
                {
                    var type = App.Fields.GetByShorthand(field.Type);
                    if (type == null)
                    {
                        type = App.Fields.GetByType(field.Type);
                    }

                    if (type != null)
                    {
                        ((IDictionary <string, object>)reg).Add(field.Id, Activator.CreateInstance(type.Type));
                    }
                }
                return(reg);
            }
            return(null);
        }