public void CreateCaseSyncFileStart(ContactTracing.ImportExport.SyncFileFilters filters /*string fileName, bool includeCases, bool includeCaseExposures, bool includeContacts, Epi.ImportExport.Filters.RowFilters filters, bool deIdentifyData, Epi.RecordProcessingScope recordProcessingScope*/) { if (IsWaitingOnOtherClients) { return; } if (String.IsNullOrEmpty(SyncFilePath.Trim())) { throw new ArgumentNullException("fileName"); } bool success = true; IsDataSyncing = true; RecordsExported = String.Empty; var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); #region Remove extraneous data //int rows = 0; MinorSyncStatus = "Deleting extraneous page table rows..."; IDbDriver db = _project.CollectedData.GetDatabase(); if (db.ToString().ToLower().Contains("sql")) { using (IDbTransaction transaction = db.OpenTransaction()) { foreach (View form in _project.Views) { Query formDeleteDuplicateQuery = Database.CreateQuery("WITH cte AS (SELECT *, ROW_NUMBER() OVER(PARTITION BY GlobalRecordId ORDER BY GlobalRecordId) 'RowRank' FROM " + form.TableName + ") " + "DELETE FROM cte " + "WHERE RowRank > 1"); Database.ExecuteNonQuery(formDeleteDuplicateQuery, transaction); foreach (Page page in form.Pages) { Query deleteQuery = db.CreateQuery("DELETE FROM " + form.Name + " WHERE GlobalRecordId NOT IN (SELECT GlobalRecordId FROM " + page.TableName + ")"); db.ExecuteNonQuery(deleteQuery, transaction); //if (rows > 0) //{ // // report ?? //} Query pageDeleteQuery = db.CreateQuery("DELETE FROM " + page.TableName + " WHERE GlobalRecordId NOT IN (SELECT GlobalRecordId FROM " + form.Name + ")"); db.ExecuteNonQuery(deleteQuery, transaction); //if (rows > 0) //{ // // report ?? //} Query pageDeleteDuplicateQuery = Database.CreateQuery("WITH cte AS (SELECT *, ROW_NUMBER() OVER(PARTITION BY GlobalRecordId ORDER BY GlobalRecordId) 'RowRank' FROM " + page.TableName + ") " + "DELETE FROM cte " + "WHERE RowRank > 1"); Database.ExecuteNonQuery(pageDeleteDuplicateQuery, transaction); } } Query linksDeleteQuery = db.CreateQuery("DELETE FROM metaLinks WHERE ToViewId = @ToViewId AND ToRecordGuid NOT IN (SELECT GlobalRecordId FROM " + ContactForm.TableName + ")"); linksDeleteQuery.Parameters.Add(new QueryParameter("@ToViewId", DbType.Int32, ContactFormId)); db.ExecuteNonQuery(linksDeleteQuery, transaction); if (db.TableExists("metaHistory")) { Query historyDeleteQuery = db.CreateQuery("DELETE FROM metaHistory WHERE ContactGUID NOT IN (SELECT GlobalRecordId FROM " + ContactForm.TableName + ")"); db.ExecuteNonQuery(historyDeleteQuery, transaction); } try { transaction.Commit(); } catch (Exception ex0) { Epi.Logger.Log(String.Format(DateTime.Now + ": " + "DB cleanup exception Type: {0}", ex0.GetType())); Epi.Logger.Log(String.Format(DateTime.Now + ": " + "DB cleanup exception Message: {0}", ex0.Message)); Epi.Logger.Log(String.Format(DateTime.Now + ": " + "DB cleanup rollback started...")); DbLogger.Log("Database cleanup failed on commit. Exception: " + ex0.Message); try { transaction.Rollback(); Epi.Logger.Log(String.Format(DateTime.Now + ": " + "DB cleanup rollback was successful.")); } catch (Exception ex1) { DbLogger.Log("Database cleanup rollback failed. Exception: " + ex1.Message); } } db.CloseTransaction(transaction); } } #endregion // Remove extraneous data RecordsExported = String.Empty; IsDataSyncing = true; IsShowingExportProgress = true; SendMessageForAwaitAll(); DbLogger.Log(String.Format("Initiated process 'export sync file' - IncludeCasesAndContacts = {0}", IncludeCasesAndContacts)); Task.Factory.StartNew( () => { success = CreateCaseSyncFile(filters /*fileName, includeCases, includeCaseExposures, includeContacts, filters, deIdentifyData, recordProcessingScope*/); }, System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).ContinueWith( delegate { SendMessageForUnAwaitAll(); TaskbarProgressState = System.Windows.Shell.TaskbarItemProgressState.None; MajorProgressValue = 0; IsDataSyncing = false; MinorProgressValue = 0; stopwatch.Stop(); MinorSyncStatus = String.Empty; if (success) { HasExportErrors = false; MajorSyncStatus = "Finished exporting data to sync file."; TimeElapsed = "Elapsed time: " + stopwatch.Elapsed.TotalMinutes.ToString("F1") + " minutes."; DbLogger.Log(String.Format("Completed process 'export sync file' successfully - elapsed time = {0} ms", stopwatch.Elapsed.TotalMilliseconds.ToString())); } else { HasExportErrors = true; MajorSyncStatus = "There was a problem exporting the data."; DbLogger.Log(String.Format("Completed process 'export sync file' with errors")); } CommandManager.InvalidateRequerySuggested(); }, TaskScheduler.FromCurrentSynchronizationContext()); }
private void LabResultActionsRowControl_DeleteRequested(object sender, EventArgs e) { MessageBoxResult result = MessageBox.Show("Are you sure you want to delete this lab record?", "Confirm deletion", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { Dialogs.AuthCodeDialog authDialog = new Dialogs.AuthCodeDialog(ContactTracing.Core.Constants.AUTH_CODE); System.Windows.Forms.DialogResult authResult = authDialog.ShowDialog(); if (authResult == System.Windows.Forms.DialogResult.OK) { if (authDialog.IsAuthorized) { try { if (this.DataContext != null) { EpiDataHelper DataHelper = this.DataContext as EpiDataHelper; if (DataHelper != null) { LabResultViewModel r = dg.SelectedItem as LabResultViewModel; if (r != null) { string guid = r.RecordId; IDbDriver db = DataHelper.Project.CollectedData.GetDatabase(); #region Soft deletion code (Unused) /* SOFT DELETE */ //int rows = 0; //string querySyntax = "UPDATE [" + DataHelper.LabForm.TableName + "] SET RecStatus = 0 WHERE [GlobalRecordId] = @GlobalRecordId"; //Query labUpdateQuery = db.CreateQuery(querySyntax); //labUpdateQuery.Parameters.Add(new QueryParameter("@GlobalRecordId", System.Data.DbType.String, guid)); //rows = db.ExecuteNonQuery(labUpdateQuery); //CaseViewModel c = DataHelper.GetCaseVM(r.CaseRecordGuid); //if (rows == 1) //{ // Core.DbLogger.Log(String.Format( // "Soft-deleted lab : Case ID = {0}, Case EpiCaseDef = {1}, FLSID = {2}, Lab GUID = {3}", // c.ID, c.EpiCaseDef, r.FieldLabSpecimenID, r.RecordId)); //} //else if (rows == 0) //{ // Core.DbLogger.Log(String.Format( // "Lab soft-deletion attempted but no lab record found in database : GUID = {0}", // guid)); //} //else if (rows > 1) //{ // Core.DbLogger.Log(String.Format( // "Lab soft-deletion affected {0} records for GUID {1}. Duplicate labs may be present.", // rows.ToString(), guid)); //} #endregion Soft deletion code /* HARD DELETE */ using (IDbTransaction transaction = db.OpenTransaction()) { Query deleteQuery = db.CreateQuery("DELETE FROM " + DataHelper.LabForm.TableName + " WHERE GlobalRecordId = @GlobalRecordId"); deleteQuery.Parameters.Add(new QueryParameter("@GlobalRecordId", System.Data.DbType.String, guid)); int rows = db.ExecuteNonQuery(deleteQuery, transaction); foreach (Epi.Page page in DataHelper.LabForm.Pages) { deleteQuery = db.CreateQuery("DELETE FROM " + page.TableName + " WHERE GlobalRecordId = @GlobalRecordId"); deleteQuery.Parameters.Add(new QueryParameter("@GlobalRecordId", System.Data.DbType.String, guid)); db.ExecuteNonQuery(deleteQuery, transaction); } try { transaction.Commit(); } catch (Exception ex0) { ContactTracing.Core.DbLogger.Log("Lab record deletion failed on commit. Transaction rolled back. Exception: " + ex0.Message); try { transaction.Rollback(); } catch (Exception ex1) { ContactTracing.Core.DbLogger.Log("Lab record deletion failed on commit rollback. Transaction rolled back. Exception: " + ex1.Message); } } CaseViewModel c = DataHelper.GetCaseVM(r.CaseRecordGuid); DataHelper.PopulateLabRecordsForCase.Execute(c); db.CloseTransaction(transaction); } } } } } catch (Exception ex) { MessageBox.Show(String.Format("An exception occurred while trying to delete a lab record. Message: {0}", ex.Message));//. Case ID: {0}. Please give this message to the application developer.\n{1}", caseVM.ID, ex.Message)); ContactTracing.Core.DbLogger.Log("Lab record deletion failed. Exception: " + ex.Message); } } } } }