Skip to content

Commit

Permalink
Pr/225 - Entity Services fixes (#226)
Browse files Browse the repository at this point in the history
* Fixes to specify the objectType in calls to EntityService.GetAll (#225)

thanks, there is a minor annoyance that the MediaIdMapper inherits the content one, so sometimes the lookup is for media, not content, but I will fix that.

* set baseObjectType for class so MediaMapper can use it too. #225

adds extra baseObjectType value so we can set the type of things we are looking up (because media mapper looks up Media)

* Base type in ContentType Serializer #225

Like the Mapper, the base serializer lookup can be used for ContentTypes, MediaTypes and MemberTypes

* update RjpMapper to do the getall lookup properly (like #225)

* Refactor RJP Mapper to use GetKeyForId / GetIdForKey EntityService calls

* tidy up, use GetIdForKey and GetKeyForId where we can #225

* ID Testing.
  • Loading branch information
KevinJump authored Jul 14, 2019
1 parent a3b7ca5 commit e84b257
Show file tree
Hide file tree
Showing 30 changed files with 505 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<div class="span12">
<h4 class="page-header">Import / Export</h4>
<p>
Perfom import and exports of your stuff - these actions are
Perform import and exports of your stuff - these actions are
processed against the files in <em>{{settings.settings.Folder}}</em>
</p>
<p>
Expand Down
29 changes: 16 additions & 13 deletions Jumoo.uSync.ContentMappers/RJPMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Umbraco.Core.Models;
using Umbraco.Core.Models.EntityBase;
using Umbraco.Core.Services;
using Jumoo.uSync.Core;

namespace Jumoo.uSync.ContentMappers
{
Expand All @@ -31,15 +32,9 @@ public string GetExportValue(int dataTypeDefinitionId, string value)
{
if (link.id != null)
{
var objectType = _entityService.GetObjectType((int)link.id);
if (objectType != UmbracoObjectTypes.Unknown)
{
var keys = _entityService.GetAll(objectType, (int)link.id);
if (keys != null && keys.Any() && keys.FirstOrDefault() != null)
{
link.id = keys.FirstOrDefault().Key;
}
}
var attempt = _entityService.uSyncGetKeyForId((int)link.Id);
if (attempt.Success)
link.id = attempt.Result;
}
}
}
Expand All @@ -59,18 +54,26 @@ public string GetImportValue(int dataTypeDefinitionId, string content)
Guid key;
if (Guid.TryParse(link.id.ToString(), out key))
{
var ids = _entityService.GetAll(key);
if (ids != null && ids.Any() && ids.FirstOrDefault() != null)
{
link.id = ids.FirstOrDefault().Id;
var attempt = GetItemIdFromGuid(key);
if (attempt.Success) {
link.id = attempt.Result;
}
}
}
}
}

return JsonConvert.SerializeObject(links, Formatting.Indented);
}

private Attempt<int> GetItemIdFromGuid(Guid key)
{
var attempt = _entityService.GetIdForKey(key, UmbracoObjectTypes.Document);
if (attempt.Success == false)
attempt = _entityService.GetIdForKey(key, UmbracoObjectTypes.Media);


return attempt;
}
}
}
54 changes: 54 additions & 0 deletions Jumoo.uSync.Core/Extensions/EntityServicePatchExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.EntityBase;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Services;

namespace Jumoo.uSync.Core
{
/// <summary>
/// Series of extensions methods to work around the change in interface for EntityService
/// in Umbraco 7.15.0 - we can't safely call Get(int id) or Get(Guid key) because things
/// changed. but other alternatives in the service need to know types before they will
/// do anything.
/// </summary>
public static class EntityServicePatchExtensions
{
/// <summary>
/// gets you the Key (Guid) for an entity if all you know is the Id (int)
/// </summary>
public static Attempt<Guid> uSyncGetKeyForId(this IEntityService entityService, int id)
{
try
{
if (entityService.Exists(id))
{
var type = entityService.GetObjectType(id);
if (type != UmbracoObjectTypes.Unknown)
{
return entityService.GetKeyForId(id, type);
}
}
}
catch (Exception ex)
{
// it shouldn't but we might fire a ObjectNotSet exception if the type is missing
// (but we do a check, so that is very unlikely)
return Attempt.Fail(Guid.Empty, ex);
}


return Attempt.Fail(Guid.Empty);
}


}
}
17 changes: 10 additions & 7 deletions Jumoo.uSync.Core/Helpers/uSyncTreeWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,25 @@ public uSyncTreeWalker(UmbracoObjectTypes type)

public string GetPathFromId(int id, UmbracoObjectTypes type)
{
var items = _entityService.GetAll(type, id);
if (items != null && items.Any())
if (_entityService.Exists(id))
{
var item = items.FirstOrDefault();
if (item != null && !item.Trashed)
var items = _entityService.GetAll(type, id);
if (items != null && items.Any())
{
return GetPath(item);
var item = items.FirstOrDefault();
if (item != null && !item.Trashed)
{
return GetPath(item);
}
}
}

return string.Empty;
}

public string GetPathFromKey(Guid key)
public string GetPathFromKey(Guid key, UmbracoObjectTypes objectType)
{
var items = _entityService.GetAll(key);
var items = _entityService.GetAll(objectType, new[] { key });
if (items != null && items.Any())
{
var item = items.FirstOrDefault();
Expand Down
1 change: 1 addition & 0 deletions Jumoo.uSync.Core/Jumoo.uSync.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@
<ItemGroup>
<Compile Include="Constants.cs" />
<Compile Include="Events.cs" />
<Compile Include="Extensions\EntityServicePatchExtensions.cs" />
<Compile Include="Extensions\XElementTrackerExtension.cs" />
<Compile Include="Extensions\XElementuSyncTypeExtension.cs" />
<Compile Include="Extensions\XElementValueExtensions.cs" />
Expand Down
28 changes: 15 additions & 13 deletions Jumoo.uSync.Core/Mappers/ContentIdMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ namespace Jumoo.uSync.Core.Mappers
{
class ContentIdMapper : IContentMapper
{
private string _exportRegex;
private string _exportRegex;
private UmbracoObjectTypes baseObjectType;

public ContentIdMapper(string regex)
: this(regex, UmbracoObjectTypes.Document) { }

public ContentIdMapper(string regex, UmbracoObjectTypes objectType)
{
if (!regex.IsNullOrWhiteSpace())
_exportRegex = regex;
else
_exportRegex = @"\d{4,9}";

this.baseObjectType = objectType;
}

public virtual string GetExportValue(int dataTypeDefinitionId, string value)
Expand Down Expand Up @@ -81,24 +88,19 @@ public virtual string GetImportValue(int dataTypeDefinitionId, string content)

internal int GetIdFromGuid(Guid guid)
{
var items = ApplicationContext.Current.Services.EntityService.GetAll(guid);
if (items != null && items.Any() && items.FirstOrDefault() != null)
return items.FirstOrDefault().Id;
var attempt = ApplicationContext.Current.Services.EntityService.GetIdForKey(guid, this.baseObjectType);
if (attempt.Success)
return attempt.Result;

return -1;
}

internal Guid? GetGuidFromId(int id)
{
var objectType = ApplicationContext.Current.Services.EntityService.GetObjectType(id);

if (objectType != UmbracoObjectTypes.Unknown)
{
var items = ApplicationContext.Current.Services.EntityService.GetAll(objectType, id);
if (items != null && items.Any() && items.FirstOrDefault() != null)
return items.FirstOrDefault().Key;
}

var attempt = ApplicationContext.Current.Services.EntityService.uSyncGetKeyForId(id);
if (attempt.Success)
return attempt.Result;

return null;
}

Expand Down
3 changes: 2 additions & 1 deletion Jumoo.uSync.Core/Mappers/MediaIdMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;

namespace Jumoo.uSync.Core.Mappers
{
class MediaIdMapper : ContentIdMapper
{
public MediaIdMapper(string regex)
: base(regex) { }
: base(regex, UmbracoObjectTypes.Media) { }

public override string GetExportValue(int dataTypeDefinitionId, string value)
{
Expand Down
19 changes: 15 additions & 4 deletions Jumoo.uSync.Core/Serializers/ContentTypeBaseSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,24 @@ abstract public class ContentTypeBaseSerializer<T> : SyncBaseSerializer<T>, ISyn
private List<IContentType> _allContentTypes;
private List<IMediaType> _allMediaTypes;

private UmbracoObjectTypes baseObjectType;

public ContentTypeBaseSerializer(string itemType): base(itemType)
/*
[Obsolete("You should pass Object Type to base class so lookups work")]
public ContentTypeBaseSerializer(string itemType) :
this(itemType, UmbracoObjectTypes.DocumentType)
{
}
*/

public ContentTypeBaseSerializer(string itemType, UmbracoObjectTypes objectType): base(itemType)
{
_contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
_dataTypeService = ApplicationContext.Current.Services.DataTypeService;
_memberTypeService = ApplicationContext.Current.Services.MemberTypeService;
_entityService = ApplicationContext.Current.Services.EntityService;

baseObjectType = objectType;
}

#region ContentTypeBase Deserialize Helpers
Expand Down Expand Up @@ -80,9 +91,9 @@ internal void DeserializeBase(IContentTypeBase item, XElement info)
var masterKey = masterNode.Attribute("Key").ValueOrDefault(Guid.Empty);
if (masterKey != Guid.Empty)
{
var masterEntities = ApplicationContext.Current.Services.EntityService.GetAll(masterKey);
if (masterEntities != null && masterEntities.Any() && masterEntities.FirstOrDefault() != null)
masterId = masterEntities.FirstOrDefault().Id;
var attempt = ApplicationContext.Current.Services.EntityService.GetIdForKey(masterKey, baseObjectType);
if (attempt.Success)
masterId = attempt.Result;
}

if (masterId == 0)
Expand Down
4 changes: 2 additions & 2 deletions Jumoo.uSync.Core/Serializers/ContentTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public override string SerializerType
}

public ContentTypeSerializer() :
base(Constants.Packaging.DocumentTypeNodeName)
base(Constants.Packaging.DocumentTypeNodeName, UmbracoObjectTypes.DocumentType)
{ }

public ContentTypeSerializer(string itemType) : base(itemType)
public ContentTypeSerializer(string itemType) : base(itemType, UmbracoObjectTypes.DocumentType)
{
}

Expand Down
4 changes: 2 additions & 2 deletions Jumoo.uSync.Core/Serializers/MediaTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class MediaTypeSerializer : ContentTypeBaseSerializer<IMediaType>, ISyncC
public override string SerializerType { get { return uSyncConstants.Serailization.MediaType; } }

public MediaTypeSerializer()
: base("MediaType") { }
: base("MediaType", UmbracoObjectTypes.MediaType) { }

public MediaTypeSerializer(string itemType)
: base (itemType) { }
: base (itemType, UmbracoObjectTypes.MediaType) { }

internal override SyncAttempt<IMediaType> DeserializeCore(XElement node)
{
Expand Down
4 changes: 2 additions & 2 deletions Jumoo.uSync.Core/Serializers/MemberTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class MemberTypeSerializer : ContentTypeBaseSerializer<IMemberType>, ISyn
public override string SerializerType { get { return uSyncConstants.Serailization.MemberType; } }

public MemberTypeSerializer() :
base("MemberType") { }
base("MemberType", UmbracoObjectTypes.MemberType) { }

public MemberTypeSerializer(string type)
: base(type) { }
: base(type, UmbracoObjectTypes.MemberType) { }

internal override SyncAttempt<IMemberType> DeserializeCore(XElement node)
{
Expand Down
2 changes: 1 addition & 1 deletion Jumoo.uSync.Site/App_Plugins/uSync/uSyncDashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<div class="span12">
<h4 class="page-header">Import / Export</h4>
<p>
Perfom import and exports of your stuff - these actions are
Perform import and exports of your stuff - these actions are
processed against the files in <em>{{settings.settings.Folder}}</em>
</p>
<p>
Expand Down
5 changes: 5 additions & 0 deletions Jumoo.uSync.Site/Views/Child.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Child>
@using ContentModels = Umbraco.Web.PublishedContentModels;
@{
Layout = null;
}
2 changes: 1 addition & 1 deletion Jumoo.uSync.Site/config/uSyncBackOffice.Config
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Folder>~/uSync/data/</Folder>

<!-- run import at startup -->
<Import>true</Import>
<Import>false</Import>

<!-- export everything to disk at startup -->
<ExportAtStartup>false</ExportAtStartup>
Expand Down
Loading

0 comments on commit e84b257

Please sign in to comment.