add Support for Getting Calculated Resource Title for Specified Resource Item ...
576 lines
17 KiB
C#
576 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xModels.Dtos;
|
|
using xStringService.Interfaces;
|
|
using xStringService.Interfaces.Dtos;
|
|
using xStringService.Models.Dtos;
|
|
|
|
namespace xStringService.Providers
|
|
{
|
|
public abstract class XBaseItemResourceProvider : IXItemResourceProvider
|
|
{
|
|
//
|
|
#region Props ...
|
|
/// <summary>
|
|
/// Item Name for Resource Providing ...
|
|
/// </summary>
|
|
private readonly string item;
|
|
|
|
/// <summary>
|
|
/// an Implementation of Item Resource Provider
|
|
/// </summary>
|
|
/// <value></value>
|
|
private readonly XBaseResourceProvider provider;
|
|
|
|
/// <summary>
|
|
/// String Provider Service ...
|
|
/// </summary>
|
|
private readonly IXStringServiceProvider stringProvider;
|
|
#endregion
|
|
|
|
//
|
|
#region Constructor ...
|
|
protected XBaseItemResourceProvider(
|
|
string item,
|
|
IXStringServiceProvider stringProvider
|
|
)
|
|
{
|
|
//
|
|
this.item = item;
|
|
this.stringProvider = stringProvider;
|
|
provider = new XBaseResourceProvider(
|
|
item,
|
|
stringProvider
|
|
);
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Group Actions ...
|
|
/// <summary>
|
|
/// Retrieve a List of Exists Languages for Current Item ...
|
|
/// </summary>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<string>> GetLanguages(
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = (await ExtractItems(cancellationToken))
|
|
.Select(x => x.Language)
|
|
.Distinct();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve all Exists Items of type ...
|
|
/// </summary>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<XStringDto>> GetResourceLocales(
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await ExtractItems(cancellationToken);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove all Exists Locales which Related to this Provider ...
|
|
/// </summary>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task RemoveResourceLocales(
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var isDelete = false;
|
|
XStringDto deleted = null;
|
|
var items = await ExtractItems(cancellationToken);
|
|
foreach (var item in items)
|
|
{
|
|
//
|
|
// Check Cancellation ...
|
|
if (cancellationToken.IsCancellationRequested)
|
|
{
|
|
break;
|
|
}
|
|
|
|
//
|
|
// Remove Item ...
|
|
deleted = await stringProvider.RemoveAsync(
|
|
id: item.Id,
|
|
softDelete: false,
|
|
saveChanges: true,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
// Check Removation Succeed ...
|
|
isDelete = !deleted.IsNullOrDefault();
|
|
if (!isDelete)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Item Actions ...
|
|
/// <summary>
|
|
/// Get all Exists Items for Specified identitifer ...
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<XStringDto>> GetResourceLocales(
|
|
object identifier,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !identifier.IsNull();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Setting Resource Title ...
|
|
SetResourceTitle(identifier);
|
|
|
|
//
|
|
// Retrieve Resource List ...
|
|
var result = await provider
|
|
.GetResourceLocales(cancellationToken);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all Exists Items for Specified identitifer ...
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<XLocaleResourceDto>> GetLocales(
|
|
object identifier,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !identifier.IsNull();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Setting Resource Title ...
|
|
SetResourceTitle(identifier);
|
|
|
|
//
|
|
// Retrieve Resource List ...
|
|
var result = await provider
|
|
.GetLocales(cancellationToken);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all Exists Locales Representaion based on Specified Identitifer
|
|
/// as XResourceDto ...
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<XResourceDto> GetResource(
|
|
object identifier,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !identifier.IsNull();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Setting Resource Title ...
|
|
SetResourceTitle(identifier);
|
|
|
|
//
|
|
// Retrieve Resource List ...
|
|
var result = await provider
|
|
.GetResource(cancellationToken);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Or Update Locale for Specified Identitifer ...
|
|
/// </summary>
|
|
/// <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 identifier,
|
|
XLocaleResourceDto locale,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!identifier.IsNull() &&
|
|
!locale.IsNullOrDefault() &&
|
|
!locale.Value.IsNullOrEmpty() &&
|
|
!locale.Language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Setting Resource Title ...
|
|
SetResourceTitle(identifier);
|
|
|
|
//
|
|
// Retrieve Resource List ...
|
|
var result = await provider
|
|
.AddOrUpdateLocale(
|
|
locale: locale,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a Resource State ...
|
|
/// </summary>
|
|
/// <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 identifier,
|
|
XResourceDto resource,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!identifier.IsNull();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve Identified Resource Title ...
|
|
var identifiedResource = GetResourceTitle(identifier);
|
|
isValid =
|
|
!resource.IsNullOrDefault() &&
|
|
(resource.Resource.IsNullOrEmpty() ||
|
|
resource.Resource == identifiedResource);
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
if (resource.Resource.IsNullOrEmpty())
|
|
{
|
|
resource.Resource = identifiedResource;
|
|
}
|
|
|
|
//
|
|
// Setting Resource Title ...
|
|
SetResourceTitle(identifier);
|
|
|
|
//
|
|
// Retrieve Resource List ...
|
|
var result = await provider
|
|
.AddOrUpdateResource(
|
|
resource: resource,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove Specified Language Locale of Specified Identifier ...
|
|
/// </summary>
|
|
/// <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 identifier,
|
|
string language,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!identifier.IsNull() &&
|
|
!language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Locale Exists ...
|
|
isValid = await HasLocale(
|
|
language: language,
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var locale = (await GetLocales(
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
))
|
|
.FirstOrDefault(x =>
|
|
x.Language.Equals(language, StringComparison.InvariantCultureIgnoreCase));
|
|
isValid = !locale.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Setting Resource Title ...
|
|
SetResourceTitle(identifier);
|
|
|
|
//
|
|
var result = await provider.RemoveLocale(
|
|
locale: locale,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove all Exists Locales for Specified Identifier ...
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task RemoveLocales(
|
|
object identifier,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!identifier.IsNull();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var resource = await GetResource(
|
|
identifier: identifier,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
isValid = !resource.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Setting Resource Title ...
|
|
SetResourceTitle(identifier);
|
|
|
|
//
|
|
await provider.RemoveResource(
|
|
resource: resource,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Specified ocale Exists for Specified Identifier ...
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="language"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<bool> HasLocale(
|
|
object identifier,
|
|
string language,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!identifier.IsNull() &&
|
|
!language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Setting Resource Title ...
|
|
SetResourceTitle(identifier);
|
|
|
|
//
|
|
var result = await provider.HasLocale(
|
|
language: language,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all Exists Languages for Specified Identifier ...
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<string>> GetLanguages(
|
|
object identifier,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !identifier.IsNull();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Setting Resource Title ...
|
|
SetResourceTitle(identifier);
|
|
|
|
//
|
|
var result = await provider
|
|
.GetLanguages(cancellationToken);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Resource Title Action ...
|
|
/// <summary>
|
|
/// Retrieve Resource Title ...
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
/// <returns></returns>
|
|
public string GetResourceTitle(object identifier = null)
|
|
{
|
|
//
|
|
var result = item;
|
|
|
|
//
|
|
if (!identifier.IsNull())
|
|
{
|
|
result = $"{item}_{identifier}";
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// Set Resource Title ...
|
|
/// </summary>
|
|
/// <param name="identifier"></param>
|
|
private void SetResourceTitle(object identifier = null)
|
|
{
|
|
//
|
|
var itemIdentifier = GetResourceTitle(identifier);
|
|
provider.SetField(
|
|
field: "resource",
|
|
value: $"{itemIdentifier}"
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// xtract all Items which Belongs to Provider ...
|
|
/// </summary>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<IEnumerable<XStringDto>> ExtractItems(
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await stringProvider
|
|
.FindManyAsync(
|
|
includeBuilder: null,
|
|
cancellationToken: cancellationToken,
|
|
orderBuilder: x => x.OrderBy(y => y.Id),
|
|
predicate: x =>
|
|
x.ResourceTitle.StartsWith(
|
|
item,
|
|
StringComparison.InvariantCultureIgnoreCase)
|
|
);
|
|
return result;
|
|
}
|
|
#endregion
|
|
}
|
|
} |