Beispiel #1
0
        private bool UpdateRadioOrCheckboxPropertyDataDto(PropertyDataDto propData, ValueListConfiguration config, bool singleValue)
        {
            //Get the INT ids stored for this property/drop down
            int[] ids = null;
            if (!propData.VarcharValue.IsNullOrWhiteSpace())
            {
                ids = ConvertStringValues(propData.VarcharValue);
            }
            else if (!propData.TextValue.IsNullOrWhiteSpace())
            {
                ids = ConvertStringValues(propData.TextValue);
            }
            else if (propData.IntegerValue.HasValue)
            {
                ids = new[] { propData.IntegerValue.Value };
            }

            //if there are INT ids, convert them to values based on the configuration
            if (ids == null || ids.Length <= 0)
            {
                return(false);
            }

            //map the ids to values
            var values     = new List <string>();
            var canConvert = true;

            foreach (var id in ids)
            {
                var val = config.Items.FirstOrDefault(x => x.Id == id);
                if (val != null)
                {
                    values.Add(val.Value);
                }
                else
                {
                    Logger.Warn <DropDownPropertyEditorsMigration>(
                        "Could not find associated data type configuration for stored Id {DataTypeId}", id);
                    canConvert = false;
                }
            }

            if (!canConvert)
            {
                return(false);
            }

            //The radio button only supports selecting a single value, so if there are multiple for some insane reason we can only use the first
            propData.VarcharValue = singleValue ? values[0] : JsonConvert.SerializeObject(values);
            propData.TextValue    = null;
            propData.IntegerValue = null;
            return(true);
        }
Beispiel #2
0
        public object GetConfiguration(int dataTypeId, string editorAlias, Dictionary <string, PreValueDto> preValues)
        {
            var config = new ValueListConfiguration();

            foreach (var preValue in preValues.Values)
            {
                config.Items.Add(new ValueListConfiguration.ValueListItem {
                    Id = preValue.Id, Value = preValue.Value
                });
            }
            return(config);
        }
Beispiel #3
0
    internal bool UpdatePropertyDataDto(PropertyDataDto propData, ValueListConfiguration config, bool isMultiple)
    {
        // Get the INT ids stored for this property/drop down
        int[]? ids = null;
        if (!propData.VarcharValue.IsNullOrWhiteSpace())
        {
            ids = ConvertStringValues(propData.VarcharValue);
        }
        else if (!propData.TextValue.IsNullOrWhiteSpace())
        {
            ids = ConvertStringValues(propData.TextValue);
        }
        else if (propData.IntegerValue.HasValue)
        {
            ids = new[] { propData.IntegerValue.Value };
        }

        // if there are INT ids, convert them to values based on the configuration
        if (ids == null || ids.Length <= 0)
        {
            return(false);
        }

        // map ids to values
        var values     = new List <string>();
        var canConvert = true;

        foreach (var id in ids)
        {
            ValueListConfiguration.ValueListItem?val = config.Items.FirstOrDefault(x => x.Id == id);
            if (val?.Value != null)
            {
                values.Add(val.Value);
                continue;
            }

            Logger.LogWarning(
                "Could not find PropertyData {PropertyDataId} value '{PropertyValue}' in the datatype configuration: {Values}.",
                propData.Id, id, string.Join(", ", config.Items.Select(x => x.Id + ":" + x.Value)));
            canConvert = false;
        }

        if (!canConvert)
        {
            return(false);
        }

        propData.VarcharValue = isMultiple ? JsonConvert.SerializeObject(values) : values[0];
        propData.TextValue    = null;
        propData.IntegerValue = null;
        return(true);
    }
        private string UpdateValueList(string propertyValue, ValueListConfiguration config, bool isMultiple)
        {
            var propData = new PropertyDataDto {
                VarcharValue = propertyValue
            };

            if (UpdatePropertyDataDto(propData, config, isMultiple: isMultiple))
            {
                return(propData.VarcharValue);
            }

            return(propertyValue);
        }
Beispiel #5
0
    private void UpdateDataType(DataTypeDto dataType, ValueListConfiguration config, bool isMultiple)
    {
        dataType.DbType      = ValueStorageType.Nvarchar.ToString();
        dataType.EditorAlias = Constants.PropertyEditors.Aliases.DropDownListFlexible;

        var flexConfig = new DropDownFlexibleConfiguration {
            Items = config.Items, Multiple = isMultiple
        };

        dataType.Configuration = ConfigurationEditor.ToDatabase(flexConfig, _configurationEditorJsonSerializer);

        Database.Update(dataType);
    }
