示例#1
0
        public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
        {
            var valueType = ConfigurationEditor.ConfigurationAs <LabelConfiguration>(propertyType.DataType.Configuration);

            switch (valueType.ValueType)
            {
            case ValueTypes.DateTime:
            case ValueTypes.Date:
                return(typeof(DateTime));

            case ValueTypes.Time:
                return(typeof(TimeSpan));

            case ValueTypes.Decimal:
                return(typeof(decimal));

            case ValueTypes.Integer:
                return(typeof(int));

            case ValueTypes.Bigint:
                return(typeof(long));

            default:     // everything else is a string
                return(typeof(string));
            }
        }
示例#2
0
        public void Can_Perform_Add_On_DataTypeDefinitionRepository()
        {
            using (ScopeProvider.CreateScope())
            {
                var dataTypeDefinition = new DataType(new LabelPropertyEditor(DataValueEditorFactory, IOHelper), ConfigurationEditorJsonSerializer)
                {
                    DatabaseType  = ValueStorageType.Integer,
                    Name          = "AgeDataType",
                    CreatorId     = 0,
                    Configuration = new LabelConfiguration {
                        ValueType = ValueTypes.Xml
                    }
                };

                // Act
                DataTypeRepository.Save(dataTypeDefinition);

                bool      exists  = DataTypeRepository.Exists(dataTypeDefinition.Id);
                IDataType fetched = DataTypeRepository.Get(dataTypeDefinition.Id);

                // Assert
                Assert.That(dataTypeDefinition.HasIdentity, Is.True);
                Assert.That(exists, Is.True);

                // cannot compare 'configuration' as it's two different objects
                TestHelper.AssertPropertyValuesAreEqual(dataTypeDefinition, fetched, ignoreProperties: new[] { "Configuration" });

                // still, can compare explicitely
                Assert.IsNotNull(dataTypeDefinition.Configuration);
                Assert.IsInstanceOf <LabelConfiguration>(dataTypeDefinition.Configuration);
                Assert.IsNotNull(fetched.Configuration);
                Assert.IsInstanceOf <LabelConfiguration>(fetched.Configuration);
                Assert.AreEqual(ConfigurationEditor.ConfigurationAs <LabelConfiguration>(dataTypeDefinition.Configuration).ValueType, ConfigurationEditor.ConfigurationAs <LabelConfiguration>(fetched.Configuration).ValueType);
            }
        }
示例#3
0
        // gets the tag configuration for a property
        // from the datatype configuration, and the editor tag configuration attribute
        public static TagConfiguration GetTagConfiguration(this IProperty property, PropertyEditorCollection propertyEditors, IDataTypeService dataTypeService)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            var editor       = propertyEditors[property.PropertyType.PropertyEditorAlias];
            var tagAttribute = editor.GetTagAttribute();

            if (tagAttribute == null)
            {
                return(null);
            }

            var configurationObject = dataTypeService.GetDataType(property.PropertyType.DataTypeId).Configuration;
            var configuration       = ConfigurationEditor.ConfigurationAs <TagConfiguration>(configurationObject);

            if (configuration.Delimiter == default)
            {
                configuration.Delimiter = tagAttribute.Delimiter;
            }

            return(configuration);
        }
    // gets the tag configuration for a property
    // from the datatype configuration, and the editor tag configuration attribute
    public static TagConfiguration?GetTagConfiguration(this IProperty property, PropertyEditorCollection propertyEditors, IDataTypeService dataTypeService)
    {
        if (property == null)
        {
            throw new ArgumentNullException(nameof(property));
        }

        IDataEditor?editor = propertyEditors[property.PropertyType?.PropertyEditorAlias];
        TagsPropertyEditorAttribute?tagAttribute = editor?.GetTagAttribute();

        if (tagAttribute == null)
        {
            return(null);
        }

        var configurationObject = property.PropertyType is null
            ? null
            : dataTypeService.GetDataType(property.PropertyType.DataTypeId)?.Configuration;
        TagConfiguration?configuration = ConfigurationEditor.ConfigurationAs <TagConfiguration>(configurationObject);

        if (configuration?.Delimiter == default && configuration?.Delimiter is not null)
        {
            configuration.Delimiter = tagAttribute.Delimiter;
        }

        return(configuration);
    }
