Exemple #1
0
        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());
        }
Exemple #2
0
        public void CreateCaseSyncFileStart(Epi.ImportExport.Filters.RowFilters filters, Epi.RecordProcessingScope recordProcessingScope)
        {
            if (IsWaitingOnOtherClients)
            {
                return;
            }

            if (String.IsNullOrEmpty(SyncFilePath.Trim()))
            {
                throw new InvalidOperationException();
            }

            var stopwatch = new System.Diagnostics.Stopwatch();

            stopwatch.Start();

            bool includeCases         = IncludeCasesAndContacts || IncludeCasesOnly;
            bool includeCaseExposures = true;
            bool includeContacts      = IncludeCasesAndContacts;
            bool deIdentifyData       = DeIdentifyData;

            #region Remove extraneous data

            int rows = 0;

            OverallSyncStatus = "Deleting extraneous page table rows...";
            IDbDriver db = _project.CollectedData.GetDatabase();

            foreach (View form in _project.Views)
            {
                foreach (Page page in form.Pages)
                {
                    Query deleteQuery = db.CreateQuery("DELETE FROM " + form.Name + " WHERE GlobalRecordId NOT IN (SELECT GlobalRecordId FROM " + page.TableName + ")");
                    rows = db.ExecuteNonQuery(deleteQuery);
                    if (rows > 0)
                    {
                        // report ??
                    }

                    Query pageDeleteQuery = db.CreateQuery("DELETE FROM " + page.TableName + " WHERE GlobalRecordId NOT IN (SELECT GlobalRecordId FROM " + form.Name + ")");
                    rows = db.ExecuteNonQuery(deleteQuery);
                    if (rows > 0)
                    {
                        // report ??
                    }
                }
            }

            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));
            rows = db.ExecuteNonQuery(linksDeleteQuery);

            if (db.TableExists("metaHistory"))
            {
                Query historyDeleteQuery = db.CreateQuery("DELETE FROM metaHistory WHERE ContactGUID NOT IN (SELECT GlobalRecordId FROM " + ContactForm.TableName + ")");
                rows = db.ExecuteNonQuery(historyDeleteQuery);
            }

            #endregion // Remove extraneous data

            RecordsExported         = String.Empty;
            IsDataSyncing           = true;
            IsShowingExportProgress = true;

            var doc = new XmlDocument {
                XmlResolver = null
            };

            SendMessageForAwaitAll();

            Task.Factory.StartNew(
                () =>
            {
                doc = CreateCaseSyncFile(includeCases, includeCaseExposures, includeContacts, filters, deIdentifyData, recordProcessingScope);
            },
                System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).ContinueWith(
                delegate
            {
                try
                {
                    if (!String.IsNullOrEmpty(doc.InnerText))
                    {
                        string compressedText = Epi.ImportExport.ImportExportHelper.Zip(doc.OuterXml);
                        compressedText        = "[[EPIINFO7_VHF_CASE_SYNC_FILE__0937]]" + compressedText;
                        Epi.Configuration.EncryptStringToFile(compressedText, SyncFilePath, "vQ@6L'<J3?)~5=vQnwh(2ic;>.<=dknF&/TZ4Uu!$78", "", "", 1000);
                    }
                }
                catch (Exception)
                {
                    // do nothing... if the XML is invalid, we should have already alerted the user in a different method
                }
                finally
                {
                    SendMessageForUnAwaitAll();
                }

                TaskbarProgressState = System.Windows.Shell.TaskbarItemProgressState.None;
                TaskbarProgressValue = 0;
                ProgressValue        = 0;

                IsDataSyncing = false;

                stopwatch.Stop();
                SyncStatus        = String.Empty;
                OverallSyncStatus = "Finished exporting data to sync file. Elapsed time: " + stopwatch.Elapsed.TotalMinutes.ToString("F1") + " minutes.";
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }