Skip to content

Commit

Permalink
cleanup, build warnings fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrch committed Dec 8, 2023
1 parent 41a0ecc commit 703fd7d
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 68 deletions.
3 changes: 1 addition & 2 deletions Examples/Kentico.Xperience.UMT.Examples/AssetSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "",

Check failure on line 13 in Examples/Kentico.Xperience.UMT.Examples/AssetSamples.cs

View workflow job for this annotation

GitHub Actions / Build and Test

'MediaFileModel' does not contain a definition for 'DataSourceBase64'

Check failure on line 13 in Examples/Kentico.Xperience.UMT.Examples/AssetSamples.cs

View workflow job for this annotation

GitHub Actions / Build and Test

'MediaFileModel' does not contain a definition for 'DataSourceBase64'
FileGUID = new Guid("214E29AA-32D5-40D7-9FEA-896591439E74"),
Expand Down
2 changes: 1 addition & 1 deletion src/Kentico.Xperience.UMT/InfoAdapter/AdapterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public AdapterFactory(ILoggerFactory loggerFactory, UmtModelService modelService
this.providerProxyFactory = providerProxyFactory;
}

internal IInfoAdapter<IUmtModel>? CreateAdapter(IUmtModel umtModel, ProviderProxyContext providerProxyContext) =>
internal IInfoAdapter<IUmtModel>? CreateAdapter(IUmtModel umtModel, IProviderProxyContext providerProxyContext) =>
umtModel switch
{
UserInfoModel => new GenericInfoAdapter<UserInfo>(loggerFactory.CreateLogger<GenericInfoAdapter<UserInfo>>(), modelService, providerProxyFactory.CreateProviderProxy<UserInfo>(providerProxyContext), providerProxyFactory),
Expand Down
16 changes: 7 additions & 9 deletions src/Kentico.Xperience.UMT/InfoAdapter/GenericAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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");
}
}

Expand All @@ -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();
}

Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Kentico.Xperience.UMT/Model/ContentItemReferenceModel.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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; }
Expand Down
2 changes: 0 additions & 2 deletions src/Kentico.Xperience.UMT/Model/DataClassModel.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -177,6 +176,5 @@ public class FormFieldSettings
/// Admin UI Component used for field data editing
/// </summary>
/// <docref uri="../Enums/FormComponents.md#module-kenticoxperienceadminbasedll">(for pages use enumeration here)</docref>
// 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; }
}
3 changes: 1 addition & 2 deletions src/Kentico.Xperience.UMT/Model/MediaFileModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CMS.MediaLibrary;
using CMS.Membership;
using Kentico.Xperience.UMT.Attributes;
// ReSharper disable InconsistentNaming

namespace Kentico.Xperience.UMT.Model;

Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/Kentico.Xperience.UMT/Model/WebSiteChannelModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel.DataAnnotations;
using CMS.ContentEngine;
using Kentico.Xperience.UMT.Attributes;
// ReSharper disable InconsistentNaming

namespace Kentico.Xperience.UMT.Model;

Expand Down Expand Up @@ -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; }
Expand Down
16 changes: 8 additions & 8 deletions src/Kentico.Xperience.UMT/ProviderProxy/IProviderProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContentItemDataInfo> GetProviderOrThrow(IUmtModel model)
{
if (model is ContentItemDataModel contentItemDataModel)
Expand Down Expand Up @@ -77,16 +77,16 @@ public BaseInfo Save(BaseInfo info, IUmtModel model)
}
}

public ProviderProxyContext Context { get; }
public IProviderProxyContext Context { get; } = context;

Check warning on line 80 in src/Kentico.Xperience.UMT/ProviderProxy/IProviderProxy.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Remove the member initializer, all constructors set an initial value for the member. (https://rules.sonarsource.com/csharp/RSPEC-3604)
}

