Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added conflict details to Content Type analyzer. #32

Merged
merged 6 commits into from
Jun 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
297 changes: 294 additions & 3 deletions DeveloperTools/Controllers/ContentTypeAnalyzerController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,305 @@
using System.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using DeveloperTools.Models;
using EPiServer.DataAbstraction;
using EPiServer.ServiceLocation;
using EPiServer.DataAbstraction.RuntimeModel;
using EPiServer.Security;

namespace DeveloperTools.Controllers
{
public class ContentTypeAnalyzerController : DeveloperToolsController
{
private readonly ContentTypeModelRepository _contentTypeModelRepository;

public ContentTypeAnalyzerController(ContentTypeModelRepository contentTypeModelRepository)
{
_contentTypeModelRepository = contentTypeModelRepository ?? throw new ArgumentNullException(nameof(contentTypeModelRepository));
}

public ActionResult Index()
{
return View(ServiceLocator.Current.GetInstance<ContentTypeModelRepository>().List());
var contentTypeModels = _contentTypeModelRepository.List();
var model = new ContentTypeAnalyzerModel
{
ContentTypes = CreateContentTypeModels(contentTypeModels)
};
return View(model);
}

private IEnumerable<ContentTypeAnalyzerModel.ContentTypeModel> CreateContentTypeModels(IEnumerable<ContentTypeModel> contentTypeModels)
{
foreach (var contentTypeModel in contentTypeModels)
{
yield return CreateContentTypeModel(contentTypeModel);
foreach (var propertyDefinitionModel in contentTypeModel.PropertyDefinitionModels)
{
yield return CreateContentTypeModel(propertyDefinitionModel, contentTypeModel);
}
}
}

private ContentTypeAnalyzerModel.ContentTypeModel CreateContentTypeModel(ContentTypeModel contentTypeModel)
{
var hasConflict = contentTypeModel.State == SynchronizationStatus.Conflict;
return new ContentTypeAnalyzerModel.ContentTypeModel
{
Type = "Content Type Model",
DisplayName = contentTypeModel.DisplayName,
Name = contentTypeModel.Name,
State = contentTypeModel.State.ToString(),
Description = contentTypeModel.Description,
HasConflict = contentTypeModel.State == SynchronizationStatus.Conflict,
Conflicts = FindConflicts(contentTypeModel, hasConflict)
};
}

private IEnumerable<ContentTypeAnalyzerModel.ConflictModel> FindConflicts(ContentTypeModel model, bool hasConflict)
{
if (!hasConflict) yield break;

var contentType = model.ExistingContentType;

if (!(model.ModelType == null
|| model.ModelType.AssemblyQualifiedName == contentType.ModelTypeString))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Model type",
ContentTypeModelValue = model.ModelType.ToString(),
ContentTypeValue = contentType.ModelTypeString
};
}

if (!string.Equals(model.Name, contentType.Name))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Name",
ContentTypeModelValue = model.Name,
ContentTypeValue = contentType.Name
};
}

if (!(string.IsNullOrEmpty(model.Description)
|| string.Equals(model.Description, contentType.Description)))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Description",
ContentTypeModelValue = model.Description,
ContentTypeValue = contentType.Description
};
}

if (!(string.IsNullOrEmpty(model.DisplayName)
|| string.Equals(model.DisplayName, contentType.DisplayName)))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Display name",
ContentTypeModelValue = model.DisplayName,
ContentTypeValue = contentType.DisplayName
};
}

if (!(!model.Order.HasValue
|| model.Order.Value == contentType.SortOrder))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Sort order",
ContentTypeModelValue = model.Order?.ToString(),
ContentTypeValue = contentType.SortOrder.ToString()
};
}

if (!(!model.Guid.HasValue
|| !(model.Guid.Value != contentType.GUID)))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "GUID",
ContentTypeModelValue = model.Guid?.ToString(),
ContentTypeValue = contentType.GUID.ToString()
};
}

if (!(!model.AvailableInEditMode.HasValue
|| model.AvailableInEditMode.Value == contentType.IsAvailable))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Availability",
ContentTypeModelValue = model.AvailableInEditMode?.ToString(),
ContentTypeValue = contentType.IsAvailable.ToString()
};
}

if (!AclInSynch(model, contentType))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "ACL",
ContentTypeModelValue = AclToString(model.ACL),
ContentTypeValue = AclToString(contentType.ACL)
};
}
}

private static bool AclInSynch(ContentTypeModel model, ContentType contentType)
{
if (contentType.ACL == null && model.ACL == null) return true;

if (contentType.ACL != null && model.ACL != null)
{
return contentType.ACL.EntriesEquals(
model.ACL ?? new AccessControlList
{
new AccessControlEntry(EveryoneRole.RoleName, AccessLevel.Create)
});
}

return false;
}

private static string AclToString(AccessControlList acl)
{
if (acl == null) return string.Empty;
var sb = new StringBuilder();
foreach (var entry in acl.Entries)
{
sb.Append($"{entry.Name}, {entry.Access}, {entry.EntityType}; ");
}
return sb.ToString().TrimEnd().TrimEnd(';');
}

private ContentTypeAnalyzerModel.ContentTypeModel CreateContentTypeModel(PropertyDefinitionModel propertyDefinitionModel, ContentTypeModel contentTypeModel)
{
var hasConflicts = propertyDefinitionModel.State == SynchronizationStatus.Conflict;
return new ContentTypeAnalyzerModel.ContentTypeModel
{
Type = "Property Type Model",
DisplayName = propertyDefinitionModel.DisplayName,
Name = $"{contentTypeModel.Name}-{propertyDefinitionModel.Name}",
State = propertyDefinitionModel.State.ToString(),
Description = propertyDefinitionModel.Description,
HasConflict = hasConflicts,
Conflicts = FindConflicts(propertyDefinitionModel, hasConflicts)
};
}

