using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Resources; using Kreta.Core; using Kreta.DataAccess.Interfaces; using Kreta.Framework; using Kreta.Framework.Util; using Kreta.Resources; using SDA.DataProvider; namespace Kreta.DataAccessManual.Util { public abstract class DataAccessBase { protected GridParameters GridParameters; protected DalHandler DalHelper { get; private set; } protected DataAccessBase(DalHandler handler) : this(handler, null) { } protected DataAccessBase(DalHandler handler, GridParameters gridParameters) { DalHelper = handler; GridParameters = gridParameters; } protected DataSet GetData(string commandText, List parameters = null, string dictionaryItemColumns = null, string booleanColumns = null) { DataTable table; using (SDACommand command = UserContext.Instance.SDAConnection.CreateCommand()) { command.Transaction = UserContext.Instance.SDATransaction; command.CommandType = CommandType.Text; command.CommandText = commandText; if (parameters != null) { foreach (var param in parameters) { if (param.Type != SDADBType.Binary) { command.Parameters.Add(param.Name, param.Type); command.Parameters[param.Name].Value = param.Value; } else { command.Parameters.Add(param.Name, param.Value); } } } if (GridParameters == null) { GridParameters = new GridParameters(); } table = DataUtil.GetData(command, GridParameters); } if (!string.IsNullOrWhiteSpace(dictionaryItemColumns)) { SetDNAME(table, dictionaryItemColumns); } if (!string.IsNullOrWhiteSpace(booleanColumns)) { SetBoolFields(table, booleanColumns); } return table.AsDataSet(); } protected DataSet GetReadOnlyData(string commandText, List parameters = null, string dictionaryItemColumns = null, string booleanColumns = null) { DataTable table; using (SDAConnection connection = DataUtil.GetReadOnlyConnection(UserContext.Instance.IntezmenyAzonosito)) { connection.Open(); using (SDACommand command = connection.CreateCommand()) { command.CommandType = CommandType.Text; command.CommandText = commandText; if (parameters != null) { foreach (var param in parameters) { if (param.Type != SDADBType.Binary) { command.Parameters.Add(param.Name, param.Type); command.Parameters[param.Name].Value = param.Value; } else { command.Parameters.Add(param.Name, param.Value); } } } if (GridParameters == null) { GridParameters = new GridParameters(); } table = DataUtil.GetData(command, GridParameters); } } if (!string.IsNullOrWhiteSpace(dictionaryItemColumns)) { SetDNAME(table, dictionaryItemColumns); } if (!string.IsNullOrWhiteSpace(booleanColumns)) { SetBoolFields(table, booleanColumns); } return table.AsDataSet(); } protected int CheckRowCount(string commandText, List parameters = null) { using (SDACommand command = UserContext.Instance.SDAConnection.CreateCommand()) { command.CommandType = CommandType.Text; command.CommandText = commandText; if (parameters != null) { foreach (var param in parameters) { if (param.Type != SDADBType.Binary) { command.Parameters.Add(param.Name, param.Type); command.Parameters[param.Name].Value = param.Value; } else { command.Parameters.Add(param.Name, param.Value); } } } return DataUtil.CheckRowCount(command); } } [Obsolete(@"A Kreta.Core.Extensions.SortingAndPaging-t kell használni model listákkal, mivel ezeket mostmár tudjuk sorbarendezni a GridParameters-el, így nincs szükség rá, hogy elmenjen a DataSet a Web-re ezért nincs is szükség itt sorrenezni őket!")] protected DataTable SortingAndPaging(DataTable dataTable, GridParameters gridParameteres) { return DataUtil.SortingAndPaging(dataTable, gridParameteres); } /// /// A vesszővel átadott oszlopokból csinál egy egy új oszlopot oszlopnév+'_DNAME' névvel amelybe betölti a kódtétel nevét /// /// Adatforrás amibe bele kell rakni a kódtételeket /// Oszlopok amelyeknél az ID helyett kódtétel neveket kell nyelvhelyesen betenni public static void SetDNAME(DataTable table, string columns) { DictionaryItemCacheUtil.TableColumnFromDictionaryCache(table, columns); } /// /// Az átadott adatforráson a _DNAME-hez hasonlóan egy _BOOL mezőt hoz létre és az átadott oszlopok /// T vagy F értékei alapján beállítja a true-t vagy false-ot /// /// /// Oszlopnevek vesszővel elválasztva "mezo1,mezo2,mezo3..." public static void SetBoolFields(DataTable srcTable, string fields) { var list = new ArrayList(fields.Split(',')); foreach (string column in list) { string boolColumnName = column + "_BOOL"; srcTable.Columns.Add(boolColumnName, typeof(bool)); string boolTextColumnName = column + "_BNAME"; srcTable.Columns.Add(boolTextColumnName, typeof(string)); foreach (DataRow myRow in srcTable.Rows) { //ha dbnull akkor nem allitunk be semmit, hagyjuk dbnull-on if (myRow[column] != DBNull.Value) { bool value = Convert.ToBoolean(myRow[column].ToString() == "T"); myRow[boolColumnName] = value; myRow[boolTextColumnName] = value ? CommonResource.Igen : CommonResource.Nem; } } } } /// /// Oszlopnevek lecserélése 'map' alapján a paraméterben kapott 'dataTable'-ben. /// /// Az átnevezendő oszlopokat tartalmazó DataTable. /// Az átnevezendő oszlopnév párok. public static void RenameDataTableColumns(DataTable dataTable, (string fromColumn, string toColumn)[] map) { foreach (var (fromColumn, toColumn) in map) { dataTable.Columns[fromColumn].ColumnName = toColumn; } } public static void SetTableRowsFromResourceFile(Type resourceFileType, DataTable processedTable, string columnNames) { var resourceManager = new ResourceManager(resourceFileType); var newColumnNamesList = new List(); var oldColumnNamesList = new List(); oldColumnNamesList.AddRange(columnNames.Split(",;".ToCharArray())); oldColumnNamesList.ForEach(name => { if (!processedTable.Columns.Contains(name)) { throw new ArgumentOutOfRangeException(name, ErrorResource.ColumnNamesParameterContainsColumnnameThatNotExistsInTheSpecifiedTable); } string newColumnName = name + "Value"; if (processedTable.Columns[newColumnName] == null) { newColumnNamesList.Add(newColumnName); processedTable.Columns.Add(newColumnName); } else { throw new Exception(ErrorResource.TheNewColumnNameExistsInDatatable); } }); foreach (DataRow dataRow in processedTable.Rows) { newColumnNamesList.ForEach(newColumnName => oldColumnNamesList.ForEach(oldColumnName => dataRow[newColumnName] = resourceManager.GetString(dataRow[oldColumnName].ToString()))); } oldColumnNamesList.ForEach(oldColumnName => processedTable.Columns.Remove(oldColumnName)); } public void FollowUp(T entity) where T : IEntity { var kovTanevId = GetKovetkezoTanevId(); var aktualisTanevId = GetAktivTanevId(); if (kovTanevId.IsEntityId()) { switch (entity) { case IDictionaryItemBase dictionaryItemBaseEntity: if (aktualisTanevId == dictionaryItemBaseEntity.TanevId) { DalHelper.AdatszotarDAL().FollowUpDictionaryItemBase(dictionaryItemBaseEntity.IntezmenyId, dictionaryItemBaseEntity.TanevId, kovTanevId.Value, dictionaryItemBaseEntity.ID); } break; case IDictionaryItemBaseNyelv dictionaryItemBaseNyelv: if (aktualisTanevId == dictionaryItemBaseNyelv.TanevId) { DalHelper.DictionaryItemBaseNyelvDal().FollowUpDictionaryItemBaseNyelv(dictionaryItemBaseNyelv.IntezmenyId, dictionaryItemBaseNyelv.TanevId, kovTanevId.Value, dictionaryItemBaseNyelv.ID); } break; case IOraTerv oraTervEntity: if (aktualisTanevId == oraTervEntity.TanevId) { DalHelper.OratervDal().FollowUpOraterv(oraTervEntity.IntezmenyId, oraTervEntity.TanevId, kovTanevId.Value, entity.ID); } break; case ITerem teremEntity: if (aktualisTanevId == teremEntity.TanevId) { DalHelper.Terem().FollowUpTerem(teremEntity.IntezmenyId, teremEntity.TanevId, kovTanevId.Value, entity.ID); } break; case IFeladatEllatasiHely feladatEllatasiHelyEntity: if (aktualisTanevId == feladatEllatasiHelyEntity.TanevId) { DalHelper.FeladatEllatasiHelyDal().FollowUpFeladatellatasiHely(feladatEllatasiHelyEntity.IntezmenyId, feladatEllatasiHelyEntity.TanevId, kovTanevId.Value, entity.ID); } break; case IIntezmenyAdatok intezmenyAdatokEntity: if (aktualisTanevId == intezmenyAdatokEntity.TanevId) { DalHelper.IntezmenyDal().FollowUpIntezmenyAdatok(intezmenyAdatokEntity.IntezmenyId, intezmenyAdatokEntity.TanevId, kovTanevId.Value); } break; case ITanterv tantervEntity: if (aktualisTanevId == tantervEntity.TanevId) { DalHelper.TantervDAL().FollowUpTanterv(tantervEntity.IntezmenyId, tantervEntity.TanevId, kovTanevId.Value, entity.ID); } break; case IMukodesiHely mukodesiHelyEntity: if (aktualisTanevId == mukodesiHelyEntity.TanevId) { DalHelper.MukodesiHelyDAL().FollowUpMukodesiHely(mukodesiHelyEntity.IntezmenyId, mukodesiHelyEntity.TanevId, kovTanevId.Value, entity.ID); } break; case IEszkoz eszkozEntity: if (aktualisTanevId == eszkozEntity.TanevId) { DalHelper.EszkozDal().FollowUpEszkoz(eszkozEntity.IntezmenyId, eszkozEntity.TanevId, kovTanevId.Value, eszkozEntity.ID); } break; case IOraTervTargy oraTervTargy: if (aktualisTanevId == oraTervTargy.TanevId) { DalHelper.OratervDal().FollowUpOratervTargy(oraTervTargy.IntezmenyId, oraTervTargy.TanevId, kovTanevId.Value, oraTervTargy.ID); } break; case IAlkalmazott alkalmazott: if (aktualisTanevId == alkalmazott.TanevId) { DalHelper.Alkalmazott().FollowUpAlkalmazott(alkalmazott.IntezmenyId, alkalmazott.TanevId, kovTanevId.Value, alkalmazott.ID); } break; case ITantargy tantargy: if (aktualisTanevId == tantargy.TanevId) { DalHelper.Tantargy().FollowUpTantargy(tantargy.IntezmenyId, tantargy.TanevId, kovTanevId.Value, tantargy.ID); } break; case IKKAMIVegzettseg kkAmiVegzettseg: if (aktualisTanevId == kkAmiVegzettseg.TanevId) { DalHelper.Alkalmazott().FollowUpAlkalmazottVegzettsegKK(kkAmiVegzettseg.IntezmenyId, kkAmiVegzettseg.TanevId, kovTanevId.Value, kkAmiVegzettseg.AlkalmazottId); } break; case IKKTanitoVezgettseg kkTanitoVezgettseg: if (aktualisTanevId == kkTanitoVezgettseg.TanevId) { DalHelper.Alkalmazott().FollowUpAlkalmazottVegzettsegKK(kkTanitoVezgettseg.IntezmenyId, kkTanitoVezgettseg.TanevId, kovTanevId.Value, kkTanitoVezgettseg.AlkalmazottId); } break; case ICsengetesiRend csengetesiRend: if (aktualisTanevId == csengetesiRend.TanevId) { DalHelper.CsengetesiRend().FollowUpCsengetesiRend(csengetesiRend.IntezmenyId, csengetesiRend.TanevId, kovTanevId.Value, csengetesiRend.ID); } break; case IVegzettseg vegzettseg: if (aktualisTanevId == vegzettseg.TanevId) { DalHelper.Alkalmazott().FollowUpAlkalmazottVegzettseg(vegzettseg.IntezmenyId, vegzettseg.TanevId, kovTanevId.Value, vegzettseg.ID); } break; case IKKGyogypedVegzettseg kkGyogypedVegzettseg: if (aktualisTanevId == kkGyogypedVegzettseg.TanevId) { DalHelper.Alkalmazott().FollowUpAlkalmazottVegzettsegKK(kkGyogypedVegzettseg.IntezmenyId, kkGyogypedVegzettseg.TanevId, kovTanevId.Value, kkGyogypedVegzettseg.AlkalmazottId); } break; case IKKTanarVegzettseg kkTanarVegzettseg: if (aktualisTanevId == kkTanarVegzettseg.TanevId) { DalHelper.Alkalmazott().FollowUpAlkalmazottVegzettsegKK(kkTanarVegzettseg.IntezmenyId, kkTanarVegzettseg.TanevId, kovTanevId.Value, kkTanarVegzettseg.AlkalmazottId); } break; case IPedagogusEletpalyamodell pedagogusEletpalyamodell: if (aktualisTanevId == pedagogusEletpalyamodell.TanevId) { DalHelper.Alkalmazott().FollowUpAlkalmazottPEP(pedagogusEletpalyamodell.IntezmenyId, pedagogusEletpalyamodell.TanevId, kovTanevId.Value, pedagogusEletpalyamodell.ID); } break; case ITovabbiMunkaugyiAdatok tovabbiMunkaugyiAdatok: if (aktualisTanevId == tovabbiMunkaugyiAdatok.TanevId) { DalHelper.Alkalmazott().FollowUpAlkalmazottTovabbiMunkaugyiAdatok(tovabbiMunkaugyiAdatok.IntezmenyId, tovabbiMunkaugyiAdatok.TanevId, kovTanevId.Value, tovabbiMunkaugyiAdatok.ID); } break; case IRendszerBeallitas rendszerBeallitas: if (aktualisTanevId == rendszerBeallitas.TanevId) { DalHelper.RendszerBeallitas().FollowupRendszerBeallitas(rendszerBeallitas.IntezmenyId, rendszerBeallitas.TanevId, kovTanevId.Value, rendszerBeallitas.ID); } break; case ITantargyNyelv tantargyNyelv: if (aktualisTanevId == tantargyNyelv.TanevId) { DalHelper.Tantargy().FollowUpTantargyNyelv(tantargyNyelv.IntezmenyId, tantargyNyelv.TanevId, kovTanevId.Value, tantargyNyelv.ID); } break; default: throw new ArgumentException("Ismeretlen típus!"); } } } private int? GetKovetkezoTanevId() { var dal = DalHelper.TanevDal(GridParameters); var ds = dal.GetKovetkezoTanev(); return ds != null && ds.Tables[0].Rows.Count > 0 ? Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]) : default; } private int GetAktivTanevId() => DalHelper.TanevDal().GetAktivTanevId(); } }