Пример #1
0
        private Term GetOrCreateTerm(TermData data)
        {
            var dbTerm = m_resourceRepository.GetTermByExternalId(data.XmlId);

            if (dbTerm != null)
            {
                if (IsTermUpdated(dbTerm, data))
                {
                    dbTerm.Position     = data.Position;
                    dbTerm.Text         = data.Text;
                    dbTerm.TermCategory = GetOrCreateTermCategory(data.TermCategoryName);

                    m_resourceRepository.Update(dbTerm);
                }

                return(dbTerm);
            }

            // some books can use different XmlId
            var dbTerm2 = m_resourceRepository.GetTermByNameAndCategoryName(data.Text, data.TermCategoryName);

            if (dbTerm2 != null)
            {
                if (dbTerm2.Position != data.Position || dbTerm2.ExternalId != data.XmlId)
                {
                    dbTerm2.Position   = data.Position;
                    dbTerm2.ExternalId = data.XmlId;

                    m_resourceRepository.Update(dbTerm2);
                }

                return(dbTerm2);
            }

            // create new Term
            var newDbTerm = new Term
            {
                ExternalId   = data.XmlId,
                Position     = data.Position,
                Text         = data.Text,
                TermCategory = GetOrCreateTermCategory(data.TermCategoryName),
            };

            m_resourceRepository.Create(newDbTerm);
            newDbTerm = m_resourceRepository.Load <Term>(newDbTerm.Id);

            return(newDbTerm);
        }
Пример #2
0
        protected override long ExecuteWorkImplementation()
        {
            var         now = DateTime.UtcNow;
            TextComment parentTextComment = null;

            if (m_newComment.ParentCommentId != null)
            {
                parentTextComment = m_resourceRepository.FindById <TextComment>(m_newComment.ParentCommentId);
                if (parentTextComment.ParentComment != null)
                {
                    throw new MainServiceException(MainServiceErrorCode.CommentsToSecondLevel, "Only comments to second level are allowed");
                }
                m_newComment.TextReferenceId = parentTextComment.TextReferenceId; // Subcomments must have the same TextReferenceId as parent
            }

            var user         = m_resourceRepository.Load <User>(m_userId);
            var resourceText = m_resourceRepository.Load <Resource>(m_textId);
            var newComment   = new TextComment
            {
                CreateTime      = now,
                CreatedByUser   = user,
                ParentComment   = parentTextComment,
                ResourceText    = resourceText,
                Text            = m_newComment.Text,
                TextReferenceId = m_newComment.TextReferenceId
            };
            var resultId = (long)m_resourceRepository.Create(newComment);

            return(resultId);
        }
Пример #3
0
        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;
        }
Пример #4
0
        public void AddTwoValuesOfSamePartitionGetTwoResourcesBack()
        {
            var resourceRepository = new ResourceRepository(_resourceContext);

            resourceRepository.Create(new Resource {
                Key = "foo1", Partition = "foo", Value = "true"
            });
            resourceRepository.Create(new Resource {
                Key = "foo2", Partition = "foo", Value = "false"
            });

            var list = resourceRepository.List("foo").ToList();

            list.Count.ShouldBe(2);

            list.Single(x => x.Key == "foo1").Value.ShouldBe("true");
            list.Single(x => x.Key == "foo2").Value.ShouldBe("false");
        }
