private void SavePreviewOrDraftContentWithStatusUpdate(Models.Content originalContent, Models.Content updatedContent, ContentStatus requestedStatus) { /* * Edit preview or draft content: * -> Save as preview or draft - look for preview or draft version in a history list or create a new history version with requested preview status with reference to an original content. * -> Save draft - just update field and save. * -> Publish - look if the published content (look for original) exists: * - published content exits: * | create a history content version of the published (clone it). update original with draft data and remove draft|preview. * - published content not exists: * | save draft content as published */ if (requestedStatus == ContentStatus.Preview || requestedStatus == ContentStatus.Draft) { var previewOrDraftContentVersion = originalContent.History.FirstOrDefault(f => f.Status == requestedStatus && !f.IsDeleted); if (previewOrDraftContentVersion == null) { if (originalContent.Status == requestedStatus || (originalContent.Status == ContentStatus.Preview && requestedStatus == ContentStatus.Draft)) { previewOrDraftContentVersion = originalContent; } else { previewOrDraftContentVersion = originalContent.Clone(); previewOrDraftContentVersion.Original = originalContent; originalContent.History.Add(previewOrDraftContentVersion); } } updatedContent.CopyDataTo(previewOrDraftContentVersion, false, false); SetContentOptions(previewOrDraftContentVersion, updatedContent); SetContentRegions(previewOrDraftContentVersion, updatedContent); previewOrDraftContentVersion.Status = requestedStatus; repository.Save(previewOrDraftContentVersion); } else if (requestedStatus == ContentStatus.Published) { var publishedVersion = originalContent.History.FirstOrDefault(f => f.Status == requestedStatus && !f.IsDeleted); if (publishedVersion != null) { var originalToArchive = originalContent.Clone(); originalToArchive.Status = ContentStatus.Archived; originalToArchive.Original = originalContent; originalContent.History.Add(originalToArchive); repository.Save(originalToArchive); } updatedContent.CopyDataTo(originalContent, false, false); SetContentOptions(originalContent, updatedContent); SetContentRegions(originalContent, updatedContent); originalContent.Status = requestedStatus; originalContent.PublishedOn = DateTime.Now; originalContent.PublishedByUser = securityService.CurrentPrincipalName; repository.Save(originalContent); } }
private void SavePublishedContentWithStatusUpdate(Models.Content originalContent, Models.Content updatedContent, ContentStatus requestedStatus) { /* * Edit published content: * -> Save as draft, preview - look for draft|preview version in history list or create a new history version with requested status (draft, preview) with reference to an original content. * -> Publish - current published version should be cloned to archive version with reference to original (archive state) and original updated with new data (published state). * Look for preview|draft versions - if exists remote it. */ if (requestedStatus == ContentStatus.Preview || requestedStatus == ContentStatus.Draft) { var contentVersionOfRequestedStatus = originalContent.History.FirstOrDefault(f => f.Status == requestedStatus && !f.IsDeleted); if (contentVersionOfRequestedStatus == null) { contentVersionOfRequestedStatus = originalContent.Clone(); } updatedContent.CopyDataTo(contentVersionOfRequestedStatus, false, false); SetContentOptions(contentVersionOfRequestedStatus, updatedContent); SetContentRegions(contentVersionOfRequestedStatus, updatedContent); contentVersionOfRequestedStatus.Original = originalContent; contentVersionOfRequestedStatus.Status = requestedStatus; originalContent.History.Add(contentVersionOfRequestedStatus); repository.Save(contentVersionOfRequestedStatus); } if (requestedStatus == ContentStatus.Published) { // Original is copied with options and saved. // Removes options from original. // Locks new stuff from view model. var originalToArchive = originalContent.Clone(); originalToArchive.Status = ContentStatus.Archived; originalToArchive.Original = originalContent; originalContent.History.Add(originalToArchive); repository.Save(originalToArchive); updatedContent.CopyDataTo(originalContent, false, false); SetContentOptions(originalContent, updatedContent); SetContentRegions(originalContent, updatedContent); originalContent.Status = requestedStatus; originalContent.PublishedOn = DateTime.Now; originalContent.PublishedByUser = securityService.CurrentPrincipalName; repository.Save(originalContent); IList <Models.Content> contentsToRemove = originalContent.History.Where(f => f.Status == ContentStatus.Preview || f.Status == ContentStatus.Draft).ToList(); foreach (var redundantContent in contentsToRemove) { repository.Delete(redundantContent); originalContent.History.Remove(redundantContent); } } }
private void SavePublishedContentWithStatusUpdate(Models.Content originalContent, Models.Content updatedContent, ContentStatus requestedStatus) { /* * Edit published content: * -> Save as draft, preview - look for draft|preview version in history list or create a new history version with requested status (draft, preview) with reference to an original content. * -> Publish - current published version should be cloned to archive version with reference to original (archive state) and original updated with new data (published state). * Look for preview|draft versions - if exists remote it. */ if (requestedStatus == ContentStatus.Preview || requestedStatus == ContentStatus.Draft) { var contentVersionOfRequestedStatus = originalContent.History.FirstOrDefault(f => f.Status == requestedStatus && !f.IsDeleted); if (contentVersionOfRequestedStatus == null) { contentVersionOfRequestedStatus = originalContent.Clone(); } updatedContent.CopyDataTo(contentVersionOfRequestedStatus, false); SetContentOptions(contentVersionOfRequestedStatus, updatedContent); SetContentRegions(contentVersionOfRequestedStatus, updatedContent); childContentService.CopyChildContents(contentVersionOfRequestedStatus, updatedContent); contentVersionOfRequestedStatus.Original = originalContent; contentVersionOfRequestedStatus.Status = requestedStatus; originalContent.History.Add(contentVersionOfRequestedStatus); repository.Save(contentVersionOfRequestedStatus); } if (requestedStatus == ContentStatus.Published) { // Original is copied with options and saved. // Removes options from original. // Locks new stuff from view model. var originalToArchive = originalContent.Clone(); originalToArchive.Status = ContentStatus.Archived; originalToArchive.Original = originalContent; originalContent.History.Add(originalToArchive); repository.Save(originalToArchive); // Load draft content's child contents options, if saving from draft to public var draftVersion = originalContent.History.FirstOrDefault(f => f.Status == ContentStatus.Draft && !f.IsDeleted); if (draftVersion != null) { updatedContent .ChildContents .ForEach(cc => cc.Options = draftVersion .ChildContents .Where(cc1 => cc1.AssignmentIdentifier == cc.AssignmentIdentifier) .SelectMany(cc1 => cc1.Options) .ToList()); } updatedContent.CopyDataTo(originalContent, false); SetContentOptions(originalContent, updatedContent); SetContentRegions(originalContent, updatedContent); childContentService.CopyChildContents(originalContent, updatedContent); originalContent.Status = requestedStatus; if (!originalContent.PublishedOn.HasValue) { originalContent.PublishedOn = DateTime.Now; } if (string.IsNullOrWhiteSpace(originalContent.PublishedByUser)) { originalContent.PublishedByUser = securityService.CurrentPrincipalName; } repository.Save(originalContent); IList <Models.Content> contentsToRemove = originalContent.History.Where(f => f.Status == ContentStatus.Preview || f.Status == ContentStatus.Draft).ToList(); foreach (var redundantContent in contentsToRemove) { repository.Delete(redundantContent); originalContent.History.Remove(redundantContent); } } }