Exemple #1
0
        private void OnBackgroundEvent()
        {
            try
            {
                var       lastPersistence = new WinPersistenceLayer();
                IWorkbook newWorkbook     = lastPersistence.Open();
                using (this.workbook.WithDuplicateProtection())
                {
                    foreach (ITask newTask in newWorkbook.Tasks)
                    {
                        if (this.workbook.Tasks.All(t => t.Id != newTask.Id))
                        {
                            // this is a new task we don't have in the "current" workbook
                            var oldTask = this.workbook.CreateTask(newTask.Id);
                            ModelHelper.CloneTask(oldTask, newTask, this.workbook);
                        }

                        var existingTask = this.workbook.Tasks.FirstOrDefault(t => t.Id == newTask.Id);
                        if (existingTask != null && existingTask.Modified < newTask.Modified)
                        {
                            // this is a task that has been updated
                            ModelHelper.CloneTask(existingTask, newTask, this.workbook);
                        }
                    }
                    lastPersistence.CloseDatabase();
                }
            }
            catch (Exception ex)
            {
                TrackingManagerHelper.Exception(ex, "BackgroundTaskManager.OnBackgroundEvent");
            }
        }
Exemple #2
0
        private async Task UpdateWorkbookAsync(SynchronizationMetadata metadata)
        {
            // load a new workbook from the persisted db to have the latest version
            var       lastPersistence = new WinPersistenceLayer();
            IWorkbook lastWorkbook    = lastPersistence.Open();

            foreach (string folderName in metadata.AfterSyncEditedFolders)
            {
                var newFolder = lastWorkbook.Folders.FirstOrDefault(f => f.Name.Equals(folderName, StringComparison.OrdinalIgnoreCase));
                var oldFolder = this.workbook.Folders.FirstOrDefault(f => newFolder != null ? f.Id == newFolder.Id : f.Name == folderName);

                if (newFolder == null)
                {
                    // folder was deleted
                    this.workbook.RemoveFolder(folderName);
                }
                else if (oldFolder != null)
                {
                    // folder was updated
                    ModelHelper.CloneFolder(oldFolder, newFolder);
                }
                else
                {
                    // folder was created
                    oldFolder = this.workbook.AddFolder(folderName, newFolder.SyncId, newFolder.Id);
                    ModelHelper.CloneFolder(oldFolder, newFolder);
                }
            }
            metadata.AfterSyncEditedFolders.Clear();

            foreach (string contextName in metadata.AfterSyncEditedContexts)
            {
                var newContext = lastWorkbook.Contexts.FirstOrDefault(c => c.Name.Equals(contextName, StringComparison.OrdinalIgnoreCase));
                var oldContext = this.workbook.Contexts.FirstOrDefault(c => newContext != null ? c.Id == newContext.Id : c.Name == contextName);

                if (newContext == null)
                {
                    // context was deleted
                    this.workbook.RemoveContext(contextName);
                }
                else if (oldContext != null)
                {
                    // context was updated
                    oldContext.Name  = newContext.Name;
                    oldContext.Order = newContext.Order;
                }
                else
                {
                    // context was created
                    this.workbook.AddContext(contextName, newContext.SyncId, newContext.Id);
                }
            }
            metadata.AfterSyncEditedContexts.Clear();

            foreach (string smartviewName in metadata.AfterSyncEditedSmartViews)
            {
                var newSmartView = lastWorkbook.SmartViews.FirstOrDefault(c => c.Name.Equals(smartviewName, StringComparison.OrdinalIgnoreCase));
                var oldSmartView = this.workbook.SmartViews.FirstOrDefault(c => newSmartView != null ? c.Id == newSmartView.Id : c.Name == smartviewName);

                if (newSmartView == null)
                {
                    // smart view was deleted
                    this.workbook.RemoveSmartView(smartviewName);
                }
                else if (oldSmartView != null)
                {
                    // smart view was updated
                    oldSmartView.Name  = newSmartView.Name;
                    oldSmartView.Order = newSmartView.Order;
                    oldSmartView.Rules = newSmartView.Rules;
                }
                else
                {
                    // smart view was created
                    var smartview = this.workbook.AddSmartView(smartviewName, newSmartView.Rules, newSmartView.Id);
                    smartview.SyncId = newSmartView.SyncId;
                }
            }
            metadata.AfterSyncEditedSmartViews.Clear();

            foreach (int id in metadata.AfterSyncEditedTasks)
            {
                var oldTask = this.workbook.Tasks.FirstOrDefault(t => t.Id == id);
                var newTask = lastWorkbook.Tasks.FirstOrDefault(t => t.Id == id);
                if (newTask == null)
                {
                    // task was deleted
                    if (oldTask != null)
                    {
                        oldTask.Delete();
                    }
                }
                else if (oldTask != null)
                {
                    // task was updated
                    ModelHelper.CloneTask(oldTask, newTask, this.workbook);
                }
                else
                {
                    // task was created
                    oldTask = this.workbook.CreateTask(newTask.Id);
                    ModelHelper.CloneTask(oldTask, newTask, this.workbook);
                }
            }
            metadata.AfterSyncEditedTasks.Clear();

            lastPersistence.CloseDatabase();
            await this.WriteSyncMetadataAsync(metadata);
        }