441 lines
17 KiB
C#
441 lines
17 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AyaNova.Util;
|
|
using AyaNova.Api.ControllerHelpers;
|
|
using AyaNova.Models;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace AyaNova.Biz
|
|
{
|
|
|
|
internal class TranslationBiz : BizObject
|
|
{
|
|
public bool SeedOrImportRelaxedRulesMode { get; set; }
|
|
|
|
internal TranslationBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles userRoles)
|
|
{
|
|
ct = dbcontext;
|
|
UserId = currentUserId;
|
|
UserTranslationId = userTranslationId;
|
|
CurrentUserRoles = userRoles;
|
|
BizType = AyaType.Translation;
|
|
SeedOrImportRelaxedRulesMode = false;//default
|
|
}
|
|
|
|
internal static TranslationBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
|
{
|
|
if (httpContext != null)
|
|
return new TranslationBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
|
else
|
|
return new TranslationBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//EXISTS
|
|
internal async Task<bool> ExistsAsync(long id)
|
|
{
|
|
return await ct.Translation.AnyAsync(z => z.Id == id);
|
|
}
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//UPDATE
|
|
//
|
|
internal async Task<Translation> PutAsync(Translation putObject)
|
|
{
|
|
Translation dbObject = await ct.Translation.SingleOrDefaultAsync(z => z.Id == putObject.Id);
|
|
if (dbObject == null)
|
|
{
|
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
|
return null;
|
|
}
|
|
Translation SnapshotOfOriginalDBObj = new Translation();
|
|
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
|
CopyObject.Copy(putObject, dbObject, "Id");
|
|
|
|
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
|
//maybe validate that there are no empty values?
|
|
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
|
if (HasErrors) return null;
|
|
try
|
|
{
|
|
await ct.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!await ExistsAsync(putObject.Id))
|
|
AddError(ApiErrorCode.NOT_FOUND);
|
|
else
|
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
|
return null;
|
|
}
|
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
|
|
|
return dbObject;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//DUPLICATE
|
|
//
|
|
internal async Task<Translation> DuplicateAsync(long id)
|
|
{
|
|
Translation dbObject = await ct.Translation.SingleOrDefaultAsync(z => z.Id == id);
|
|
|
|
if (dbObject == null)
|
|
{
|
|
AddError(ApiErrorCode.NOT_FOUND, "id");
|
|
return null;
|
|
}
|
|
Translation newObject = new Translation();
|
|
//CopyObject.Copy(dbObject, newObject, "Id, Salt, Login, Password, CurrentAuthToken, DlKey, DlKeyExpire, Wiki, Serial");
|
|
string newUniqueName = string.Empty;
|
|
bool NotUnique = true;
|
|
long l = 1;
|
|
do
|
|
{
|
|
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
|
NotUnique = await ct.Translation.AnyAsync(z => z.Name == newUniqueName);
|
|
} while (NotUnique);
|
|
newObject.Name = newUniqueName;
|
|
|
|
newObject.Stock = false;
|
|
newObject.CjkIndex = false;
|
|
foreach (TranslationItem i in dbObject.TranslationItems)
|
|
{
|
|
newObject.TranslationItems.Add(new TranslationItem() { Key = i.Key, Display = i.Display });
|
|
}
|
|
|
|
newObject.Id = 0;
|
|
newObject.Concurrency = 0;
|
|
await ct.Translation.AddAsync(newObject);
|
|
await ct.SaveChangesAsync();
|
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
|
// await SearchIndexAsync(newObject, true);
|
|
// await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
|
return newObject;
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
/// GET
|
|
|
|
//Get entire translation
|
|
internal async Task<Translation> GetAsync(long fetchId)
|
|
{
|
|
//This is simple so nothing more here, but often will be copying to a different output object or some other ops
|
|
return await ct.Translation.Include(z => z.TranslationItems).SingleOrDefaultAsync(z => z.Id == fetchId);
|
|
}
|
|
|
|
|
|
|
|
//get list (simple non-paged)
|
|
internal async Task<List<NameIdItem>> GetTranslationListAsync()
|
|
{
|
|
List<NameIdItem> l = new List<NameIdItem>();
|
|
l = await ct.Translation
|
|
.AsNoTracking()
|
|
.OrderBy(z => z.Name)
|
|
.Select(z => new NameIdItem()
|
|
{
|
|
Id = z.Id,
|
|
Name = z.Name
|
|
}).ToListAsync();
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
#if (DEBUG)
|
|
internal async Task<AyaNova.Api.Controllers.TranslationController.TranslationCoverageInfo> TranslationKeyCoverageAsync()
|
|
{
|
|
AyaNova.Api.Controllers.TranslationController.TranslationCoverageInfo L = new AyaNova.Api.Controllers.TranslationController.TranslationCoverageInfo();
|
|
L.RequestedKeys = ServerBootConfig.TranslationKeysRequested;
|
|
L.RequestedKeys.Sort();
|
|
var AllKeys = await GetKeyListAsync();
|
|
foreach (string StockKey in AllKeys)
|
|
{
|
|
if (!L.RequestedKeys.Contains(StockKey))
|
|
{
|
|
L.NotRequestedKeys.Add(StockKey);
|
|
}
|
|
}
|
|
L.NotRequestedKeys.Sort();
|
|
L.RequestedKeyCount = L.RequestedKeys.Count;
|
|
L.NotRequestedKeyCount = L.NotRequestedKeys.Count;
|
|
|
|
return L;
|
|
}
|
|
|
|
//Track requests for keys so we can determine which are being used and which are not
|
|
//TODO: Ideally this should be paired with tests that either directly request each key that are def. being used
|
|
//or the UI needs to be tested in a way that triggers every key to be used even errors etc
|
|
internal static void TrackRequestedKey(string key)
|
|
{
|
|
if (!ServerBootConfig.TranslationKeysRequested.Contains(key))
|
|
ServerBootConfig.TranslationKeysRequested.Add(key);
|
|
}
|
|
|
|
internal static void TrackRequestedKey(List<string> keys)
|
|
{
|
|
foreach (string Key in keys)
|
|
{
|
|
if (!ServerBootConfig.TranslationKeysRequested.Contains(Key))
|
|
ServerBootConfig.TranslationKeysRequested.Add(Key);
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
//Get the keys for a list of keys provided
|
|
internal async Task<List<KeyValuePair<string, string>>> GetSubsetAsync(List<string> param)
|
|
{
|
|
|
|
#if (DEBUG)
|
|
TrackRequestedKey(param);
|
|
#endif
|
|
var ret = await ct.TranslationItem.Where(z => z.TranslationId == UserTranslationId && param.Contains(z.Key)).ToDictionaryAsync(z => z.Key, z => z.Display);
|
|
return ret.ToList();
|
|
}
|
|
|
|
//Get the keys for a list of keys provided, static format for calling from other internal classes
|
|
internal static async Task<Dictionary<string, string>> GetSubsetStaticAsync(List<string> param, long translationId)
|
|
{
|
|
#if (DEBUG)
|
|
TrackRequestedKey(param);
|
|
#endif
|
|
using (AyContext ct = ServiceProviderProvider.DBContext)
|
|
{
|
|
if (!await ct.Translation.AnyAsync(e => e.Id == translationId))
|
|
translationId = ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID;
|
|
var ret = await ct.TranslationItem.Where(z => z.TranslationId == translationId && param.Contains(z.Key)).AsNoTracking().ToDictionaryAsync(z => z.Key, z => z.Display);
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
|
|
//Get the CJKIndex value for the translation specified
|
|
internal static async Task<bool> GetCJKIndexAsync(long translationId, AyContext ct)
|
|
{
|
|
var ret = await ct.Translation.Where(z => z.Id == translationId).AsNoTracking().Select(z => z.CjkIndex).SingleOrDefaultAsync();
|
|
return ret;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get the value of the key provided in the default translation chosen
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
internal static async Task<string> GetDefaultTranslationAsync(string key)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
return "ERROR: GetDefaultTranslation NO KEY VALUE SPECIFIED";
|
|
#if (DEBUG)
|
|
TrackRequestedKey(key);
|
|
#endif
|
|
using (AyContext ct = ServiceProviderProvider.DBContext)
|
|
return await ct.TranslationItem.Where(z => z.TranslationId == ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID && z.Key == key).Select(z => z.Display).AsNoTracking().FirstOrDefaultAsync();
|
|
}
|
|
|
|
//Get all stock keys that are valid (used for key coverage reporting)
|
|
internal static async Task<List<string>> GetKeyListAsync()
|
|
{
|
|
using (AyContext ct = ServiceProviderProvider.DBContext)
|
|
return await ct.TranslationItem.Where(z => z.TranslationId == 1).OrderBy(z => z.Key).Select(z => z.Key).AsNoTracking().ToListAsync();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//DELETE
|
|
//
|
|
|
|
internal async Task<bool> DeleteAsync(Translation dbObj)
|
|
{
|
|
//Determine if the object can be deleted, do the deletion tentatively
|
|
await ValidateCanDeleteAsync(dbObj);
|
|
if (HasErrors)
|
|
return false;
|
|
ct.Translation.Remove(dbObj);
|
|
await ct.SaveChangesAsync();
|
|
//Log
|
|
await EventLogProcessor.DeleteObjectLogAsync(UserId, AyaType.Translation, dbObj.Id, dbObj.Name, ct);
|
|
return true;
|
|
}
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//VALIDATION
|
|
//
|
|
|
|
//Can save or update?
|
|
private async Task ValidateAsync(Translation proposedObj, Translation currentObj)
|
|
{
|
|
//run validation and biz rules
|
|
|
|
//Name required
|
|
if (string.IsNullOrWhiteSpace(proposedObj.Name))
|
|
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name");
|
|
|
|
//Name must be less than 255 characters
|
|
if (proposedObj.Name.Length > 255)
|
|
AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "Name", "255 char max");
|
|
|
|
//Name must be unique
|
|
if (await ct.Translation.AnyAsync(z => z.Name == proposedObj.Name))
|
|
AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name");
|
|
|
|
//Ensure there are no empty keys
|
|
if (proposedObj.TranslationItems.Where(z => z.Display.Length < 1).Any())
|
|
{
|
|
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Display", "One or more items are missing a display value");
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
//Can delete?
|
|
private async Task ValidateCanDeleteAsync(Translation inObj)
|
|
{
|
|
//Decided to short circuit these; if there is one issue then return immediately (fail fast rule)
|
|
|
|
//Ensure it's not a stock translation
|
|
if (inObj.Stock == true)
|
|
{
|
|
AddError(ApiErrorCode.INVALID_OPERATION, "object", "Translation is a Stock translation and cannot be deleted");
|
|
return;
|
|
}
|
|
|
|
if (inObj.Id == ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID)
|
|
{
|
|
AddError(ApiErrorCode.INVALID_OPERATION, "object", "Translation is set as the default server translation (AYANOVA_DEFAULT_LANGUAGE_ID) and can not be deleted");
|
|
return;
|
|
}
|
|
|
|
//See if any users exist with this translation selected in which case it's not deleteable
|
|
if (await ct.UserOptions.AnyAsync(e => e.TranslationId == inObj.Id))
|
|
{
|
|
AddError(ApiErrorCode.VALIDATION_REFERENTIAL_INTEGRITY, "object", "Can't be deleted in use by one or more Users");
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//UTILITIES
|
|
//
|
|
|
|
public async Task<long> TranslationNameToIdAsync(string translationName)
|
|
{
|
|
var v = await ct.Translation.AsNoTracking().FirstOrDefaultAsync(z => z.Name == translationName);
|
|
if (v == null) return 0;
|
|
return v.Id;
|
|
}
|
|
|
|
public static async Task<long> TranslationNameToIdStaticAsync(string translationName)
|
|
{
|
|
using (AyContext ct = ServiceProviderProvider.DBContext)
|
|
{
|
|
var v = await ct.Translation.AsNoTracking().FirstOrDefaultAsync(z => z.Name == translationName);
|
|
if (v == null) return ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID;
|
|
return v.Id;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> TranslationExistsAsync(string translationName)
|
|
{
|
|
return await ct.Translation.AnyAsync(z => z.Name == translationName);
|
|
|
|
}
|
|
|
|
public async Task<bool> TranslationExistsAsync(long id)
|
|
{
|
|
return await ct.Translation.AnyAsync(z => z.Id == id);
|
|
}
|
|
|
|
|
|
|
|
//this is only called by Search.cs to cache a local cjk and stopwords, no one else calls it currently
|
|
public static async Task<long> ReturnSpecifiedTranslationIdIfExistsOrDefaultTranslationId(long id, AyContext ct)
|
|
{
|
|
if (!await ct.Translation.AnyAsync(z => z.Id == id))
|
|
return ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID;
|
|
return id;
|
|
}
|
|
|
|
public async Task<bool> TranslationItemExistsAsync(long id)
|
|
{
|
|
return await ct.TranslationItem.AnyAsync(z => z.Id == id);
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Ensure stock Translations and setup defaults
|
|
/// Called by boot preflight check code AFTER it has already ensured the translation is a two letter code if stock one was chosen
|
|
/// </summary>
|
|
public async Task ValidateTranslationsAsync()
|
|
{
|
|
//Ensure default translations are present and that there is a server default translation that exists
|
|
|
|
if (!await TranslationExistsAsync("en"))
|
|
{
|
|
throw new System.Exception($"E1015: stock translation English (en) not found in database!");
|
|
}
|
|
if (!await TranslationExistsAsync("es"))
|
|
{
|
|
throw new System.Exception($"E1015: stock translation Spanish (es) not found in database!");
|
|
}
|
|
if (!await TranslationExistsAsync("de"))
|
|
{
|
|
throw new System.Exception($"E1015: stock translation German (de) not found in database!");
|
|
}
|
|
if (!await TranslationExistsAsync("fr"))
|
|
{
|
|
throw new System.Exception($"E1015: stock translation French (fr) not found in database!");
|
|
}
|
|
|
|
//Ensure chosen default translation exists
|
|
switch (ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION)
|
|
{
|
|
case "en":
|
|
case "es":
|
|
case "de":
|
|
case "fr":
|
|
break;
|
|
default:
|
|
if (!await TranslationExistsAsync(ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION))
|
|
{
|
|
throw new System.Exception($"E1015: stock translation {ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION} not found in database!");
|
|
}
|
|
break;
|
|
|
|
}
|
|
|
|
//Put the default translation ID number into the ServerBootConfig for later use
|
|
ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID = await TranslationNameToIdAsync(ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
}//eoc
|
|
|
|
//
|
|
}//eons
|
|
|