Skip to content

Instantly share code, notes, and snippets.

@davidknipe
Last active October 13, 2019 23:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidknipe/9fae12f2d79aff49218355db745e1058 to your computer and use it in GitHub Desktop.
Save davidknipe/9fae12f2d79aff49218355db745e1058 to your computer and use it in GitHub Desktop.
Migrates content to use Geta Categories. Note will only migrate categories that exist in the Geta Categories structure.
using System.Collections.Generic;
using System.Linq;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.PlugIn;
using EPiServer.Scheduler;
using EPiServer.Security;
using Foundation.Cms.Categories;
using Geta.EpiCategories;
using Geta.EpiCategories.Extensions;
namespace Foundation.CategoryMigration
{
[ScheduledPlugIn(
DisplayName = "Migrate content to use Geta Categories"
, Description =
"Migrates content to use Geta Categories. Note will only migrate categories that exist in the Geta Categories structure."
, SortIndex = int.MaxValue)]
public class MigrateContentToGetaCategoriesJob : ScheduledJobBase
{
private readonly CategoryRepository _categoryRepository;
private readonly IContentRepository _contentRepository;
private bool _stopSignaled;
private int _itemsUpdatedCount = 0;
private Dictionary<string, ContentReference> _allGetaCategories;
public MigrateContentToGetaCategoriesJob(CategoryRepository categoryRepository, IContentRepository repo)
{
IsStoppable = true;
_categoryRepository = categoryRepository;
_contentRepository = repo;
}
public override void Stop()
{
_stopSignaled = true;
}
public override string Execute()
{
OnStatusChanged($"Starting execution of {this.GetType()}");
UpdateContentRecursive(ContentReference.RootPage);
return $"{_itemsUpdatedCount} content items updated";
}
private void GetAllGetaCategories()
{
List<StandardCategory> listOfCategories = new List<StandardCategory>();
var allGetaCats =
GetCategoriesRecursive(_contentRepository.GetOrCreateGlobalCategoriesRoot(), listOfCategories);
_allGetaCategories = new Dictionary<string, ContentReference>();
foreach (var standardCategory in allGetaCats)
{
try
{
_allGetaCategories.Add(standardCategory.Name, standardCategory.ContentLink);
}
catch { }
}
}
private ContentReference GetCategoryMap(int categoryId)
{
// Look up category
var categoryName = _categoryRepository.Get(categoryId).Name;
if (_allGetaCategories == null) GetAllGetaCategories();
try
{
return _allGetaCategories[categoryName];
}
catch
{
return ContentReference.EmptyReference;
}
}
private void UpdateContentRecursive(ContentReference root)
{
if (_stopSignaled) return;
if (root == ContentReference.WasteBasket) return;
var allContent = _contentRepository.GetChildren<IContent>(root);
if (allContent == null || !allContent.Any()) return;
foreach (var contentItem in allContent)
{
if (contentItem is ICategorizable contentCategories &&
contentCategories is ICategorizableContent &&
contentItem is ContentData contentData)
{
var doContentUpdate = false;
var contentUpdate = contentData?.CreateWritableClone() as ICategorizableContent;
if (contentUpdate != null)
{
// Ok so the content supports Episerver categories and Geta categories so we should migrate
foreach (var category in contentCategories.Category)
{
OnStatusChanged(
$"Adding categories for Id: {contentItem.ContentLink.ID}, Name: {contentItem.Name}");
var mappedCategory = GetCategoryMap(category);
if (mappedCategory != ContentReference.EmptyReference)
{
if (contentUpdate.Categories == null)
{
contentUpdate.Categories = new List<ContentReference>();
}
if (!contentUpdate.Categories.Contains(mappedCategory))
{
contentUpdate.Categories.Add(mappedCategory);
doContentUpdate = true;
}
}
}
}
if (doContentUpdate)
{
_itemsUpdatedCount++;
_contentRepository.Publish(contentUpdate as IContent, AccessLevel.NoAccess);
}
}
UpdateContentRecursive(contentItem.ContentLink);
}
}
private List<StandardCategory> GetCategoriesRecursive(ContentReference parent, List<StandardCategory> list)
{
foreach (var child in _contentRepository.GetChildren<StandardCategory>(parent))
{
list.Add(child);
GetCategoriesRecursive(child.ContentLink, list);
}
return list;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment