Esempio n. 1
0
        // looks safer but probably useless... ppl should not call these methods directly
        // and if they do... they have to take care about not doing stupid things

        //public static PublishedPropertyType GetModelPropertyType2<T>(Expression<Func<T, object>> selector)
        //    where T : PublishedContentModel
        //{
        //    var type = typeof (T);
        //    var s1 = type.GetField("ModelTypeAlias", BindingFlags.Public | BindingFlags.Static);
        //    var alias = (s1.IsLiteral && s1.IsInitOnly && s1.FieldType == typeof(string)) ? (string)s1.GetValue(null) : null;
        //    var s2 = type.GetField("ModelItemType", BindingFlags.Public | BindingFlags.Static);
        //    var itemType = (s2.IsLiteral && s2.IsInitOnly && s2.FieldType == typeof(PublishedItemType)) ? (PublishedItemType)s2.GetValue(null) : 0;

        //    var contentType = PublishedContentType.Get(itemType, alias);
        //    // etc...
        //}

        public static PublishedPropertyType GetModelPropertyType <TModel, TValue>(PublishedContentType contentType, Expression <Func <TModel, TValue> > selector)
            where TModel : PublishedContentModel
        {
            var expr = selector.Body as MemberExpression;

            if (expr == null)
            {
                throw new ArgumentException("Not a property expression.", nameof(selector));
            }

            // there _is_ a risk that contentType and T do not match
            // see note above : accepted risk...

            var attr = expr.Member
                       .GetCustomAttributes(typeof(ImplementPropertyTypeAttribute), false)
                       .OfType <ImplementPropertyTypeAttribute>()
                       .SingleOrDefault();

            if (string.IsNullOrWhiteSpace(attr?.Alias))
            {
                throw new InvalidOperationException($"Could not figure out property alias for property \"{expr.Member.Name}\".");
            }

            return(contentType.GetPropertyType(attr.Alias));
        }
Esempio n. 2
0
        /// <summary>
        /// The data values as published properties.
        /// </summary>
        /// <param name="pvd">
        /// The <see cref="ProductVariantDetachedContentDisplay"/>.
        /// </param>
        /// <param name="contentType">
        /// The content type.
        /// </param>
        /// <returns>
        /// The <see cref="IEnumerable{IPublishedProperty}"/>.
        /// </returns>
        public static IEnumerable <IPublishedProperty> DataValuesAsPublishedProperties(this ProductVariantDetachedContentDisplay pvd, PublishedContentType contentType)
        {
            var properties = new List <IPublishedProperty>();

            foreach (var dcv in pvd.DetachedDataValues)
            {
                var    propType = contentType.GetPropertyType(dcv.Key);
                object valObj;
                try
                {
                    valObj = DetachedValuesConverter.Current.ConvertDbForContent(propType, dcv).Value;
                }
                catch
                {
                    valObj = dcv.Value;
                }

                if (propType != null)
                {
                    properties.Add(new DetachedPublishedProperty(propType, valObj));
                }
            }

            return(properties);
        }
        /// <summary>
        /// The data values as published properties.
        /// </summary>
        /// <param name="pvd">
        /// The <see cref="ProductVariantDetachedContentDisplay"/>.
        /// </param>
        /// <param name="contentType">
        /// The content type.
        /// </param>
        /// <returns>
        /// The <see cref="IEnumerable{IPublishedProperty}"/>.
        /// </returns>
        public static IEnumerable <IPublishedProperty> DataValuesAsPublishedProperties(this ProductVariantDetachedContentDisplay pvd, PublishedContentType contentType)
        {
            var properties = new List <IPublishedProperty>();

            foreach (var value in pvd.DetachedDataValues)
            {
                var    propType = contentType.GetPropertyType(value.Key);
                object valObj;
                try
                {
                    valObj = JsonConvert.DeserializeObject <object>(value.Value);
                }
                catch
                {
                    valObj = value.Value.Substring(1, value.Value.Length - 1);
                }

                if (propType != null)
                {
                    properties.Add(new DetachedPublishedProperty(propType, valObj));
                }
            }

            return(properties);
        }
        protected IPublishedContent ConvertDataToSource_DocType(PublishedPropertyType propertyType,
                                                                PublishedContentType contentType, object value, bool preview)
        {
            var properties = new List <IPublishedProperty>();

            // Convert all the properties
            var propValues = ((JObject)value).ToObject <Dictionary <string, object> >();

            foreach (var jProp in propValues)
            {
                var propType = contentType.GetPropertyType(jProp.Key);
                if (propType != null)
                {
                    var nestedPropType = propType.ExecuteMethod <PublishedPropertyType>("Nested",
                                                                                        propertyType);
                    var prop = typeof(PublishedProperty).ExecuteMethod <IPublishedProperty>("GetDetached",
                                                                                            nestedPropType,
                                                                                            (jProp.Value == null ? "" : jProp.Value.ToString()) as object,
                                                                                            preview);
                    properties.Add(prop);
                }
            }

            // Parse out the name manually
            //object nameObj = null;
            //if (propValues.TryGetValue("name", out nameObj))
            //{
            //	// Do nothing, we just want to parse out the name if we can
            //}

            return(new DetachedPublishedContent(contentType, properties.ToArray()));
        }
        public PublishedEmbeddedContent(IUserService userService,
                                        EmbeddedContentItem item,
                                        PublishedContentType contentType,
                                        PublishedContentSet <IPublishedContent> contentSet,
                                        int sortOrder,
                                        bool isPreview)
        {
            Name        = item.Name;
            Key         = item.Key;
            UpdateDate  = item.UpdateDate;
            CreateDate  = item.CreateDate;
            CreatorId   = item.CreatorId;
            WriterId    = item.WriterId;
            IsDraft     = isPreview;
            SortOrder   = sortOrder;
            ContentType = contentType;
            ContentSet  = contentSet;

            _writerName  = new Lazy <string>(() => userService.GetByProviderKey(WriterId).Name);
            _creatorName = new Lazy <string>(() => userService.GetByProviderKey(CreatorId).Name);

            _properties = (from property in item.Properties
                           let propType = contentType.GetPropertyType(property.Key)
                                          where propType != null
                                          select new PublishedEmbeddedContentProperty(propType, property.Value, isPreview)
                           ).ToList <IPublishedProperty>();
        }
        private static void AddIf(PublishedContentType contentType, IDictionary <string, PropertyData[]> properties, string alias, object value)
        {
            var propertyType = contentType.GetPropertyType(alias);

            if (propertyType == null || propertyType.IsUserProperty)
            {
                return;
            }
            properties[alias] = new[] { new PropertyData {
                                            Value = value, Culture = string.Empty, Segment = string.Empty
                                        } };
        }
        private object ConvertProperty(IPublishedProperty property, IPublishedContent owner, PublishedContentType contentType)
        {
            string editorAlias = contentType.GetPropertyType(property.PropertyTypeAlias).PropertyEditorAlias.ToLowerInvariant();

            // Property converter exist
            if (SerializerManager.PropertyConverters.ContainsKey(editorAlias))
            {
                return(SerializerManager.PropertyConverters[editorAlias].Convert(property, owner, this, this._umbraco));
            }

            return(property.Value);
        }
