Ejemplo n.º 1
0
    private bool Migrate(IEnumerable <DataTypeDto> dataTypes, bool isMultiple)
    {
        var refreshCache = false;
        ConfigurationEditor?configurationEditor = null;

        foreach (DataTypeDto dataType in dataTypes)
        {
            ValueListConfiguration config;

            if (dataType.Configuration.IsNullOrWhiteSpace())
            {
                continue;
            }

            // 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);

                continue;
            }

            // get property data dtos
            List <PropertyDataDto>?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
            IEnumerable <PropertyDataDto> updatedDtos =
                propertyDataDtos.Where(x => UpdatePropertyDataDto(x, config, isMultiple));

            // persist changes
            foreach (PropertyDataDto?propertyDataDto in updatedDtos)
            {
                Database.Update(propertyDataDto);
            }

            UpdateDataType(dataType);
            refreshCache = true;
        }

        return(refreshCache);
    }
        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);
        }
        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);
        }