Пример #5
0
        protected override void ExecuteWorkImplementation()
        {
            var now  = DateTime.UtcNow;
            var user = m_resourceRepository.Load <User>(m_userId);

            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 pageResource = new PageResource
            {
                Name          = m_pageData.Name,
                Comment       = m_pageData.Comment,
                Position      = m_pageData.Position,
                Resource      = null,
                VersionNumber = 0,
                CreateTime    = now,
                CreatedByUser = user,
                Terms         = null, // Terms must be also updated
            };

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

                pageResource.Resource      = latestPageResource.Resource;
                pageResource.VersionNumber = latestPageResource.VersionNumber + 1;
                pageResource.Terms         = new List <Term>(latestPageResource.Terms); // Lazy fetch
            }
            else
            {
                var resource = new Resource
                {
                    ContentType  = ContentTypeEnum.Page,
                    ResourceType = ResourceTypeEnum.Page,
                    Project      = m_resourceRepository.Load <Project>(m_projectId),
                };

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

            pageResource.Resource.Name          = m_pageData.Name;
            pageResource.Resource.LatestVersion = pageResource;

            m_resourceRepository.Create(pageResource);

            ResourceId = pageResource.Resource.Id;
            VersionId  = pageResource.Id;
        }
        public void CreateTest()
        {
            // Arrange
            ResourceRepository target   = new ResourceRepository(_context);
            Resource           expected = _mockdata.NewResource();

            // Act
            Resource actual = target.Create(expected);

            // Assert
            //Assert.AreEqual(expected.Name, actual.Name);
            Assert.IsFalse(actual.Id == 0);
        }
Пример #7
0
        protected override long ExecuteWorkImplementation()
        {
            var timeNow           = DateTime.UtcNow;
            var latestPageVersion = m_resourceRepository.GetLatestResourceVersion <PageResource>(m_pageId);

            if (latestPageVersion == null)
            {
                throw new MainServiceException(MainServiceErrorCode.EntityNotFound, $"PageResource with ResourceId={m_pageId} was not found");
            }

            var latestTextVersion = m_resourceRepository.GetLatestPageText(m_pageId);

            if (latestTextVersion != null)
            {
                throw new MainServiceException(
                          MainServiceErrorCode.ChangeInConflict,
                          $"Conflict. Text already exists for specified page with ID {m_pageId}."
                          );
            }

            var newResource = new Resource
            {
                Project            = latestPageVersion.Resource.Project,
                Name               = $"{latestPageVersion.Name}.md",
                ContentType        = ContentTypeEnum.Page,
                NamedResourceGroup = null,
                ResourceType       = ResourceTypeEnum.Text,
            };
            var newVersion = new TextResource
            {
                CreatedByUser = m_resourceRepository.Load <User>(m_userId),
                CreateTime    = timeNow,
                ExternalId    = null,
                ResourcePage  = latestPageVersion.Resource,
                Resource      = newResource,
                VersionNumber = 1,
            };

            newVersion.Resource.LatestVersion = newVersion;

            m_resourceRepository.Create(newVersion);

            var externalTextId = m_fulltextStorage.CreateNewTextVersion(newVersion, m_data.Text);

            newVersion.ExternalId = externalTextId;
            m_resourceRepository.Update(newVersion);

            return(newResource.Id);
        }