示例#5
0
    private bool IsMultipleDataType(PublishedDataType dataType)
    {
        MediaPickerConfiguration?config =
            ConfigurationEditor.ConfigurationAs <MediaPickerConfiguration>(dataType.Configuration);

        return(config?.Multiple ?? false);
    }
        public void Can_Perform_Add_On_DataTypeDefinitionRepository()
        {
            var provider = TestObjects.GetScopeProvider(Logger);

            using (provider.CreateScope())
            {
                var repository         = CreateRepository();
                var dataTypeDefinition = new DataType(new LabelPropertyEditor(Logger))
                {
                    DatabaseType  = ValueStorageType.Integer,
                    Name          = "AgeDataType",
                    CreatorId     = 0,
                    Configuration = new LabelConfiguration {
                        ValueType = ValueTypes.Xml
                    }
                };

                // Act
                repository.Save(dataTypeDefinition);

                var exists  = repository.Exists(dataTypeDefinition.Id);
                var fetched = repository.Get(dataTypeDefinition.Id);

                // Assert
                Assert.That(dataTypeDefinition.HasIdentity, Is.True);
                Assert.That(exists, Is.True);

                // cannot compare 'configuration' as it's two different objects
                TestHelper.AssertPropertyValuesAreEqual(dataTypeDefinition, fetched, "yyyy-MM-dd HH:mm:ss", ignoreProperties: new [] { "Configuration" });

                // still, can compare explicitely
                Assert.IsNotNull(dataTypeDefinition.Configuration);
                Assert.IsInstanceOf <LabelConfiguration>(dataTypeDefinition.Configuration);
                Assert.IsNotNull(fetched.Configuration);
                Assert.IsInstanceOf <LabelConfiguration>(fetched.Configuration);
                Assert.AreEqual(ConfigurationEditor.ConfigurationAs <LabelConfiguration>(dataTypeDefinition.Configuration).ValueType, ConfigurationEditor.ConfigurationAs <LabelConfiguration>(fetched.Configuration).ValueType);
            }
        }
示例#7
0
        public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
        {
            var valueType = ConfigurationEditor.ConfigurationAs <LabelConfiguration>(propertyType.DataType.Configuration);

            switch (valueType.ValueType)
            {
            case ValueTypes.DateTime:
            case ValueTypes.Date:
                if (source is DateTime sourceDateTime)
                {
                    return(sourceDateTime);
                }
                if (source is string sourceDateTimeString)
                {
                    return(DateTime.TryParse(sourceDateTimeString, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dt) ? dt : DateTime.MinValue);
                }
                return(DateTime.MinValue);

            case ValueTypes.Time:
                if (source is DateTime sourceTime)
                {
                    return(sourceTime.TimeOfDay);
                }
                if (source is string sourceTimeString)
                {
                    return(TimeSpan.TryParse(sourceTimeString, CultureInfo.InvariantCulture, out var ts) ? ts : TimeSpan.Zero);
                }
                return(TimeSpan.Zero);

            case ValueTypes.Decimal:
                if (source is decimal sourceDecimal)
                {
                    return(sourceDecimal);
                }
                if (source is string sourceDecimalString)
                {
                    return(decimal.TryParse(sourceDecimalString, NumberStyles.Any, CultureInfo.InvariantCulture, out var d) ? d : 0);
                }
                if (source is double sourceDouble)
                {
                    return(Convert.ToDecimal(sourceDouble));
                }
                return((decimal)0);

            case ValueTypes.Integer:
                if (source is int sourceInt)
                {
                    return(sourceInt);
                }
                if (source is string sourceIntString)
                {
                    return(int.TryParse(sourceIntString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var i) ? i : 0);
                }
                return(0);

            case ValueTypes.Bigint:
                if (source is string sourceLongString)
                {
                    return(long.TryParse(sourceLongString, out var i) ? i : 0);
                }
                return((long)0);

            default:     // everything else is a string
                return(source?.ToString() ?? string.Empty);
            }
        }
