private string GetRegistryData()
        {
            var viewModel = new ModuleRegistryViewModel();

            // Retrieve module data
            ModuleDefinition[] moduleDefinitions;

            const string moduleDefinitionSqlText =
                @"SELECT Id, Name, DisplayName, Description, CmsType
                  FROM CSF_Modules";

            moduleDefinitions = ExecuteDbQuery<ModuleDefinition>(moduleDefinitionSqlText);

            // Retrieve module view data
            ModuleView[] moduleViews;

            const string moduleViewSqlText =
                @"SELECT ModuleName, Name, DisplayName, Description, ViewType
                  FROM CSF_ModuleViews";

            moduleViews = ExecuteDbQuery<ModuleView>(moduleViewSqlText);

            foreach (var module in moduleDefinitions)
            {
                module.Views.AddRange(moduleViews.Where(v => v.ModuleName == module.Name));
            }

            // Retrieve module instance data
            ModuleInstance[] moduleInstances;

            const string moduleInstanceSqlText =
                @"SELECT Id, ModuleName, ModuleViewName, TemplateName, NULL AS SettingsData, DisplayName, IsReusable
                  FROM CSF_ModuleInstances";

            moduleInstances = ExecuteDbQuery<ModuleInstance>(moduleInstanceSqlText);

            foreach (var module in moduleDefinitions)
            {
                var filteredInstances = moduleInstances.Where(i => i.ModuleName == module.Name).ToList();

                foreach (var instance in filteredInstances)
                {
                    instance.ModuleDisplayName = module.DisplayName;
                    instance.ModuleViewDisplayName = moduleViews.First(v => v.ModuleName == module.Name && v.Name == instance.ModuleViewName).DisplayName;
                }

                module.Instances.AddRange(filteredInstances);
            }

            // Serialize and return data as JSON
            viewModel.Modules = moduleDefinitions;
            var jsonData = CommonUtils.JsonSerialize(viewModel);

            return jsonData;
        }
        public void UpdateModuleInstance(ModuleInstance instance)
        {
            var registryData = new ModuleRegistryData();

            var module = GetModule(instance.ModuleName);

            if (instance.Action == RecordActions.Added && registryData.ModuleInstances.ContainsKey(instance.Id))
                instance.Action = RecordActions.Edited;

            if (instance.Action == RecordActions.Edited && !registryData.ModuleInstances.ContainsKey(instance.Id))
                instance.Action = RecordActions.Added;

            // Create view model for added/updated instance
            var clonedModule = module.Clone(false, false);
            clonedModule.Instances.Add(instance);

            var viewModel = new ModuleRegistryViewModel();
            viewModel.Modules = new List<ModuleDefinition>() { clonedModule };

            var data = JsonConvert.SerializeObject(viewModel);

            // Post view model to data provider
            ServiceContext.DataProviderFactory.CreateDataProvider(module.CmsType).PostModuleRegistryData(data);
        }