/// <summary> /// Create sync state in database for a sync down and return corresponding SyncState /// </summary> /// <param name="store"></param> /// <param name="target"></param> /// <param name="options"></param> /// <param name="soupName"></param> /// <returns></returns> public static SyncState CreateSyncDown(SmartStore.Store.SmartStore store, SyncDownTarget target, string soupName, SyncOptions options = null) { var sync = new JObject { { Constants.SyncType, SyncTypes.SyncDown.ToString() }, { Constants.SyncTarget, target.AsJson() }, { Constants.SyncSoupName, soupName }, { Constants.SyncStatus, SyncStatusTypes.New.ToString() }, { Constants.SyncProgress, 0 }, { Constants.SyncTotalSize, -1 }, { Constants.SyncMaxTimeStamp, -1 }, }; if (options != null) { sync[Constants.SyncOptions] = options.AsJson(); } JObject upserted = store.Upsert(Constants.SyncsSoup, sync); if (upserted != null) { return(FromJson(upserted)); } sync[Constants.SyncStatus] = SyncStatusTypes.Failed.ToString(); return(FromJson(sync)); }
/// <summary> /// Build SyncState from json /// </summary> /// <param name="sync"></param> /// <returns></returns> public static SyncState FromJson(JObject sync) { if (sync == null) { return(null); } var jsonTarget = sync.ExtractValue <JObject>(Constants.SyncTarget); var syncType = (SyncTypes)Enum.Parse(typeof(SyncTypes), sync.ExtractValue <string>(Constants.SyncType)); var state = new SyncState { Id = sync.ExtractValue <long>(SmartStore.Store.SmartStore.SoupEntryId), Target = (syncType == SyncTypes.SyncDown ? (SyncTarget)SyncDownTarget.FromJson(jsonTarget) : SyncUpTarget.FromJson(jsonTarget)), Options = SyncOptions.FromJson(sync.ExtractValue <JObject>(Constants.SyncOptions)), SoupName = sync.ExtractValue <string>(Constants.SyncSoupName), Progress = sync.ExtractValue <int>(Constants.SyncProgress), TotalSize = sync.ExtractValue <int>(Constants.SyncTotalSize), Type = syncType, Status = (SyncStatusTypes) Enum.Parse(typeof(SyncStatusTypes), sync.ExtractValue <string>(Constants.SyncStatus)), MaxTimeStamp = sync.ExtractValue <long>(Constants.SyncMaxTimeStamp) }; return(state); }
private static SyncTarget GetSyncTargetFromJson(SyncTypes syncType, JObject jsonTarget) { SyncTarget st = null; switch (syncType) { case SyncTypes.SyncDown: st = SyncDownTarget.FromJson(jsonTarget); break; case SyncTypes.SyncDownAttachment: st = AttachmentSyncDownTarget.FromJson(jsonTarget); break; case SyncTypes.SyncUp: st = SyncUpTarget.FromJSON(jsonTarget); break; } return(st); }
/// <summary> /// Create sync state in database for a sync down and return corresponding SyncState /// </summary> /// <param name="store"></param> /// <param name="target"></param> /// <param name="options"></param> /// <param name="soupName"></param> /// <returns></returns> public static SyncState CreateSyncDown(SmartStore.Store.SmartStore store, SyncDownTarget target, string soupName, SyncOptions options = null) { var sync = new JObject { {Constants.SyncType, SyncTypes.SyncDown.ToString()}, {Constants.SyncTarget, target.AsJson()}, {Constants.SyncSoupName, soupName}, {Constants.SyncStatus, SyncStatusTypes.New.ToString()}, {Constants.SyncProgress, 0}, {Constants.SyncTotalSize, -1}, {Constants.SyncMaxTimeStamp, -1}, }; if (options != null) { sync[Constants.SyncOptions] = options.AsJson(); } JObject upserted = store.Upsert(Constants.SyncsSoup, sync); if (upserted != null) { return FromJson(upserted); } sync[Constants.SyncStatus] = SyncStatusTypes.Failed.ToString(); return FromJson(sync); }