Esempio n. 1
0
        public async Task <Key> Save(DbSaveableEntity saveableEntity)
        {
            var assets    = AppInstance.Instance.ApiAssets;
            var projectId = assets[Constants.ASSET_PROJECT_ID];
            var now       = DateTime.Now;
            var asBinary  = now.ToBinary();
            var entity    = new Entity()
            {
                Key = new Key()
                {
                    PartitionId = new PartitionId()
                    {
                        NamespaceId = "", ProjectId = projectId
                    },
                    Path = new List <PathElement>()
                    {
                        new PathElement()
                        {
                            Kind = saveableEntity.kindName.Value,
                            Name = saveableEntity.Id.Value
                        }
                    }
                },
                Properties = new Dictionary <string, Value>()
                {
                    { "id", new Value()
                      {
                          StringValue = saveableEntity.Id.Value
                      } },
                    { "entityid", new Value()
                      {
                          StringValue = saveableEntity.EntityId.Value
                      } },
                    //{"dateadded", new Value() { IntegerValue = now.toYMDInt() } },
                    { "editday", new Value()
                      {
                          IntegerValue = now.toYMDInt()
                      } },
                    { "editdate", new Value()
                      {
                          IntegerValue = asBinary
                      } },
                    { "datablob", new Value()
                      {
                          ExcludeFromIndexes = true,
                          StringValue        = saveableEntity
                                               .getJson()
                                               .Encrypt()
                                               .Value
                      } },
                    { "kindmetadata", new Value()
                      {
                          StringValue = saveableEntity.Entity.KindMetaData ?? string.Empty
                      } }
                }
            };
            var datastore = GetDatastoreService(GetDefaultCredential(assets, _assetManager), assets);

            return(await SaveToCloud(datastore, projectId, entity));
        }
Esempio n. 2
0
        public int AddToOutQueue(DbSaveableEntity saveableEntity)
        {
            var asString  = saveableEntity.getJson();
            var outEntity = new OutEntity()
            {
                Id       = saveableEntity.Id.Value,
                DataBlob = asString
            };
            var res = new OutDb().DB.InsertOrReplace(outEntity);

            return(res);
        }
Esempio n. 3
0
        async Task <int> doServerSync(Action <string, ToastLength> makeToast)
        {
            var recs = GetRecordsToSync();

            if (recs.Count == 0)
            {
                makeToast("No unsynced records", ToastLength.Short);
                return(0);
            }

            var hasConnection = await checkConnection();

            if (!hasConnection)
            {
                isRunning = 0;
                if (makeToast != null)
                {
                    makeToast("No connection detected", ToastLength.Long);
                }
                return(0);
            }
            makeToast("Connected tested", ToastLength.Short);
            var cloudDb = new OutDb().DB;

            var recIndex = recs.Count - 1;

            while (recIndex >= 0 && hasConnection)
            {
                var outEntity = recs[recIndex];
                var ppdataset = DbSaveableEntity.fromJson <GeneralEntityDataset>(new KindItem(outEntity.DataBlob));
                var saveable  = new DbSaveableEntity(ppdataset)
                {
                    kindName = new KindName(ppdataset.FormName)
                };

                var saved = false;
                for (int i = 0; i < 4; i++)
                {
                    try
                    {
                        await Save(saveable);

                        //we remove this key from the database
                        saved = true;
                    }
                    catch (Google.GoogleApiException gex)
                    {
                        //todo: mark this record as bad to prevent it blocking for life

                        //cloudDb.InsertOrReplace(new OutEntityUnsynced().load(outEntity));
                        //cloudDb.Delete<OutEntity>(saveable.Id.Value);
                        //break;
                    }
                    catch (System.Net.WebException wex)
                    {
                        //perhaps lost connection
                        //we alllow it to spin for now
                    }
                    catch (Exception ex)
                    {
                        //unknown exception
                    }
                    finally { }
                    if (saved)
                    {
                        try
                        {
                            var deleted = cloudDb.Delete <OutEntity>(saveable.Id.Value);
                        }
                        catch
                        {
                        }
                        break;
                    }
                    else
                    {
                        //lets add a 2 second delay in case it failed the first time
                        await Task.Delay(TimeSpan.FromMilliseconds(2000));
                    }
                }
                recIndex--;
                hasConnection = await checkConnection();
            }
            return(0);
        }