Exemplo n.º 1
0
        /// <summary>
        /// This method handles different down sync server responses in a common place.
        /// </summary>
        /// <typeparam name="TModel">The type model for which to handle the response</typeparam>
        /// <param name="response">The response itself</param>
        /// <returns>Returns a void Task</returns>
        private async Task HandleDownSyncResponse <TModel>(DownSyncServerResponse <TModel> response) where TModel : class
        {
            if (response == null)
            {
                return;
            }

            for (int i = 0; i < response.Package.Count; i++)
            {
                SynchronizableModelBase item = response.Package[i] as SynchronizableModelBase;
                if (item == null)
                {
                    break;
                }

                item.DontSync       = true;
                response.Package[i] = item as TModel;
            }

            Type t = typeof(TModel);

            if (t == typeof(Prospect))
            {
                // handle prospect down sync
                var prospectResponse = response as DownSyncServerResponse <Prospect>;
                await this.HandleResponse(prospectResponse);
            }

            if (t == typeof(Customer))
            {
                // handle customer down sync
                var customerResponse = response as DownSyncServerResponse <Customer>;
                await this.HandleResponse(customerResponse);
            }

            if (t == typeof(CustomerTicket))
            {
                // handle customer ticket down sync
                var customerTicketResponse = response as DownSyncServerResponse <CustomerTicket>;
                await this.HandleResponse(customerTicketResponse);
            }

            if (t == typeof(DsrTicket))
            {
                // handle customer dsr down sync
                var dsrTicketResponse = response as DownSyncServerResponse <DsrTicket>;
                await this.HandleResponse(dsrTicketResponse);
            }
        }
Exemplo n.º 2
0
        private async Task <PostResponse> Synchronize(SQLiteConnection connTran, object synchronizableModel)
        {
            SynchronizableModelBase syncModel = synchronizableModel as SynchronizableModelBase;

            if (syncModel == null)
            {
                throw new NullReferenceException("Unable to cast passed model into a syncable model.");
            }

            ApiBase apiBaseInstance = new SyncRegistry().GetApiBaseInstance(syncModel.TableName);

            if (apiBaseInstance == null)
            {
                Exception ex = new ApiControllerNotFoundException(syncModel.TableName);
                this.Logger.Error(ex);
                throw ex;
            }

            SyncRecord syncRec = await this.GetSyncRecordAsync(syncModel.RequestId);

            if (syncModel.DontSync)
            {
                if (syncRec == null)
                {
                    syncRec = new SyncRecord
                    {
                        ModelId   = syncModel.Id,
                        ModelType = syncModel.TableName,
                        RequestId = syncModel.RequestId,
                        Status    = syncModel.Channel == DataChannel.Fallback ? RecordStatus.FallbackSent : RecordStatus.Synced
                    };

                    this.Logger.Debug("Sync Status " + syncRec.Status);
                    await this.SaveAsync(connTran, syncRec);
                }

                return(new PostResponse
                {
                    RequestId = syncModel.RequestId,
                    Successful = true,
                    ResponseText = string.Empty
                });
            }

            PostResponse postResponse = await this.SyncUpAsync(synchronizableModel, apiBaseInstance);

            if (postResponse != null && postResponse.Successful)
            {
                this.Logger.Debug("Synced successfully");
                return(postResponse);
            }

            if (postResponse != null)
            {
                this.Logger.Debug("Synced successfully but the server response had an error");
                postResponse = await apiBaseInstance.AfterSyncUpProcessing(synchronizableModel, postResponse);

                if (postResponse.Successful)
                {
                    return(postResponse);
                }
            }

            if (connTran == null)
            {
                return(postResponse);
            }

            this.Logger.Debug("Didn't sync so we try and queue");
            syncRec = new SyncRecord
            {
                ModelType = syncModel.TableName,
                RequestId = syncModel.RequestId,
                ModelId   = syncModel.Id,
                Status    = RecordStatus.Pending
            };

            bool queued = this.QueueRecordForSyncing(connTran, syncRec);

            if (queued)
            {
                if (postResponse != null)
                {
                    return(postResponse);
                }

                Guid requestId = syncModel.RequestId;

                return(new PostResponse
                {
                    RequestId = requestId,
                    Successful = false,
                    ResponseText = string.Empty
                });
            }

            throw new NotQueuedException(syncModel.TableName);
        }