예제 #1
0
        public WindowViewModel(ISchedulerProvider schedulerProvider)
        {
            _settings = ApplicationOptions.Get();
            Logger.SetLevel(_settings.LogLevel);
            _settings.ApplyThemeCommand.Execute(_settings.Theme.Equals(Theme.Dark));
            _settings.ApplyTheme();
            _isEmpty           = true;
            _plugableProviders = new PlugableProviders();
            _plugableProviders.Load();
            _dataSourceProviders = (from p in _plugableProviders.Plugins
                                    let item = p as IDatasourceProvider
                                               where item != null
                                               select item).ToList();
            _dataSetConsumers = (from p in _plugableProviders.Plugins
                                 let item = p as IDatasetConsumer
                                            where item != null
                                            select item).ToList();
            _tableConsumers = (from p in _plugableProviders.Plugins
                               let item = p as ITableConsumer
                                          where item != null
                                          select item).ToList();
            _dataSourceProvider = _dataSourceProviders.FirstOrDefault(p => p.IsDefault);
            _dataSourceProvider.SetLog(Logger.Log);
            _schedulerProvider = schedulerProvider;

            Version = $"v{Assembly.GetEntryAssembly().GetName().Version.ToString(3)}";

            if (File.Exists(DefaultConfigFile))
            {
                OpenFiles(DefaultConfigFile.Split());
            }
        }
예제 #2
0
        private void LoadAndSaveConfiguration(CDatasource ds)
        {
            IsBusy = true;
            _schedulerProvider.Background.Schedule(async() =>
            {
                try
                {
                    _dataSourceProvider = _dataSourceProviders.FirstOrDefault(p => p.Name.Equals(ds.providerName));
                    _dataSourceProvider.SetLog(Logger.Log);

                    var schema = await _dataSourceProvider.GetSchema(ds);
                    if (!string.IsNullOrEmpty(schema))
                    {
                        var builder = XmlBuilder.BuildElements(ds, schema);
                        if (null != builder)
                        {
                            var config = XmlHelper <CConfiguration> .LoadFromString(builder.ToString());
                            if (!File.Exists(DefaultConfigFile))
                            {
                                //fix relationship names
                                foreach (var dset in config.Dataset)
                                {
                                    foreach (var rel in dset.Relationship)
                                    {
                                        rel.name = rel.GetName();
                                    }
                                }

                                XmlHelper <CConfiguration> .Save(DefaultConfigFile, config);

                                Configuration = Configuration ?? InitConfiguration(config);
                                SetDataset.Execute(Configuration.Dataset.FirstOrDefault());
                                IsEmpty = false;

                                var queryConfig = await LoadAndSaveQueries(ds, SelectedDataset.name);

                                _schedulerProvider.MainThread.Schedule(() =>
                                {
                                    SelectedDataset.Query.AddRange(queryConfig.Dataset.FirstOrDefault().Query);
                                    SelectedQuery       = SelectedDataset.Query.FirstOrDefault(q => q.isStoreProcedure);
                                    SelectedScriptQuery = SelectedDataset.Query.LastOrDefault(q => q.isScript);
                                });
                            }
                            else
                            {
                                if (Configuration.Dataset.Any(cds => cds.name.Equals(config.defaultDataset)))
                                {
                                    for (var i = 1; i < 10000; i++)
                                    {
                                        if (!Configuration.Dataset.Any(cds => cds.name.Equals(config.defaultDataset + i)))
                                        {
                                            var newName = config.Dataset.FirstOrDefault().name + i;
                                            config.Dataset.FirstOrDefault().name           = newName;
                                            config.Dataset.FirstOrDefault().dataSourceName = newName;
                                            config.Datasource.FirstOrDefault().name        = newName;
                                            break;
                                        }
                                    }
                                }

                                _schedulerProvider.MainThread.Schedule(() =>
                                {
                                    Configuration.Datasource.Add(config.Datasource.FirstOrDefault());
                                    Configuration.Dataset.Add(config.Dataset.FirstOrDefault());
                                    foreach (var dset in Configuration.Dataset)
                                    {
                                        dset.Query.Clear();
                                    }

                                    XmlHelper <CConfiguration> .Save(DefaultConfigFile, Configuration);

                                    _dataSetConsumers.ToList().ForEach(c => c.OnConnected(Configuration, Logger.Log));
                                });

                                var queryConfig = await LoadAndSaveQueries(ds, config.Dataset.FirstOrDefault().name);
                                _schedulerProvider.MainThread.Schedule(() => {
                                    foreach (var dset in queryConfig.Dataset)
                                    {
                                        Configuration.Dataset.FirstOrDefault(d => d.name.Equals(dset.name))
                                        .Query.AddRange(dset.Query);
                                    }
                                });
                            }
                        }
                    }
                } catch (Exception e)
                {
                    Logger.Log.Error(e, "Exception while loading and saving configuration for {0}", ds.ConnectionString.SecureString());
                    MessageQueue.Enqueue(e.Message);
                }
                finally
                {
                    _schedulerProvider.MainThread.Schedule(() => IsBusy = false);
                }
            });
        }