Пример #8
0
        private void CreateHeadwordResource(int version, Resource resource, List <BookHeadwordData> headwordDataList, User user, DateTime now, BookVersionResource bookVersion, Dictionary <string, PageResource> dbPagesByImage)
        {
            var firstHeadwordData = headwordDataList.First();

            var newDbHeadword = new HeadwordResource
            {
                Resource        = resource,
                BookVersion     = bookVersion,
                Comment         = null,
                CreateTime      = now,
                CreatedByUser   = user,
                VersionNumber   = version,
                HeadwordItems   = null, // Headword Items are created in following for-each
                ExternalId      = firstHeadwordData.XmlEntryId,
                DefaultHeadword = firstHeadwordData.DefaultHeadword,
                Sorting         = firstHeadwordData.SortOrder,
            };

            resource.LatestVersion = newDbHeadword;

            m_resourceRepository.Create(newDbHeadword);
            m_importedResourceVersionIds.Add(newDbHeadword.Id);

            foreach (var bookHeadwordData in headwordDataList)
            {
                var dbPage            = GetPageResourceByImage(dbPagesByImage, bookHeadwordData.Image);
                var newDbHeadwordItem = new HeadwordItem
                {
                    HeadwordResource = newDbHeadword,
                    Headword         = bookHeadwordData.Headword,
                    HeadwordOriginal = bookHeadwordData.HeadwordOriginal,
                    ResourcePage     = dbPage?.Resource
                };
                m_resourceRepository.Create(newDbHeadwordItem);
            }
        }
        protected override long ExecuteWorkImplementation()
        {
            var project  = m_resourceRepository.Load <Project>(m_projectId);
            var textType = m_mapper.Map <TextTypeEnum>(m_newResourceGroup.TextType);
            var newNamedResourceGroup = new NamedResourceGroup
            {
                Project  = project,
                Name     = m_newResourceGroup.Name,
                TextType = textType,
            };
            //TODO check existing name?
            var resultId = m_resourceRepository.Create(newNamedResourceGroup);

            return((long)resultId);
        }
        protected override long ExecuteWorkImplementation()
        {
            var now = DateTime.UtcNow;

            var latestAudio = m_resourceRepository.GetLatestResourceVersion <AudioResource>(m_audioId);

            if (latestAudio.Id != m_data.OriginalVersionId)
            {
                throw new MainServiceException(
                          MainServiceErrorCode.ResourceModified,
                          $"Conflict. Current latest versionId is {latestAudio.Id}, but originalVersionId was specified {m_data.OriginalVersionId}",
                          HttpStatusCode.Conflict
                          );
            }

            var audioType     = AudioEnumHelper.FileNameToAudioType(m_data.FileName);
            var mimeType      = MimeMapping.MimeUtility.GetMimeMapping(m_data.FileName);
            var user          = m_resourceRepository.Load <User>(m_userId);
            var resourceTrack = m_data.ResourceTrackId != null
                ? m_resourceRepository.Load <Resource>(m_data.ResourceTrackId.Value)
                : null;

            var newAudioResource = new AudioResource
            {
                CreateTime    = now,
                CreatedByUser = user,
                Comment       = m_data.Comment,
                FileId        = null, // Must be added after saving in FileStorageManager
                FileName      = m_data.FileName,
                MimeType      = mimeType,
                Resource      = latestAudio.Resource,
                ResourceTrack = resourceTrack,
                AudioType     = audioType,
                Duration      = m_data.Duration,
                VersionNumber = latestAudio.VersionNumber + 1,
            };

            newAudioResource.Resource.LatestVersion = newAudioResource;

            var resourceVersionId = (long)m_resourceRepository.Create(newAudioResource);

            var fileInfo = m_fileSystemManager.SaveResource(ResourceType.Audio, latestAudio.Resource.Project.Id, m_fileStream);

            newAudioResource.FileId = fileInfo.FileNameId;
            m_resourceRepository.Update(newAudioResource);

            return(resourceVersionId);
        }
Пример #11
0
        protected override long ExecuteWorkImplementation()
        {
            var now  = DateTime.UtcNow;
            var user = m_resourceRepository.Load <User>(m_userId);
            var latestEditionNote = m_resourceRepository.GetLatestEditionNote(m_projectId);

            if (latestEditionNote != null && latestEditionNote.Id != m_data.OriginalVersionId)
            {
                throw new MainServiceException(
                          MainServiceErrorCode.EditionNoteConflict,
                          $"Conflict. Current latest versionId is {latestEditionNote.Id}, but originalVersionId was specified {m_data.OriginalVersionId}",
                          HttpStatusCode.Conflict
                          );
            }

            if (latestEditionNote == null)
            {
                latestEditionNote = new EditionNoteResource
                {
                    Resource = new Resource
                    {
                        Project      = m_resourceRepository.Load <Project>(m_projectId),
                        ContentType  = ContentTypeEnum.None,
                        ResourceType = ResourceTypeEnum.EditionNote,
                        Name         = "Edition note"
                    },
                    VersionNumber = 0
                };
            }

            var newEditionNote = new EditionNoteResource
            {
                Resource      = latestEditionNote.Resource,
                CreateTime    = now,
                CreatedByUser = user,
                Comment       = m_data.Comment,
                VersionNumber = latestEditionNote.VersionNumber + 1,
                Text          = m_data.Text
            };

            newEditionNote.Resource.LatestVersion = newEditionNote;

            var resourceVersionId = (long)m_resourceRepository.Create(newEditionNote);

            return(resourceVersionId);
        }
