Ejemplo n.º 1
0
        public static SyncManager GetInstance(Account account, string communityId, SmartStore.Store.SmartStore smartStore)
        {
            if (account == null)
            {
                account = AccountManager.GetAccount();
            }

            if (smartStore == null)
            {
                smartStore = SmartStore.Store.SmartStore.GetSmartStore(account);
            }

            string uniqueId = Constants.GenerateUniqueId(account, smartStore);

            lock (Synclock)
            {
                var         client   = new ClientManager().PeekRestClient(account);
                SyncManager instance = null;
                if (_instances != null)
                {
                    if (_instances.TryGetValue(uniqueId, out instance))
                    {
                        SyncState.SetupSyncsSoupIfNeeded(instance._smartStore);
                        instance = new SyncManager(instance._smartStore, client);
                        return(instance);
                    }

                    instance = new SyncManager(smartStore, client);
                    _instances.Add(uniqueId, instance);
                }
                else
                {
                    _instances = new Dictionary <string, SyncManager>();
                    instance   = new SyncManager(smartStore, client);
                    _instances.Add(uniqueId, instance);
                }
                SyncState.SetupSyncsSoupIfNeeded(instance._smartStore);
                return(instance);
            }
        }
Ejemplo n.º 2
0
        private async Task SyncUp(SyncState sync, Action <SyncState> callback, CancellationToken token = default(CancellationToken), Action <string, string> idChangeHandler = default(Action <string, string>))
        {
            if (sync == null)
            {
                throw new SmartStoreException("SyncState sync was null");
            }
            var target = (SyncUpTarget)sync.Target;

            try
            {
                var account = AccountManager.GetAccount();
                if (account != null)
                {
                    await OAuth2.CallIdentityService(account.IdentityUrl, SyncManager.GetInstance().RestClient);
                }
            }
            catch (Exception e)
            {
                PlatformAdapter.SendToCustomLogger(e, LoggingLevel.Error);
                if (!string.IsNullOrEmpty(e.Message) && (e.Message.ToUpper().Contains("FORBIDDEN") || e.Message.Contains("403")))
                {
                    throw new OAuthException("Token Expired");
                }
            }

            HashSet <string> dirtyRecordIds = GetDirtyRecordIds(sync.SoupName, SmartStore.Store.SmartStore.SoupEntryId);
            int totalSize = dirtyRecordIds.Count;

            sync.TotalSize = totalSize;
            int i = 0;

            foreach (
                JObject record in
                dirtyRecordIds.Select(
                    id => _smartStore.Retrieve(sync.SoupName, long.Parse(id))[0].ToObject <JObject>()))
            {
                if (token.IsCancellationRequested)
                {
                    token.ThrowIfCancellationRequested();
                }

                List <string> fields = new List <string>(sync.Options.FieldList);
                if (record.ExtractValue <bool>(LocallyDeleted))
                {
                }
                else if (record.ExtractValue <bool>(LocallyCreated))
                {
                }
                else if (record.ExtractValue <bool>(LocallyUpdated))
                {
                    foreach (string value in sync.Options.fieldsToExcludeOnUpdate)
                    {
                        if (fields.Contains(value))
                        {
                            fields.Remove(value);
                        }
                    }
                }
                bool res = await SyncUpOneRecord(target, sync.SoupName, fields, record, sync.MergeMode, idChangeHandler);

                if (res)
                {
                    i++;
                }
                int progress = i * 100 / totalSize;
                if (progress < 100)
                {
                    UpdateSync(sync, SyncState.SyncStatusTypes.Running, progress, callback);
                }
            }
            if (i < totalSize)
            {
                throw new SmartStoreException("Cannot sync all records");
            }
        }