Files
Umbraco-CMS/src/Umbraco.ModelsBuilder.Embedded/ModelsBuilderNotificationHandler.cs

203 lines
8.8 KiB
C#
Raw Normal View History

2021-01-13 12:48:41 +11:00
using System;
2019-06-24 11:58:36 +02:00
using System.Collections.Generic;
using System.Reflection;
2020-07-13 13:38:41 +02:00
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Options;
using Umbraco.Core.Configuration;
2020-09-14 09:56:48 +02:00
using Umbraco.Core.Configuration.Models;
2021-01-13 12:48:41 +11:00
using Umbraco.Core.Events;
2019-06-24 11:58:36 +02:00
using Umbraco.Core.IO;
2021-01-13 12:48:41 +11:00
using Umbraco.Core.Models;
2019-06-24 11:58:36 +02:00
using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;
using Umbraco.Core.Strings;
2020-07-13 13:38:41 +02:00
using Umbraco.Extensions;
using Umbraco.ModelsBuilder.Embedded.BackOffice;
2020-07-13 13:38:41 +02:00
using Umbraco.Web.Common.ModelBinders;
using Umbraco.Web.WebAssets;
2019-06-24 11:58:36 +02:00
2021-01-13 12:48:41 +11:00
namespace Umbraco.ModelsBuilder.Embedded
2019-06-24 11:58:36 +02:00
{
2021-01-13 12:48:41 +11:00
/// <summary>
/// Handles <see cref="UmbracoApplicationStarting"/> and <see cref="ServerVariablesParsing"/> notifications to initialize MB
/// </summary>
2021-02-01 15:37:41 +11:00
internal class ModelsBuilderNotificationHandler : INotificationHandler<UmbracoApplicationStarting>, INotificationHandler<ServerVariablesParsing>, INotificationHandler<ModelBindingError>
2019-06-24 11:58:36 +02:00
{
private readonly ModelsBuilderSettings _config;
private readonly IShortStringHelper _shortStringHelper;
2020-07-13 13:38:41 +02:00
private readonly LinkGenerator _linkGenerator;
2019-06-24 11:58:36 +02:00
2021-01-13 12:48:41 +11:00
public ModelsBuilderNotificationHandler(
IOptions<ModelsBuilderSettings> config,
IShortStringHelper shortStringHelper,
2021-02-01 15:37:41 +11:00
LinkGenerator linkGenerator)
2019-06-24 11:58:36 +02:00
{
_config = config.Value;
_shortStringHelper = shortStringHelper;
_shortStringHelper = shortStringHelper;
2020-07-13 13:38:41 +02:00
_linkGenerator = linkGenerator;
2019-06-24 11:58:36 +02:00
}
2021-01-13 12:48:41 +11:00
/// <summary>
/// Handles the <see cref="UmbracoApplicationStarting"/> notification
/// </summary>
2021-01-25 07:37:05 +01:00
public void Handle(UmbracoApplicationStarting notification)
2019-06-24 11:58:36 +02:00
{
// always setup the dashboard
// note: UmbracoApiController instances are automatically registered
if (_config.ModelsMode != ModelsMode.Nothing)
2021-01-13 12:48:41 +11:00
{
2019-10-29 11:02:18 +11:00
FileService.SavingTemplate += FileService_SavingTemplate;
2021-01-13 12:48:41 +11:00
}
2020-07-13 13:38:41 +02:00
}
2021-01-13 12:48:41 +11:00
/// <summary>
/// Handles the <see cref="ServerVariablesParsing"/> notification
/// </summary>
2021-01-25 07:37:05 +01:00
public void Handle(ServerVariablesParsing notification)
2019-06-24 11:58:36 +02:00
{
IDictionary<string, object> serverVars = notification.ServerVariables;
if (!serverVars.ContainsKey("umbracoUrls"))
2021-01-13 12:48:41 +11:00
{
throw new ArgumentException("Missing umbracoUrls.");
2021-01-13 12:48:41 +11:00
}
var umbracoUrlsObject = serverVars["umbracoUrls"];
if (umbracoUrlsObject == null)
2021-01-13 12:48:41 +11:00
{
throw new ArgumentException("Null umbracoUrls");
2021-01-13 12:48:41 +11:00
}
if (!(umbracoUrlsObject is Dictionary<string, object> umbracoUrls))
2021-01-13 12:48:41 +11:00
{
throw new ArgumentException("Invalid umbracoUrls");
2021-01-13 12:48:41 +11:00
}
if (!serverVars.ContainsKey("umbracoPlugins"))
2021-01-13 12:48:41 +11:00
{
throw new ArgumentException("Missing umbracoPlugins.");
2021-01-13 12:48:41 +11:00
}
if (!(serverVars["umbracoPlugins"] is Dictionary<string, object> umbracoPlugins))
2021-01-13 12:48:41 +11:00
{
throw new ArgumentException("Invalid umbracoPlugins");
2021-01-13 12:48:41 +11:00
}
umbracoUrls["modelsBuilderBaseUrl"] = _linkGenerator.GetUmbracoApiServiceBaseUrl<ModelsBuilderDashboardController>(controller => controller.BuildModels());
umbracoPlugins["modelsBuilder"] = GetModelsBuilderSettings();
2019-06-24 11:58:36 +02:00
}
private Dictionary<string, object> GetModelsBuilderSettings()
{
var settings = new Dictionary<string, object>
{
{"mode", _config.ModelsMode.ToString()}
2019-06-24 11:58:36 +02:00
};
return settings;
}
/// <summary>
/// Used to check if a template is being created based on a document type, in this case we need to
/// ensure the template markup is correct based on the model name of the document type
/// </summary>
2021-01-13 12:48:41 +11:00
private void FileService_SavingTemplate(IFileService sender, SaveEventArgs<ITemplate> e)
2019-06-24 11:58:36 +02:00
{
// don't do anything if this special key is not found
2021-01-13 12:48:41 +11:00
if (!e.AdditionalData.ContainsKey("CreateTemplateForContentType"))
{
return;
}
2019-06-24 11:58:36 +02:00
// ensure we have the content type alias
if (!e.AdditionalData.ContainsKey("ContentTypeAlias"))
2021-01-13 12:48:41 +11:00
{
2019-06-24 11:58:36 +02:00
throw new InvalidOperationException("The additionalData key: ContentTypeAlias was not found");
2021-01-13 12:48:41 +11:00
}
2019-06-24 11:58:36 +02:00
2021-01-13 12:48:41 +11:00
foreach (ITemplate template in e.SavedEntities)
{
2019-06-24 11:58:36 +02:00
// if it is in fact a new entity (not been saved yet) and the "CreateTemplateForContentType" key
// is found, then it means a new template is being created based on the creation of a document type
if (!template.HasIdentity && string.IsNullOrWhiteSpace(template.Content))
{
// ensure is safe and always pascal cased, per razor standard
// + this is how we get the default model name in Umbraco.ModelsBuilder.Umbraco.Application
var alias = e.AdditionalData["ContentTypeAlias"].ToString();
var name = template.Name; // will be the name of the content type since we are creating
var className = UmbracoServices.GetClrName(_shortStringHelper, name, alias);
2019-06-24 11:58:36 +02:00
var modelNamespace = _config.ModelsNamespace;
// we do not support configuring this at the moment, so just let Umbraco use its default value
2021-01-13 12:48:41 +11:00
// var modelNamespaceAlias = ...;
2019-06-24 11:58:36 +02:00
var markup = ViewHelper.GetDefaultFileContent(
modelClassName: className,
modelNamespace: modelNamespace/*,
modelNamespaceAlias: modelNamespaceAlias*/);
2021-01-13 12:48:41 +11:00
// set the template content to the new markup
2019-06-24 11:58:36 +02:00
template.Content = markup;
}
2021-01-13 12:48:41 +11:00
}
2019-06-24 11:58:36 +02:00
}
2021-02-01 15:37:41 +11:00
/// <summary>
/// Handles when a model binding error occurs
/// </summary>
public void Handle(ModelBindingError notification)
2019-06-24 11:58:36 +02:00
{
2021-02-01 15:37:41 +11:00
ModelsBuilderAssemblyAttribute sourceAttr = notification.SourceType.Assembly.GetCustomAttribute<ModelsBuilderAssemblyAttribute>();
ModelsBuilderAssemblyAttribute modelAttr = notification.ModelType.Assembly.GetCustomAttribute<ModelsBuilderAssemblyAttribute>();
2019-06-24 11:58:36 +02:00
// if source or model is not a ModelsBuider type...
if (sourceAttr == null || modelAttr == null)
{
// if neither are ModelsBuilder types, give up entirely
if (sourceAttr == null && modelAttr == null)
2021-01-13 12:48:41 +11:00
{
2019-06-24 11:58:36 +02:00
return;
2021-01-13 12:48:41 +11:00
}
2019-06-24 11:58:36 +02:00
// else report, but better not restart (loops?)
2021-02-01 15:37:41 +11:00
notification.Message.Append(" The ");
notification.Message.Append(sourceAttr == null ? "view model" : "source");
notification.Message.Append(" is a ModelsBuilder type, but the ");
notification.Message.Append(sourceAttr != null ? "view model" : "source");
notification.Message.Append(" is not. The application is in an unstable state and should be restarted.");
2019-06-24 11:58:36 +02:00
return;
}
// both are ModelsBuilder types
var pureSource = sourceAttr.PureLive;
var pureModel = modelAttr.PureLive;
2019-06-24 11:58:36 +02:00
if (sourceAttr.PureLive || modelAttr.PureLive)
2021-01-13 12:48:41 +11:00
{
if (pureSource == false || pureModel == false)
{
2019-06-24 11:58:36 +02:00
// only one is pure - report, but better not restart (loops?)
2021-02-01 15:37:41 +11:00
notification.Message.Append(pureSource
? " The content model is PureLive, but the view model is not."
: " The view model is PureLive, but the content model is not.");
2021-02-01 15:37:41 +11:00
notification.Message.Append(" The application is in an unstable state and should be restarted.");
}
else
{
2019-06-24 11:58:36 +02:00
// both are pure - report, and if different versions, restart
// if same version... makes no sense... and better not restart (loops?)
2021-02-01 15:37:41 +11:00
var sourceVersion = notification.SourceType.Assembly.GetName().Version;
var modelVersion = notification.ModelType.Assembly.GetName().Version;
notification.Message.Append(" Both view and content models are PureLive, with ");
notification.Message.Append(sourceVersion == modelVersion
? "same version. The application is in an unstable state and should be restarted."
: "different versions. The application is in an unstable state and is going to be restarted.");
2021-02-01 15:37:41 +11:00
notification.Restart = sourceVersion != modelVersion;
}
2021-01-13 12:48:41 +11:00
}
2019-06-24 11:58:36 +02:00
}
}
}