// Copyright (c) 2023-present schick Informatik // Description: Container class for self-learning, volatile information that // is added by parsing Excel sheets. Here common wrong/misspelled data fields // are mapped to valid ones. using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace bsmd.database { public class ValueMapping : DatabaseEntityAsync { #region enums public enum MappingType { UNKNOWN, COUNTRY, GENDER, DOCUMENT_TYPE, LOCODE, INVALID }; #endregion #region Fields private static readonly Dictionary> _dicts = new Dictionary>(); #endregion #region Properties public static Dictionary> Dicts { get { return _dicts; } } public MappingType EntryType { get; private set; } public string Key { get; private set; } public string Value { get; private set; } public DateTime? Created { get; private set; } public DateTime? Changed { get; private set; } #endregion #region public funcs /// /// creates and saves a new entry and adds it to the internal dictionaries /// /// true if entry was actually created, false if already present public static async Task Create(MappingType type, string key, string value) { if (!_dicts.ContainsKey(type)) _dicts[type] = new Dictionary(); if (!_dicts[type].ContainsKey(key)) { ValueMapping vm = new ValueMapping() { EntryType = type, Key = key, Value = value }; _dicts[type][key] = vm; return await DBManagerAsync.SaveAsync(vm) == 1; } return false; } /// /// deletes an entry and removes it from the database /// /// true if successful, false if value was not found public static async Task Delete(MappingType type, string key) { if (!_dicts.ContainsKey(type)) return false; if (!_dicts[type].ContainsKey(key)) return false; ValueMapping vm = _dicts[type][key]; _dicts[type].Remove(key); return await DBManagerAsync.DeleteAsync(vm) == 1; } /// /// updates an existing value and saves it /// /// true if successful public async Task Update(string newValue) { this.Value = newValue; return await DBManagerAsync.SaveAsync(this) == 1; } #endregion #region DatabaseEntity implementation public override void PrepareSave(System.Data.IDbCommand cmd) { SqlCommand scmd = cmd as SqlCommand; scmd.Parameters.AddWithValue("@TYPE", this.EntryType); scmd.Parameters.AddWithNullableValue("@KEY", this.Key); scmd.Parameters.AddWithNullableValue("@VALUE", this.Value); if(this.IsNew) { this.CreateId(); scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = $"INSERT INTO {Tablename} (Id, EntryType, MappingKey, MappingValue) VALUES (@ID, @TYPE, @KEY, @VALUE)"; } else { scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = $"UPDATE {Tablename} SET EntryType = @TYPE, MappingKey = @KEY, MappingValue = @VALUE WHERE Id = @ID"; } } public override void PrepareLoadCommand(System.Data.IDbCommand cmd, Message.LoadFilter filter, params object[] criteria) { string query = $"SELECT Id, EntryType, MappingKey, MappingValue, Created, Changed FROM {Tablename}"; switch (filter) { case Message.LoadFilter.BY_TYPE: query += " WHERE EntryType = @TYPE"; ((SqlCommand)cmd).Parameters.AddWithValue("@TYPE", criteria[0]); break; default: break; } } protected override DatabaseEntityAsync ReadRowFromReader(System.Data.IDataReader reader) { ValueMapping vm = null; if (reader != null) { vm = new ValueMapping(); vm.id = reader.GetGuid(0); if (!reader.IsDBNull(1)) vm.EntryType = (ValueMapping.MappingType)reader.GetByte(1); if (!reader.IsDBNull(2)) vm.Key = reader.GetString(2); if (!reader.IsDBNull(3)) vm.Value = reader.GetString(3); if (!reader.IsDBNull(4)) vm.Created = reader.GetDateTime(4); if (!reader.IsDBNull(5)) vm.Changed = reader.GetDateTime(5); } return vm; } #endregion } }