688 lines
20 KiB
C#
688 lines
20 KiB
C#
using System;
|
|
using System.Linq;
|
|
using xModels.Dtos;
|
|
using System.Threading;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using System.Threading.Tasks;
|
|
using xCommons.Configurations;
|
|
using xStringService.Extensions;
|
|
using xStringService.Interfaces;
|
|
using System.Collections.Generic;
|
|
using xStringService.Models.Dtos;
|
|
using xStringService.Interfaces.Dtos;
|
|
|
|
namespace xStringService.Providers
|
|
{
|
|
public abstract class XBaseOwnedItemResourceProvider : IXOwnedItemResourceProvider
|
|
{
|
|
//
|
|
#region Props ...
|
|
/// <summary>
|
|
/// Item Name for Resource Providing ...
|
|
/// </summary>
|
|
private readonly string item;
|
|
|
|
/// <summary>
|
|
/// Application Configuration ...
|
|
/// </summary>
|
|
private readonly XAppConfiguration appConfig;
|
|
|
|
/// <summary>
|
|
/// String Provider Service ...
|
|
/// </summary>
|
|
private readonly IXStringServiceProvider stringProvider;
|
|
#endregion
|
|
|
|
//
|
|
#region Constructor ...
|
|
protected XBaseOwnedItemResourceProvider(
|
|
string item,
|
|
XAppConfiguration appConfig,
|
|
IXStringServiceProvider stringProvider
|
|
)
|
|
{
|
|
//
|
|
this.item = item;
|
|
this.appConfig = appConfig;
|
|
this.stringProvider = stringProvider;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Group Actions ...
|
|
/// <summary>
|
|
/// Retrieve a List of Exists Languages for Current Item ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<string>> GetLanguages(
|
|
object owner,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var items = await ExtractStrings(
|
|
owner: owner,
|
|
identifier: null,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
var result =
|
|
items
|
|
.Select(x => x.Language)
|
|
.Distinct();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve all Exists Items of type ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<XStringDto>> GetResourceLocales(
|
|
object owner,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await ExtractStrings(
|
|
owner: owner,
|
|
identifier: null,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove all Exists Locales which Related to this Provider ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task RemoveResourceLocales(
|
|
object owner,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var items = await ExtractStrings(
|
|
owner: owner,
|
|
identifier: null,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
foreach (var item in items)
|
|
{
|
|
//
|
|
// Check Cancellation Token ...
|
|
if (cancellationToken.IsCancellationRequested)
|
|
{
|
|
break;
|
|
}
|
|
|
|
//
|
|
// Try to Remove it ...
|
|
try
|
|
{
|
|
//
|
|
await stringProvider.RemoveAsync(
|
|
id: item.Id,
|
|
softDelete: false,
|
|
saveChanges: true,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Item Actions ...
|
|
/// <summary>
|
|
/// Get all Exists Items for Specified identitifer ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<XStringDto>> GetResourceLocales(
|
|
object owner,
|
|
object identifier,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var resourceTitle = GetResourceTitle(
|
|
owner: owner,
|
|
identifier: identifier
|
|
);
|
|
|
|
//
|
|
var result = await stringProvider.FindManyAsync(
|
|
includeBuilder: null,
|
|
ignoreSoftDeleteds: true,
|
|
cancellationToken: cancellationToken,
|
|
predicate: x => x.ResourceTitle
|
|
.ToNormalString()
|
|
.Contains(resourceTitle.ToNormalString())
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all Exists Items for Specified identitifer ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<XLocaleResourceDto>> GetLocales(
|
|
object owner,
|
|
object identifier,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = (await GetResourceLocales(
|
|
owner: owner,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
))
|
|
.Select(x => x.ToXLocaleResourceDto());
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all Exists Locales Representaion based on Specified Identitifer
|
|
/// as XResourceDto ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<XResourceDto> GetResource(
|
|
object owner,
|
|
object identifier,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var resource = GetResourceTitle(
|
|
owner: owner,
|
|
identifier: identifier
|
|
);
|
|
var locales = await GetLocales(
|
|
owner: owner,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
var result = new XResourceDto
|
|
{
|
|
Locales = locales,
|
|
Resource = resource
|
|
};
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Or Update Locale for Specified Identitifer ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="locale"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<XResourceDto> AddOrUpdateLocale(
|
|
object owner,
|
|
object identifier,
|
|
XLocaleResourceDto locale,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var resource = GetResourceTitle(
|
|
owner: owner,
|
|
identifier: identifier
|
|
);
|
|
|
|
//
|
|
var resourceDto = await stringProvider.AddOrUpdateByLocaleResource(
|
|
item: locale,
|
|
resource: resource,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
// Retrieve XResourceDto ...
|
|
var result = await GetResource(
|
|
owner: owner,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a Resource State ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="resource"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<XResourceDto> AddOrUpdateResource(
|
|
object owner,
|
|
object identifier,
|
|
XResourceDto resource,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var resourceTitle = GetResourceTitle(
|
|
owner: owner,
|
|
identifier: identifier
|
|
);
|
|
|
|
//
|
|
var isValid =
|
|
!resource.IsNullOrDefault() &&
|
|
resource.Resource == resourceTitle;
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve Exists Resource ...
|
|
var result = await GetResource(
|
|
owner: owner,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
isValid =
|
|
!result.IsNullOrDefault() &&
|
|
result.Locales.HasChild();
|
|
if (isValid)
|
|
{
|
|
await RemoveLocales(
|
|
owner: owner,
|
|
identifier: identifier,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
//
|
|
foreach (var locale in resource.Locales)
|
|
{
|
|
//
|
|
var item = new XStringDto
|
|
{
|
|
Language = locale.Language,
|
|
ResourceTitle = resourceTitle,
|
|
TranslatedValue = locale.Value,
|
|
};
|
|
|
|
//
|
|
await stringProvider.AddAsync(
|
|
item: item,
|
|
saveChanges: true,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
//
|
|
result = await GetResource(
|
|
owner: owner,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove Specified Language Locale of Specified Identifier ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="language"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<XResourceDto> RemoveLocale(
|
|
object owner,
|
|
object identifier,
|
|
string language,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
XResourceDto result = null;
|
|
language = NormalizedLanguage(language);
|
|
|
|
//
|
|
// Check Locale Exists ...
|
|
var isValid = await HasLocale(
|
|
owner: owner,
|
|
language: language,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var resourceTitle = GetResourceTitle(
|
|
owner: owner,
|
|
identifier: identifier
|
|
);
|
|
|
|
//
|
|
var item = await stringProvider.FindOneAsync(
|
|
includeBuilder: null,
|
|
ignoreSoftDeleteds: true,
|
|
cancellationToken: cancellationToken,
|
|
predicate: x =>
|
|
x.Language.Equals(
|
|
language,
|
|
StringComparison.InvariantCultureIgnoreCase) &&
|
|
x.ResourceTitle.Equals(
|
|
resourceTitle,
|
|
StringComparison.InvariantCultureIgnoreCase)
|
|
);
|
|
isValid = !item.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Remove XStringDto ...
|
|
await stringProvider.RemoveAsync(
|
|
id: item.Id,
|
|
softDelete: false,
|
|
saveChanges: true,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
try
|
|
{
|
|
//
|
|
result = await GetResource(
|
|
owner: owner,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
catch { }
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove all Exists Locales for Specified Identifier ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task RemoveLocales(
|
|
object owner,
|
|
object identifier,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var resourceTitle = GetResourceTitle(
|
|
owner: owner,
|
|
identifier: identifier
|
|
);
|
|
|
|
//
|
|
var items = await stringProvider.FindManyAsync(
|
|
includeBuilder: null,
|
|
ignoreSoftDeleteds: true,
|
|
cancellationToken: cancellationToken,
|
|
predicate: x =>
|
|
x.ResourceTitle.Equals(
|
|
resourceTitle,
|
|
StringComparison.InvariantCultureIgnoreCase)
|
|
);
|
|
var isValid = !item.IsNull();
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
await stringProvider.RemoveResource(
|
|
resource: resourceTitle,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Specified ocale Exists for Specified Identifier ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="language"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<bool> HasLocale(
|
|
object owner,
|
|
object identifier,
|
|
string language,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
language = NormalizedLanguage(language);
|
|
var locales = await GetLocales(
|
|
owner: owner,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
var result =
|
|
language.HasChild() &&
|
|
locales.Any(l => l.Language.Equals(
|
|
language,
|
|
StringComparison.InvariantCultureIgnoreCase)
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all Exists Languages for Specified Identifier ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<string>> GetLanguages(
|
|
object owner,
|
|
object identifier = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var items = await ExtractStrings(
|
|
owner: owner,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
var result =
|
|
items
|
|
.Select(x => x.Language)
|
|
.Distinct();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Resource Title Action ...
|
|
/// <summary>
|
|
/// Retrieve Resource Title ...
|
|
/// </summary>
|
|
/// <param name="owner"></param>
|
|
/// <param name="identifier"></param>
|
|
/// <returns></returns>
|
|
public virtual string GetResourceTitle(
|
|
object owner = null,
|
|
object identifier = null
|
|
)
|
|
{
|
|
//
|
|
var result = item;
|
|
|
|
//
|
|
// Normalization ...
|
|
var ownerStr =
|
|
!owner.IsNull()
|
|
? $"{owner}"
|
|
: string.Empty;
|
|
var identifierStr =
|
|
!identifier.IsNull()
|
|
? $"{identifier}"
|
|
: string.Empty;
|
|
|
|
//
|
|
// Handle Owner Attach ...
|
|
if (!ownerStr.IsNullOrEmpty())
|
|
{
|
|
result = $"{result}_{ownerStr}";
|
|
}
|
|
|
|
//
|
|
// Handle Identifier Attach ...
|
|
if (!identifierStr.IsNullOrEmpty())
|
|
{
|
|
result = $"{result}_{identifierStr}";
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Private ...
|
|
private string NormalizedLanguage(
|
|
string language = null
|
|
)
|
|
{
|
|
//
|
|
var result = language;
|
|
if (result.IsNullOrEmpty())
|
|
{
|
|
result = GetDefaultLanguage();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
private string GetDefaultLanguage()
|
|
{
|
|
//
|
|
var result = appConfig.DefaultLanguage;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
private async Task<IEnumerable<XStringDto>> ExtractStrings(
|
|
string resourceTitle,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
IEnumerable<XStringDto> result = [];
|
|
|
|
//
|
|
if (resourceTitle.IsNullOrEmpty())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = await stringProvider.FindManyAsync(
|
|
includeBuilder: null,
|
|
ignoreSoftDeleteds: true,
|
|
cancellationToken: cancellationToken,
|
|
orderBuilder: x => x.OrderBy(y => y.Id),
|
|
predicate: x => x.ResourceTitle
|
|
.ToNormalString()
|
|
.Contains(resourceTitle.ToNormalString())
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
private async Task<IEnumerable<XStringDto>> ExtractStrings(
|
|
object owner,
|
|
object identifier,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var resourceTitle = GetResourceTitle(
|
|
owner: owner,
|
|
identifier: identifier
|
|
);
|
|
|
|
//
|
|
var result = await ExtractStrings(
|
|
resourceTitle: resourceTitle,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
}
|
|
} |