Skip to content

Commit

Permalink
Merge pull request #14 from enterspeedhq/feature/updated-existing--do…
Browse files Browse the repository at this point in the history
…cumenttypes-instead-of-manual-delete

Update existing document types instead of manually have to delete them. This also mean that existing uSync export of document types can be reused as it's the same document type being updated.
  • Loading branch information
jesperweber authored May 27, 2024
2 parents d72e2c6 + 88ae112 commit 87abdfa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
migratorResource.importDocumentTypes().then(function (result) {
notificationsService.success("Document types imported");
}, function (error) {
notificationsService.error("Something w ent wrong when importing document types: " + error);
notificationsService.error("Something went wrong when importing document types: " + error);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,36 @@ private void CreateBlockListDataType(IEntity dataTypeFolder)

private void CreatePageDocType(Schema page, IEntity pagesFolder, IEntity compositionsFolder)
{
if (!_contentTypeAliasList.Any(c => c.Equals(page.MetaSchema.SourceEntityAlias)))
{
// Build new doc type
var newPageDocumentType = BuildNewPageDocType(page, pagesFolder);
// Build new doc type or reuse existing
var pageDocumentType = GetOrCreatePageDocType(page, pagesFolder);

// Add the rest of the properties
AddProperties(page, newPageDocumentType, compositionsFolder);
RemoveExistingCompositions(pageDocumentType);
DeleteExistingProperties(pageDocumentType);

// TODO: Ensure that this is handled in another way. This is an assumption!
newPageDocumentType.AddPropertyType(new PropertyType(_shortStringHelper,
_dataTypes.Find(d => d.Name == BlockListName))
{
Name = _umbracoMigrationConfiguration.ContentPropertyAlias.ToUmbracoName(),
Alias = _umbracoMigrationConfiguration.ContentPropertyAlias
}, "content");
// Add the rest of the properties
AddProperties(page, pageDocumentType, compositionsFolder);

// Save the new document type
_contentTypeService.Save(newPageDocumentType);
}
// TODO: Ensure that this is handled in another way. This is an assumption!
pageDocumentType.AddPropertyType(new PropertyType(_shortStringHelper,
_dataTypes.Find(d => d.Name == BlockListName))
{
Name = _umbracoMigrationConfiguration.ContentPropertyAlias.ToUmbracoName(),
Alias = _umbracoMigrationConfiguration.ContentPropertyAlias
}, "content");

// Save the new document type
_contentTypeService.Save(pageDocumentType);
}

private ContentType BuildNewPageDocType(Schema page, IEntity pagesFolder)
private IContentType GetOrCreatePageDocType(Schema page, IEntity pagesFolder)
{
var existingContentTypeAlias = _contentTypeAliasList.FirstOrDefault(c =>
c.Equals(page.MetaSchema.SourceEntityAlias, StringComparison.InvariantCultureIgnoreCase));
if (!string.IsNullOrWhiteSpace(existingContentTypeAlias))
{
return _contentTypeService.Get(existingContentTypeAlias);
}

var newPageDocumentType = new ContentType(_shortStringHelper, pagesFolder.Id)
{
Alias = page.MetaSchema.SourceEntityAlias.ToFirstLowerInvariant(),
Expand All @@ -158,8 +165,15 @@ private ContentType BuildNewPageDocType(Schema page, IEntity pagesFolder)
return newPageDocumentType;
}

private IContentType CreateComposition(IEntity compositionsFolder, EnterspeedPropertyType enterspeedProperty)
private IContentType GetOrCreateComposition(IEntity compositionsFolder, EnterspeedPropertyType enterspeedProperty)
{
var existingContentTypeAlias = _contentTypeAliasList.FirstOrDefault(c =>
c.Equals(enterspeedProperty.Alias, StringComparison.InvariantCultureIgnoreCase));
if (!string.IsNullOrWhiteSpace(existingContentTypeAlias))
{
return _contentTypeService.Get(existingContentTypeAlias);
}

var composition = new ContentType(_shortStringHelper, compositionsFolder.Id)
{
Alias = enterspeedProperty.Alias,
Expand All @@ -171,7 +185,7 @@ private IContentType CreateComposition(IEntity compositionsFolder, EnterspeedPro
return composition;
}

private void AddProperties(Schema schema, ContentType pageDocumentType, IEntity compositionsFolder)
private void AddProperties(Schema schema, IContentType pageDocumentType, IEntity compositionsFolder)
{
if (_dataTypes?.Any() == true)
{
Expand All @@ -188,29 +202,41 @@ private void AddProperties(Schema schema, ContentType pageDocumentType, IEntity
}
}

private void HandleComposition(ContentType pageDocumentType, IEntity compositionsFolder, EnterspeedPropertyType enterspeedProperty)
private void DeleteExistingProperties(IContentType documentType)
{
var propertyTypes = documentType.PropertyTypes.ToList();
foreach (var propertyType in propertyTypes)
{
documentType.RemovePropertyType(propertyType.Alias);
}
}

private void RemoveExistingCompositions(IContentType documentType)
{
var compositionAliases = documentType.CompositionAliases().ToList();
foreach (var compositionAlias in compositionAliases)
{
documentType.RemoveContentType(compositionAlias);
}
}

private void HandleComposition(IContentType pageDocumentType, IEntity compositionsFolder, EnterspeedPropertyType enterspeedProperty)
{
var compositionExists = pageDocumentType.ContentTypeCompositionExists(enterspeedProperty.Alias);
if (!compositionExists)
{
// Check if already exists
var composition = _contentTypeService.Get(enterspeedProperty.Alias);
if (composition == null)
{
composition = CreateComposition(compositionsFolder, enterspeedProperty);

// Check if any properties on composition
if (!enterspeedProperty.ChildProperties.Any()) return;
var composition = GetOrCreateComposition(compositionsFolder, enterspeedProperty);

// Add properties to composition
foreach (var childProperty in enterspeedProperty.ChildProperties)
{
AddProperties(childProperty, composition, true);
}
DeleteExistingProperties(composition);

_contentTypeService.Save(composition);
// Add properties to composition
foreach (var childProperty in enterspeedProperty.ChildProperties)
{
AddProperties(childProperty, composition, true);
}

_contentTypeService.Save(composition);

pageDocumentType.AddContentType(composition);
}
}
Expand Down

0 comments on commit 87abdfa

Please sign in to comment.