Beispiel #1
0
 private bool IsChapterChanged(ChapterResource dbChapter, BookContentItemData bookContentItem, long newPageResourceResourceId, long?newParentResourceId)
 {
     return(bookContentItem.ItemOrder != dbChapter.Position ||
            newPageResourceResourceId != dbChapter.ResourceBeginningPage.Id ||
            newParentResourceId != dbChapter.ParentResource?.Id ||
            bookContentItem.Text != dbChapter.Name);
 }
        protected override void ExecuteWorkImplementation()
        {
            var now  = DateTime.UtcNow;
            var user = m_resourceRepository.Load <User>(m_userId);
            var resourceBeginningPage = m_resourceRepository.Load <Resource>(m_chapterData.BeginningPageId);
            var resourceParentChapter = m_chapterData.ParentChapterId != null
                ? m_resourceRepository.Load <Resource>(m_chapterData.ParentChapterId.Value)
                : null;

            if ((m_projectId == null && m_resourceId == null) || (m_projectId != null && m_resourceId != null))
            {
                throw new MainServiceException(MainServiceErrorCode.ProjectIdOrResourceId, "Exactly one parameter (ProjectId or ResourceId) has to be specified");
            }

            var chapterResource = new ChapterResource
            {
                Name                  = m_chapterData.Name,
                Comment               = m_chapterData.Comment,
                Position              = m_chapterData.Position,
                ParentResource        = resourceParentChapter,
                ResourceBeginningPage = resourceBeginningPage,
                Resource              = null,
                VersionNumber         = 0,
                CreateTime            = now,
                CreatedByUser         = user,
            };

            if (m_resourceId != null)
            {
                var latestChapterResource = m_resourceRepository.GetLatestResourceVersion <ChapterResource>(m_resourceId.Value);
                if (latestChapterResource == null)
                {
                    throw new MainServiceException(MainServiceErrorCode.EntityNotFound, "The entity was not found.");
                }

                chapterResource.Resource      = latestChapterResource.Resource;
                chapterResource.VersionNumber = latestChapterResource.VersionNumber + 1;
            }
            else
            {
                var resource = new Resource
                {
                    ContentType  = ContentTypeEnum.Chapter,
                    ResourceType = ResourceTypeEnum.Chapter,
                    Project      = m_resourceRepository.Load <Project>(m_projectId),
                };

                chapterResource.Resource      = resource;
                chapterResource.VersionNumber = 1;
            }

            chapterResource.Resource.Name          = m_chapterData.Name;
            chapterResource.Resource.LatestVersion = chapterResource;

            m_resourceRepository.Create(chapterResource);

            ResourceId = chapterResource.Resource.Id;
            VersionId  = chapterResource.Id;
        }
Beispiel #3
0
        protected override void ExecuteWorkImplementation()
        {
            var now        = DateTime.UtcNow;
            var user       = m_resourceRepository.Load <User>(m_userId);
            var dbChapters = m_resourceRepository.GetProjectLatestChapters(m_projectId);
            var updatedResourceChapterIds = new List <long>();

            foreach (var chapter in m_chapterData)
            {
                var resourceBeginningPage = m_resourceRepository.Load <Resource>(chapter.BeginningPageId);
                var resourceParentChapter = chapter.ParentChapterId != null
                    ? m_resourceRepository.Load <Resource>(chapter.ParentChapterId.Value)
                    : null;

                var chapterResource = new ChapterResource
                {
                    Name                  = chapter.Name,
                    Comment               = chapter.Comment,
                    Position              = chapter.Position,
                    Resource              = null,
                    ParentResource        = resourceParentChapter,
                    ResourceBeginningPage = resourceBeginningPage,
                    VersionNumber         = 0,
                    CreateTime            = now,
                    CreatedByUser         = user,
                };

                if (chapter.Id == null)
                {
                    var resource = new Resource
                    {
                        ContentType  = ContentTypeEnum.Chapter,
                        ResourceType = ResourceTypeEnum.Chapter,
                        Project      = m_resourceRepository.Load <Project>(m_projectId),
                    };

                    chapterResource.Resource      = resource;
                    chapterResource.VersionNumber = 1;
                }
                else
                {
                    var latestChapterResource = m_resourceRepository.GetLatestResourceVersion <ChapterResource>(chapter.Id.Value);
                    if (latestChapterResource == null)
                    {
                        throw new MainServiceException(MainServiceErrorCode.EntityNotFound, "The entity was not found.");
                    }

                    chapterResource.Resource      = latestChapterResource.Resource;
                    chapterResource.VersionNumber = latestChapterResource.VersionNumber + 1;

                    updatedResourceChapterIds.Add(chapter.Id.Value);
                }

                chapterResource.Resource.Name          = chapter.Name;
                chapterResource.Resource.LatestVersion = chapterResource;

                m_resourceRepository.Create(chapterResource);
            }

            var removeResourceSubwork = new RemoveResourceSubwork(m_resourceRepository);

            foreach (var dbChapter in dbChapters)
            {
                if (!updatedResourceChapterIds.Contains(dbChapter.Resource.Id))
                {
                    removeResourceSubwork.RemoveResource(dbChapter.Resource.Id);
                }
            }
        }
