private void StartExportCommandExecute() { switch (RecordScope) { case "Deleted": RecordProcessingScope = RecordProcessingScope.Deleted; break; case "Both": RecordProcessingScope = RecordProcessingScope.Both; break; case "Active": default: RecordProcessingScope = RecordProcessingScope.Undeleted; break; } Epi.ImportExport.Filters.ConditionJoinTypes op = Epi.ImportExport.Filters.ConditionJoinTypes.And; if (FilterJoinType != null && FilterJoinType.Equals("or", StringComparison.OrdinalIgnoreCase)) { op = Epi.ImportExport.Filters.ConditionJoinTypes.Or; } ContactTracing.ImportExport.SyncFileFilters filters = new ContactTracing.ImportExport.SyncFileFilters(Database, op); if (ApplyFilters == true) { #region Check to see if user's filtering options make sense //if (String.IsNullOrEmpty(varName1) && String.IsNullOrEmpty(value1)) //{ // MessageBox.Show("Neither a variable nor a value have been selected for the first condition. Please ensure both a variable and a value are present before proceeding.", "Missing filter information", MessageBoxButton.OK, MessageBoxImage.Error); // return; //} //if (!String.IsNullOrEmpty(varName1) && String.IsNullOrEmpty(value1)) //{ // MessageBox.Show("A variable has been selected for the first condition, but no value has been specified. Please specify a value and try again.", "No value specified", MessageBoxButton.OK, MessageBoxImage.Error); // return; //} //if (!String.IsNullOrEmpty(value1) && String.IsNullOrEmpty(varName1)) //{ // MessageBox.Show("A value has been selected for the first condition, but no variable has been specified. Please specify a variable on which to filter and try again.", "No variable specified", MessageBoxButton.OK, MessageBoxImage.Error); // return; //} //if (cmbLogicalOperator.SelectedIndex == 1 && String.IsNullOrEmpty(varName2) && String.IsNullOrEmpty(value2)) //{ // MessageBox.Show("Neither a variable nor a value have been selected for the second condition. Please ensure both a variable and a value are present before proceeding.", "Missing filter information", MessageBoxButton.OK, MessageBoxImage.Error); // return; //} //if (cmbLogicalOperator.SelectedIndex == 1 && !String.IsNullOrEmpty(varName2) && String.IsNullOrEmpty(value2)) //{ // MessageBox.Show("A variable has been selected for the second condition, but no value has been specified. Please specify a value and try again.", "No value specified", MessageBoxButton.OK, MessageBoxImage.Error); // return; //} //if (cmbLogicalOperator.SelectedIndex == 1 && !String.IsNullOrEmpty(value2) && String.IsNullOrEmpty(varName2)) //{ // MessageBox.Show("A value has been selected for the second condition, but no variable has been specified. Please specify a variable on which to filter and try again.", "No variable specified", MessageBoxButton.OK, MessageBoxImage.Error); // return; //} #endregion if (!String.IsNullOrEmpty(FilterField1) && !String.IsNullOrEmpty(FilterValue1)) { if (FilterOperator1.Equals("equals", StringComparison.OrdinalIgnoreCase)) { TextRowFilterCondition tfc = new TextRowFilterCondition("[" + FilterField1 + "] = @" + FilterField1 + "", "" + FilterField1 + "", "@" + FilterField1 + "", FilterValue1); tfc.Description = "" + FilterField1 + " equals " + FilterValue1; filters.Add(tfc); } else { string tempFilterValue1 = "%" + FilterValue1 + "%"; TextRowFilterCondition tfc = new TextRowFilterCondition("[" + FilterField1 + "] LIKE @" + FilterField1 + "", "" + FilterField1 + "", "@" + FilterField1 + "", tempFilterValue1); tfc.Description = "" + FilterField1 + " contains " + tempFilterValue1; tfc.ConditionOperator = ConditionOperators.Contains; filters.Add(tfc); } } if (!String.IsNullOrEmpty(FilterField2) && !String.IsNullOrEmpty(FilterValue2)) { if (FilterOperator2.Equals("equals", StringComparison.OrdinalIgnoreCase)) { TextRowFilterCondition tfc = new TextRowFilterCondition("[" + FilterField2 + "] = @" + FilterField2 + "", "" + FilterField2 + "", "@" + FilterField2 + "", FilterValue2); tfc.Description = "" + FilterField2 + " equals " + FilterValue2; filters.Add(tfc); } else { string tempFilterValue2 = "%" + FilterValue2 + "%"; TextRowFilterCondition tfc = new TextRowFilterCondition("[" + FilterField2 + "] LIKE @" + FilterField2 + "", "" + FilterField2 + "", "@" + FilterField2 + "", tempFilterValue2); tfc.Description = "" + FilterField2 + " contains " + tempFilterValue2; tfc.ConditionOperator = ConditionOperators.Contains; filters.Add(tfc); } } } CreateCaseSyncFileStart(filters); }
private bool CreateCaseSyncFile(ContactTracing.ImportExport.SyncFileFilters filters /*string fileName, bool includeCases, bool includeCaseExposures, bool includeContacts, Epi.ImportExport.Filters.RowFilters filters, bool deIdentifyData, Epi.RecordProcessingScope recordProcessingScope*/) { MajorProgressValue = 0; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); MajorSyncStatus = "Exporting data to " + SyncFilePath + "..."; ContactTracing.ImportExport.XmlDataExporter exporter; // = new ContactTracing.ImportExport.XmlSqlDataExporter(Project, includeContacts, recordProcessingScope); if (Database.ToString().ToLower().Contains("sql") && !(Database is Epi.Data.Office.OleDbDatabase)) { exporter = new ContactTracing.ImportExport.XmlSqlDataExporter(_project, IncludeCasesAndContacts, RecordProcessingScope); } else { exporter = new ContactTracing.ImportExport.XmlDataExporter(_project, IncludeCasesAndContacts, RecordProcessingScope); } if (filters != null) { if (exporter.Filters.ContainsKey(Core.Constants.CASE_FORM_NAME)) { exporter.Filters[Core.Constants.CASE_FORM_NAME] = filters; } else { exporter.Filters.Add(Core.Constants.CASE_FORM_NAME, filters); } } string tempXmlFileName = SyncFilePath + ".xml"; exporter.MinorProgressChanged += exporter_MinorProgressChanged; exporter.MajorProgressChanged += exporter_MajorProgressChanged; exporter.MinorStatusChanged += exporter_MinorStatusChanged; if (ApplyLastSaveFilter) { exporter.StartDate = StartDate; exporter.EndDate = EndDate; } if (DeIdentifyData) { if (!IsCountryUS) { exporter.AddFieldsToNull(new List <string> { "Surname", "OtherNames", "PhoneNumber", "PhoneOwner", "HeadHouse", "ContactName1", "ContactName2", "ContactName3", "FuneralName1", "FuneralName2", "HospitalBeforeIllPatient", "TradHealerName", "InterviewerName", "InterviewerPhone", "InterviwerEmail", "ProxyName" }, CaseForm.Name); exporter.AddFieldsToNull(new List <string> { "SurnameLab", "OtherNameLab" }, LabForm.Name); exporter.AddFieldsToNull(new List <string> { "ContactSurname", "ContactOtherNames", "ContactHeadHouse", "ContactPhone", "LC1" }, ContactForm.Name); } else { exporter.AddFieldsToNull(new List <string> { "Surname", "OtherNames", "PhoneNumber", "PhoneOwner", "HeadHouse", "ContactName1", "ContactName2", "ContactName3", "FuneralName1", "FuneralName2", "HospitalBeforeIllPatient", "TradHealerName", "InterviewerName", "InterviewerPhone", "InterviwerEmail", "ProxyName", "DOB", "Email", "AddressRes", "AddressOnset", "ProxyPhone", "ProxyEmail" }, CaseForm.Name); exporter.AddFieldsToNull(new List <string> { "SurnameLab", "OtherNameLab", "PersonLabSubmit", "PhoneLabSubmit", "EmailLabSubmit" }, LabForm.Name); exporter.AddFieldsToNull(new List <string> { "ContactSurname", "ContactOtherNames", "ContactHeadHouse", "ContactPhone", "LC1", "ContactDOB", "ContactAddress", "ContactEmail" }, ContactForm.Name); } } _increment = exporter.MajorIncrement; bool success = false; try { exporter.WriteTo(tempXmlFileName); success = true; } catch (Exception ex) { RecordsExported = "Export failed during write operation: " + ex.Message; } finally { exporter.MinorProgressChanged -= exporter_MinorProgressChanged; exporter.MajorProgressChanged -= exporter_MajorProgressChanged; exporter.MinorStatusChanged -= exporter_MinorStatusChanged; } if (!success) { MajorSyncStatus = "There was a problem exporting data."; sw.Stop(); System.IO.File.Delete(tempXmlFileName); return(success); } string casesExported = exporter.ExportInfo.RecordsPackaged[CaseForm].ToString(); string contactsExported = exporter.ExportInfo.RecordsPackaged[ContactForm].ToString(); string labsExported = exporter.ExportInfo.RecordsPackaged[LabForm].ToString(); RecordsExported = "Exported " + casesExported + " cases, " + contactsExported + " contacts, " + labsExported + " lab results."; DbLogger.Log(String.Format("Process 'export sync file' reports {0} case records, {1} contact records, and {2} lab records were written to disk.", casesExported, contactsExported, labsExported)); MajorSyncStatus = "Compressing data..."; try { System.IO.FileInfo fi = new System.IO.FileInfo(tempXmlFileName); Epi.ImportExport.ImportExportHelper.CompressDataPackage(fi); } catch (Exception ex) { RecordsExported = "Export failed during file compression: " + ex.Message; return(false); } string tempGzFileName = tempXmlFileName + ".gz"; MajorSyncStatus = "Encrypting data..."; try { Epi.Configuration.EncryptFile(tempGzFileName, SyncFilePath, "vQ@6L'<J3?)~5=vQnwh(2ic;>.<=dknF&/TZ4Uu!$78", String.Empty, String.Empty, 1000); } catch (Exception ex) { RecordsExported = "Export failed during file encryption: " + ex.Message; return(false); } MajorSyncStatus = "Deleting temporary files..."; try { System.IO.File.Delete(tempXmlFileName); System.IO.File.Delete(tempGzFileName); } catch (Exception ex) { RecordsExported = "Warning: Temporary files could not be cleaned. Message: " + ex.Message; } sw.Stop(); System.Diagnostics.Debug.Print("Export completed in " + sw.Elapsed.TotalMilliseconds + " ms."); return(success); //SyncStatus = "Export completed in " + sw.Elapsed.TotalSeconds.ToString("F1") + " seconds."; }
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()); }