Beispiel #6
0
        private bool UpdatePropertyDataDto(PropertyDataDto propData, ValueListConfiguration config)
        {
            //Get the INT ids stored for this property/drop down
            int[] ids = null;
            if (!propData.VarcharValue.IsNullOrWhiteSpace())
            {
                ids = ConvertStringValues(propData.VarcharValue);
            }
            else if (!propData.TextValue.IsNullOrWhiteSpace())
            {
                ids = ConvertStringValues(propData.TextValue);
            }
            else if (propData.IntegerValue.HasValue)
            {
                ids = new[] { propData.IntegerValue.Value };
            }

            //if there are INT ids, convert them to values based on the configured pre-values
            if (ids != null && ids.Length > 0)
            {
                //map the ids to values
                var vals       = new List <string>();
                var canConvert = true;
                foreach (var id in ids)
                {
                    var val = config.Items.FirstOrDefault(x => x.Id == id);
                    if (val != null)
                    {
                        vals.Add(val.Value);
                    }
                    else
                    {
                        Logger.Warn <DropDownPropertyEditorsMigration>(
                            "Could not find associated data type configuration for stored Id {DataTypeId}", id);
                        canConvert = false;
                    }
                }
                if (canConvert)
                {
                    propData.VarcharValue = string.Join(",", vals);
                    propData.TextValue    = null;
                    propData.IntegerValue = null;
                    return(true);
                }
            }

            return(false);
        }
        public void DropDownPreValueEditor_Format_Data_For_Editor()
        {
            // editor wants ApplicationContext.Current.Services.TextService
            // (that should be fixed with proper injection)
            var logger      = Mock.Of <ILogger>();
            var textService = new Mock <ILocalizedTextService>();

            textService.Setup(x => x.Localize(It.IsAny <string>(), It.IsAny <CultureInfo>(), It.IsAny <IDictionary <string, string> >())).Returns("blah");
            //var appContext = new ApplicationContext(
            //    new DatabaseContext(TestObjects.GetIDatabaseFactoryMock(), logger, Mock.Of<IRuntimeState>(), Mock.Of<IMigrationEntryService>()),
            //    new ServiceContext(
            //        localizedTextService: textService.Object
            //    ),
            //    Mock.Of<CacheHelper>(),
            //    new ProfilingLogger(logger, Mock.Of<IProfiler>()))
            //{
            //    //IsReady = true
            //};
            //Current.ApplicationContext = appContext;

            var configuration = new ValueListConfiguration
            {
                Items = new List <ValueListConfiguration.ValueListItem>
                {
                    new ValueListConfiguration.ValueListItem {
                        Id = 1, Value = "Item 1"
                    },
                    new ValueListConfiguration.ValueListItem {
                        Id = 2, Value = "Item 2"
                    },
                    new ValueListConfiguration.ValueListItem {
                        Id = 3, Value = "Item 3"
                    }
                }
            };

            var editor = new ValueListConfigurationEditor(Mock.Of <ILocalizedTextService>());

            var result = editor.ToConfigurationEditor(configuration);

            // 'result' is meant to be serialized, is built with anonymous objects
            // so we cannot really test what's in it - but by serializing it
            var json = JsonConvert.SerializeObject(result);

            Assert.AreEqual("{\"items\":{\"1\":{\"value\":\"Item 1\",\"sortOrder\":1},\"2\":{\"value\":\"Item 2\",\"sortOrder\":2},\"3\":{\"value\":\"Item 3\",\"sortOrder\":3}}}", json);
        }
