Beispiel #1
0
        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();
        }
Beispiel #2
0
        private void btnCreateCatalog_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtCatalog.Text))
            {
                var newCatalog = new SourceCatalog()
                {
                    DocumentId = Guid.NewGuid().ToString(),
                    Name       = txtCatalog.Text
                };

                var catalogs = (List <SourceCatalog>)cboCatalog.DataSource;
                catalogs.Add(newCatalog);

                cboCatalog.DataSource    = null;
                cboCatalog.DataSource    = catalogs;
                cboCatalog.DisplayMember = "Name";
                txtCatalog.Text          = string.Empty;

                cboCatalog.SelectedItem = newCatalog;
            }
        }
Beispiel #3
0
        public async Task <IEnumerable <SourceCatalog> > AddRecentFile(SourceCatalog sourceCatalog)
        {
            var catalog = await _dbContext.SourceCatalogs.FirstOrDefaultAsync(o => o.DocumentId == sourceCatalog.DocumentId);

            if (catalog == null)
            {
                catalog = new SourceCatalog()
                {
                    Name = sourceCatalog.Name, DocumentId = sourceCatalog.DocumentId
                };
                _dbContext.SourceCatalogs.Add(catalog);
            }
            var recentFile = new RecentFile();

            recentFile.User      = _userProfileService.GetCurrentUser().Username;
            recentFile.Date      = DateTime.Now.ToUniversalTime().ToPSTDateTime();
            recentFile.CatalogId = catalog.Id;
            _dbContext.RecentFiles.Add(recentFile);

            await _dbContext.SaveChangesAsync();

            return(await GetRecentFiles());
        }
        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);
            }
        }
        public async Task <SourcePoint> AddSourcePoint(string fileName, string documentId, SourcePoint sourcePoint)
        {
            try
            {
                var  sourceCatalog    = _dbContext.SourceCatalogs.FirstOrDefault(o => o.DocumentId == documentId);
                bool addSourceCatalog = (sourceCatalog == null);
                if (addSourceCatalog)
                {
                    try
                    {
                        sourceCatalog = new SourceCatalog()
                        {
                            Name = fileName, DocumentId = documentId
                        };
                        _dbContext.SourceCatalogs.Add(sourceCatalog);
                    }
                    catch (Exception ex)
                    {
                        var entity = new LogEntity()
                        {
                            LogId      = "30006",
                            Action     = Constant.ACTIONTYPE_ADD,
                            ActionType = ActionTypeEnum.ErrorLog,
                            PointType  = Constant.POINTTYPE_SOURCECATALOG,
                            Message    = ".Net Error",
                        };
                        entity.Subject = $"{entity.LogId} - {entity.Action} - {entity.PointType} - Error";
                        await _logService.WriteLog(entity);

                        throw new ApplicationException("Add Source Catalog failed", ex);
                    }
                }

                sourcePoint.Created = DateTime.Now.ToUniversalTime().ToPSTDateTime();
                sourcePoint.Creator = _userProfileService.GetCurrentUser().Username;

                sourceCatalog.SourcePoints.Add(sourcePoint);

                _dbContext.PublishedHistories.Add(new PublishedHistory()
                {
                    Name          = sourcePoint.Name,
                    Position      = sourcePoint.Position,
                    Value         = sourcePoint.Value,
                    PublishedDate = sourcePoint.Created,
                    PublishedUser = sourcePoint.Creator,
                    SourcePointId = sourcePoint.Id
                });

                await _dbContext.SaveChangesAsync();

                if (addSourceCatalog)
                {
                    await _logService.WriteLog(new LogEntity()
                    {
                        LogId      = "30003",
                        Action     = Constant.ACTIONTYPE_ADD,
                        PointType  = Constant.POINTTYPE_SOURCECATALOG,
                        ActionType = ActionTypeEnum.AuditLog,
                        Message    = $"Add Source Catalog named {sourceCatalog.Name}."
                    });
                }
                await _logService.WriteLog(new LogEntity()
                {
                    LogId      = "10001",
                    Action     = Constant.ACTIONTYPE_ADD,
                    PointType  = Constant.POINTTYPE_SOURCEPOINT,
                    ActionType = ActionTypeEnum.AuditLog,
                    Message    = $"Create source point named {sourcePoint.Name} in the location: {sourcePoint.Position}, value: {sourcePoint.Value} in the excel file named: {sourceCatalog.FileName} by {sourcePoint.Creator}"
                });
            }
            catch (ApplicationException ex)
            {
                throw ex.InnerException;
            }
            catch (Exception ex)
            {
                var logEntity = new LogEntity()
                {
                    LogId      = "10005",
                    Action     = Constant.ACTIONTYPE_ADD,
                    ActionType = ActionTypeEnum.ErrorLog,
                    PointType  = Constant.POINTTYPE_SOURCEPOINT,
                    Message    = ".Net Error",
                    Detail     = ex.ToString()
                };
                logEntity.Subject = $"{logEntity.LogId} - {logEntity.Action} - {logEntity.PointType} - Error";
                await _logService.WriteLog(logEntity);

                throw ex;
            }
            return(sourcePoint);
        }