Пример #12
0
        private long CreateEditionNoteResource(Resource resource, int version, User user, DateTime now, BookVersionResource bookVersion)
        {
            var newDbEditionNote = new EditionNoteResource
            {
                Resource      = resource,
                BookVersion   = bookVersion,
                VersionNumber = version,
                Comment       = string.Empty,
                CreateTime    = now,
                CreatedByUser = user,
            };

            resource.LatestVersion = newDbEditionNote;
            var resultId = m_resourceRepository.Create(newDbEditionNote);

            return((long)resultId);
        }
Пример #13
0
        private long CreateBookVersionResource(Resource resource, int version, string versionExternalId, User user, DateTime now)
        {
            var newDbBookVersion = new BookVersionResource
            {
                Resource      = resource,
                ExternalId    = versionExternalId,
                VersionNumber = version,
                Comment       = string.Empty,
                CreateTime    = now,
                CreatedByUser = user,
            };

            resource.LatestVersion = newDbBookVersion;
            var resultId = m_resourceRepository.Create(newDbBookVersion);

            return((long)resultId);
        }
Пример #14
0
        protected override long ExecuteWorkImplementation()
        {
            var timeNow       = DateTime.UtcNow;
            var latestVersion = m_resourceRepository.GetTextResource(m_textId);

            if (latestVersion == null)
            {
                throw new MainServiceException(MainServiceErrorCode.EntityNotFound, $"TextResource with ResourceId={m_textId} was not found");
            }

            if (latestVersion.Id != m_newTextContract.ResourceVersionId)
            {
                throw new MainServiceException(
                          MainServiceErrorCode.ChangeInConflict,
                          $"Conflict. Current latest versionId is {latestVersion.Id}, but originalVersionId was specified {m_newTextContract.ResourceVersionId}",
                          HttpStatusCode.Conflict
                          );
            }

            var newVersion = new TextResource
            {
                CreatedByUser = m_resourceRepository.Load <User>(m_userId),
                CreateTime    = timeNow,
                ExternalId    = null,
                ResourcePage  = latestVersion.ResourcePage,
                Resource      = latestVersion.Resource,
                VersionNumber = latestVersion.VersionNumber + 1,
            };

            newVersion.Resource.LatestVersion = newVersion;
            newVersion.Resource.Name          = $"{newVersion.ResourcePage.Name}.md";

            var result = (long)m_resourceRepository.Create(newVersion);

            var externalTextId = m_fulltextStorage.CreateNewTextVersion(newVersion, m_newTextContract.Text);

            newVersion.ExternalId = externalTextId;
            m_resourceRepository.Update(newVersion);

            return(result);
        }
Пример #15
0
        protected override void ExecuteWorkImplementation()
        {
            var now     = DateTime.UtcNow;
            var user    = m_resourceRepository.Load <User>(m_userId);
            var dbPages = m_resourceRepository.GetProjectLatestPages(m_projectId);
            var updatedResourcePageIds = new List <long>();

            foreach (var newPage in m_newPages)
            {
                var pageResource = new PageResource
                {
                    Name          = newPage.Name,
                    Comment       = null,
                    Position      = newPage.Position,
                    Resource      = null,
                    VersionNumber = 0,
                    CreateTime    = now,
                    CreatedByUser = user,
                    Terms         = null, // Terms must be also updated
                };

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

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

                    pageResource.Resource      = latestPageResource.Resource;
                    pageResource.VersionNumber = latestPageResource.VersionNumber + 1;
                    pageResource.Terms         = new List <Term>(latestPageResource.Terms); // Lazy fetch

                    updatedResourcePageIds.Add(newPage.Id.Value);
                }

                pageResource.Resource.Name          = newPage.Name;
                pageResource.Resource.LatestVersion = pageResource;

                m_resourceRepository.Create(pageResource);
            }

            var removeResourceSubwork = new RemoveResourceSubwork(m_resourceRepository);

            foreach (var dbPage in dbPages)
            {
                if (!updatedResourcePageIds.Contains(dbPage.Resource.Id))
                {
                    removeResourceSubwork.RemoveResource(dbPage.Resource.Id);
                }
            }
        }