internal class ProviderProxy<TInfo> : IProviderProxy where TInfo : AbstractInfoBase<TInfo>, new()
{
public ProviderProxyContext Context { get; }
public IProviderProxyContext Context { get; }

protected readonly IInfoProvider<TInfo> ProviderInstance;

public ProviderProxy(ProviderProxyContext context)
public ProviderProxy(IProviderProxyContext context)
{
Context = context;
if (typeof(TInfo).IsAssignableTo(typeof(DataClassInfo)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace Kentico.Xperience.UMT.ProviderProxy;

internal interface IProviderProxyFactory
{
IProviderProxy CreateProviderProxy<TInfo>(ProviderProxyContext context) where TInfo : BaseInfo;
IProviderProxy CreateProviderProxy(Type? infoType, ProviderProxyContext context);
IProviderProxy CreateProviderProxy<TInfo>(IProviderProxyContext context) where TInfo : BaseInfo;
IProviderProxy CreateProviderProxy(Type? infoType, IProviderProxyContext context);
}

internal class ProviderProxyFactory : IProviderProxyFactory
{
public IProviderProxy CreateProviderProxy<TInfo>(ProviderProxyContext context) where TInfo : BaseInfo => CreateProviderProxy(typeof(TInfo), context);
public IProviderProxy CreateProviderProxy<TInfo>(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));

Check warning on line 18 in src/Kentico.Xperience.UMT/ProviderProxy/ProviderProxyFactory.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Remove this argument from the method call; it hides the caller information. (https://rules.sonarsource.com/csharp/RSPEC-3236)

Expand Down
5 changes: 2 additions & 3 deletions src/Kentico.Xperience.UMT/Services/IImporter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
using CMS.DataEngine;
using Kentico.Xperience.UMT.Model;

Expand All @@ -11,10 +10,10 @@ public interface IImportResult
int PrimaryKey { get; }
BaseInfo? Imported { get; }
Exception? Exception { get; set; }
List<ValidationResult>? ModelValidationResults { get; set; } // TODO tomas.krch: 2023-11-16 convert to interface
List<ValidationResult>? ModelValidationResults { get; set; }
}

public interface IImporter : IDisposable
public interface IImporter
{
Task<IImportResult> ImportAsync(IUmtModel model);
}
2 changes: 1 addition & 1 deletion src/Kentico.Xperience.UMT/Services/ImportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public Task<ImportStateObserver> StartImportAsync(IAsyncEnumerable<IUmtModel> 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)
Expand Down
49 changes: 17 additions & 32 deletions src/Kentico.Xperience.UMT/Services/Importer.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,66 +17,52 @@ public class ImportResult : IImportResult
public List<ValidationResult>? ModelValidationResults { get; set; }
}

internal class Importer : IImporter
internal class Importer(ILogger<Importer> logger, AdapterFactory factory) : IImporter
{
private readonly ILogger<Importer> logger;
private readonly AdapterFactory adapterFactory;

public Importer(ILogger<Importer> logger, AdapterFactory adapterFactory)
{
this.logger = logger;
this.adapterFactory = adapterFactory;
}

public async Task<IImportResult> ImportAsync(IUmtModel umtModel)
public async Task<IImportResult> 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
};
}
}

public void Dispose()
{

}

private async Task<ImportResult> ImportObject(IUmtModel model, ProviderProxyContext providerProxyContext)
private Task<ImportResult> 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<ValidationResult>();
if (!ValidationService.Instance.TryValidateModel(model, ref modelValidationResults))
{
return new ImportResult()
return Task.FromResult(new ImportResult
{
Success = false,
ModelValidationResults = modelValidationResults
};
});
}

BaseInfo? adapted = null;
Expand All @@ -88,32 +73,32 @@ private async Task<ImportResult> 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
};
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, List<ValidationResult>> validationResults = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class ValidationService : IValidationService
/// <returns>true if valid, false if invalid</returns>
public bool TryValidateModel(IUmtModel model, ref List<ValidationResult> 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);
}
Expand Down

0 comments on commit 703fd7d

Please sign in to comment.