private IEnumerable<ContentTypeAnalyzerModel.ConflictModel> FindConflicts(PropertyDefinitionModel model, bool hasConflicts)
{
if(!hasConflicts) yield break;

var propertyDefinition = model.ExistingPropertyDefinition;

if (!string.IsNullOrEmpty(model.TabName)
&& (TabIsNotPersisted(propertyDefinition.Tab) || !string.Equals(model.TabName, propertyDefinition.Tab.Name)))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Tab name",
ContentTypeModelValue = model.TabName,
ContentTypeValue = propertyDefinition.Tab.Name
};
}

if (!string.IsNullOrEmpty(model.Name)
&& !string.Equals(model.Name, propertyDefinition.Name))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Name",
ContentTypeModelValue = model.Name,
ContentTypeValue = propertyDefinition.Name
};
}

if (!string.IsNullOrEmpty(model.Description)
&& !string.Equals(model.Description, propertyDefinition.HelpText))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Description (help text)",
ContentTypeModelValue = model.Description,
ContentTypeValue = propertyDefinition.HelpText
};
}

if (!string.IsNullOrEmpty(model.DisplayName)
&& !string.Equals(model.DisplayName, propertyDefinition.EditCaption))
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Display name (edit caption)",
ContentTypeModelValue = model.DisplayName,
ContentTypeValue = propertyDefinition.Tab.Name
};
}

if ((model.CultureSpecific ?? false) != propertyDefinition.LanguageSpecific)
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Culture specific (language specific)",
ContentTypeModelValue = model.CultureSpecific?.ToString(),
ContentTypeValue = propertyDefinition.LanguageSpecific.ToString()
};
}

if (model.Required.HasValue
&& model.Required.Value != propertyDefinition.Required)
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Required",
ContentTypeModelValue = model.Required?.ToString(),
ContentTypeValue = propertyDefinition.Required.ToString()
};
}

if (model.Searchable.HasValue
&& model.Searchable.Value != propertyDefinition.Searchable)
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Searchable",
ContentTypeModelValue = model.Searchable?.ToString(),
ContentTypeValue = propertyDefinition.Searchable.ToString()
};
}

if (model.AvailableInEditMode.HasValue
&& model.AvailableInEditMode.Value != propertyDefinition.DisplayEditUI)
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Available in edit mode (display edit UI)",
ContentTypeModelValue = model.AvailableInEditMode?.ToString(),
ContentTypeValue = propertyDefinition.DisplayEditUI.ToString()
};
}

if (model.Order.HasValue
&& model.Order.Value != propertyDefinition.FieldOrder)
{
yield return new ContentTypeAnalyzerModel.ConflictModel
{
Name = "Field order",
ContentTypeModelValue = model.Order?.ToString(),
ContentTypeValue = propertyDefinition.FieldOrder.ToString()
};
}
}

private static bool TabIsNotPersisted(TabDefinition tab)
{
if (tab != null)
return tab.ID == -1;
return true;
}
}
}
5 changes: 4 additions & 1 deletion DeveloperTools/DeveloperTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
<Compile Include="Core\DeveloperMenuProvider.cs" />
<Compile Include="Core\EnumerableExtensions.cs" />
<Compile Include="Models\AssembliesModel.cs" />
<Compile Include="Models\ContentTypeAnalyzerModel.cs" />
<Compile Include="Models\ExtractedModuleInfo.cs" />
<Compile Include="Models\ModuleDependency.cs" />
<Compile Include="Models\ModuleDependencyViewModel.cs" />
Expand All @@ -238,7 +239,9 @@
<None Include="app.config" />
<Compile Include="Controllers\ModuleDependenciesController.cs" />
<None Include="EPiServer.DeveloperTools.zip" />
<None Include="modules\_protected\EPiServer.DeveloperTools\Views\ContentTypeAnalyzer\Index.aspx" />
<None Include="modules\_protected\EPiServer.DeveloperTools\Views\ContentTypeAnalyzer\Index.aspx">
<SubType>ASPXCodeBehind</SubType>
</None>
<None Include="modules\_protected\EPiServer.DeveloperTools\Views\Routes\Index.aspx" />
<None Include="modules\_protected\EPiServer.DeveloperTools\Views\RemoteEvent\Index.aspx">
<SubType>ASPXCodeBehind</SubType>
Expand Down
38 changes: 38 additions & 0 deletions DeveloperTools/Models/ContentTypeAnalyzerModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.Linq;

namespace DeveloperTools.Models
{
public class ContentTypeAnalyzerModel
{
public ContentTypeAnalyzerModel()
{
ContentTypes = Enumerable.Empty<ContentTypeModel>();
}

public IEnumerable<ContentTypeModel> ContentTypes { get; set; }

public class ContentTypeModel
{
public ContentTypeModel()
{
Conflicts = Enumerable.Empty<ConflictModel>();
}

public string Type { get; set; }
public string DisplayName { get; set; }
public string Name { get; set; }
public string State { get; set; }
public bool HasConflict { get; set; }
public string Description { get; set; }
public IEnumerable<ConflictModel> Conflicts { get; set; }
}

public class ConflictModel
{
public string Name { get; set; }
public string ContentTypeModelValue { get; set; }
public string ContentTypeValue { get; set; }
}
}
}
Loading