Files
raven/server/AyaNova/biz/TranslationBiz.cs
2020-03-18 19:56:58 +00:00

656 lines
31 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, IImportAyaNova7Object
{
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);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//DUPLICATE - only way to create a new translation
//
internal async Task<Translation> DuplicateAsync(NameIdItem inObj)
{
//make sure sourceid exists
if (!await TranslationExistsAsync(inObj.Id))
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "Id", "Source translation id does not exist");
//Ensure name is unique and not too long and not empty
await ValidateAsync(inObj.Name, true);
if (HasErrors)
return null;
//fetch the existing translation for duplication
var SourceTranslation = await ct.Translation.Include(x => x.TranslationItems).SingleOrDefaultAsync(m => m.Id == inObj.Id);
//replicate the source to a new dest and save
Translation NewTranslation = new Translation();
NewTranslation.Name = inObj.Name;
NewTranslation.Stock = false;
NewTranslation.CjkIndex = false;
foreach (TranslationItem i in SourceTranslation.TranslationItems)
{
NewTranslation.TranslationItems.Add(new TranslationItem() { Key = i.Key, Display = i.Display });
}
//Add it to the context so the controller can save it
await ct.Translation.AddAsync(NewTranslation);
await ct.SaveChangesAsync();
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, NewTranslation.Id, AyaType.Translation, AyaEvent.Created), ct);
return NewTranslation;
}
////////////////////////////////////////////////////////////////////////////////////////////////
/// 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(x => x.TranslationItems).SingleOrDefaultAsync(m => m.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(m => m.Name)
.Select(m => new NameIdItem()
{
Id = m.Id,
Name = m.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(x => x.TranslationId == UserTranslationId && param.Contains(x.Key)).ToDictionaryAsync(x => x.Key, x => x.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
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(x => x.TranslationId == translationId && param.Contains(x.Key)).AsNoTracking().ToDictionaryAsync(x => x.Key, x => x.Display);
return ret;
}
//Get the CJKIndex value for the translation specified
internal static async Task<bool> GetCJKIndexAsync(long translationId, AyContext ct = null)
{
if (ct == null)
ct = ServiceProviderProvider.DBContext;
var ret = await ct.Translation.Where(x => x.Id == translationId).AsNoTracking().Select(m => m.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
AyContext ct = ServiceProviderProvider.DBContext;
return await ct.TranslationItem.Where(m => m.TranslationId == ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID && m.Key == key).Select(m => m.Display).AsNoTracking().FirstOrDefaultAsync();
}
//Get all stock keys that are valid (used for import)
internal static async Task<List<string>> GetKeyListAsync()
{
AyContext ct = ServiceProviderProvider.DBContext;
return await ct.TranslationItem.Where(m => m.TranslationId == 1).OrderBy(m => m.Key).Select(m => m.Key).AsNoTracking().ToListAsync();
}
////////////////////////////////////////////////////////////////////////////////////////////////
//UPDATE
//
internal async Task<bool> PutTranslationItemDisplayTextAsync(TranslationItem dbObj, NewTextIdConcurrencyTokenItem inObj, Translation dbParent)
{
if (dbParent.Stock == true)
{
AddError(ApiErrorCode.INVALID_OPERATION, "object", "TranslationItem is from a Stock translation and cannot be modified");
return false;
}
//Replace the db object with the PUT object
//CopyObject.Copy(inObj, dbObj, "Id");
dbObj.Display = inObj.NewText;
//Set "original" value of concurrency token to input token
//this will allow EF to check it out
ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = inObj.ConcurrencyToken;
//Only thing to validate is if it has data at all in it
if (string.IsNullOrWhiteSpace(inObj.NewText))
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Display (NewText)");
if (HasErrors)
return false;
await ct.SaveChangesAsync();
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbParent.Id, AyaType.Translation, AyaEvent.Modified), ct);
return true;
}
internal async Task<bool> PutTranslationNameAsync(Translation dbObj, NewTextIdConcurrencyTokenItem inObj)
{
if (dbObj.Stock == true)
{
AddError(ApiErrorCode.INVALID_OPERATION, "object", "Translation is a Stock translation and cannot be modified");
return false;
}
dbObj.Name = inObj.NewText;
//Set "original" value of concurrency token to input token
//this will allow EF to check it out
ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = inObj.ConcurrencyToken;
await ValidateAsync(dbObj.Name, false);
if (HasErrors)
return false;
await ct.SaveChangesAsync();
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, AyaType.Translation, AyaEvent.Modified), ct);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//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(string inObjName, bool isNew)
{
//run validation and biz rules
//Name required
if (string.IsNullOrWhiteSpace(inObjName))
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name");
//Name must be less than 255 characters
if (inObjName.Length > 255)
AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "Name", "255 char max");
//Name must be unique
if (await ct.Translation.AnyAsync(m => m.Name == inObjName))
AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name");
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(c => c.Name == translationName);
if (v == null) return 0;
return v.Id;
}
public static async Task<long> TranslationNameToIdStaticAsync(string translationName, AyContext ct = null)
{
if (ct == null)
{
ct = ServiceProviderProvider.DBContext;
}
var v = await ct.Translation.AsNoTracking().FirstOrDefaultAsync(c => c.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(c => c.Name == translationName);
}
public async Task<bool> TranslationExistsAsync(long id)
{
return await ct.Translation.AnyAsync(e => e.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(e => e.Id == id))
return ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID;
return id;
}
public async Task<bool> TranslationItemExistsAsync(long id)
{
return await ct.TranslationItem.AnyAsync(e => e.Id == id);
}
/// <summary>
/// Used by import, translate the old v7 translation key name into the new shorter version
/// </summary>
/// <param name="oldKey"></param>
/// <returns></returns>
public string Translatev7TranslationKey(string oldKey)
{
string s = oldKey.Replace(".Label.", ".", StringComparison.InvariantCultureIgnoreCase);
if (s.StartsWith("O.", StringComparison.InvariantCultureIgnoreCase))
s = s.Replace("O.", "", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace(".ToolBar.", ".", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace(".Go.", ".", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace(".Command.", ".", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace(".Error.", ".", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace(".Object.", ".", StringComparison.InvariantCultureIgnoreCase);
if (s.StartsWith("UI.", StringComparison.InvariantCultureIgnoreCase))
s = s.Replace("UI.", "", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace(".", "", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("AddressAddress", "Address", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ContactPhoneContactPhone", "ContactPhone", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ContactPhonePhone", "ContactPhone", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("PurchaseOrderPurchaseOrder", "PurchaseOrder", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("WorkorderItemMiscExpenseExpense", "WorkorderItemMiscExpense", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("WorkorderItemTravelTravel", "WorkorderItemTravel", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("DashboardDashboard", "Dashboard", StringComparison.InvariantCultureIgnoreCase);
//ScheduleMarkers -> Reminder
s = s.Replace("ScheduleMarker", "Reminder", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerARGB", "ReminderARGB", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerColor", "ReminderColor", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerCompleted", "ReminderCompleted", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerEventCreated", "ReminderEventCreated", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerEventPendingAlert", "ReminderEventPendingAlert", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerFollowUp", "ReminderFollowUp", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerList", "ReminderList", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerName", "ReminderName", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerNotes", "ReminderNotes", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerRecurrence", "ReminderRecurrence", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerScheduleMarkerSourceType", "ReminderSourceType", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerSourceID", "ReminderSourceID", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerStartDate", "ReminderStartDate", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleMarkerStopDate", "ReminderStopDate", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleEditScheduleMarker", "ScheduleEditReminder", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("ScheduleNewScheduleMarker", "ScheduleNewReminder", StringComparison.InvariantCultureIgnoreCase);
//Custom fields were 0 to 9, now 1 to 16
s = s.Replace("Custom9", "Custom10", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("Custom8", "Custom9", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("Custom7", "Custom8", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("Custom6", "Custom7", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("Custom5", "Custom6", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("Custom4", "Custom5", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("Custom3", "Custom4", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("Custom2", "Custom3", StringComparison.InvariantCultureIgnoreCase);
if (!s.EndsWith("Custom10"))
s = s.Replace("Custom1", "Custom2", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("Custom0", "Custom1", StringComparison.InvariantCultureIgnoreCase);
//separate code will handle adding the new keys that didn't exist in v7 (custom 11 - 16)
//CommonActive CommonID etc remove Common
s = s.Replace("Common", "");
//Misc
s = s.Replace("FormFieldDataType", "UiFieldDataType", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("UserUserType", "UserType", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("UserTypesUtilityNotification", "UserTypesUtility", StringComparison.InvariantCultureIgnoreCase);
//Localized -> Translation
s = s.Replace("LocaleCustomizeText", "TranslationCustomizeText", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocaleExport", "TranslationExport", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocaleImport", "TranslationImport", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocaleList", "TranslationList", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocaleLocaleFile", "TranslationFile", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocaleUIDestLocale", "TranslationDest", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocaleUISourceLocale", "TranslationSource", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocaleWarnLocaleLocked", "TranslationWarnLocked", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocalizedTextDisplayText", "TranslationDisplayText", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocalizedTextDisplayTextCustom", "TranslationDisplayTextCustom", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocalizedTextKey", "TranslationKey", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocalizedTextLocale", "Translation", StringComparison.InvariantCultureIgnoreCase);
s = s.Replace("LocalizedText", "TranslatedText", StringComparison.InvariantCultureIgnoreCase);
//FUTURE
// s = s.Replace("WASXXX", "NOWXXXX", StringComparison.InvariantCultureIgnoreCase);
// s = s.Replace("WASXXX", "NOWXXXX", StringComparison.InvariantCultureIgnoreCase);
// s = s.Replace("WASXXX", "NOWXXXX", StringComparison.InvariantCultureIgnoreCase);
// s = s.Replace("WASXXX", "NOWXXXX", StringComparison.InvariantCultureIgnoreCase);
// s = s.Replace("WASXXX", "NOWXXXX", StringComparison.InvariantCultureIgnoreCase);
// s = s.Replace("WASXXX", "NOWXXXX", StringComparison.InvariantCultureIgnoreCase);
// s = s.Replace("WASXXX", "NOWXXXX", StringComparison.InvariantCultureIgnoreCase);
// s = s.Replace("WASXXX", "NOWXXXX", StringComparison.InvariantCultureIgnoreCase);
// s = s.Replace("WASXXX", "NOWXXXX", StringComparison.InvariantCultureIgnoreCase);
return s;
}
/// <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);
}
/////////////////////////////////////////////////////////////////////
/// IMPORT v7 implementation
public async Task<bool> ImportV7Async(
JObject j, List<ImportAyaNova7MapItem> importMap,
Guid jobId, Dictionary<string, Dictionary<Guid, string>> tagLists)
{
//some types need to import from more than one source hence the seemingly redundant switch statement for futureproofing
switch (j["IMPORT_TASK"].Value<string>())
{
case "main":
{
//Get source translation name from filename using regex
var SourceTranslationFileName = j["V7_SOURCE_FILE_NAME"].Value<string>();
Regex RxExtractTranslationName = new Regex(@"translation\.(.*)\.json");
var v = RxExtractTranslationName.Match(SourceTranslationFileName);
var SourceTranslationName = v.Groups[1].ToString();
//Ensure doesn't already exist
if (await TranslationExistsAsync(SourceTranslationName))
{
//If there are any validation errors, log in joblog and move on
await JobsBiz.LogJobAsync(jobId, $"TranslationBiz::ImportV7Async -> - Translation \"{SourceTranslationName}\" already exists in database, can not import over an existing translation", ct);
return false;
}
//keys to skip importing
List<string> SkipKeys = new List<string>();
SkipKeys.Add("UI.Label.CurrentUserName");
SkipKeys.Add("V7_SOURCE_FILE_NAME");
SkipKeys.Add("V7_TYPE");
SkipKeys.Add("IMPORT_TASK");
List<string> ValidKeys = await GetKeyListAsync();
Dictionary<string, string> NewTranslationDict = new Dictionary<string, string>();
foreach (var Pair in j.Children())
{
var V7Value = Pair.First.Value<string>().Replace(" && ", " ").Replace(" & ", " ").Replace("&", "");//clean out LT values that had double ampersands for old Windows menu shortcut scheme
var V7KeyName = ((JProperty)Pair).Name;
if (!SkipKeys.Contains(V7KeyName))
{
var RavenKeyName = Translatev7TranslationKey(V7KeyName);
if (!ValidKeys.Contains(RavenKeyName))
{
throw new System.ArgumentOutOfRangeException($"TranslationBiz::ImportV7 - old Key \"{V7KeyName}\" translates to new Key \"{RavenKeyName}\" which is not valid!");
}
if (!NewTranslationDict.ContainsKey(RavenKeyName))
{
NewTranslationDict.Add(RavenKeyName, V7Value);
}
else
{
//Use the shortest V7Value string in the case of dupes
if (NewTranslationDict[RavenKeyName].Length > V7Value.Length)
{
NewTranslationDict[RavenKeyName] = V7Value;
}
}
}
}
//Now add keys that were added after v7 for RAVEN using default translation values
foreach (string s in ValidKeys)
{
if (!NewTranslationDict.ContainsKey(s))
{
NewTranslationDict.Add(s, await GetDefaultTranslationAsync(s));
}
}
//Validate it's the correct number of keys expected
if (NewTranslationDict.Count != ValidKeys.Count)
{
throw new System.ArgumentOutOfRangeException($"TranslationBiz::ImportV7 - Import translation \"{SourceTranslationName}\" has an unexpected number of keys: {NewTranslationDict.Count}, expected {ValidKeys.Count} ");
}
//have file name, have all localized text
Translation l = new Translation();
l.Name = SourceTranslationName;
l.Stock = false;
foreach (KeyValuePair<string, string> K in NewTranslationDict)
{
l.TranslationItems.Add(new TranslationItem() { Key = K.Key, Display = K.Value });
}
await ct.Translation.AddAsync(l);
await ct.SaveChangesAsync();
//Log now that we have the Id, note that there is no source created / modified for this so just attributing to current userId
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, l.Id, AyaType.Translation, AyaEvent.Created), ct);
}
break;
}
//this is the equivalent of returning void for a Task signature with nothing to return
return true;
}
/////////////////////////////////////////////////////////////////////
}//eoc
//
}//eons