@using Rms.Risk.Mango.Services.Context @inject IUserSession UserSession @inject IDatabaseConfigurationService DatabaseConfig @* * dbMango * * Copyright 2025 Deutsche Bank AG * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *@
@if( UserSession.IsInstanceSelectionAllowed(SourceDatabase) ) {
}
@JobDescription
@code { [CascadingParameter] public BlazoredModalInstance ModalInstance { get; set; } = null!; [CascadingParameter] public IModalService Modal { get; set; } = null!; private string SourceDatabase { get; set { if (field == value) return; field = value; if (!string.IsNullOrWhiteSpace(field)) InvokeAsync(SourceDatabaseChanged); } } = ""; private string SourceDatabaseInstance { get; set { if (field == value) return; field = value; if (!string.IsNullOrWhiteSpace(field)) InvokeAsync(SourceDatabaseChanged); } } = ""; private string SourceCollection { get; set { if (field == value) return; field = value; if (!string.IsNullOrWhiteSpace(field)) InvokeAsync(StateHasChanged); } } = ""; private string DestCollection { get; set { if (field == value) return; field = value; if (!string.IsNullOrWhiteSpace(field)) InvokeAsync(StateHasChanged); } } = ""; private string Find { get; set; } = "{ _id : { $ne : \"\" } }"; private string Transform { get; set; } = ""; private string DestDatabase => UserSession.Database; private bool WipeDestination { get; set; } private string BatchSize { get; set; } = "1000"; private bool Upsert { get; set; } private bool IsReady { get; set; } private bool IsReadyToRun => IsReady && !string.IsNullOrWhiteSpace(SourceCollection) && !string.IsNullOrWhiteSpace(EffectiveDestCollection) && (SourceDatabase != DestDatabase || SourceCollection != EffectiveDestCollection) ; private string Error { get; set; } = ""; private EditContext EditContext => _editContext!; private string EffectiveDestCollection => string.IsNullOrWhiteSpace(DestCollection) ? SourceCollection : DestCollection; private Task SourceDatabaseChanged() { _ = Task.Run(LoadCollections); return Task.CompletedTask; } private List SourceDatabases => DatabaseConfig.Databases.Keys .OrderBy(x => x) .ToList() ; //TODO: load on change private List SourceDatabaseInstances => []; private MarkupString JobDescription => new( $"Copy collection {SourceCollection} from {SourceDatabase} to {EffectiveDestCollection} of {DestDatabase}." + (!string.IsNullOrWhiteSpace(Transform) ? "
Applying documents transformation." : "
Without any documents transformation.") + (WipeDestination ? "
Destination collections will be cleared first." : "") + (Upsert ? "
Existing documents will be updated." : "") ); private EditContext? _editContext; protected override void OnInitialized() { _editContext = new(this); } protected override void OnAfterRender(bool firstRender) { if (!firstRender) return; if (SourceDatabases.Count == 0) return; SourceDatabase = SourceDatabases.First(); StateHasChanged(); } private async Task LoadCollections() { try { IsReady = false; await InvokeAsync(StateHasChanged); if (string.IsNullOrWhiteSpace(SourceDatabase)) return; var admin = UserSession.GetCustomAdmin(SourceDatabase, SourceDatabaseInstance); SourceCollections = await admin.ListCollections(); SourceCollection = SourceCollections.FirstOrDefault() ?? ""; } catch (Exception ex) { await Display(ex); } finally { IsReady = true; await InvokeAsync(StateHasChanged); } } private IReadOnlyCollection SourceCollections { get; set; } = []; private Task Display(Exception e) { Error = e.ToString(); return InvokeAsync(StateHasChanged); } private async Task OnOK() { try { var job = new MigrationJob() { SourceDatabase = SourceDatabase, DestinationDatabase = DestDatabase, Email = UserSession.User.GetEmail(), Upsert = Upsert, ClearDestinationBefore = WipeDestination, BatchSize = int.Parse(BatchSize), Status = [ new MigrationJob.CollectionJob { SourceCollection = SourceCollection, DestinationCollection = EffectiveDestCollection, Filter = BsonDocument.Parse(Find), Projection = string.IsNullOrWhiteSpace(Transform) ? null : BsonDocument.Parse(Transform) } ] }; await ModalInstance.CloseAsync(ModalResult.Ok(job)); } catch (Exception e) { await ModalDialogUtils.ShowExceptionDialog(Modal, "Error", e); } } public static Task ShowDialog(IModalService service) { var parameters = new ModalParameters { }; var options = new ModalOptions { HideCloseButton = false, DisableBackgroundCancel = true }; var form = service.Show("New transformation", parameters, options); return form.Result; } }