private void UpwardProcess(AnnotationTaskContext context, string content) { var upwardReponse = JsonConvert.DeserializeObject <UpwardSyncResponse>(content); foreach (var item in upwardReponse.SyncStatus) { if (!item.Status) { continue; } var unSyncedAnnotations = allUnSyncedAnnotations.Where(o => o.BookID == item.DlId); foreach (var unSyncedAnnotation in unSyncedAnnotations) { if (unSyncedAnnotation.Status == (int)AnnotationStatusEnum.Deleted) { context.AnnotationAccess.DeleteAnnotation(unSyncedAnnotation.AnnotationCode); } else { unSyncedAnnotation.IsSynced = true; unSyncedAnnotation.LastSyncDate = DateTime.Now; context.AnnotationAccess.UpdateAnnotation(unSyncedAnnotation); } } } }
private UpwardRequests BuildRequest(AnnotationTaskContext context, int bookId, List <Annotations> unSyncedAnnotations) { UpwardRequests upwardRequest = new UpwardRequests { DeviceID = context.AnnotationSyncTask.DeviceId, DlCurrentVersion = context.AnnotationSyncTask.CurrentVersion[bookId], Email = context.AnnotationSyncTask.Email, DlId = bookId }; upwardRequest.UpwardSyncRequests = new List <UpwardSyncRequestEntity>(); foreach (var unSyncedAnnotation in unSyncedAnnotations) { if (unSyncedAnnotation == null) { continue; } UpwardSyncRequestEntity entity = new UpwardSyncRequestEntity { Annotation = unSyncedAnnotation.AnnotationContent, AnnotationID = unSyncedAnnotation.AnnotationCode.ToString(), Status = ((AnnotationStatusEnum)unSyncedAnnotation.Status).ToString().ToLower() }; upwardRequest.UpwardSyncRequests.Add(entity); } return(upwardRequest); }
public async Task PerformAction(AnnotationTaskContext context) { if (context.AnnotationSyncTask.BookIds == null || !context.AnnotationSyncTask.BookIds.Any()) { throw new InvalidOperationException("Book Id Shouldn't be empty."); } List <UpwardRequests> requests = new List <UpwardRequests>(); foreach (var bookId in context.AnnotationSyncTask.BookIds) { var unSyncedAnnotations = context.AnnotationAccess.GetUnSyncedAnnotations(context.AnnotationSyncTask.Email, context.AnnotationSyncTask.ServiceCode, bookId); if (unSyncedAnnotations.Count == 0) { continue; } allUnSyncedAnnotations.AddRange(unSyncedAnnotations); requests.Add(BuildRequest(context, bookId, unSyncedAnnotations)); } if (requests.Count > 0) { UpwardRequestsInput upwardRequestsInput = new UpwardRequestsInput(); upwardRequestsInput.UpwardRequests = requests; var upwardResult = await context.SyncService.UpwardSyncRequestV2(upwardRequestsInput); if (upwardResult.IsSuccess) { UpwardProcess(context, upwardResult.Content); } } }
public async Task PerformAction(AnnotationTaskContext context) { if (context.AnnotationSyncTask.BookIds == null || !context.AnnotationSyncTask.BookIds.Any()) { throw new InvalidOperationException("Book Id Shouldn't be empty."); } DownwardSyncRequests bookSpecificDownwardSyncRequest = new DownwardSyncRequests { DownwardSyncRequest = new List <DownwardSyncRequest>() }; foreach (var bookId in context.AnnotationSyncTask.BookIds) { bookSpecificDownwardSyncRequest.DownwardSyncRequest.Add(new DownwardSyncRequest { UserID = context.AnnotationSyncTask.Email, DLID = bookId, DLVersionID = context.AnnotationSyncTask.CurrentVersion[bookId], DeviceID = context.AnnotationSyncTask.DeviceId }); } await CleanTempFiles(context); await context.SyncService.DownwardSyncRequest(bookSpecificDownwardSyncRequest, context.AnnotationDownloadFile); await IoCContainer.Instance.ResolveInterface <IPackageFile>().DepressFile(context.AnnotationDownloadFile, context.AnnotationDownloadTempFolder, default(CancellationToken)); foreach (var bookId in context.AnnotationSyncTask.BookIds) { string targetDirectory = Path.Combine(context.AnnotationDownloadTempFolder, bookId.ToString()); if (!await GlobalAccess.DirectoryService.DirectoryExists(targetDirectory)) { await GlobalAccess.DirectoryService.CreateDirectory(targetDirectory); } string sourceFile = Path.Combine(context.AnnotationDownloadTempFolder, string.Format("DL_{0}.zip", bookId)); await IoCContainer.Instance.ResolveInterface <IPackageFile>().DepressFile(sourceFile, targetDirectory, default(CancellationToken)); if (await GlobalAccess.DirectoryService.FileExists(Path.Combine(targetDirectory, Constants.EmptyStreamXml))) { continue; } foreach (var fileName in await GlobalAccess.DirectoryService.GetFiles(targetDirectory)) { if (fileName.EndsWith(Constants.SDeletedAnnotationsXml)) //if the annotation had been deleted then delete from local { await DeleteAnnotation(context, fileName); } else if (!fileName.EndsWith(Constants.SRequestxml) && !fileName.EndsWith(Constants.SErrorxml)) //if it is in the request file, then merge with local file. { await MergeAnnotation(context, fileName, bookId); } } } await CleanTempFiles(context); }
public async Task PerformAction(AnnotationTaskContext context) { var syncResult = await SyncFromRemote(context); if (!syncResult) { syncResult = await SyncFromRemote(context); } }
private static async Task DeleteAnnotation(AnnotationTaskContext context, string strFileName) { using (var stream = await GlobalAccess.DirectoryService.OpenFile(strFileName, BusinessModel.FileModeEnum.Open)) { var responseElement = XDocument.Load(strFileName).Root; foreach (var idElement in responseElement.Descendants(Constants.SId)) { var annotationCode = Guid.Parse(idElement.Value); context.AnnotationAccess.DeleteAnnotation(annotationCode); } } }
private static async Task CleanTempFiles(AnnotationTaskContext context) { try { if (await GlobalAccess.DirectoryService.FileExists(context.AnnotationDownloadFile)) { await GlobalAccess.DirectoryService.DeleteFile(context.AnnotationDownloadFile); } if (await GlobalAccess.DirectoryService.DirectoryExists(context.AnnotationDownloadTempFolder)) { await GlobalAccess.DirectoryService.DeleteDirectory(context.AnnotationDownloadTempFolder); } } catch (Exception ex) { Logger.Log(ex.ToString()); } }
private static async Task MergeAnnotation(AnnotationTaskContext context, string strFileName, int bookID) { XDocument xDocument = null; using (var stream = await GlobalAccess.DirectoryService.OpenFile(strFileName, BusinessModel.FileModeEnum.Open)) { xDocument = XDocument.Load(stream); } if (xDocument != null) { var annotationCode = Guid.Parse(xDocument.Root.Attribute(Constants.SId).Value); var localAnnotations = context.AnnotationAccess.getAnnotation(annotationCode); if (localAnnotations != null) { var annotation = AnnotationFactory.CreateAnnotation(localAnnotations.AnnotationContent); var localDateTime = annotation.UpdatedTime; var serverDateTime = DateTime.Parse(xDocument.Root.Attribute(XName.Get(Constants.SUpdatedOn)).Value); var dateDiff = DateTime.Compare(localDateTime, serverDateTime); if (dateDiff > 0) //local version is newer than the server version { return; } localAnnotations.AnnotationContent = xDocument.Root.ToString(); localAnnotations.IsSynced = true; localAnnotations.AnnotationType = int.Parse(xDocument.Root.Attribute(Constants.SType).Value); localAnnotations.LastSyncDate = DateTime.Now; var statusString = xDocument.Root.Attribute(Constants.Status).Value; AnnotationStatusEnum annoStatus = (AnnotationStatusEnum)Enum.Parse(typeof(AnnotationStatusEnum), statusString[0].ToString().ToUpper() + statusString.Substring(1)); localAnnotations.Status = (int)annoStatus; localAnnotations.DocumentID = xDocument.Root.Attribute(Constants.DocId).Value; context.AnnotationAccess.UpdateAnnotation(localAnnotations); } else { Annotations annotations = new Annotations { Email = context.AnnotationSyncTask.Email, ServiceCode = context.AnnotationSyncTask.ServiceCode, AnnotationCode = annotationCode, BookID = bookID, IsSynced = true, CreatedDate = DateTime.Now, LastModifiedDate = DateTime.Now, LastSyncDate = DateTime.Now }; annotations.DocumentID = xDocument.Root.Attribute(Constants.DocId).Value; annotations.AnnotationType = int.Parse(xDocument.Root.Attribute(Constants.SType).Value); var statusString = xDocument.Root.Attribute(Constants.Status).Value; AnnotationStatusEnum annoStatus = (AnnotationStatusEnum)Enum.Parse(typeof(AnnotationStatusEnum), statusString[0].ToString().ToUpper() + statusString.Substring(1)); annotations.Status = (int)annoStatus; annotations.AnnotationContent = xDocument.Root.ToString(); context.AnnotationAccess.AddAnnotation(annotations); } } }
private async Task <bool> SyncFromRemote(AnnotationTaskContext context) { bool syncResult = false; try { List <AnnotationTag> tags; AnnCategoryTags annCategorytag = new AnnCategoryTags(); string email = context.AnnotationSyncTask.Email; string serviceCode = context.AnnotationSyncTask.ServiceCode; tagDomainService.LoadTagsFromDb(email, serviceCode, out annCategorytag, out tags); AnnCategoryTagsSyncData syncData = new AnnCategoryTagsSyncData(); syncData.UserName = email; syncData.LSST = tagDomainService.AnnCategorytag.LastServerSyncTime.HasValue ? tagDomainService.AnnCategorytag.LastServerSyncTime.Value : DateTime.Now.AddYears(-20); syncData.AnnotationTagsXml = tagDomainService.AnnCategorytag.IsModified ? tagDomainService.ToXmlForSync(tags) : null; syncData = await context.SyncService.AnnCategoryTagsSync(syncData); if (syncData != null) { if (syncData.LSST.HasValue) { tagDomainService.AnnCategorytag.LastServerSyncTime = syncData.LSST.Value; } if (tagDomainService.AnnCategorytag.IsModified) { if (string.IsNullOrEmpty(syncData.AnnotationTagsXml)) {//sync success tagDomainService.AnnCategorytag.IsModified = false; syncResult = true; } else { //sync failure since server tags have been updated by other device syncResult = false; } } else { syncResult = true; } if (!string.IsNullOrEmpty(syncData.AnnotationTagsXml)) { var serverTags = tagDomainService.GenerateTagsFromTagXML(syncData.AnnotationTagsXml); if (!syncResult) { var exceptTags = serverTags.Except(tags, new TagEqualityComparer()); tags.AddRange(exceptTags); } else { tags = serverTags; } tagDomainService.SaveToSqlite(tags, email, serviceCode); } } } catch (Exception ex) { Logger.Log("PerformSyncTags error:" + ex.ToString()); } return(syncResult); }