Пример #16
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);
                }
            }
        }
 public void CreateTest()
 {
     Assert.AreEqual(ModifyDataResultEnum.ErrConflict, testObj.Create(1));
     Assert.AreEqual(ModifyDataResultEnum.Success, testObj.Create(12, "teststr"));
 }
        protected override NewResourceResultContract ExecuteWorkImplementation()
        {
            var now = DateTime.UtcNow;

            ImageResource latestImage;

            if (m_data.ImageId == null || m_data.OriginalVersionId == null)
            {
                var newResourceResult = TryCreateNewResource();
                latestImage = new ImageResource // simulate previous ImageResource version which doesn't exist yet
                {
                    Resource      = newResourceResult.Item1,
                    ResourcePage  = newResourceResult.Item2.Resource,
                    VersionNumber = 0,
                };
            }
            else
            {
                latestImage = m_resourceRepository.GetLatestResourceVersion <ImageResource>(m_data.ImageId.Value);
                if (latestImage.Id != m_data.OriginalVersionId)
                {
                    throw new MainServiceException(
                              MainServiceErrorCode.ResourceModified,
                              $"Conflict. Current latest versionId is {latestImage.Id}, but originalVersionId was specified {m_data.OriginalVersionId}",
                              HttpStatusCode.Conflict
                              );
                }
            }


            var user     = m_resourceRepository.Load <User>(m_userId);
            var mimeType = MimeMapping.MimeUtility.GetMimeMapping(m_data.FileName);

            var newImageResource = new ImageResource
            {
                CreateTime    = now,
                CreatedByUser = user,
                Comment       = m_data.Comment,
                FileId        = null, // Must be added after saving in FileStorageManager
                FileName      = m_data.FileName,
                MimeType      = mimeType,
                Resource      = latestImage.Resource,
                ResourcePage  = latestImage.ResourcePage,
                Size          = 0, // Must be added after saving in FileStorageManager
                VersionNumber = latestImage.VersionNumber + 1,
            };

            newImageResource.Resource.LatestVersion = newImageResource;
            newImageResource.Resource.Name          = m_data.FileName;

            m_resourceRepository.Create(newImageResource);

            var fileInfo = m_fileSystemManager.SaveResource(ResourceType.Image, latestImage.Resource.Project.Id, m_fileStream);

            newImageResource.FileId = fileInfo.FileNameId;
            newImageResource.Size   = fileInfo.FileSize;
            m_resourceRepository.Update(newImageResource);

            return(new NewResourceResultContract
            {
                ResourceId = newImageResource.Resource.Id,
                ResourceVersionId = newImageResource.Id,
                VersionNumber = newImageResource.VersionNumber,
            });
        }
Пример #19
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);
                }
            }
        }
