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 ... /// /// Item Name for Resource Providing ... /// private readonly string item; /// /// Application Configuration ... /// private readonly XAppConfiguration appConfig; /// /// String Provider Service ... /// 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 ... /// /// Retrieve a List of Exists Languages for Current Item ... /// /// /// /// public virtual async Task> 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; } /// /// Retrieve all Exists Items of type ... /// /// /// /// public virtual async Task> GetResourceLocales( object owner, CancellationToken cancellationToken = default ) { // var result = await ExtractStrings( owner: owner, identifier: null, cancellationToken: cancellationToken ); // return result; } /// /// Remove all Exists Locales which Related to this Provider ... /// /// /// /// /// 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 ... /// /// Get all Exists Items for Specified identitifer ... /// /// /// /// /// public virtual async Task> 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; } /// /// Get all Exists Items for Specified identitifer ... /// /// /// /// /// public virtual async Task> GetLocales( object owner, object identifier, CancellationToken cancellationToken = default ) { // var result = (await GetResourceLocales( owner: owner, identifier: identifier, cancellationToken: cancellationToken )) .Select(x => x.ToXLocaleResourceDto()); // return result; } /// /// Get all Exists Locales Representaion based on Specified Identitifer /// as XResourceDto ... /// /// /// /// /// public virtual async Task GetResource( object owner, object identifier, CancellationToken cancellationToken = default ) { // var resource = GetResourceTitle( owner: owner ); var locales = await GetLocales( owner: owner, identifier: identifier, cancellationToken: cancellationToken ); // var result = new XResourceDto { Locales = locales, Resource = resource }; // return result; } /// /// Add Or Update Locale for Specified Identitifer ... /// /// /// /// /// /// /// public virtual async Task 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; } /// /// Update a Resource State ... /// /// /// /// /// /// /// public virtual async Task 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; } /// /// Remove Specified Language Locale of Specified Identifier ... /// /// /// /// /// /// /// public virtual async Task 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; } /// /// Remove all Exists Locales for Specified Identifier ... /// /// /// /// /// /// 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 ); } /// /// Check Specified ocale Exists for Specified Identifier ... /// /// /// /// /// /// public virtual async Task 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; } /// /// Get all Exists Languages for Specified Identifier ... /// /// /// /// /// public virtual async Task> 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 ... /// /// Retrieve Resource Title ... /// /// /// /// 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> ExtractStrings( string resourceTitle, CancellationToken cancellationToken = default ) { // IEnumerable 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> 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 } }