From 703fd7dc037d42a5fa5345726d7e2dba67ceba68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Krch?= Date: Fri, 8 Dec 2023 18:53:55 +0100 Subject: [PATCH] cleanup, build warnings fix --- .../AssetSamples.cs | 3 +- .../InfoAdapter/AdapterFactory.cs | 2 +- .../InfoAdapter/GenericAdapter.cs | 16 +++--- .../Model/ContentItemReferenceModel.cs | 3 +- .../Model/DataClassModel.cs | 2 - .../Model/MediaFileModel.cs | 3 +- .../Model/WebSiteChannelModel.cs | 2 +- .../ProviderProxy/IProviderProxy.cs | 16 +++--- .../ProviderProxy/ProviderProxyFactory.cs | 8 +-- .../Services/IImporter.cs | 5 +- .../Services/ImportService.cs | 2 +- .../Services/Importer.cs | 49 +++++++------------ .../Services/Validation/CheckEnumerable.cs | 1 - .../Services/Validation/ValidationService.cs | 1 - 14 files changed, 45 insertions(+), 68 deletions(-) diff --git a/Examples/Kentico.Xperience.UMT.Examples/AssetSamples.cs b/Examples/Kentico.Xperience.UMT.Examples/AssetSamples.cs index 334fe90..ab56c11 100644 --- a/Examples/Kentico.Xperience.UMT.Examples/AssetSamples.cs +++ b/Examples/Kentico.Xperience.UMT.Examples/AssetSamples.cs @@ -4,12 +4,11 @@ namespace Kentico.Xperience.UMT.Examples { public static class AssetSamples { - public static Guid MEDIA_LIBRARY_SAMPLE_GUID = new Guid("E3A9C50C-2B76-4BA8-AC19-2F0AA64C47D5"); + public static readonly Guid MEDIA_LIBRARY_SAMPLE_GUID = new("E3A9C50C-2B76-4BA8-AC19-2F0AA64C47D5"); [Sample("mediafile.sample", "", "Sample of media file")] public static MediaFileModel SampleMediaFile => new() { - // TODO tomas.krch: 2023-11-26 CHANGE SAMPLE => PATH SHALL BE RELATIVE TO SAMPLE PROJECT DataSourcePath = ".\\sample.png", DataSourceBase64 = "", FileGUID = new Guid("214E29AA-32D5-40D7-9FEA-896591439E74"), diff --git a/src/Kentico.Xperience.UMT/InfoAdapter/AdapterFactory.cs b/src/Kentico.Xperience.UMT/InfoAdapter/AdapterFactory.cs index c328074..2c981d4 100644 --- a/src/Kentico.Xperience.UMT/InfoAdapter/AdapterFactory.cs +++ b/src/Kentico.Xperience.UMT/InfoAdapter/AdapterFactory.cs @@ -26,7 +26,7 @@ public AdapterFactory(ILoggerFactory loggerFactory, UmtModelService modelService this.providerProxyFactory = providerProxyFactory; } - internal IInfoAdapter? CreateAdapter(IUmtModel umtModel, ProviderProxyContext providerProxyContext) => + internal IInfoAdapter? CreateAdapter(IUmtModel umtModel, IProviderProxyContext providerProxyContext) => umtModel switch { UserInfoModel => new GenericInfoAdapter(loggerFactory.CreateLogger>(), modelService, providerProxyFactory.CreateProviderProxy(providerProxyContext), providerProxyFactory), diff --git a/src/Kentico.Xperience.UMT/InfoAdapter/GenericAdapter.cs b/src/Kentico.Xperience.UMT/InfoAdapter/GenericAdapter.cs index 76976e0..c1ce4da 100644 --- a/src/Kentico.Xperience.UMT/InfoAdapter/GenericAdapter.cs +++ b/src/Kentico.Xperience.UMT/InfoAdapter/GenericAdapter.cs @@ -2,8 +2,6 @@ using System.Reflection; using System.Runtime.CompilerServices; using System.Text.Json; -using CMS.ContentEngine; -using CMS.ContentEngine.Internal; using CMS.DataEngine; using Kentico.Xperience.UMT.Attributes; using Kentico.Xperience.UMT.Model; @@ -108,7 +106,7 @@ public virtual TTargetInfo Adapt(IUmtModel input) if (!modelService.TryGetModelInfo(input.GetType(), out var model) || model == null) { Logger.LogError("Model info for type {Type} not found => unsupported model", input.GetType().FullName); - throw new InvalidOperationException($"Model info for type {input?.GetType().FullName} not found => unsupported model"); + throw new InvalidOperationException($"Model info for type {input.GetType().FullName} not found => unsupported model"); } TTargetInfo? current; @@ -191,9 +189,9 @@ public virtual TTargetInfo Adapt(IUmtModel input) // map all foreign references to ensure they exist foreach (var referenceProperty in model.ReferenceProperties) { - Logger.LogDebug("Mapping reference property '{RefProp}' from '{RefType}' ObjectId", referenceProperty.ReferencedPropertyName, referenceProperty.ReferencedInfoType?.Name); + Logger.LogDebug("Mapping reference property '{RefProp}' from '{RefType}' ObjectId", referenceProperty.ReferencedPropertyName, referenceProperty.ReferencedInfoType.Name); - object? refObject = referenceProperty.Property?.GetValue(input); + object? refObject = referenceProperty.Property.GetValue(input); if (refObject is Guid foreignObjectGuid) { var providerProxy = providerProxyFactory.CreateProviderProxy(referenceProperty.ReferencedInfoType, ProviderProxy.Context); @@ -215,8 +213,8 @@ public virtual TTargetInfo Adapt(IUmtModel input) } else if (referenceProperty.IsRequired) { - Logger.LogError("Missing required dependency - '{PropName}' is not valid ObjectGUID", referenceProperty.Property?.Name); - throw new InvalidOperationException($"Missing required dependency - '{referenceProperty.Property?.Name}' is not valid ObjectGUID"); + Logger.LogError("Missing required dependency - '{PropName}' is not valid ObjectGUID", referenceProperty.Property.Name); + throw new InvalidOperationException($"Missing required dependency - '{referenceProperty.Property.Name}' is not valid ObjectGUID"); } } @@ -235,7 +233,7 @@ public virtual TTargetInfo Adapt(IUmtModel input) object? value = input.CustomProperties[customProperty]; if (value is JsonElement jsonElement) { - // TODO tomas.krch: 2023-06-27 convert to correct column type (don't rely on internal handling) from perspective of UMT this is unpredictability + value = jsonElement.ToString(); } @@ -261,7 +259,7 @@ public virtual TTargetInfo Adapt(IUmtModel input) } if (!modelService.TryGetModelInfo(input.GetType(), out var model)) { - Logger.LogError("Model info for type {Type} not found => unsupported model", input?.GetType()?.FullName); + Logger.LogError("Model info for type {Type} not found => unsupported model", input.GetType().FullName); return null; } diff --git a/src/Kentico.Xperience.UMT/Model/ContentItemReferenceModel.cs b/src/Kentico.Xperience.UMT/Model/ContentItemReferenceModel.cs index fcfe04f..68edf8a 100644 --- a/src/Kentico.Xperience.UMT/Model/ContentItemReferenceModel.cs +++ b/src/Kentico.Xperience.UMT/Model/ContentItemReferenceModel.cs @@ -1,6 +1,8 @@ using System.ComponentModel.DataAnnotations; using CMS.ContentEngine.Internal; using Kentico.Xperience.UMT.Attributes; +// ReSharper disable InconsistentNaming +// ReSharper disable UnusedMember.Global // used implicitly namespace Kentico.Xperience.UMT.Model; @@ -18,7 +20,6 @@ public class ContentItemReferenceModel : UmtModel [ReferenceProperty(typeof(ContentItemCommonDataInfo), "ContentItemReferenceSourceCommonDataID", IsRequired = true)] public Guid? ContentItemReferenceSourceCommonDataGuid { get; set; } - //TODO check if target item is ContentItem [Required] [ReferenceProperty(typeof(ContentItemInfo), "ContentItemReferenceTargetItemID", IsRequired = true)] public Guid? ContentItemReferenceTargetItemGuid { get; set; } diff --git a/src/Kentico.Xperience.UMT/Model/DataClassModel.cs b/src/Kentico.Xperience.UMT/Model/DataClassModel.cs index 6194d71..06dc4e1 100644 --- a/src/Kentico.Xperience.UMT/Model/DataClassModel.cs +++ b/src/Kentico.Xperience.UMT/Model/DataClassModel.cs @@ -1,6 +1,5 @@ using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; -using CMS.DataEngine; using CMS.Modules; using Kentico.Xperience.UMT.Attributes; using Kentico.Xperience.UMT.Services.Validation; @@ -177,6 +176,5 @@ public class FormFieldSettings /// Admin UI Component used for field data editing /// /// (for pages use enumeration here) - // TODO tomas.krch: 2023-07-23 validation needs to be done at runtime, target instance could contain custom form components public string? ControlName { get; set; } } diff --git a/src/Kentico.Xperience.UMT/Model/MediaFileModel.cs b/src/Kentico.Xperience.UMT/Model/MediaFileModel.cs index dc18734..9cda52f 100644 --- a/src/Kentico.Xperience.UMT/Model/MediaFileModel.cs +++ b/src/Kentico.Xperience.UMT/Model/MediaFileModel.cs @@ -2,6 +2,7 @@ using CMS.MediaLibrary; using CMS.Membership; using Kentico.Xperience.UMT.Attributes; +// ReSharper disable InconsistentNaming namespace Kentico.Xperience.UMT.Model; @@ -15,8 +16,6 @@ public class MediaFileModel : UmtModel public const string DISCRIMINATOR = "Media_File"; public string? DataSourcePath { get; set; } - public string? DataSourceBase64 { get; set; } // TODO tomas.krch: 2023-12-06 implement - public string? DataSourceUrl { get; set; } // TODO tomas.krch: 2023-12-06 impl [Map] [Required] diff --git a/src/Kentico.Xperience.UMT/Model/WebSiteChannelModel.cs b/src/Kentico.Xperience.UMT/Model/WebSiteChannelModel.cs index 197df7c..07a3bea 100644 --- a/src/Kentico.Xperience.UMT/Model/WebSiteChannelModel.cs +++ b/src/Kentico.Xperience.UMT/Model/WebSiteChannelModel.cs @@ -1,6 +1,7 @@ using System.ComponentModel.DataAnnotations; using CMS.ContentEngine; using Kentico.Xperience.UMT.Attributes; +// ReSharper disable InconsistentNaming namespace Kentico.Xperience.UMT.Model; @@ -34,7 +35,6 @@ public class WebsiteChannelModel : UmtModel [ReferenceProperty(typeof(ContentLanguageInfo), "WebsiteChannelPrimaryContentLanguageID", IsRequired = true)] public Guid? WebsiteChannelPrimaryContentLanguageGuid { get; set; } - // TODO tomas.krch: 2023-11-02 CookieLevelConstants.ALL [Map] [Required] public int? WebsiteChannelDefaultCookieLevel { get; set; } diff --git a/src/Kentico.Xperience.UMT/ProviderProxy/IProviderProxy.cs b/src/Kentico.Xperience.UMT/ProviderProxy/IProviderProxy.cs index 023b628..66589f4 100644 --- a/src/Kentico.Xperience.UMT/ProviderProxy/IProviderProxy.cs +++ b/src/Kentico.Xperience.UMT/ProviderProxy/IProviderProxy.cs @@ -17,15 +17,15 @@ public interface IProviderProxy BaseInfo Save(BaseInfo info, IUmtModel model); - ProviderProxyContext Context { get; } + IProviderProxyContext Context { get; } } -public record ProviderProxyContext(); +public interface IProviderProxyContext; -internal class ContentItemDataProxy : IProviderProxy -{ - public ContentItemDataProxy(ProviderProxyContext context) => Context = context; +public class ProviderProxyContext: IProviderProxyContext; +internal class ContentItemDataProxy(IProviderProxyContext context) : IProviderProxy +{ private IInfoProvider GetProviderOrThrow(IUmtModel model) { if (model is ContentItemDataModel contentItemDataModel) @@ -77,16 +77,16 @@ public BaseInfo Save(BaseInfo info, IUmtModel model) } } - public ProviderProxyContext Context { get; } + public IProviderProxyContext Context { get; } = context; } internal class ProviderProxy : IProviderProxy where TInfo : AbstractInfoBase, new() { - public ProviderProxyContext Context { get; } + public IProviderProxyContext Context { get; } protected readonly IInfoProvider ProviderInstance; - public ProviderProxy(ProviderProxyContext context) + public ProviderProxy(IProviderProxyContext context) { Context = context; if (typeof(TInfo).IsAssignableTo(typeof(DataClassInfo))) diff --git a/src/Kentico.Xperience.UMT/ProviderProxy/ProviderProxyFactory.cs b/src/Kentico.Xperience.UMT/ProviderProxy/ProviderProxyFactory.cs index 0cc8a88..fcfa845 100644 --- a/src/Kentico.Xperience.UMT/ProviderProxy/ProviderProxyFactory.cs +++ b/src/Kentico.Xperience.UMT/ProviderProxy/ProviderProxyFactory.cs @@ -5,15 +5,15 @@ namespace Kentico.Xperience.UMT.ProviderProxy; internal interface IProviderProxyFactory { - IProviderProxy CreateProviderProxy(ProviderProxyContext context) where TInfo : BaseInfo; - IProviderProxy CreateProviderProxy(Type? infoType, ProviderProxyContext context); + IProviderProxy CreateProviderProxy(IProviderProxyContext context) where TInfo : BaseInfo; + IProviderProxy CreateProviderProxy(Type? infoType, IProviderProxyContext context); } internal class ProviderProxyFactory : IProviderProxyFactory { - public IProviderProxy CreateProviderProxy(ProviderProxyContext context) where TInfo : BaseInfo => CreateProviderProxy(typeof(TInfo), context); + public IProviderProxy CreateProviderProxy(IProviderProxyContext context) where TInfo : BaseInfo => CreateProviderProxy(typeof(TInfo), context); - public IProviderProxy CreateProviderProxy(Type? infoType, ProviderProxyContext context) + public IProviderProxy CreateProviderProxy(Type? infoType, IProviderProxyContext context) { ArgumentNullException.ThrowIfNull(infoType, nameof(infoType)); diff --git a/src/Kentico.Xperience.UMT/Services/IImporter.cs b/src/Kentico.Xperience.UMT/Services/IImporter.cs index 53a6a4c..71bac50 100644 --- a/src/Kentico.Xperience.UMT/Services/IImporter.cs +++ b/src/Kentico.Xperience.UMT/Services/IImporter.cs @@ -1,5 +1,4 @@ using System.ComponentModel.DataAnnotations; -using System.Xml.Serialization; using CMS.DataEngine; using Kentico.Xperience.UMT.Model; @@ -11,10 +10,10 @@ public interface IImportResult int PrimaryKey { get; } BaseInfo? Imported { get; } Exception? Exception { get; set; } - List? ModelValidationResults { get; set; } // TODO tomas.krch: 2023-11-16 convert to interface + List? ModelValidationResults { get; set; } } -public interface IImporter : IDisposable +public interface IImporter { Task ImportAsync(IUmtModel model); } diff --git a/src/Kentico.Xperience.UMT/Services/ImportService.cs b/src/Kentico.Xperience.UMT/Services/ImportService.cs index 49ddfd2..490215f 100644 --- a/src/Kentico.Xperience.UMT/Services/ImportService.cs +++ b/src/Kentico.Xperience.UMT/Services/ImportService.cs @@ -159,7 +159,7 @@ public Task StartImportAsync(IAsyncEnumerable im return Task.FromResult(observer); } - private void ImportObject(IUmtModel model, ImportStateObserver observer, ProviderProxyContext providerProxyContext) + private void ImportObject(IUmtModel model, ImportStateObserver observer, IProviderProxyContext providerProxyContext) { var adapter = adapterFactory.CreateAdapter(model, providerProxyContext); if (adapter == null) diff --git a/src/Kentico.Xperience.UMT/Services/Importer.cs b/src/Kentico.Xperience.UMT/Services/Importer.cs index 911fa52..a03b7f7 100644 --- a/src/Kentico.Xperience.UMT/Services/Importer.cs +++ b/src/Kentico.Xperience.UMT/Services/Importer.cs @@ -1,5 +1,4 @@ using System.ComponentModel.DataAnnotations; -using System.Xml.Serialization; using CMS.DataEngine; using Kentico.Xperience.UMT.InfoAdapter; using Kentico.Xperience.UMT.Model; @@ -18,31 +17,22 @@ public class ImportResult : IImportResult public List? ModelValidationResults { get; set; } } -internal class Importer : IImporter +internal class Importer(ILogger logger, AdapterFactory factory) : IImporter { - private readonly ILogger logger; - private readonly AdapterFactory adapterFactory; - - public Importer(ILogger logger, AdapterFactory adapterFactory) - { - this.logger = logger; - this.adapterFactory = adapterFactory; - } - - public async Task ImportAsync(IUmtModel umtModel) + public async Task ImportAsync(IUmtModel model) { var providerProxyContext = new ProviderProxyContext(); try { - var importResult = await ImportObject(umtModel, providerProxyContext); + var importResult = await ImportObject(model, providerProxyContext); return importResult; } catch (Exception ex) { logger.LogError(ex, "Error occured"); - return new ImportResult() + return new ImportResult { Success = false, Exception = ex @@ -50,34 +40,29 @@ public async Task ImportAsync(IUmtModel umtModel) } } - public void Dispose() - { - - } - - private async Task ImportObject(IUmtModel model, ProviderProxyContext providerProxyContext) + private Task ImportObject(IUmtModel model, IProviderProxyContext providerProxyContext) { - var adapter = adapterFactory.CreateAdapter(model, providerProxyContext); + var adapter = factory.CreateAdapter(model, providerProxyContext); if (adapter == null) { logger.LogError("Unable to find import object adapter for type '{Type}'", model.GetType()); - return new ImportResult() + return Task.FromResult(new ImportResult { Success = false, Exception = new InvalidOperationException($"Unable to find import object adapter for type '{model.GetType()}'") - }; + }); } var modelValidationResults = new List(); if (!ValidationService.Instance.TryValidateModel(model, ref modelValidationResults)) { - return new ImportResult() + return Task.FromResult(new ImportResult { Success = false, ModelValidationResults = modelValidationResults - }; + }); } BaseInfo? adapted = null; @@ -88,32 +73,32 @@ private async Task ImportObject(IUmtModel model, ProviderProxyCont catch(Exception ex) { logger.LogError(ex, "Model adaptation to InfoObject failed"); - return new ImportResult() + return Task.FromResult(new ImportResult { Exception = ex, Success = false, Imported = adapted - }; + }); } try { adapter.ProviderProxy.Save(adapted, model); - return new ImportResult() + return Task.FromResult(new ImportResult { Success = true, Imported = adapted, PrimaryKey = adapted.GetIntegerValue(adapted.TypeInfo.IDColumn, 0) - }; + }); } catch (Exception ex) { - logger.LogError(ex, "Entity persistance failed"); - return new ImportResult() + logger.LogError(ex, "Entity persistence failed"); + return Task.FromResult(new ImportResult { Exception = ex, Success = false - }; + }); } } } diff --git a/src/Kentico.Xperience.UMT/Services/Validation/CheckEnumerable.cs b/src/Kentico.Xperience.UMT/Services/Validation/CheckEnumerable.cs index 7055c73..0d811ee 100644 --- a/src/Kentico.Xperience.UMT/Services/Validation/CheckEnumerable.cs +++ b/src/Kentico.Xperience.UMT/Services/Validation/CheckEnumerable.cs @@ -5,7 +5,6 @@ namespace Kentico.Xperience.UMT.Services.Validation; -// TODO tomas.krch: 2023-07-23 replace this and propose whole object graph validation solution public class CheckEnumerableAttribute: ValidationAttribute { private readonly ConcurrentDictionary> validationResults = new(); diff --git a/src/Kentico.Xperience.UMT/Services/Validation/ValidationService.cs b/src/Kentico.Xperience.UMT/Services/Validation/ValidationService.cs index a4a1308..7bc3dc1 100644 --- a/src/Kentico.Xperience.UMT/Services/Validation/ValidationService.cs +++ b/src/Kentico.Xperience.UMT/Services/Validation/ValidationService.cs @@ -19,7 +19,6 @@ public class ValidationService : IValidationService /// true if valid, false if invalid public bool TryValidateModel(IUmtModel model, ref List result) { - // TODO tomas.krch: 2023-07-22 validation context factory to enable DI? var validationContext = new ValidationContext(model); return Validator.TryValidateObject(model, validationContext, result, true); }