2017-07-19 13:42:47 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Examine;
|
|
|
|
|
|
using Examine.LuceneEngine.Providers;
|
|
|
|
|
|
using Umbraco.Core;
|
2019-03-26 10:39:50 +01:00
|
|
|
|
using Umbraco.Core.Mapping;
|
2017-07-19 13:42:47 +02:00
|
|
|
|
using Umbraco.Core.Models;
|
2018-01-15 11:32:30 +01:00
|
|
|
|
using Umbraco.Core.Models.Entities;
|
2017-07-19 13:42:47 +02:00
|
|
|
|
using Umbraco.Core.Models.Membership;
|
|
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2017-07-27 12:01:38 +02:00
|
|
|
|
using Umbraco.Examine;
|
2017-07-19 13:42:47 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models.Mapping
|
|
|
|
|
|
{
|
2019-04-03 10:39:49 +02:00
|
|
|
|
internal class EntityMapDefinition : IMapDefinition
|
2017-07-19 13:42:47 +02:00
|
|
|
|
{
|
2019-04-03 10:39:49 +02:00
|
|
|
|
public void DefineMaps(UmbracoMapper mapper)
|
2019-03-26 10:39:50 +01:00
|
|
|
|
{
|
|
|
|
|
|
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);
|
2019-04-21 21:00:34 +02:00
|
|
|
|
mapper.Define<IEntitySlim, SearchResultEntity>((source, context) => new SearchResultEntity(), Map);
|
2019-03-26 10:39:50 +01:00
|
|
|
|
mapper.Define<ISearchResult, SearchResultEntity>((source, context) => new SearchResultEntity(), Map);
|
2019-04-08 10:05:21 +02:00
|
|
|
|
mapper.Define<ISearchResults, IEnumerable<SearchResultEntity>>((source, context) => context.MapEnumerable<ISearchResult, SearchResultEntity>(source));
|
|
|
|
|
|
mapper.Define<IEnumerable<ISearchResult>, IEnumerable<SearchResultEntity>>((source, context) => context.MapEnumerable<ISearchResult, SearchResultEntity>(source));
|
2019-03-26 10:39:50 +01:00
|
|
|
|
}
|
2018-01-15 11:32:30 +01:00
|
|
|
|
|
2019-03-26 10:39:50 +01:00
|
|
|
|
// Umbraco.Code.MapAll -Alias
|
|
|
|
|
|
private static void Map(IEntitySlim source, EntityBasic target, MapperContext context)
|
2017-07-19 13:42:47 +02:00
|
|
|
|
{
|
2019-03-26 10:39:50 +01: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.NodeObjectType == Constants.ObjectTypes.Member && target.Icon.IsNullOrWhiteSpace())
|
|
|
|
|
|
target.Icon = "icon-user";
|
|
|
|
|
|
|
2019-04-06 12:24:41 +02:00
|
|
|
|
// 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(var kvp in source.AdditionalData)
|
|
|
|
|
|
{
|
|
|
|
|
|
target.AdditionalData[kvp.Key] = kvp.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-26 10:39:50 +01:00
|
|
|
|
target.AdditionalData.Add("IsContainer", source.IsContainer);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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 = "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Umbraco.Code.MapAll -Udi -Trashed
|
|
|
|
|
|
private static void Map(PropertyGroup source, EntityBasic target, MapperContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
target.Alias = source.Name.ToLowerInvariant();
|
|
|
|
|
|
target.Icon = "icon-tab";
|
|
|
|
|
|
target.Id = source.Id;
|
|
|
|
|
|
target.Key = source.Key;
|
|
|
|
|
|
target.Name = source.Name;
|
|
|
|
|
|
target.ParentId = -1;
|
|
|
|
|
|
target.Path = "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Umbraco.Code.MapAll -Udi -Trashed
|
|
|
|
|
|
private static void Map(IUser source, EntityBasic target, MapperContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
target.Alias = source.Username;
|
|
|
|
|
|
target.Icon = "icon-user";
|
|
|
|
|
|
target.Id = source.Id;
|
|
|
|
|
|
target.Key = source.Key;
|
|
|
|
|
|
target.Name = source.Name;
|
|
|
|
|
|
target.ParentId = -1;
|
|
|
|
|
|
target.Path = "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Umbraco.Code.MapAll -Trashed
|
|
|
|
|
|
private static void Map(ITemplate source, EntityBasic target, MapperContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
target.Alias = source.Alias;
|
|
|
|
|
|
target.Icon = "icon-layout";
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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;
|
2019-04-03 10:39:49 +02:00
|
|
|
|
target.Udi = ContentTypeMapDefinition.MapContentTypeUdi(source);
|
2019-03-26 10:39:50 +01: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())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (source.NodeObjectType == Constants.ObjectTypes.Member)
|
|
|
|
|
|
target.Icon = "icon-user";
|
|
|
|
|
|
else if (source.NodeObjectType == Constants.ObjectTypes.DataType)
|
|
|
|
|
|
target.Icon = "icon-autofill";
|
|
|
|
|
|
else if (source.NodeObjectType == Constants.ObjectTypes.DocumentType)
|
|
|
|
|
|
target.Icon = "icon-item-arrangement";
|
|
|
|
|
|
else if (source.NodeObjectType == Constants.ObjectTypes.MediaType)
|
|
|
|
|
|
target.Icon = "icon-thumbnails";
|
|
|
|
|
|
else if (source.NodeObjectType == Constants.ObjectTypes.TemplateType)
|
|
|
|
|
|
target.Icon = "icon-newspaper-alt";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Umbraco.Code.MapAll -Alias -Trashed
|
|
|
|
|
|
private static void Map(ISearchResult source, SearchResultEntity target, MapperContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
target.Id = source.Id;
|
|
|
|
|
|
target.Score = source.Score;
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Properly map this (not aftermap)
|
|
|
|
|
|
|
|
|
|
|
|
//get the icon if there is one
|
|
|
|
|
|
target.Icon = source.Values.ContainsKey(UmbracoExamineIndex.IconFieldName)
|
|
|
|
|
|
? source.Values[UmbracoExamineIndex.IconFieldName]
|
|
|
|
|
|
: "icon-document";
|
|
|
|
|
|
|
|
|
|
|
|
target.Name = source.Values.ContainsKey("nodeName") ? source.Values["nodeName"] : "[no name]";
|
|
|
|
|
|
|
|
|
|
|
|
if (source.Values.ContainsKey(UmbracoExamineIndex.NodeKeyFieldName))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Guid.TryParse(source.Values[UmbracoExamineIndex.NodeKeyFieldName], out var key))
|
2017-07-19 13:42:47 +02:00
|
|
|
|
{
|
2019-03-26 10:39:50 +01:00
|
|
|
|
target.Key = key;
|
|
|
|
|
|
|
|
|
|
|
|
//need to set the UDI
|
|
|
|
|
|
if (source.Values.ContainsKey(LuceneIndex.CategoryFieldName))
|
2017-07-19 13:42:47 +02:00
|
|
|
|
{
|
2019-03-26 10:39:50 +01:00
|
|
|
|
switch (source.Values[LuceneIndex.CategoryFieldName])
|
|
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-15 15:10:50 +01:00
|
|
|
|
|
2019-03-26 10:39:50 +01:00
|
|
|
|
if (source.Values.ContainsKey("parentID"))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (int.TryParse(source.Values["parentID"], out var parentId))
|
2017-09-12 16:22:16 +02:00
|
|
|
|
{
|
2019-03-26 10:39:50 +01:00
|
|
|
|
target.ParentId = parentId;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
target.ParentId = -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
target.Path = source.Values.ContainsKey(UmbracoExamineIndex.IndexPathFieldName) ? source.Values[UmbracoExamineIndex.IndexPathFieldName] : "";
|
|
|
|
|
|
|
|
|
|
|
|
if (source.Values.ContainsKey(LuceneIndex.ItemTypeFieldName))
|
|
|
|
|
|
{
|
|
|
|
|
|
target.AdditionalData.Add("contentType", source.Values[LuceneIndex.ItemTypeFieldName]);
|
|
|
|
|
|
}
|
2017-07-19 13:42:47 +02:00
|
|
|
|
}
|
2018-09-26 16:27:34 +02:00
|
|
|
|
|
2019-03-26 10:39:50 +01:00
|
|
|
|
private static string MapContentTypeIcon(IEntitySlim entity)
|
|
|
|
|
|
=> entity is ContentEntitySlim contentEntity ? contentEntity.ContentTypeIcon : null;
|
|
|
|
|
|
|
|
|
|
|
|
private static string MapName(IEntitySlim source, MapperContext context)
|
2018-09-26 16:27:34 +02:00
|
|
|
|
{
|
2019-03-26 10:39:50 +01:00
|
|
|
|
if (!(source is DocumentEntitySlim doc))
|
|
|
|
|
|
return source.Name;
|
2018-09-26 16:27:34 +02:00
|
|
|
|
|
2019-03-26 10:39:50 +01:00
|
|
|
|
// invariant = only 1 name
|
|
|
|
|
|
if (!doc.Variations.VariesByCulture()) return source.Name;
|
2018-09-26 16:27:34 +02:00
|
|
|
|
|
2019-03-26 10:39:50 +01:00
|
|
|
|
// variant = depends on culture
|
|
|
|
|
|
var culture = context.GetCulture();
|
2018-09-26 16:27:34 +02:00
|
|
|
|
|
2019-03-26 10:39:50 +01:00
|
|
|
|
// if there's no culture here, the issue is somewhere else (UI, whatever) - throw!
|
|
|
|
|
|
if (culture == null)
|
|
|
|
|
|
//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
|
|
|
|
|
2019-03-26 10:39:50 +01: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})";
|
2018-09-26 16:27:34 +02:00
|
|
|
|
}
|
2017-07-19 13:42:47 +02:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|