示例#8
0
        /// <summary>
        /// Maps the dto property values to the persisted model
        /// </summary>
        internal void MapPropertyValuesForPersistence <TPersisted, TSaved>(
            TSaved contentItem,
            ContentPropertyCollectionDto dto,
            Func <TSaved, Property, object> getPropertyValue,
            Action <TSaved, Property, object> savePropertyValue,
            string culture)
            where TPersisted : IContentBase
            where TSaved : IContentSave <TPersisted>
        {
            // map the property values
            foreach (var propertyDto in dto.Properties)
            {
                // get the property editor
                if (propertyDto.PropertyEditor == null)
                {
                    Logger.Warn <ContentController>("No property editor found for property {PropertyAlias}", propertyDto.Alias);
                    continue;
                }

                // get the value editor
                // nothing to save/map if it is readonly
                var valueEditor = propertyDto.PropertyEditor.GetValueEditor();
                if (valueEditor.IsReadOnly)
                {
                    continue;
                }

                // get the property
                var property = contentItem.PersistedContent.Properties[propertyDto.Alias];

                // prepare files, if any matching property and culture
                var files = contentItem.UploadedFiles
                            .Where(x => x.PropertyAlias == propertyDto.Alias && x.Culture == propertyDto.Culture)
                            .ToArray();

                foreach (var file in files)
                {
                    file.FileName = file.FileName.ToSafeFileName();
                }

                // create the property data for the property editor
                var data = new ContentPropertyData(propertyDto.Value, propertyDto.DataType.Configuration)
                {
                    ContentKey      = contentItem.PersistedContent.Key,
                    PropertyTypeKey = property.PropertyType.Key,
                    Files           = files
                };

                // let the editor convert the value that was received, deal with files, etc
                var value = valueEditor.FromEditor(data, getPropertyValue(contentItem, property));

                // set the value - tags are special
                var tagAttribute = propertyDto.PropertyEditor.GetTagAttribute();
                if (tagAttribute != null)
                {
                    var tagConfiguration = ConfigurationEditor.ConfigurationAs <TagConfiguration>(propertyDto.DataType.Configuration);
                    if (tagConfiguration.Delimiter == default)
                    {
                        tagConfiguration.Delimiter = tagAttribute.Delimiter;
                    }
                    var tagCulture = property.PropertyType.VariesByCulture() ? culture : null;
                    property.SetTagsValue(value, tagConfiguration, tagCulture);
                }
                else
                {
                    savePropertyValue(contentItem, property, value);
                }
            }
        }
        /// <summary>
        /// Maps the dto property values to the persisted model
        /// </summary>
        internal void MapPropertyValuesForPersistence <TPersisted, TSaved>(
            TSaved contentItem,
            ContentPropertyCollectionDto?dto,
            Func <TSaved, IProperty?, object?> getPropertyValue,
            Action <TSaved, IProperty?, object?> savePropertyValue,
            string?culture)
            where TPersisted : IContentBase
            where TSaved : IContentSave <TPersisted>
        {
            if (dto is null)
            {
                return;
            }

            // map the property values
            foreach (ContentPropertyDto propertyDto in dto.Properties)
            {
                // get the property editor
                if (propertyDto.PropertyEditor == null)
                {
                    _logger.LogWarning("No property editor found for property {PropertyAlias}", propertyDto.Alias);
                    continue;
                }

                // get the value editor
                // nothing to save/map if it is readonly
                IDataValueEditor valueEditor = propertyDto.PropertyEditor.GetValueEditor();
                if (valueEditor.IsReadOnly)
                {
                    continue;
                }

                // get the property
                IProperty property = contentItem.PersistedContent.Properties[propertyDto.Alias] !;

                // prepare files, if any matching property and culture
                ContentPropertyFile[] files = contentItem.UploadedFiles
                                              .Where(x => x.PropertyAlias == propertyDto.Alias && x.Culture == propertyDto.Culture && x.Segment == propertyDto.Segment)
                                              .ToArray();

                foreach (ContentPropertyFile file in files)
                {
                    file.FileName = file.FileName?.ToSafeFileName(ShortStringHelper);
                }


                // create the property data for the property editor
                var data = new ContentPropertyData(propertyDto.Value, propertyDto.DataType?.Configuration)
                {
                    ContentKey      = contentItem.PersistedContent !.Key,
                    PropertyTypeKey = property.PropertyType.Key,
                    Files           = files
                };

                // let the editor convert the value that was received, deal with files, etc
                object?value = valueEditor.FromEditor(data, getPropertyValue(contentItem, property));

                // set the value - tags are special
                TagsPropertyEditorAttribute?tagAttribute = propertyDto.PropertyEditor.GetTagAttribute();
                if (tagAttribute != null)
                {
                    TagConfiguration?tagConfiguration = ConfigurationEditor.ConfigurationAs <TagConfiguration>(propertyDto.DataType?.Configuration);
                    if (tagConfiguration is not null && tagConfiguration.Delimiter == default)
                    {
                        tagConfiguration.Delimiter = tagAttribute.Delimiter;
                    }

                    var tagCulture = property?.PropertyType.VariesByCulture() ?? false ? culture : null;
                    property?.SetTagsValue(_serializer, value, tagConfiguration, tagCulture);
                }
                else
                {
                    savePropertyValue(contentItem, property, value);
                }
            }
        }
 private bool UseLabel(IPublishedPropertyType propertyType)
 {
     return(ConfigurationEditor.ConfigurationAs <ColorPickerConfiguration>(propertyType.DataType.Configuration).UseLabel);
 }
        private static bool IsOnlyImagesDataType(PublishedDataType dataType)
        {
            var config = ConfigurationEditor.ConfigurationAs <MediaPickerConfiguration>(dataType.Configuration);

            return(config.OnlyImages);
        }