Ejemplo n.º 1
0
        void AddOrUpdateBaks(IEnumerable <string> bakupFilePaths)
        {
            if (_isBackingUp)
            {
                return;
            }
            _isBackingUp = true;
            foreach (var f in bakupFilePaths)
            {
                MigrateIfRequired(f);
            }

#pragma warning disable IDE0067 // Dispose objects before losing scope
            BackgroundWorker worker = new BackgroundWorker()
            {
                WorkerReportsProgress = true
            };
#pragma warning restore IDE0067 // Dispose objects before losing scope
            worker.DoWork += SyncronisationResult.Sync;

            if (UpdateProgress != null)
            {
                worker.ProgressChanged += UpdateProgress;
            }
            bool isForUpdate = ParticipantAdded != null || ParticipantUpdated != null || ScreenedPatientAdded != null;
            if (isForUpdate)
            {
                worker.RunWorkerCompleted += WhenSyncronisationResultsAvailable;
            }
            if (AnyParticipantChange != null)
            {
                worker.RunWorkerCompleted += (o, e) => AnyParticipantChange(this, new LastUpdatedChangedEventAgs(LastCreateModifyParticipant()));
            }
            if (DatabaseUpdating != null)
            {
                DatabaseUpdating(this, new DatabaseUpdatingEventAgs(true));
                worker.RunWorkerCompleted += (o, e) => DatabaseUpdating(this, new DatabaseUpdatingEventAgs(false));
            }
            worker.RunWorkerCompleted += (o, e) => _isBackingUp = false;
            worker.RunWorkerCompleted += (o, e) =>
            {
                string fn = App.DataDirectory + '\\' + _dbContext.DbName + _bakExtension;
                _dbContext.Dispose();
                SyncronisationResult.RepairDb(fn);
                _dbContext = _createContext.Invoke();
                worker.Dispose();
            };
            worker.RunWorkerAsync(new SyncronisationResult.SyncArgs
            {
                DestContext   = _dbContext,
                DbFileNames   = bakupFilePaths.ToList(),
                UpdateResults = isForUpdate
            });
        }
Ejemplo n.º 2
0
        private void WhenSyncronisationResultsAvailable(object sender, RunWorkerCompletedEventArgs e)
        {
            SyncronisationResult syncResults = (SyncronisationResult)e.Result;

            if (ParticipantAdded != null)
            {
                foreach (var p in (from part in _dbContext.Participants.Include("VaccinesAdministered").Include("ProtocolViolations").Include("UnsuccessfulFollowUps")
                                   where syncResults.AddedParticipantIds.Contains(part.Id)
                                   select part))
                {
                    ParticipantAdded(this, new ParticipantEventArgs(p));
                }
            }
            if (ParticipantUpdated != null)
            {
                foreach (var p in (from part in _dbContext.Participants.Include("VaccinesAdministered").Include("ProtocolViolations").Include("UnsuccessfulFollowUps")
                                   where syncResults.UpdatedParticipantIds.Contains(part.Id) ||
                                   ((part.VaccinesAdministered.Any(v => syncResults.UpsertedVaccineAdministeredIds.Contains(v.Id)) ||
                                     part.UnsuccessfulFollowUps.Any(v => syncResults.UpsertedUnsuccessfulFollowUpIds.Contains(v.Id)) &&
                                     !syncResults.AddedParticipantIds.Contains(part.Id)))
                                   select part))
                {
                    ParticipantUpdated(this, new ParticipantEventArgs(p));
                }
            }
            if (ScreenedPatientAdded != null)
            {
                foreach (var s in (from screen in _dbContext.ScreenedPatients
                                   where syncResults.AddedScreenPatientIds.Contains(screen.Id)
                                   select screen))
                {
                    ScreenedPatientAdded(this, new ScreenedPatientEventArgs(s));
                }
            }
            if (ProtocolViolationAddOrUpdate != null)
            {
                foreach (var pv in (from v in _dbContext.ProtocolViolations
                                    where syncResults.AddedProtocolViolationIds.Contains(v.Id)
                                    select v))
                {
                    ProtocolViolationAddOrUpdate(this, new ProtocolViolationEventArgs(pv, CRUD.Created));
                }
                foreach (var pv in (from v in _dbContext.ProtocolViolations
                                    where syncResults.UpdatedProtocolViolationIds.Contains(v.Id)
                                    select v))
                {
                    ProtocolViolationAddOrUpdate(this, new ProtocolViolationEventArgs(pv, CRUD.Updated));
                }
            }
        }
Ejemplo n.º 3
0
        public void Backup()
        {
            string   bakFileName = _dbContext.BackupDb();
            FileInfo bakFile     = new FileInfo(bakFileName);

            if (!bakFile.Exists)
            {
                return;
            }
#if DEBUG
            if (!(CloudDirectories.Count() == 1))
            {
                throw new InvalidOperationException("Backup called without cloud directories set to a single directory");
            }
#endif
            string uniqueFileNameSuffix = '_' + LocalStudyCentres.First().DuplicateIdCheck.ToString("N");
            string cloudDir             = CloudDirectories.First();
            string cloudZipName         = cloudDir + '\\' + Path.GetFileNameWithoutExtension(bakFileName) + uniqueFileNameSuffix + ".zip";

            var cloudFile = new FileInfo(cloudZipName);

            if (cloudFile.Exists)
            {
                DateTime?mostRecentEntry = SyncronisationResult.MostRecentEntry(_dbContext);
                if (mostRecentEntry == null || cloudFile.LastWriteTimeUtc >= mostRecentEntry)
                {
                    return;
                }
            }
            _dbContext.Dispose(); // only necessary for ce
            SyncronisationResult.RepairDb(bakFileName);
            int    dotPos         = bakFileName.LastIndexOf('.');
            string copiedFileName = bakFileName.Insert(dotPos, uniqueFileNameSuffix);
            File.Copy(bakFileName, copiedFileName, true);

            void work()
            {
                BackupHelper.ZipVerifyAndPutInCloudDir(copiedFileName, cloudDir);
            }

            new Thread(work).Start();
            _dbContext = _createContext.Invoke();
        }