2021-09-15 13:40:08 +02:00
|
|
|
using System.Globalization;
|
2017-07-19 13:42:47 +02:00
|
|
|
using Examine;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Mapping;
|
|
|
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
|
|
|
using Umbraco.Cms.Core.Models.Entities;
|
|
|
|
|
using Umbraco.Cms.Core.Models.Membership;
|
2021-02-12 10:57:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Examine;
|
2021-02-09 11:26:22 +01:00
|
|
|
using Umbraco.Extensions;
|
2017-07-19 13:42:47 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
namespace Umbraco.Cms.Core.Models.Mapping;
|
|
|
|
|
|
|
|
|
|
public class EntityMapDefinition : IMapDefinition
|
2017-07-19 13:42:47 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
public void DefineMaps(IUmbracoMapper mapper)
|
|
|
|
|
{
|
|
|
|
|
mapper.Define<IEntitySlim, EntityBasic>((source, context) => new EntityBasic(), Map);
|
|
|
|
|
mapper.Define<PropertyType, EntityBasic>((source, context) => new EntityBasic(), Map);
|
|
|
|
|
mapper.Define<PropertyGroup, EntityBasic>((source, context) => new EntityBasic(), Map);
|
|
|
|
|
mapper.Define<IUser, EntityBasic>((source, context) => new EntityBasic(), Map);
|
|
|
|
|
mapper.Define<ITemplate, EntityBasic>((source, context) => new EntityBasic(), Map);
|
|
|
|
|
mapper.Define<EntityBasic, ContentTypeSort>((source, context) => new ContentTypeSort(), Map);
|
|
|
|
|
mapper.Define<IContentTypeComposition, EntityBasic>((source, context) => new EntityBasic(), Map);
|
|
|
|
|
mapper.Define<IEntitySlim, SearchResultEntity>((source, context) => new SearchResultEntity(), Map);
|
|
|
|
|
mapper.Define<ISearchResult, SearchResultEntity>((source, context) => new SearchResultEntity(), Map);
|
|
|
|
|
mapper.Define<ISearchResults, IEnumerable<SearchResultEntity>>((source, context) =>
|
|
|
|
|
context.MapEnumerable<ISearchResult, SearchResultEntity>(source).WhereNotNull());
|
|
|
|
|
mapper.Define<IEnumerable<ISearchResult>, IEnumerable<SearchResultEntity>>((source, context) =>
|
|
|
|
|
context.MapEnumerable<ISearchResult, SearchResultEntity>(source).WhereNotNull());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Umbraco.Code.MapAll -Alias
|
|
|
|
|
private static void Map(IEntitySlim source, EntityBasic target, MapperContext context)
|
2017-07-19 13:42:47 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
target.Icon = MapContentTypeIcon(source);
|
|
|
|
|
target.Id = source.Id;
|
|
|
|
|
target.Key = source.Key;
|
|
|
|
|
target.Name = MapName(source, context);
|
|
|
|
|
target.ParentId = source.ParentId;
|
|
|
|
|
target.Path = source.Path;
|
|
|
|
|
target.Trashed = source.Trashed;
|
|
|
|
|
target.Udi = Udi.Create(ObjectTypes.GetUdiType(source.NodeObjectType), source.Key);
|
|
|
|
|
|
|
|
|
|
if (source is IContentEntitySlim contentSlim)
|
2019-03-26 10:39:50 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
source.AdditionalData!["ContentTypeAlias"] = contentSlim.ContentTypeAlias;
|
2019-03-26 10:39:50 +01:00
|
|
|
}
|
2018-01-15 11:32:30 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (source is IDocumentEntitySlim documentSlim)
|
2017-07-19 13:42:47 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
source.AdditionalData!["IsPublished"] = documentSlim.Published;
|
|
|
|
|
}
|
2019-08-19 16:30:13 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (source is IMediaEntitySlim mediaSlim)
|
|
|
|
|
{
|
|
|
|
|
if (source.AdditionalData is not null)
|
2019-08-19 16:30:13 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
// pass UpdateDate for MediaPicker ListView ordering
|
|
|
|
|
source.AdditionalData["UpdateDate"] = mediaSlim.UpdateDate;
|
|
|
|
|
source.AdditionalData["MediaPath"] = mediaSlim.MediaPath;
|
2019-08-19 16:30:13 +02:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2019-06-13 23:39:36 +10:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (source.AdditionalData is not null)
|
|
|
|
|
{
|
|
|
|
|
// NOTE: we're mapping the objects in AdditionalData by object reference here.
|
|
|
|
|
// it works fine for now, but it's something to keep in mind in the future
|
|
|
|
|
foreach (KeyValuePair<string, object?> kvp in source.AdditionalData)
|
2019-08-19 16:30:13 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (kvp.Value is not null)
|
2022-02-24 14:39:29 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
target.AdditionalData[kvp.Key] = kvp.Value;
|
2022-02-24 14:39:29 +01:00
|
|
|
}
|
2019-08-19 16:30:13 +02:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2019-06-13 23:39:36 +10:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
target.AdditionalData.Add("IsContainer", source.IsContainer);
|
|
|
|
|
}
|
2019-04-06 12:24:41 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// Umbraco.Code.MapAll -Udi -Trashed
|
|
|
|
|
private static void Map(PropertyType source, EntityBasic target, MapperContext context)
|
|
|
|
|
{
|
|
|
|
|
target.Alias = source.Alias;
|
|
|
|
|
target.Icon = "icon-box";
|
|
|
|
|
target.Id = source.Id;
|
|
|
|
|
target.Key = source.Key;
|
|
|
|
|
target.Name = source.Name;
|
|
|
|
|
target.ParentId = -1;
|
|
|
|
|
target.Path = string.Empty;
|
|
|
|
|
}
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// Umbraco.Code.MapAll -Udi -Trashed
|
|
|
|
|
private static void Map(PropertyGroup source, EntityBasic target, MapperContext context)
|
|
|
|
|
{
|
|
|
|
|
target.Alias = source.Alias;
|
|
|
|
|
target.Icon = "icon-tab";
|
|
|
|
|
target.Id = source.Id;
|
|
|
|
|
target.Key = source.Key;
|
|
|
|
|
target.Name = source.Name;
|
|
|
|
|
target.ParentId = -1;
|
|
|
|
|
target.Path = string.Empty;
|
|
|
|
|
}
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// Umbraco.Code.MapAll -Udi -Trashed
|
|
|
|
|
private static void Map(IUser source, EntityBasic target, MapperContext context)
|
|
|
|
|
{
|
|
|
|
|
target.Alias = source.Username;
|
|
|
|
|
target.Icon = Constants.Icons.User;
|
|
|
|
|
target.Id = source.Id;
|
|
|
|
|
target.Key = source.Key;
|
|
|
|
|
target.Name = source.Name;
|
|
|
|
|
target.ParentId = -1;
|
|
|
|
|
target.Path = string.Empty;
|
|
|
|
|
}
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// Umbraco.Code.MapAll -Trashed
|
|
|
|
|
private static void Map(ITemplate source, EntityBasic target, MapperContext context)
|
|
|
|
|
{
|
|
|
|
|
target.Alias = source.Alias;
|
|
|
|
|
target.Icon = Constants.Icons.Template;
|
|
|
|
|
target.Id = source.Id;
|
|
|
|
|
target.Key = source.Key;
|
|
|
|
|
target.Name = source.Name;
|
|
|
|
|
target.ParentId = -1;
|
|
|
|
|
target.Path = source.Path;
|
|
|
|
|
target.Udi = Udi.Create(Constants.UdiEntityType.Template, source.Key);
|
|
|
|
|
}
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// Umbraco.Code.MapAll -SortOrder
|
|
|
|
|
private static void Map(EntityBasic source, ContentTypeSort target, MapperContext context)
|
|
|
|
|
{
|
|
|
|
|
target.Alias = source.Alias;
|
|
|
|
|
target.Id = new Lazy<int>(() => Convert.ToInt32(source.Id));
|
|
|
|
|
}
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// Umbraco.Code.MapAll -Trashed
|
|
|
|
|
private static void Map(IContentTypeComposition source, EntityBasic target, MapperContext context)
|
|
|
|
|
{
|
|
|
|
|
target.Alias = source.Alias;
|
|
|
|
|
target.Icon = source.Icon;
|
|
|
|
|
target.Id = source.Id;
|
|
|
|
|
target.Key = source.Key;
|
|
|
|
|
target.Name = source.Name;
|
|
|
|
|
target.ParentId = source.ParentId;
|
|
|
|
|
target.Path = source.Path;
|
|
|
|
|
target.Udi = ContentTypeMapDefinition.MapContentTypeUdi(source);
|
|
|
|
|
}
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// Umbraco.Code.MapAll -Trashed -Alias -Score
|
|
|
|
|
private static void Map(EntitySlim source, SearchResultEntity target, MapperContext context)
|
|
|
|
|
{
|
|
|
|
|
target.Icon = MapContentTypeIcon(source);
|
|
|
|
|
target.Id = source.Id;
|
|
|
|
|
target.Key = source.Key;
|
|
|
|
|
target.Name = source.Name;
|
|
|
|
|
target.ParentId = source.ParentId;
|
|
|
|
|
target.Path = source.Path;
|
|
|
|
|
target.Udi = Udi.Create(ObjectTypes.GetUdiType(source.NodeObjectType), source.Key);
|
|
|
|
|
|
|
|
|
|
if (target.Icon.IsNullOrWhiteSpace())
|
2019-03-26 10:39:50 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (source.NodeObjectType == Constants.ObjectTypes.Document)
|
|
|
|
|
{
|
|
|
|
|
target.Icon = Constants.Icons.Content;
|
|
|
|
|
}
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (source.NodeObjectType == Constants.ObjectTypes.Media)
|
|
|
|
|
{
|
|
|
|
|
target.Icon = Constants.Icons.Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (source.NodeObjectType == Constants.ObjectTypes.Member)
|
|
|
|
|
{
|
|
|
|
|
target.Icon = Constants.Icons.Member;
|
|
|
|
|
}
|
|
|
|
|
else if (source.NodeObjectType == Constants.ObjectTypes.DataType)
|
2019-03-26 10:39:50 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
target.Icon = Constants.Icons.DataType;
|
|
|
|
|
}
|
|
|
|
|
else if (source.NodeObjectType == Constants.ObjectTypes.DocumentType)
|
|
|
|
|
{
|
|
|
|
|
target.Icon = Constants.Icons.ContentType;
|
|
|
|
|
}
|
|
|
|
|
else if (source.NodeObjectType == Constants.ObjectTypes.MediaType)
|
|
|
|
|
{
|
|
|
|
|
target.Icon = Constants.Icons.MediaType;
|
|
|
|
|
}
|
|
|
|
|
else if (source.NodeObjectType == Constants.ObjectTypes.MemberType)
|
|
|
|
|
{
|
|
|
|
|
target.Icon = Constants.Icons.MemberType;
|
|
|
|
|
}
|
|
|
|
|
else if (source.NodeObjectType == Constants.ObjectTypes.TemplateType)
|
|
|
|
|
{
|
|
|
|
|
target.Icon = Constants.Icons.Template;
|
2019-03-26 10:39:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// Umbraco.Code.MapAll -Alias -Trashed
|
|
|
|
|
private static void Map(ISearchResult source, SearchResultEntity target, MapperContext context)
|
|
|
|
|
{
|
|
|
|
|
target.Id = source.Id;
|
|
|
|
|
target.Score = source.Score;
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// TODO: Properly map this (not aftermap)
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// get the icon if there is one
|
|
|
|
|
target.Icon = source.Values.ContainsKey(UmbracoExamineFieldNames.IconFieldName)
|
|
|
|
|
? source.Values[UmbracoExamineFieldNames.IconFieldName]
|
|
|
|
|
: Constants.Icons.DefaultIcon;
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
target.Name = source.Values.ContainsKey(UmbracoExamineFieldNames.NodeNameFieldName)
|
|
|
|
|
? source.Values[UmbracoExamineFieldNames.NodeNameFieldName]
|
|
|
|
|
: "[no name]";
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
var culture = context.GetCulture()?.ToLowerInvariant();
|
|
|
|
|
if (culture.IsNullOrWhiteSpace() == false)
|
|
|
|
|
{
|
|
|
|
|
target.Name = source.Values.ContainsKey($"nodeName_{culture}")
|
|
|
|
|
? source.Values[$"nodeName_{culture}"]
|
|
|
|
|
: target.Name;
|
|
|
|
|
}
|
2020-02-26 16:16:26 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (source.Values.TryGetValue(UmbracoExamineFieldNames.UmbracoFileFieldName, out var umbracoFile) &&
|
|
|
|
|
umbracoFile.IsNullOrWhiteSpace() == false)
|
|
|
|
|
{
|
|
|
|
|
if (umbracoFile != null)
|
2019-10-03 17:46:18 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
target.Name = $"{target.Name} ({umbracoFile})";
|
2019-10-03 17:46:18 +01:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2019-10-03 17:46:18 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (source.Values.ContainsKey(UmbracoExamineFieldNames.NodeKeyFieldName))
|
|
|
|
|
{
|
|
|
|
|
if (Guid.TryParse(source.Values[UmbracoExamineFieldNames.NodeKeyFieldName], out Guid key))
|
2019-03-26 10:39:50 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
target.Key = key;
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// need to set the UDI
|
|
|
|
|
if (source.Values.ContainsKey(ExamineFieldNames.CategoryFieldName))
|
|
|
|
|
{
|
|
|
|
|
switch (source.Values[ExamineFieldNames.CategoryFieldName])
|
2017-07-19 13:42:47 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
case IndexTypes.Member:
|
|
|
|
|
target.Udi = new GuidUdi(Constants.UdiEntityType.Member, target.Key);
|
|
|
|
|
break;
|
|
|
|
|
case IndexTypes.Content:
|
|
|
|
|
target.Udi = new GuidUdi(Constants.UdiEntityType.Document, target.Key);
|
|
|
|
|
break;
|
|
|
|
|
case IndexTypes.Media:
|
|
|
|
|
target.Udi = new GuidUdi(Constants.UdiEntityType.Media, target.Key);
|
|
|
|
|
break;
|
2017-07-19 13:42:47 +02:00
|
|
|
}
|
2019-03-26 10:39:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2019-01-15 15:10:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (source.Values.ContainsKey("parentID"))
|
|
|
|
|
{
|
|
|
|
|
if (int.TryParse(source.Values["parentID"], NumberStyles.Integer, CultureInfo.InvariantCulture,
|
|
|
|
|
out var parentId))
|
2019-03-26 10:39:50 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
target.ParentId = parentId;
|
2019-03-26 10:39:50 +01:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
else
|
2019-03-26 10:39:50 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
target.ParentId = -1;
|
2019-03-26 10:39:50 +01:00
|
|
|
}
|
2017-07-19 13:42:47 +02:00
|
|
|
}
|
2018-09-26 16:27:34 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
target.Path = source.Values.ContainsKey(UmbracoExamineFieldNames.IndexPathFieldName)
|
|
|
|
|
? source.Values[UmbracoExamineFieldNames.IndexPathFieldName]
|
|
|
|
|
: string.Empty;
|
|
|
|
|
|
|
|
|
|
if (source.Values.ContainsKey(ExamineFieldNames.ItemTypeFieldName))
|
2019-10-24 20:46:01 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
target.AdditionalData.Add("contentType", source.Values[ExamineFieldNames.ItemTypeFieldName]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-24 20:46:01 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private static string? MapContentTypeIcon(IEntitySlim entity)
|
|
|
|
|
{
|
|
|
|
|
switch (entity)
|
|
|
|
|
{
|
|
|
|
|
case IMemberEntitySlim memberEntity:
|
|
|
|
|
return memberEntity.ContentTypeIcon;
|
|
|
|
|
case IContentEntitySlim contentEntity:
|
|
|
|
|
// NOTE: this case covers both content and media entities
|
|
|
|
|
return contentEntity.ContentTypeIcon;
|
2019-10-24 20:46:01 +02:00
|
|
|
}
|
2019-03-26 10:39:50 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string MapName(IEntitySlim source, MapperContext context)
|
|
|
|
|
{
|
|
|
|
|
if (!(source is DocumentEntitySlim doc))
|
2018-09-26 16:27:34 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
return source.Name!;
|
|
|
|
|
}
|
2018-09-26 16:27:34 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// invariant = only 1 name
|
|
|
|
|
if (!doc.Variations.VariesByCulture())
|
|
|
|
|
{
|
|
|
|
|
return source.Name!;
|
|
|
|
|
}
|
2018-09-26 16:27:34 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// variant = depends on culture
|
|
|
|
|
var culture = context.GetCulture();
|
2018-09-26 16:27:34 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// if there's no culture here, the issue is somewhere else (UI, whatever) - throw!
|
|
|
|
|
if (culture == null)
|
2018-09-26 16:27:34 +02:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// throw new InvalidOperationException("Missing culture in mapping options.");
|
|
|
|
|
// TODO: we should throw, but this is used in various places that won't set a culture yet
|
|
|
|
|
{
|
|
|
|
|
return source.Name!;
|
2018-09-26 16:27:34 +02:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
|
|
|
|
|
// if we don't have a name for a culture, it means the culture is not available, and
|
|
|
|
|
// hey we should probably not be mapping it, but it's too late, return a fallback name
|
|
|
|
|
return doc.CultureNames.TryGetValue(culture, out var name) && !name.IsNullOrWhiteSpace()
|
|
|
|
|
? name
|
|
|
|
|
: $"({source.Name})";
|
2017-07-19 13:42:47 +02:00
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
}
|