Example #1
0
        public async Task <VercorsContext> UpdateContext(VercorsContext vercorsContext)
        {
            this.CheckCancellation();

            try
            {
                vercorsContext.UserId = this.mobileService.CurrentUser.UserId;

                await this.mobileService.GetTable <VercorsContext>().UpdateAsync(vercorsContext);

                var result = await this.mobileService.GetTable <VercorsContext>().Where(t => t.Name == vercorsContext.Name).ToListAsync();

                if (result.Count == 1)
                {
                    return(result[0]);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception e)
            {
                LogService.Log(LogSource, e.ToString());

                return(null);
            }
        }
Example #2
0
        public async Task <bool> DeleteContext(VercorsContext vercorsContext)
        {
            this.CheckCancellation();

            try
            {
                await this.mobileService.GetTable <VercorsContext>().DeleteAsync(vercorsContext);

                var result = await this.mobileService.GetTable <VercorsContext>().Where(t => t.Name == vercorsContext.Name).ToListAsync();

                return(result.Count == 0);
            }
            catch (Exception e)
            {
                LogService.Log(LogSource, e.ToString());

                return(false);
            }
        }
Example #3
0
        private async Task ContextSync()
        {
            // add new local contexts in Vercors
            // use ToList() to have a copy of the list as each statement in the loop will change the collection
            foreach (var id in this.Metadata.AddedContexts.ToList())
            {
                IContext context = this.Workbook.Contexts.FirstOrDefault(c => c.Id == id);

                // we should never have a folder in the PendingAdded list that does not exist
                // but who knows...
                if (context != null)
                {
                    this.OnSynchronizationProgressChanged(string.Format(StringResources.SyncProgress_AddingContextFormat, context.Name));

                    VercorsContext vercorsContext = await this.service.AddContext(new VercorsContext(context));

                    if (vercorsContext != null)
                    {
                        context.SyncId = vercorsContext.Id;
                        this.Changes.WebAdd++;
                    }
                }

                this.Metadata.AddedContexts.Remove(id);
            }

            // remove deleted local contexts from Vercors
            this.UpdateDeletedContexts();
            foreach (var deletedEntry in this.Metadata.DeletedContexts.ToList())
            {
                this.OnSynchronizationProgressChanged(StringResources.SyncProgress_DeletingContext);
                await this.service.DeleteContext(new VercorsContext { ItemId = int.Parse(deletedEntry.SyncId) });

                this.Changes.WebDelete++;
                this.Metadata.DeletedContexts.Remove(deletedEntry);
            }

            // is LastFolderEdit newer than the last sync
            if (this.account.ContextEditTimestamp > this.contextEditTimestamp)
            {
                this.OnSynchronizationProgressChanged(StringResources.SyncProgress_GettingContexts);

                // there are probably changes in Vercors we must perform in the workbook...
                // start by fetching contexts
                var vercorsContexts = await this.service.GetContexts();

                // does the server have contexts we don't have ?
                this.EnsureWorkbookHasContext(vercorsContexts);

                // does the server misses contexts that we have);
                foreach (var context in this.Workbook.Contexts.ToList())
                {
                    var  vercorsContext = vercorsContexts.FirstOrDefault(f => f.Id == context.SyncId);
                    int  id             = -1;
                    bool hasId          = int.TryParse(context.SyncId, out id);

                    if (vercorsContext == null && hasId && id > 0)
                    {
                        // this context is no longer in Vercors, delete it from the workbook
                        this.DeleteContext(context.Name);

                        this.Changes.LocalDelete++;
                    }
                }

                foreach (var context in this.Workbook.Contexts)
                {
                    // does a context exist in both place
                    var vercorsContext = vercorsContexts.FirstOrDefault(f => f.Id == context.SyncId);

                    // if the name is not the same locally and in Vercors
                    if (!this.Metadata.EditedContexts.Contains(context.Id) && vercorsContext != null &&
                        !context.Name.Equals(vercorsContext.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        // Vercors has the latest value
                        vercorsContext.UpdateTarget(context);
                    }
                }
            }

            // do we need to edit contexts
            foreach (var contextId in this.Metadata.EditedContexts.ToList())
            {
                IContext context = this.Workbook.Contexts.FirstOrDefault(c => c.Id == contextId);
                if (context != null)
                {
                    this.OnSynchronizationProgressChanged(string.Format(StringResources.SyncProgress_UpdatingContextFormat, context.Name));

                    await this.service.UpdateContext(new VercorsContext(context));

                    this.Changes.WebEdit++;
                    this.Metadata.EditedContexts.Remove(contextId);
                }
            }
        }