Esempio n. 8
0
        public VariantPublishedContent(Variant variant, PublishedContentType contentType)
        {
            _variantIdentifier = variant.Id;
            _contentType       = contentType;

            _combination = variant.Combination;
            Validation   = variant.Validation;

            _properties = new List <IPublishedProperty>();

            foreach (string key in variant.Properties.Keys)
            {
                _properties.Add(new VariantPublishedProperty(contentType.GetPropertyType(key), variant.Properties[key]));
            }
        }
Esempio n. 9
0
        private MimicGeneratedClass GenerateClass(IContentType documentType, IEnumerable <PropertyGroup> properties, string className)
        {
            var root = new MimicGeneratedClass();

            root.ClassName = className;

            //var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, documentType.Alias);
            //PublishedContentType.
            //Current.PublishedContentTypeFactory

            IPublishedContentType PublishedContentType = new PublishedContentType(documentType, Current.PublishedContentTypeFactory);

            //var publishedContentType = PublishedContentType.GetPropertyType(documentType.Alias);

            foreach (var propertyGroup in properties)
            {
                foreach (var propertyType in propertyGroup.PropertyTypes.OrderBy(pt => pt.Name))
                {
                    var publishedPropertyType = PublishedContentType.GetPropertyType(propertyType.Alias);

                    if (publishedPropertyType == null)
                    {
                        throw new Exception($"Chill: could not get published property type {documentType.Alias}.{propertyType.Alias}.");
                    }

                    var propertyModelClrType = publishedPropertyType.ClrType;

                    var _propertyType = ResolvePropertyType(publishedPropertyType);
                    var _propertyName = char.ToUpper(propertyType.Alias[0]) + propertyType.Alias.Substring(1);


                    if (_propertyType.Contains("IPublishedContent"))
                    {
                        var preValue = GetDataTypePreValue(propertyType.DataTypeId, "filter");

                        //var preValues = ApplicationContext.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(propertyType.DataTypeDefinitionId);
                        //var filters = preValues.PreValuesAsDictionary.FirstOrDefault(x => x.Key == "filter");

                        if (preValue != null)
                        {
                            if (!string.IsNullOrEmpty(preValue) && !preValue.Contains(','))
                            {
                                var mappedClass = char.ToUpper(preValue[0]) + preValue.Substring(1);
                                _propertyType = _propertyType.Replace("IPublishedContent", mappedClass);
                            }
                        }
                        else
                        {
                            //var contentTypes = preValues.PreValuesAsDictionary.FirstOrDefault(x => x.Key == "contentTypes");
                            var contentTypes = GetDataTypePreValue(propertyType.DataTypeId, "contentTypes");
                            if (contentTypes != null)
                            {
                                if (!string.IsNullOrEmpty(preValue))
                                {
                                    preValue = preValue.Trim('[').Trim(']');
                                    var nestedContent = JsonConvert.DeserializeObject <MimicNestedContent>(preValue);
                                    if (!string.IsNullOrEmpty(nestedContent.ncAlias) && !nestedContent.ncAlias.Contains(','))
                                    {
                                        var mappedClass = char.ToUpper(nestedContent.ncAlias[0]) + nestedContent.ncAlias.Substring(1);
                                        _propertyType = _propertyType.Replace("IPublishedContent", mappedClass);
                                    }
                                }
                            }
                        }
                    }

                    if (_propertyName == className)
                    {
                        _propertyName = "_" + _propertyName;
                    }

                    root.PropertyTypes.Add(new MimicPropertyType
                    {
                        PropertyGroup = GetSafeName(propertyGroup.Name),
                        Name          = _propertyName,
                        PropertyType  = _propertyType,
                        IsMandatory   = propertyType.Mandatory
                    });
                }
            }

            return(root);
        }