Ejemplo n.º 1
0
        private async void btnClone_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(cbxFromDb.Text) && !string.IsNullOrWhiteSpace(cbxToDb.Text) && cbxFromDb.Text != cbxToDb.Text)
            {
                try
                {
                    if (DataProviders.ConfirmDialog($"Clone database '{cbxFromDb.Text}' to '{cbxToDb.Text}', with drop option {(chkbDrop.Checked ? "TRUE" : "FALSE")}"))
                    {
                        cbxFromDb.Enabled = cbxToDb.Enabled = btnClone.Enabled = false;
                        var config = Program.AppConfiguration;
                        FromDb = new MongoDbContext(connectionString: config.DbConnections[cbxFromDb.Text] ?? MongoDbContext.LocalConnection);
                        ToDb   = new MongoDbContext(connectionString: config.DbConnections[cbxToDb.Text] ?? MongoDbContext.LocalConnection);
                        if (chkbDrop.Checked)
                        {
                            await ToDb.Drop();

                            if ((await FromDb.BotCollection.CountDocumentsAsync(x => true)) > 0)
                            {
                                await ToDb.BotCollection.InsertManyAsync(await FromDb.BotCollection.Find(x => true).ToListAsync());
                            }
                        }
                        var fromBotConfiguration = await FromDb.BotCollection.FindOneById(MongoDbContext.BotAlphaName);

                        if (chkbSelectProfile.Checked)
                        {
                            if (!chkbDrop.Checked)
                            {
                                var chatProfile = fromBotConfiguration.Configuration.ChatProfiles.FirstOrDefault(x => x.Name == cbxChatProfile.Text);
                                await ToDb.BotCollection.AddOrUpdateChatProfileById(MongoDbContext.BotAlphaName, chatProfile);

                                var toBotConfiguration = await ToDb.BotCollection.FindOneById(MongoDbContext.BotAlphaName);

                                var tempList = new List <KeyValuePair <string, string> >();
                                fromBotConfiguration.Configuration?.ResourceStrings?.ForEach(str =>
                                {
                                    var search = (KeyValuePair <string, string>)toBotConfiguration.Configuration?.ResourceStrings.FirstOrDefault(res => res.Key == str.Key);
                                    if (string.IsNullOrWhiteSpace(search.Key))
                                    {
                                        tempList.Add(str);
                                    }
                                });

                                if (tempList.Count > 0)
                                {
                                    await ToDb.BotCollection.AddStringResourceBatchById(MongoDbContext.BotAlphaName, tempList);
                                }
                            }

                            FromDb.SyncChatProfile(cbxChatProfile.Text);
                            ToDb.SyncChatProfile(cbxChatProfile.Text);
                            await ToDb.DropAllNodeCollections();
                            await CopyDb();
                        }
                        else
                        {
                            foreach (var profile in fromBotConfiguration?.Configuration?.ChatProfiles)
                            {
                                FromDb.SyncChatProfile(profile.Name);
                                ToDb.SyncChatProfile(profile.Name);
                                await CopyDb();
                            }
                        }
                        DialogResult      = DialogResult.OK;
                        cbxFromDb.Enabled = cbxToDb.Enabled = btnClone.Enabled = true;

                        MessageBox.Show("Operation completed without any error.", "Clone confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.StackTrace, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (DialogResult == DialogResult.OK)
                    {
                        this.Close();
                    }
                }
            }
        }