Beispiel #4
0
        protected override void ExecuteWorkImplementation()
        {
            var now = DateTime.UtcNow;
            var chapters = m_resourceRepository.GetProjectLatestChapters(m_projectId);
            var project = m_resourceRepository.Load<Project>(m_projectId);
            var user = m_resourceRepository.Load<User>(m_userId);

            // Fix levels
            var currentLevel = 0; // min level is 1
            foreach (var heading in m_pageWithHeadingsList.SelectMany(x => x.Item2))
            {
                if (heading.Level > currentLevel + 1)
                {
                    heading.Level = currentLevel + 1;
                }
                else
                {
                    currentLevel = heading.Level;
                }
            }

            // Update chapters
            var updatedResourceChapters = new List<Resource>();
            var currentChapterByLevel = new ChapterResource[7];
            var position = 0;
            
            foreach (var pageWithHeadings in m_pageWithHeadingsList)
            {
                var headings = pageWithHeadings.Item2;
                var resourcePage = m_resourceRepository.Load<Resource>(pageWithHeadings.Item1.Resource.Id);

                foreach (var markdownHeadingData in headings)
                {
                    position++;

                    // Find original Resource
                    ChapterResource originalChapterResource = null;
                    var originalChapters = chapters.Where(x => x.Name == markdownHeadingData.Heading).ToList();
                    if (originalChapters.Count == 1)
                    {
                        originalChapterResource = originalChapters[0];
                    }
                    else if (originalChapters.Count > 1)
                    {
                        originalChapterResource = originalChapters.FirstOrDefault(x => x.Position == position);
                    }

                    if (updatedResourceChapters.Contains(originalChapterResource?.Resource))
                    {
                        originalChapterResource = null;
                    }

                    // Create ChapterResource
                    var chapterResource = new ChapterResource
                    {
                        Comment = null,
                        CreateTime = now,
                        CreatedByUser = user,
                        Name = markdownHeadingData.Heading,
                        ParentResource = currentChapterByLevel[markdownHeadingData.Level - 1]?.Resource,
                        Position = position,
                        Resource = null, // is updated below
                        ResourceBeginningPage = resourcePage,
                        VersionNumber = 1, // is updated below
                    };

                    if (originalChapterResource != null)
                    {
                        chapterResource.Resource = originalChapterResource.Resource;
                        chapterResource.VersionNumber = originalChapterResource.VersionNumber + 1;

                        updatedResourceChapters.Add(chapterResource.Resource);
                    }
                    else
                    {
                        chapterResource.Resource = new Resource
                        {
                            ContentType = ContentTypeEnum.Chapter,
                            IsRemoved = false,
                            Name = chapterResource.Name,
                            Project = project,
                            ResourceType = ResourceTypeEnum.Chapter,
                        };
                    }

                    chapterResource.Resource.LatestVersion = chapterResource;
                    currentChapterByLevel[markdownHeadingData.Level] = chapterResource;

                    m_resourceRepository.Create(chapterResource);
                }
            }

            // Remove unused chapters
            var removeResourceSubwork = new RemoveResourceSubwork(m_resourceRepository);
            foreach (var chapterResource in chapters)
            {
                var resource = chapterResource.Resource;
                if (!updatedResourceChapters.Contains(resource))
                {
                    removeResourceSubwork.RemoveResource(resource.Id);
                }
            }
        }
Beispiel #5
0
        private void UpdateChapterList(IList <BookContentItemData> bookContentItems, ChapterResource parentChapter, ChapterRecursionData data)
        {
            var parentChapterResourceResource = parentChapter?.Resource;

            var updatedChapterResourceIds = data.UpdatedChapterResourceIds;
            var dbPagesByPosition         = data.DbPagesByPosition;
            var dbChaptersByName          = data.DbChaptersByName;
            var project = data.Project;
            var user    = data.User;
            var now     = data.Now;

            foreach (var bookContentItem in bookContentItems)
            {
                PageResource dbPage;
                if (bookContentItem.Page == null)
                {
                    dbPage = null;
                }
                else if (!dbPagesByPosition.TryGetValue(bookContentItem.Page.Position, out dbPage))
                {
                    throw new ArgumentException($"Trying assign Chapter to non-existent Page with position {bookContentItem.Page.Position} (page name: {bookContentItem.Page.Text})");
                }

                ChapterResource        dbChapter = null;
                List <ChapterResource> dbChapters;
                if (dbChaptersByName.TryGetValue(bookContentItem.Text, out dbChapters))
                {
                    dbChapter = dbChapters.Count == 1
                        ? dbChapters.First()
                        : dbChapters.FirstOrDefault(x => x.Position == bookContentItem.ItemOrder); // If multiple chapters have the same name, try identify chapter by Position
                }

                var newDbChapter = new ChapterResource
                {
                    Resource = null,
                    Name     = bookContentItem.Text,
                    ResourceBeginningPage = dbPage?.Resource,
                    Comment        = string.Empty,
                    CreateTime     = now,
                    CreatedByUser  = user,
                    Position       = bookContentItem.ItemOrder,
                    VersionNumber  = 0,
                    ParentResource = parentChapterResourceResource
                };

                if (dbChapter == null)
                {
                    var newResource = new Resource
                    {
                        Project       = project,
                        ContentType   = ContentTypeEnum.Chapter,
                        Name          = bookContentItem.Text,
                        LatestVersion = null,
                        ResourceType  = ResourceTypeEnum.Chapter
                    };

                    newDbChapter.Resource      = newResource;
                    newDbChapter.VersionNumber = 1;
                }
                else
                {
                    newDbChapter.Resource      = dbChapter.Resource;
                    newDbChapter.VersionNumber = dbChapter.VersionNumber + 1;

                    updatedChapterResourceIds.Add(dbChapter.Id);
                }

                newDbChapter.Resource.LatestVersion = newDbChapter;
                m_resourceRepository.Create(newDbChapter);

                m_allImportedResourceVersionIds.Add(newDbChapter.Id);
                UpdateChapterList(bookContentItem.SubContentItems, newDbChapter, data);
            }
        }