Пример #20
0
        public void UpdateTracks(long projectId, int userId, BookData bookData)
        {
            if (bookData.Tracks == null)
            {
                return;
            }

            var updatedTrackResourceIds = new List <long>();
            var updatedAudioResourceIds = new List <long>();
            var now     = DateTime.UtcNow;
            var project = m_resourceRepository.Load <Project>(projectId);
            var user    = m_resourceRepository.Load <User>(userId);

            var dbTracks    = m_resourceRepository.GetProjectLatestTracks(projectId);
            var dbAudioList = m_resourceRepository.GetProjectLatestAudioResources(projectId)
                              .Where(x => x.ResourceTrack != null)
                              .ToList();
            var dbAudioGroups = dbAudioList
                                .GroupBy(x => x.ResourceTrack.Id)
                                .ToDictionary(x => x.Key, x => x.ToList());

            foreach (var track in bookData.Tracks)
            {
                var dbTrack = dbTracks.FirstOrDefault(x => x.Name == track.Name);

                var newDbTrack = new TrackResource
                {
                    VersionNumber = 0,
                    Resource      = null,
                    Comment       = string.Empty,
                    CreatedByUser = user,
                    CreateTime    = now,
                    Name          = track.Name,
                    Text          = track.Text,
                    Position      = track.Position,
                };

                if (dbTrack == null)
                {
                    var newResource = new Resource
                    {
                        Project      = project,
                        Name         = track.Name,
                        ContentType  = ContentTypeEnum.AudioTrack,
                        ResourceType = ResourceTypeEnum.AudioTrack,
                    };

                    newDbTrack.Resource      = newResource;
                    newDbTrack.VersionNumber = 1;
                }
                else
                {
                    newDbTrack.Resource      = dbTrack.Resource;
                    newDbTrack.VersionNumber = dbTrack.VersionNumber + 1;

                    updatedTrackResourceIds.Add(dbTrack.Id);
                }

                newDbTrack.Resource.LatestVersion = newDbTrack;

                m_resourceRepository.Create(newDbTrack);


                m_allImportedResourceVersionIds.Add(newDbTrack.Id);
                UpdateAudioResources(track.Recordings, dbAudioGroups, newDbTrack, project, user, now, bookData.FileNameMapping, updatedAudioResourceIds);
            }

            // Remove unused resources
            var unusedDbTracks = dbTracks.Where(x => !updatedTrackResourceIds.Contains(x.Id));

            RemoveResourceVersions(unusedDbTracks);

            var unusedDbAudioList = dbAudioList.Where(x => !updatedAudioResourceIds.Contains(x.Id));

            RemoveResourceVersions(unusedDbAudioList);
        }
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ImportModule implements the IPortable ImportModule Interface.
        /// </summary>
        /// <param name="moduleId">The Id of the module to be imported.</param>
        /// <param name="content">The content to be imported.</param>
        /// <param name="version">The version of the module to be imported.</param>
        /// <param name="userId">The Id of the user performing the import.</param>
        /// -----------------------------------------------------------------------------
        public void ImportModule(int moduleId, string content, string version, int userId)
        {
            var groups = new GroupRepository();
            var xmldnngroups = DotNetNuke.Common.Globals.GetContent(content, "Groups");
            var xmlGroupsNodeList = xmldnngroups.SelectNodes("Group");

            if (xmlGroupsNodeList == null) return;

            foreach (XmlNode xmldnngroup in xmlGroupsNodeList)
            {
                //Import a group
                var resourceGroupId = xmldnngroup.SelectSingleNode("ResourceGroupId");
                var resourceName = xmldnngroup.SelectSingleNode("ResourceName");

                if (resourceGroupId == null || resourceName == null) continue;

                var objdnngroup = new Group
                {
                    ResourceGroupId = new Guid(resourceGroupId.InnerText),
                    ResourceName = resourceName.InnerText,
                };

                groups.ImportCreate(objdnngroup);

                var resources = new ResourceRepository();
                var xmlResourcesNodeList = xmldnngroup.SelectNodes("Resources/Resource");

                //Import the resources of this group
                if (xmlResourcesNodeList == null) return;
                foreach (XmlNode xmldnnresource in xmlResourcesNodeList)
                {
                    var rGroupId = xmldnnresource.SelectSingleNode("ResourceGroupId");
                    var resourceKey = xmldnnresource.SelectSingleNode("ResourceKey");
                    var resourceValue = xmldnnresource.SelectSingleNode("ResourceValue");

                    if (rGroupId == null || resourceKey == null || resourceValue == null) continue;

                    var objdnnresource = new Resource
                    {
                        ResourceGroupId = new Guid(rGroupId.InnerText),
                        ResourceKey = resourceKey.InnerText,
                        ResourceValue = resourceValue.InnerText,

                    };

                    resources.Create(objdnnresource);
                }
            }
        }