static private void InitDatabase() { var dbContext = new ContosoO365DocSyncDbContext(); var SourceCatalog = new SourceCatalog() { Name = "https://cand3.onmicrosoft.com/test/SourceCatalog1.xlsx" }; dbContext.SourceCatalogs.Add(SourceCatalog); var sourcePoints = new SourcePoint[500]; var destinationCatalog = new DestinationCatalog[500]; for (int i = 0; i < 500; i++) { sourcePoints[i] = new SourcePoint() { Name = $"SourcePoint{i}", Catalog = SourceCatalog, RangeId = $"Range{i}", Creator = $"Creator{i}", Position = $"Position{i}", Value = $"Value{i}", Status = SourcePointStatus.Created, Created = DateTime.UtcNow }; dbContext.SourcePoints.Add(sourcePoints[i]); destinationCatalog[i] = new DestinationCatalog() { Name = $"https://cand3.onmicrosoft.com/test/DestinationCatalog{i}.docx" }; dbContext.DestinationCatalogs.Add(destinationCatalog[i]); destinationCatalog[i].DestinationPoints.Add(new DestinationPoint() { Catalog = destinationCatalog[i], RangeId = $"Range{i}", Creator = $"Creator{i}", Created = DateTime.UtcNow, ReferencedSourcePoint = sourcePoints[i] }); } dbContext.SaveChanges(); }
public async Task <DestinationPoint> AddDestinationPoint(string fileName, string documentId, DestinationPoint destinationPoint) { try { var destinationCatalog = _dbContext.DestinationCatalogs.FirstOrDefault(o => o.DocumentId == documentId); bool addDestinationCatalog = (destinationCatalog == null); if (addDestinationCatalog) { try { destinationCatalog = new DestinationCatalog() { Name = fileName, DocumentId = documentId }; _dbContext.DestinationCatalogs.Add(destinationCatalog); } catch (Exception ex) { var entity = new LogEntity() { LogId = "40002", Action = Constant.ACTIONTYPE_ADD, ActionType = ActionTypeEnum.ErrorLog, PointType = Constant.POINTYTPE_DESTINATIONCATALOG, Message = ".Net Error", }; entity.Subject = $"{entity.LogId} - {entity.Action} - {entity.PointType} - Error"; await _logService.WriteLog(entity); throw new ApplicationException("Add Source Catalog failed", ex); } } destinationPoint.Created = DateTime.Now.ToUniversalTime().ToPSTDateTime(); destinationPoint.Creator = _userProfileService.GetCurrentUser().Username; destinationCatalog.DestinationPoints.Add(destinationPoint); _dbContext.SourcePoints.Attach(destinationPoint.ReferencedSourcePoint); _dbContext.DestinationPoints.Add(destinationPoint); await _dbContext.Entry(destinationPoint.ReferencedSourcePoint).ReloadAsync(); // Add recent files var recentFile = new RecentFile(); recentFile.User = _userProfileService.GetCurrentUser().Username; recentFile.Date = DateTime.Now.ToUniversalTime().ToPSTDateTime(); recentFile.CatalogId = (await _dbContext.SourcePoints.FirstOrDefaultAsync(o => o.Id == destinationPoint.ReferencedSourcePoint.Id)).CatalogId; _dbContext.RecentFiles.Add(recentFile); await _dbContext.SaveChangesAsync(); if (addDestinationCatalog) { await _logService.WriteLog(new LogEntity() { LogId = "40001", Action = Constant.ACTIONTYPE_ADD, PointType = Constant.POINTYTPE_DESTINATIONCATALOG, ActionType = ActionTypeEnum.AuditLog, Message = $"Add destination catalog {destinationCatalog.Name}." }); } await _logService.WriteLog(new LogEntity() { LogId = "20001", Action = Constant.ACTIONTYPE_ADD, PointType = Constant.POINTTYPE_DESTINATIONPOINT, ActionType = ActionTypeEnum.AuditLog, Message = $"Create destination point value: {destinationPoint.ReferencedSourcePoint.Value} in the word file named:{destinationCatalog.FileName} by {_userProfileService.GetCurrentUser().Username}" }); destinationPoint = await _dbContext.DestinationPoints .Include(o => o.ReferencedSourcePoint) .Include(o => o.ReferencedSourcePoint.Catalog) .Include(o => o.DestinationPointCustomFormats).ThenInclude(x => x.CustomFormat).FirstAsync(o => o.Id == destinationPoint.Id); destinationPoint.ReferencedSourcePoint.PublishedHistories = (await _dbContext.PublishedHistories.Where(o => o.SourcePointId == destinationPoint.ReferencedSourcePoint.Id).ToArrayAsync()).OrderByDescending(p => p.PublishedDate).ToArray(); destinationPoint.ReferencedSourcePoint.SerializeCatalog = true; destinationPoint.ReferencedSourcePoint.Catalog.SerializeSourcePoints = false; destinationPoint.DestinationPointCustomFormats = destinationPoint.DestinationPointCustomFormats; await _logService.WriteLog(new LogEntity() { LogId = "30002", Action = Constant.ACTIONTYPE_GET, PointType = Constant.POINTTYPE_SOURCECATALOGLIST, ActionType = ActionTypeEnum.AuditLog, Message = $"Get source catalogs." }); return(destinationPoint); } catch (ApplicationException ex) { throw ex.InnerException; } catch (Exception ex) { var logEntity = new LogEntity() { LogId = "20003", Action = Constant.ACTIONTYPE_ADD, ActionType = ActionTypeEnum.ErrorLog, PointType = Constant.POINTTYPE_DESTINATIONPOINT, Message = ".Net Error", Detail = ex.ToString() }; logEntity.Subject = $"{logEntity.LogId} - {logEntity.Action} - {logEntity.PointType} - Error"; await _logService.WriteLog(logEntity); throw ex; } }
public async Task CloneFiles(IEnumerable <CloneForm> files) { try { var filesWillClone = files.Where(o => o.Clone); var clonedSourcePoints = new Dictionary <Guid, SourcePoint>(); foreach (var item in filesWillClone) { if (item.IsExcel) { var catalog = await _dbContext.SourceCatalogs.Where(o => o.DocumentId.Equals(item.DocumentId, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefaultAsync(); if (catalog != null) { var sourcePoints = await _dbContext.SourcePoints.Where(o => o.Status == SourcePointStatus.Created && o.CatalogId == catalog.Id) .Include(o => o.DestinationPoints) .ToArrayAsync(); var sourcePointIds = sourcePoints.Select(point => point.Id).ToArray(); var publishedHistories = await(from pb in _dbContext.PublishedHistories where sourcePointIds.Contains(pb.SourcePointId) select pb).ToArrayAsync(); #region Add new source catalog SourceCatalog newSourceCatalog = new SourceCatalog() { Name = item.DestinationFileUrl, DocumentId = item.DestinationFileDocumentId, IsDeleted = false }; _dbContext.SourceCatalogs.Add(newSourceCatalog); #endregion #region Add new source point foreach (var sourcePoint in sourcePoints) { var lastPublishedValue = publishedHistories.Where(o => o.SourcePointId == sourcePoint.Id).OrderByDescending(o => o.PublishedDate).FirstOrDefault().Value; var newSourcePoint = new SourcePoint() { Name = sourcePoint.Name, RangeId = sourcePoint.RangeId, Position = sourcePoint.Position, Value = sourcePoint.Value, Created = DateTime.Now.ToUniversalTime().ToPSTDateTime(), Creator = _userProfileService.GetCurrentUser().Username, Status = SourcePointStatus.Created, NamePosition = sourcePoint.NamePosition, NameRangeId = sourcePoint.NameRangeId, SourceType = sourcePoint.SourceType }; _dbContext.PublishedHistories.Add(new PublishedHistory() { Name = sourcePoint.Name, Position = sourcePoint.Position, Value = "Cloned", PublishedUser = _userProfileService.GetCurrentUser().Username, PublishedDate = DateTime.Now.ToUniversalTime().ToPSTDateTime(), SourcePointId = newSourcePoint.Id }); _dbContext.PublishedHistories.Add(new PublishedHistory() { Name = sourcePoint.Name, Position = sourcePoint.Position, Value = lastPublishedValue, PublishedUser = _userProfileService.GetCurrentUser().Username, PublishedDate = DateTime.Now.AddSeconds(1).ToUniversalTime().ToPSTDateTime(), SourcePointId = newSourcePoint.Id }); newSourceCatalog.SourcePoints.Add(newSourcePoint); clonedSourcePoints.Add(sourcePoint.Id, newSourcePoint); } #endregion } } } foreach (var item in filesWillClone) { if (item.IsWord) { var catalog = await _dbContext.DestinationCatalogs.Where(o => o.DocumentId.Equals(item.DocumentId, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefaultAsync(); if (catalog != null) { var destinationPoints = await _dbContext.DestinationPoints.Where(o => o.CatalogId == catalog.Id) .Include(o => o.CustomFormats) .Include(o => o.ReferencedSourcePoint) .Include(o => o.ReferencedSourcePoint.Catalog).ToArrayAsync(); var sourcePointDocumentIds = destinationPoints.Select(o => o.ReferencedSourcePoint.Catalog.DocumentId); var clonedExcelDocumentIds = files.Where(o => o.Clone && o.IsExcel).Select(o => o.DocumentId); if (sourcePointDocumentIds.Any(o => clonedExcelDocumentIds.Any(p => p.Equals(o, StringComparison.CurrentCultureIgnoreCase)))) { #region Add new destination catalog DestinationCatalog newDestinationCatalog = new DestinationCatalog() { Name = item.DestinationFileUrl, DocumentId = item.DestinationFileDocumentId, IsDeleted = false }; _dbContext.DestinationCatalogs.Add(newDestinationCatalog); #endregion #region Add new destination point foreach (var destinationPoint in destinationPoints) { var referencedSourcePoint = clonedSourcePoints.FirstOrDefault(o => o.Key == destinationPoint.ReferencedSourcePoint.Id).Value; if (referencedSourcePoint != null) { DestinationPoint newDestinationPoint = new DestinationPoint() { RangeId = destinationPoint.RangeId, Created = DateTime.Now.ToUniversalTime().ToPSTDateTime(), Creator = _userProfileService.GetCurrentUser().Username, DecimalPlace = destinationPoint.DecimalPlace, DestinationType = destinationPoint.DestinationType }; var newCustomFormatIds = destinationPoint.CustomFormats.Select(o => o.Id); var newCustomFormats = _dbContext.CustomFormats.Where(o => newCustomFormatIds.Contains(o.Id)); foreach (var format in newCustomFormats) { newDestinationPoint.CustomFormats.Add(format); } newDestinationCatalog.DestinationPoints.Add(newDestinationPoint); newDestinationPoint.ReferencedSourcePoint = referencedSourcePoint; _dbContext.DestinationPoints.Add(newDestinationPoint); } } #endregion } } } } await _dbContext.SaveChangesAsync(); } catch (Exception ex) { var entity = new LogEntity() { LogId = "50001", Action = Constant.ACTIONTYPE_CLONE, ActionType = ActionTypeEnum.ErrorLog, PointType = Constant.POINTTYPE_CLONE, Message = ".Net Error", }; entity.Subject = $"{entity.LogId} - {entity.Action} - {entity.PointType} - Error"; await _logService.WriteLog(entity); throw new ApplicationException("Clone folder failed", ex); } }