Skip to content

Instantly share code, notes, and snippets.

@MattisOlsson
Created August 23, 2019 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattisOlsson/3378fc702723c2d2b0f3d5347929c4ef to your computer and use it in GitHub Desktop.
Save MattisOlsson/3378fc702723c2d2b0f3d5347929c4ef to your computer and use it in GitHub Desktop.
Sample EpiCategories IContentDataImporter
public class CategoryContentImporter : IContentImporter
{
private readonly IContentImporter _defaultContentImporter;
private readonly IContentRepository _contentRepository;
public CategoryContentImporter(IContentImporter defaultContentImporter, IContentRepository contentRepository)
{
_defaultContentImporter = defaultContentImporter;
_contentRepository = contentRepository;
}
public ContentReference Import(ITransferContentData content, AccessLevel requiredDestinationAccess, IContentTransferContext context, TransferImportOptions options)
{
var contentLink = _defaultContentImporter.Import(content, requiredDestinationAccess, context, options);
if (_contentRepository.TryGet(contentLink, out CategoryData category))
{
if (category is CategoryRoot)
{
return contentLink;
}
var siteCategoryRoot = _contentRepository.GetOrCreateSiteCategoriesRoot();
var categoryParent = _contentRepository.Get<IContent>(category.ParentLink);
if (!(categoryParent is CategoryData) || categoryParent is CategoryRoot && !categoryParent.ContentLink.CompareToIgnoreWorkID(siteCategoryRoot))
{
_contentRepository.Move(contentLink, siteCategoryRoot, AccessLevel.NoAccess, AccessLevel.NoAccess);
}
}
return contentLink;
}
}
public class CategoryDataImporter : IDataImporter
{
private readonly IDataImporter _defaultDataImporter;
private readonly IContentRepository _contentRepository;
public CategoryDataImporter(IDataImporter defaultDataImporter, IContentRepository contentRepository)
{
_defaultDataImporter = defaultDataImporter;
_contentRepository = contentRepository;
}
public ITransferLog Import(Stream stream, ContentReference destinationRoot, ImportOptions options)
{
var log = _defaultDataImporter.Import(stream, destinationRoot, options);
CleanupCategoryRoots();
return log;
}
public void Abort()
{
_defaultDataImporter.Abort();
}
private void CleanupCategoryRoots()
{
var siteCategoryRoot = _contentRepository.GetOrCreateSiteCategoriesRoot();
foreach (var categoryRoot in _contentRepository.GetChildren<CategoryRoot>(siteCategoryRoot))
{
_contentRepository.Delete(categoryRoot.ContentLink, true, AccessLevel.NoAccess);
}
foreach (var categoryRoot in _contentRepository.GetChildren<CategoryRoot>(SiteDefinition.Current.SiteAssetsRoot))
{
if (!categoryRoot.ContentLink.CompareToIgnoreWorkID(siteCategoryRoot))
{
_contentRepository.Delete(categoryRoot.ContentLink, true, AccessLevel.NoAccess);
}
}
}
public IImportStatus Status => _defaultDataImporter.Status;
}
[ModuleDependency(typeof(ServiceContainerInitialization))]
[InitializableModule]
public class ServiceInterceptionInitialization : IConfigurableModule
{
public void ConfigureContainer(ServiceConfigurationContext context)
{
context.Services.Intercept<IDataImporter>((locator, defaultDataImporter) => new CategoryDataImporter(
defaultDataImporter,
locator.GetInstance<IContentRepository>()
));
context.Services.Intercept<IContentImporter>((locator, defaultContentImporter) => new CategoryContentImporter(
defaultContentImporter,
locator.GetInstance<IContentRepository>()
));
}
public void Initialize(InitializationEngine context)
{
}
public void Uninitialize(InitializationEngine context)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment