generated from Kentico/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #243 from Kentico/feat/migrate_to_assets
Migration of media file and attachments to content items
- Loading branch information
Showing
84 changed files
with
2,584 additions
and
3,754 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
KVA/Migration.Toolkit.Source/Auxiliary/EntityIdentityImpl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System.Collections.Frozen; | ||
using CMS.DataEngine; | ||
using Microsoft.Extensions.Logging; | ||
using Migration.Toolkit.Common.Helpers; | ||
|
||
namespace Migration.Toolkit.Source.Auxiliary; | ||
|
||
public interface ISourceGuidContext | ||
{ | ||
int[]? GetSiteIds<T>(Guid entityGuid) where T : ISourceGuidEntity; | ||
} | ||
|
||
public class SourceGuidContext(ModelFacade modelFacade, ILogger<ISourceGuidContext> logger) : ISourceGuidContext | ||
{ | ||
private static class Cache<T> | ||
{ | ||
// ReSharper disable once StaticMemberInGenericType | ||
public static FrozenDictionary<Guid, int[]>? SourceGuidSites; | ||
} | ||
|
||
public int[]? GetSiteIds<T>(Guid entityGuid) where T : ISourceGuidEntity | ||
{ | ||
var cache = Cache<T>.SourceGuidSites ??= T.Load(modelFacade); | ||
int[]? result = cache.GetValueOrDefault(entityGuid); | ||
switch (result) | ||
{ | ||
case { Length: 0 }: | ||
{ | ||
logger.LogDebug("Guid {Guid} has no site at all", entityGuid); | ||
return []; | ||
} | ||
case { Length: 1 }: | ||
{ | ||
return result; | ||
} | ||
case null: | ||
{ | ||
logger.LogDebug("Guid {Guid} is not found at all", entityGuid); | ||
return null; | ||
} | ||
default: | ||
{ | ||
logger.LogDebug("Guid {Guid} is present on multiple sites {Sites}", entityGuid, string.Join("|", result)); | ||
return result; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public record EntityIdentityGuidResult(bool IsFixed, Guid Identity); | ||
public class EntityIdentityFacade(ISourceGuidContext sourceGuidContext) | ||
{ | ||
public EntityIdentityGuidResult Translate<T>(Guid guid, int? siteId) where T : ISourceGuidEntity => | ||
sourceGuidContext.GetSiteIds<T>(guid) switch | ||
{ | ||
// not found => leave it as it is or throw | ||
{ Length: 0 } => new EntityIdentityGuidResult(false, guid), | ||
// OK! lets leave guid unchanged | ||
{ Length: 1 } => new EntityIdentityGuidResult(false, guid), | ||
// BAD, guid is not unique, we cannot use it in XbyK | ||
_ => new EntityIdentityGuidResult(true, GuidV5.NewNameBased(T.NewGuidNs, $"{guid}|{siteId ?? -1}")) | ||
}; | ||
|
||
public EntityIdentityGuidResult Translate<T>(T s) where T : ISourceGuidEntity => | ||
s.GetIdentity() is var (guid, siteId) | ||
? Translate<T>(guid, siteId) | ||
: throw new InvalidOperationException($"Unable to determine source entity identity: {s}"); | ||
} | ||
|
||
public class IdentityLocator(EntityIdentityFacade entityIdentityFacade, ILogger<IdentityLocator> logger) | ||
{ | ||
public (TTarget target, bool guidIsFixed) LocateTarget<TTarget, TSource>(Guid entityGuid, int? siteId) | ||
where TTarget : AbstractInfoBase<TTarget>, IInfoWithGuid, new() | ||
where TSource : ISourceGuidEntity | ||
{ | ||
(bool isFixed, var safeAttachmentGuid) = entityIdentityFacade.Translate<TSource>(entityGuid, siteId); | ||
var target = Provider<TTarget>.Instance.Get(safeAttachmentGuid); | ||
|
||
logger.LogTrace("Located {Target} from source {Source}", | ||
new { ID = target.Generalized.ObjectID, CodeName = target.Generalized.ObjectCodeName, type = target.TypeInfo.ObjectClassName }, | ||
new { type = typeof(TSource).Name, entityGuid, siteId } | ||
); | ||
|
||
return (target, isFixed); | ||
} | ||
|
||
public (TTarget target, bool guidIsFixed) LocateTarget<TTarget, TSource>(TSource s) | ||
where TTarget : AbstractInfoBase<TTarget>, IInfoWithGuid, new() | ||
where TSource : ISourceGuidEntity | ||
{ | ||
// TODO tomas.krch: 2024-07-23 specialized approach for CMS_USER and CMS_MEMBER | ||
|
||
(var entityGuid, int? siteId) = s.GetIdentity(); | ||
return LocateTarget<TTarget, TSource>(entityGuid, siteId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
namespace Migration.Toolkit.Source; | ||
|
||
public class MappingFailureException : InvalidOperationException | ||
public class MappingFailureException(string keyName, string reason) : InvalidOperationException($"Key '{keyName}' mapping failed: {reason}") | ||
{ | ||
public MappingFailureException(string keyName, string reason) : base($"Key '{keyName}' mapping failed: {reason}") | ||
{ | ||
KeyName = keyName; | ||
Reason = reason; | ||
} | ||
|
||
public string KeyName { get; } | ||
public string Reason { get; } | ||
public string KeyName { get; } = keyName; | ||
public string Reason { get; } = reason; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.