Skip to content

Commit

Permalink
Merge pull request #309 from yuriys-kentico/issue-308-metadata-servic…
Browse files Browse the repository at this point in the history
…e-loading-default-language

Issue 308 metadata service loading default language
  • Loading branch information
ChristopherJennings authored Aug 7, 2019
2 parents 6f3eea9 + fd1bd22 commit 66ae5b3
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 12 deletions.
20 changes: 14 additions & 6 deletions KenticoInspector.Core/AbstractReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,33 @@ namespace KenticoInspector.Core
{
protected readonly IReportMetadataService reportMetadataService;

private ReportMetadata<T> metadata;

public AbstractReport(IReportMetadataService reportMetadataService)
{
this.reportMetadataService = reportMetadataService;
}

public string Codename => GetCodename(this.GetType());

public static string GetCodename(Type reportType)
{
return GetDirectParentNamespace(reportType);
}

public abstract IList<Version> CompatibleVersions { get; }

public virtual IList<Version> IncompatibleVersions => new List<Version>();

public ReportMetadata<T> Metadata
{
get
{
return metadata ?? (metadata = reportMetadataService.GetReportMetadata<T>(Codename));
}
}

public abstract IList<string> Tags { get; }

public ReportMetadata<T> Metadata => reportMetadataService.GetReportMetadata<T>(Codename);
public static string GetCodename(Type reportType)
{
return GetDirectParentNamespace(reportType);
}

public abstract ReportResults GetResults();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace KenticoInspector.Core.Services.Interfaces
{
public interface IReportMetadataService : IService
{
string DefaultCultureName { get; }

string CurrentCultureName { get; }

ReportMetadata<T> GetReportMetadata<T>(string reportCodename) where T : new();
Expand Down
90 changes: 84 additions & 6 deletions KenticoInspector.Infrastructure/Services/ReportMetadataService.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,108 @@
using KenticoInspector.Core.Models;
using KenticoInspector.Core.Services.Interfaces;
using System;
using System.IO;
using System.Threading;

using KenticoInspector.Core.Models;
using KenticoInspector.Core.Services.Interfaces;

using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace KenticoInspector.Core.Helpers
{
public class ReportMetadataService : IReportMetadataService
{
public string DefaultCultureName => "en-US";

public string CurrentCultureName => Thread.CurrentThread.CurrentCulture.Name;

public ReportMetadata<T> GetReportMetadata<T>(string reportCodename) where T : new()
{
var yamlPath = $"{DirectoryHelper.GetExecutingDirectory()}\\{reportCodename}\\Metadata\\{CurrentCultureName}.yaml";
return DeserializeYaml<ReportMetadata<T>>(yamlPath);
var metadataDirectory = $"{DirectoryHelper.GetExecutingDirectory()}\\{reportCodename}\\Metadata\\";

var reportMetadata = GetReportMetadataInternal<T>(metadataDirectory, CurrentCultureName);

var isCurrentCultureDefaultCulture = CurrentCultureName == DefaultCultureName;
if (!isCurrentCultureDefaultCulture)
{
var defaultReportMetadata = GetReportMetadataInternal<T>(metadataDirectory, DefaultCultureName);
reportMetadata = GetMergedMetadata(defaultReportMetadata, reportMetadata);
}

return reportMetadata;
}

public T DeserializeYaml<T>(string path)

private ReportMetadata<T> GetReportMetadataInternal<T>(string metadataDirectory, string cultureName) where T : new()
{
var deserializer = new DeserializerBuilder()
var reportMetadataPath = $"{metadataDirectory}{cultureName}.yaml";
var reportMetadataPathExists = File.Exists(reportMetadataPath);
return reportMetadataPathExists
? DeserializeYaml<ReportMetadata<T>>(reportMetadataPath)
: new ReportMetadata<T>();
}

private T DeserializeYaml<T>(string path)
{
var deserializerBuilder = new DeserializerBuilder()
.WithNamingConvention(new CamelCaseNamingConvention())
.IgnoreUnmatchedProperties();

var deserializer = deserializerBuilder
.Build();

var yamlFile = File.ReadAllText(path);

return deserializer.Deserialize<T>(yamlFile);
}

private ReportMetadata<T> GetMergedMetadata<T>(ReportMetadata<T> defaultMetadata, ReportMetadata<T> overrideMetadata) where T : new()
{
var mergedMetadata = new ReportMetadata<T>
{
Details = new ReportDetails(),
Terms = new T()
};

mergedMetadata.Details.Name = overrideMetadata.Details?.Name ?? defaultMetadata.Details.Name;
mergedMetadata.Details.ShortDescription = overrideMetadata.Details?.ShortDescription ?? defaultMetadata.Details.ShortDescription;
mergedMetadata.Details.LongDescription = overrideMetadata.Details?.LongDescription ?? defaultMetadata.Details.LongDescription;

RecursivelySetPropertyValues(typeof(T), defaultMetadata.Terms, overrideMetadata.Terms, mergedMetadata.Terms);

return mergedMetadata;
}

private static void RecursivelySetPropertyValues(Type objectType, object defaultObject, object overrideObject, object targetObject)
{
var objectTypeProperties = objectType.GetProperties();

foreach (var objectTypeProperty in objectTypeProperties)
{
var objectTypePropertyType = objectTypeProperty.PropertyType;

var defaultObjectPropertyValue = objectTypeProperty.GetValue(defaultObject);

object overrideObjectPropertyValue = null;

if (overrideObject != null)
{
overrideObjectPropertyValue = objectTypeProperty.GetValue(overrideObject);
}

if (objectTypePropertyType.Namespace == objectType.Namespace)
{
var targetObjectPropertyValue = Activator.CreateInstance(objectTypePropertyType);

objectTypeProperty.SetValue(targetObject, targetObjectPropertyValue);

RecursivelySetPropertyValues(objectTypePropertyType, defaultObjectPropertyValue, overrideObjectPropertyValue, targetObjectPropertyValue);
}
else
{
objectTypeProperty.SetValue(targetObject, overrideObjectPropertyValue ?? defaultObjectPropertyValue);
}
}
}
}
}

0 comments on commit 66ae5b3

Please sign in to comment.