Beispiel #8
0
        private bool UpdateDropDownPropertyDataDto(PropertyDataDto propData, ValueListConfiguration config)
        {
            //Get the INT ids stored for this property/drop down
            var values = propData.VarcharValue.Split(new [] { "," }, StringSplitOptions.RemoveEmptyEntries);

            //if there are INT ids, convert them to values based on the configuration
            if (values == null || values.Length <= 0)
            {
                return(false);
            }

            //The radio button only supports selecting a single value, so if there are multiple for some insane reason we can only use the first
            propData.VarcharValue = JsonConvert.SerializeObject(values);
            propData.TextValue    = null;
            propData.IntegerValue = null;
            return(true);
        }
        private bool Migrate(IEnumerable <DataTypeDto> dataTypes)
        {
            var refreshCache = false;
            ConfigurationEditor?configurationEditor = null;

            foreach (var dataType in dataTypes)
            {
                ValueListConfiguration config;

                if (!dataType.Configuration.IsNullOrWhiteSpace())
                {
                    // parse configuration, and update everything accordingly
                    if (configurationEditor == null)
                    {
                        configurationEditor = new ValueListConfigurationEditor(_ioHelper, _editorConfigurationParser);
                    }
                    try
                    {
                        config = (ValueListConfiguration)configurationEditor.FromDatabase(dataType.Configuration, _configurationEditorJsonSerializer);
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(
                            ex, "Invalid configuration: \"{Configuration}\", cannot convert editor.",
                            dataType.Configuration);

                        // reset
                        config = new ValueListConfiguration();
                    }

                    // get property data dtos
                    var propertyDataDtos = Database.Fetch <PropertyDataDto>(Sql()
                                                                            .Select <PropertyDataDto>()
                                                                            .From <PropertyDataDto>()
                                                                            .InnerJoin <PropertyTypeDto>().On <PropertyTypeDto, PropertyDataDto>((pt, pd) => pt.Id == pd.PropertyTypeId)
                                                                            .InnerJoin <DataTypeDto>().On <DataTypeDto, PropertyTypeDto>((dt, pt) => dt.NodeId == pt.DataTypeId)
                                                                            .Where <PropertyTypeDto>(x => x.DataTypeId == dataType.NodeId));

                    // update dtos
                    var updatedDtos = propertyDataDtos.Where(x => UpdatePropertyDataDto(x, config, true));

                    // persist changes
                    foreach (var propertyDataDto in updatedDtos)
                    {
                        Database.Update(propertyDataDto);
                    }
                }
                else
                {
                    // default configuration
                    config = new ValueListConfiguration();
                }

                switch (dataType.EditorAlias)
                {
                case string ea when ea.InvariantEquals("Umbraco.DropDown"):
                    UpdateDataType(dataType, config, false);

                    break;

                case string ea when ea.InvariantEquals("Umbraco.DropdownlistPublishingKeys"):
                    UpdateDataType(dataType, config, false);

                    break;

                case string ea when ea.InvariantEquals("Umbraco.DropDownMultiple"):
                    UpdateDataType(dataType, config, true);

                    break;

                case string ea when ea.InvariantEquals("Umbraco.DropdownlistMultiplePublishKeys"):
                    UpdateDataType(dataType, config, true);

                    break;
                }

                refreshCache = true;
            }

            return(refreshCache);
        }
Beispiel #10
0
        public override void Migrate()
        {
            //need to convert the old drop down data types to use the new one
            var dataTypes = Database.Fetch <DataTypeDto>(Sql()
                                                         .Select <DataTypeDto>()
                                                         .From <DataTypeDto>()
                                                         .Where <DataTypeDto>(x => x.EditorAlias.Contains(".DropDown")));

            foreach (var dataType in dataTypes)
            {
                ValueListConfiguration config;

                if (!dataType.Configuration.IsNullOrWhiteSpace())
                {
                    // parse configuration, and update everything accordingly
                    try
                    {
                        config = (ValueListConfiguration) new ValueListConfigurationEditor().FromDatabase(dataType.Configuration);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error <DropDownPropertyEditorsMigration>(
                            ex, "Invalid drop down configuration detected: \"{Configuration}\", cannot convert editor, values will be cleared",
                            dataType.Configuration);

                        // reset
                        config = new ValueListConfiguration();
                    }

                    // get property data dtos
                    var propertyDataDtos = Database.Fetch <PropertyDataDto>(Sql()
                                                                            .Select <PropertyDataDto>()
                                                                            .From <PropertyDataDto>()
                                                                            .InnerJoin <PropertyTypeDto>().On <PropertyTypeDto, PropertyDataDto>((pt, pd) => pt.Id == pd.PropertyTypeId)
                                                                            .InnerJoin <DataTypeDto>().On <DataTypeDto, PropertyTypeDto>((dt, pt) => dt.NodeId == pt.DataTypeId)
                                                                            .Where <PropertyTypeDto>(x => x.DataTypeId == dataType.NodeId));

                    // update dtos
                    var updatedDtos = propertyDataDtos.Where(x => UpdatePropertyDataDto(x, config));

                    // persist changes
                    foreach (var propertyDataDto in updatedDtos)
                    {
                        Database.Update(propertyDataDto);
                    }
                }
                else
                {
                    // default configuration
                    config = new ValueListConfiguration();
                }

                var requiresCacheRebuild = false;
                switch (dataType.EditorAlias)
                {
                case string ea when ea.InvariantEquals("Umbraco.DropDown"):
                    UpdateDataType(dataType, config, false);

                    break;

                case string ea when ea.InvariantEquals("Umbraco.DropdownlistPublishingKeys"):
                    UpdateDataType(dataType, config, false);

                    requiresCacheRebuild = true;
                    break;

                case string ea when ea.InvariantEquals("Umbraco.DropDownMultiple"):
                    UpdateDataType(dataType, config, true);

                    break;

                case string ea when ea.InvariantEquals("Umbraco.DropdownlistMultiplePublishKeys"):
                    UpdateDataType(dataType, config, true);

                    requiresCacheRebuild = true;
                    break;
                }

                if (requiresCacheRebuild)
                {
                    var dataTypeCacheRefresher = _cacheRefreshers[Guid.Parse("35B16C25-A17E-45D7-BC8F-EDAB1DCC28D2")];
                    _serverMessenger.PerformRefreshAll(dataTypeCacheRefresher);
                }
            }
        }