Files
BusinessMgr-2019/SaBusMgrStandardDbas/Dbas/Mgr/SbmAccountMgr_Dbas-SystemAccount.cs
T

617 lines
30 KiB
C#

using Sarand.BusinessMgr.Net.Standard.Base.App;
using Sarand.BusinessMgr.Net.Standard.Base.Bus;
using Sarand.BusinessMgr.Net.Standard.Dbas.Bus;
using Sarand.BusinessMgr.Net.Standard.Dbas.Internal;
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Dbas;
using Sarand.Net.Standard.Base.Utils;
using Sarand.Net.Standard.Dbas.Data;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
namespace Sarand.BusinessMgr.Net.Standard.Dbas.Mgr
{
//=======================================================================
// Account manager - System Accounts.
//=======================================================================
partial class SbmAccountMgr_Dbas
{
//-------------------------------------------------------------------
// Utilities.
private List<StringPair> GetLogProperties(SbmSystemAccount systemAccount)
{
return new List<StringPair>()
{
new StringPair("Id",SysId.ToString(systemAccount.Id)),
new StringPair("Status",systemAccount.StatusCode)
};
}
//-------------------------------------------------------------------
// Associated data.
public override bool SystemAccountHasUsers(SysId systemAccountId)
{
if (!SysId.IsValid(systemAccountId)) return false;
String table = SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccountUser);
if (Database.TableExists(table))
{
String column = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId);
List<String> filters = new List<String>();
filters.Add($"{SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId)}={SysId.ToLong(systemAccountId)}");
return Database.GetRowCount(table, filters.Concat(" AND ", "(", ")")) > 0;
}
return false;
}
//-------------------------------------------------------------------
// Create Item.
public override SbmSystemAccount CreateSystemAccount(Sbm.SystemAccountType systemAccountType, SysId interfaceId, SysId assocId)
{
ClearErrorState();
SbmSystemAccount systemAccount = null;
if (SysId.IsValid(AffiliateId_Owner))
{
if (systemAccountType > 0)
systemAccount = new SbmSystemAccount_Dbas(new SysId(), AffiliateId_Owner, systemAccountType, interfaceId, assocId, 0);
else SetErrorState("Invalid account type");
}
else SetErrorState("Owner not selected");
return systemAccount;
}
private SbmSystemAccount_Dbas CreateSystemAccount(SystemAccount_Db dbSystemAccount)
{
ClearErrorState();
SbmSystemAccount_Dbas systemAccount = new SbmSystemAccount_Dbas(new SysId(dbSystemAccount._Id), new SysId(dbSystemAccount._AffIdOwner), (Sbm.SystemAccountType)dbSystemAccount._Type, new SysId(dbSystemAccount._InterfaceId), new SysId(dbSystemAccount._AssocId), (ushort)dbSystemAccount._Flags);
PopulateFrom(dbSystemAccount, systemAccount);
return systemAccount;
}
//-------------------------------------------------------------------
// Get SystemAccount.
public override SbmSystemAccount GetSystemAccount(SbmSystemAccount.Params prm)
{
return GetSystemAccount(GetSystemAccountQueryFilter(prm).Concat(" AND ", "(", ")"));
}
private SbmSystemAccount GetSystemAccount(String filter)
{
ClearErrorState();
long count = GetSystemAccountCount(filter);
if (count == 0) return null;
if (count != 1)
{
SetErrorState(ErrorMessage.MultipleItemsFound);
return null;
}
SbmSystemAccount systemAccount = null;
// Query data.
using (var dbConnection = Database.CreateConnection())
using (var dbCommand = dbConnection.CreateCommand())
{
try
{
dbConnection.Open();
SystemAccount_Db dbSystemAccount = new SystemAccount_Db(DbInfo);
dbSystemAccount.PopulateFrom(dbCommand, aliasSystemAccount, filter);
systemAccount = CreateSystemAccount(dbSystemAccount);
}
catch (Exception exc)
{
SetErrorState(ErrorMessage.DatabaseQueryException);
SaLogger.Warn($"{SysUtil.Fn}\n\tQuery: {dbCommand.CommandText}\n\t{ExceptionUtil.GetErrorStateMessage(exc)}");
}
}
return systemAccount;
}
//-------------------------------------------------------------------
// Validate SystemAccount.
public override bool IsValidSystemAccountData(SbmSystemAccount.Data data, SbmSystemAccount.ErrField errField)
{
ClearErrorState();
if (!base.IsValidSystemAccountData(data, errField)) return false;
return true;
}
//-------------------------------------------------------------------
// Insert SystemAccount.
public override bool InsertSystemAccount(SbmSystemAccount systemAccount, DbUpdateInfo dbUpdateInfo)
{
ClearErrorState();
if (!IsValidSystemAccount(systemAccount)) return false;
if (SysId.IsValid(systemAccount.Id)) return SetErrorState(ErrorMessage.ValidItemId);
using (var dbConnection = Database.CreateConnection())
using (var dbCommand = dbConnection.CreateCommand())
{
try
{
dbConnection.Open();
SystemAccount_Db dbSystemAccount = new SystemAccount_Db(DbInfo);
PopulateTo(dbSystemAccount, systemAccount, dbUpdateInfo);
if (SystemAccount_Db.FieldsNonId.Contains("Id"))
{
// Generate next Id.
dbSystemAccount._Id = SysId.ToInt(Database.GetNextId("SystemAccount", dbUpdateInfo));
}
dbSystemAccount.ExecuteInsertCommand(dbCommand);
systemAccount.Id = new SysId(dbSystemAccount._Id);
systemAccount.Changed = false;
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Inserted. {GetLogProperties(systemAccount).Concat(": ").Concat("", "\n\t")}");
}
catch (Exception exc) { SetErrorState(exc.Message); }
}
return OK;
}
//-------------------------------------------------------------------
// Update SystemAccount.
public override bool UpdateSystemAccount(SbmSystemAccount systemAccount, DbUpdateInfo dbUpdateInfo)
{
ClearErrorState();
if (!IsValidSystemAccount(systemAccount)) return false;
if (!SysId.IsValid(systemAccount.Id)) return SetErrorState(ErrorMessage.InvalidItemId);
if (!systemAccount.Changed)
{
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Data not changed. {GetLogProperties(systemAccount).Concat(": ").Concat("", "\n\t")}");
return true;
}
using (var dbConnection = Database.CreateConnection())
using (var dbCommand = dbConnection.CreateCommand())
{
try
{
// Retrieve SystemAccount.
dbConnection.Open();
SystemAccount_Db dbSystemAccount = new SystemAccount_Db(DbInfo);
dbSystemAccount.PopulateFrom(dbCommand, aliasSystemAccount, "Id=" + SysId.ToString(systemAccount.Id));
//SbmUpdateLogItemMonitor_SystemAccount updateLogMonitor = new SbmUpdateLogItemMonitor_SystemAccount(SbmGlobals.SessionProfile.SessionContext, CreateSystemAccount(dbSystemAccount), dbUpdateInfo);
// Update properties.
PopulateTo(dbSystemAccount, systemAccount, dbUpdateInfo);
dbSystemAccount.ExecuteUpdateCommand(dbCommand);
systemAccount.Changed = false;
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Updated. {GetLogProperties(systemAccount).Concat(": ").Concat("", "\n\t")}");
// Record updates.
/*updateLogMonitor.RecordUpdatedValues(systemAccount);
updateLogMonitor.LogUpdates();*/
}
catch (Exception exc) { SetErrorState(exc.Message); }
}
return OK;
}
//-------------------------------------------------------------------
// Delete SystemAccount.
public override bool SystemAccountCanBeDeleted(SbmSystemAccount systemAccount)
{
ClearErrorState();
if (!base.SystemAccountCanBeDeleted(systemAccount)) return false;
return true;
}
public override bool DeleteSystemAccount(SbmSystemAccount systemAccount, DbUpdateInfo dbUpdateInfo)
{
ClearErrorState();
if (!SystemAccountCanBeDeleted(systemAccount)) return false;
using (var dbConnection = Database.CreateConnection())
using (var dbCommand = dbConnection.CreateCommand())
{
try
{
// Retrieve SystemAccount.
dbConnection.Open();
SystemAccount_Db dbSystemAccount = new SystemAccount_Db(DbInfo);
dbSystemAccount.PopulateFrom(dbCommand, aliasSystemAccount, "Id=" + systemAccount.Id.ToString());
// Delete SystemAccount.
dbCommand.CommandType = System.Data.CommandType.Text;
dbCommand.CommandText = "DELETE FROM " + SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccount) + " WHERE Id=" + SysId.ToString(systemAccount.Id) + ";";
dbCommand.ExecuteNonQuery();
systemAccount.Changed = false;
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Deleted. {GetLogProperties(systemAccount).Concat(": ").Concat("", "\n\t")}");
}
catch (Exception exc) { SetErrorState(exc.Message); }
}
return OK;
}
//-------------------------------------------------------------------
// Copy data properties to and from database.
private void PopulateTo(SystemAccount_Db dbSystemAccount, SbmSystemAccount systemAccount, DbUpdateInfo dbUpdateInfo)
{
dbSystemAccount._Id = systemAccount.Id.ToInt();
dbSystemAccount._AffIdOwner = systemAccount.AffiliateIdOwner.ToInt();
dbSystemAccount._Type = (short)systemAccount.SystemAccountType;
dbSystemAccount._InterfaceId = systemAccount.InterfaceId.ToInt();
dbSystemAccount._AssocId = systemAccount.AssociationId.ToInt();
dbSystemAccount._Code = systemAccount.Code;
dbSystemAccount._Name = systemAccount.Name;
dbSystemAccount._AuthCodeEnc = systemAccount.AuthCodeEnc;
// Common properties.
dbSystemAccount._Notes = systemAccount.Notes;
dbSystemAccount._Status = SacsSystem.GetStatusCode(systemAccount.Status);
dbSystemAccount._Flags = (short)systemAccount.Flags;
if (!SysId.IsValid(systemAccount.Id))
{
dbSystemAccount._CreateOn = DateTime.UtcNow;
dbSystemAccount._CreateBy = dbUpdateInfo.UserId.ToInt();
dbSystemAccount._CreateHost = dbUpdateInfo.Host;
}
dbSystemAccount._UpdateOn = DateTime.UtcNow;
dbSystemAccount._UpdateBy = dbUpdateInfo.UserId.ToInt();
dbSystemAccount._UpdateHost = dbUpdateInfo.Host;
}
private void PopulateFrom(SystemAccount_Db dbSystemAccount, SbmSystemAccount systemAccount)
{
//systemAccount.Id = new SysId(dbSystemAccount._Id);
systemAccount.AffiliateIdOwner = new SysId(dbSystemAccount._AffIdOwner);
systemAccount.SystemAccountType = (Sbm.SystemAccountType)(dbSystemAccount._Type ?? 0);
systemAccount.InterfaceId = new SysId(dbSystemAccount._InterfaceId);
systemAccount.AssociationId = new SysId(dbSystemAccount._AssocId);
systemAccount.Code = dbSystemAccount._Code;
systemAccount.Name = dbSystemAccount._Name;
systemAccount.AuthCodeEnc = dbSystemAccount._AuthCodeEnc;
// Common properties.
systemAccount.Notes = dbSystemAccount._Notes;
systemAccount.Status = SacsSystem.GetStatusForCode(dbSystemAccount._Status);
systemAccount.SetCreateStamp(dbSystemAccount._CreateOn, new SysId(dbSystemAccount._CreateBy), dbSystemAccount._CreateHost);
systemAccount.SetUpdateStamp(dbSystemAccount._UpdateOn, new SysId(dbSystemAccount._UpdateBy), dbSystemAccount._UpdateHost);
systemAccount.AcceptChanges();
}
//-------------------------------------------------------------------
// Column definitions.
private static readonly String aliasSystemAccount = "aSysAcc";
private static readonly String aliasSystemAccount_SysUser = "SysAcc_SysUsr";
private static readonly String aliasSystemAccount_SysAccUser = aliasSystemAccount_SysUser + "_";
private static readonly String aliasSystemAccount_Association = "SysAcc_Ass";
private static readonly String prefixSystemAccount_Association = aliasSystemAccount_Association + "_";
private static readonly String aliasSystemAccount_Interface = "SysAcc_Int";
private static readonly String prefixSystemAccount_Interface = aliasSystemAccount_Interface + "_";
private static readonly String colnameSysAcc_UserCount = "SysAccUsrCount";
// Column definition population.
private void SetSystemAccountColDfn
(ColumnDfn col, String caption, int width, SbmDbDfn.ColumnIdx dbCol,
ColumnAlignment align = ColumnAlignment.Default, DataValue.DataType dataType = DataValue.DataType.String,
String alias = "", String prefix = "")
{
if (String.IsNullOrEmpty(alias) && SbmDbDfn.GetTableForColumn(dbCol) == SbmDbDfn.TableIdx.SystemAccount)
alias = aliasSystemAccount;
col.Set(caption, width, (int)dbCol, prefix + SbmDbDfn.GetColumnName(dbCol, false), SbmDbDfn.GetColumnNameQual(dbCol, alias), dataType, align);
if (dataType == DataValue.DataType.String && dbCol != SbmDbDfn.ColumnIdx.Unknown)
col.MaxLength = (int)Internals.GetColTextLen(dbCol);
}
//-------------------------------------------------------------------
// Getting SystemAccount column definition.
public override ColumnDfn GetSystemAccountColumnDfn(SbmSystemAccount.Column columnId, string caption = "", int width = 0)
{
ColumnDfn columnDfn = new ColumnDfn((int)columnId, caption, width);
switch (columnId)
{
case SbmSystemAccount.Column.Pos:
SetSystemAccountColDfn(columnDfn, "Pos", 45, SbmDbDfn.ColumnIdx.Unknown, ColumnAlignment.Right);
columnDfn.SortMode = ColumnDfn.ColumnSortMode.Off;
columnDfn.ReadOnly = true;
columnDfn.Resizable = false;
break;
case SbmSystemAccount.Column.Id:
SetSystemAccountColDfn(columnDfn, "Id", 60, SbmDbDfn.ColumnIdx.SystemAccount_Id, ColumnAlignment.Right, DataValue.DataType.Long);
columnDfn.ReadOnly = true;
break;
// SystemAccount Properties.
case SbmSystemAccount.Column.Code:
SetSystemAccountColDfn(columnDfn, "Code", 65, SbmDbDfn.ColumnIdx.SystemAccount_Code);
break;
case SbmSystemAccount.Column.Name:
SetSystemAccountColDfn(columnDfn, "Name", 160, SbmDbDfn.ColumnIdx.SystemAccount_Name);
break;
case SbmSystemAccount.Column.UserCount:
SetSystemAccountColDfn(columnDfn, "Users", 60, SbmDbDfn.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
columnDfn.DataName = colnameSysAcc_UserCount;
break;
case SbmSystemAccount.Column.Interface_Code:
SetSystemAccountColDfn(columnDfn, "Interface code", 80, SbmDbDfn.ColumnIdx.Interface_Code, ColumnAlignment.Center, DataValue.DataType.String, aliasSystemAccount_Interface, prefixSystemAccount_Interface);
break;
case SbmSystemAccount.Column.Interface_Caption:
SetSystemAccountColDfn(columnDfn, "Interface caption", 120, SbmDbDfn.ColumnIdx.Interface_Caption, ColumnAlignment.Left, DataValue.DataType.String, aliasSystemAccount_Interface, prefixSystemAccount_Interface);
break;
//-----------------------------------------------------------------
// Association details:
case SbmSystemAccount.Column.Assoc_Status_Desc:
SetSystemAccountColDfn(columnDfn, "Assoc Stat", 50, SbmDbDfn.ColumnIdx.Association_Status);
break;
case SbmSystemAccount.Column.Assoc_Status_Ind:
SetSystemAccountColDfn(columnDfn, "Assoc Stat", 70, SbmDbDfn.ColumnIdx.Association_Status, ColumnAlignment.Center, DataValue.DataType.TriState, aliasSystemAccount_Association, prefixSystemAccount_Association);
columnDfn.TriStateFormat = ColumnTriStateFormat.Lights;
break;
// Common properties.
case SbmSystemAccount.Column.Notes:
SetSystemAccountColDfn(columnDfn, "Notes", 200, SbmDbDfn.ColumnIdx.SystemAccount_Notes);
break;
case SbmSystemAccount.Column.Flag:
SetSystemAccountColDfn(columnDfn, "Flags", 40, SbmDbDfn.ColumnIdx.SystemAccount_Flags, ColumnAlignment.Center, DataValue.DataType.Long);
break;
case SbmSystemAccount.Column.Status:
SetSystemAccountColDfn(columnDfn, "Status", 50, SbmDbDfn.ColumnIdx.SystemAccount_Status, ColumnAlignment.Center);
break;
case SbmSystemAccount.Column.Status_Desc:
SetSystemAccountColDfn(columnDfn, "Status", 50, SbmDbDfn.ColumnIdx.SystemAccount_Status);
break;
case SbmSystemAccount.Column.Status_Ind:
SetSystemAccountColDfn(columnDfn, "Status", 50, SbmDbDfn.ColumnIdx.SystemAccount_Status, ColumnAlignment.Center, DataValue.DataType.TriState);
columnDfn.TriStateFormat = ColumnTriStateFormat.Lights;
break;
case SbmSystemAccount.Column.CreateOn_Date:
SetSystemAccountColDfn(columnDfn, "Created", 65, SbmDbDfn.ColumnIdx.SystemAccount_CreateOn, ColumnAlignment.Center, DataValue.DataType.Date);
columnDfn.DateFormat = "d-MMM-yy";
columnDfn.ReadOnly = true;
break;
case SbmSystemAccount.Column.UpdateOn_Date:
SetSystemAccountColDfn(columnDfn, "Updated", 65, SbmDbDfn.ColumnIdx.SystemAccount_UpdateOn, ColumnAlignment.Center, DataValue.DataType.Date);
columnDfn.DateFormat = "d-MMM-yy";
columnDfn.ReadOnly = true;
break;
}
if (!String.IsNullOrEmpty(caption))
columnDfn.Caption = caption;
if (width > 0)
columnDfn.Width = width;
return columnDfn;
}
//-------------------------------------------------------------------
// SystemAccount query filter.
public override List<string> GetSystemAccountQueryFilter(SbmSystemAccount.Params prm)
{
List<String> filter = new List<String>();
// SystemAccount Id.
String includedIds = prm.IdsIncluded.Concat(",");
String colSystemAccount_Id = SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Id, aliasSystemAccount);
if (!includedIds.IsEmpty())
filter.Add(colSystemAccount_Id + " IN (" + includedIds + ")");
String excludedIds = prm.IdsExcluded.Concat(",");
if (!excludedIds.IsEmpty())
filter.Add(colSystemAccount_Id + " NOT IN (" + excludedIds + ")");
//--------------------------------------------------------------- -
// Affiliate Owner.
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_AffIdOwner, aliasSystemAccount) + "=" + AffiliateId_Owner.ToString());
// Interface.
if (SysId.IsValid(prm.InterfaceId))
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_InterfaceId, aliasSystemAccount) + "=" + prm.InterfaceId.ToString());
// Association.
if (SysId.IsValid(prm.AssocId))
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_AssocId, aliasSystemAccount) + "=" + prm.AssocId.ToString());
//--------------------------------------------------------------- -
// Partial Code & Name.
if (!StringUtil.IsEmpty(prm.CodePartial))
filter.Add(DbQuery.FormatFn_Left(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Code, aliasSystemAccount), prm.CodePartial.Length) + "='" + DbQuery.FormatForCondition(prm.CodePartial) + "'");
if (!StringUtil.IsEmpty(prm.NamePartial))
filter.Add(DbQuery.FormatFn_Left(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Name, aliasSystemAccount), prm.NamePartial.Length) + "='" + DbQuery.FormatForCondition(prm.NamePartial) + "'");
//--------------------------------------------------------------- -
// Exclude Accounts that are already associated with the specified System User (by Interface & Affiliate):
if (SysId.IsValid(prm.AffiliateExcludeIdUserAccounts))
{
// Query SysUsr.
DbQuery qryUsr = new DbQuery(Database.DbmsType);
qryUsr.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemUser), aliasSystemAccount_SysUser);
qryUsr.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Id, aliasSystemAccount_SysUser));
qryUsr.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_AffiliateId, aliasSystemAccount_SysUser) + "=" + prm.AffiliateExcludeIdUserAccounts.ToString());
if (SysId.IsValid(prm.InterfaceId))
qryUsr.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_InterfaceId, aliasSystemAccount_SysUser) + "=" + prm.InterfaceId.ToString());
// Query SysAccUsr - accounts that are already associated with this user.
DbQuery qryAccUsr = new DbQuery(Database.DbmsType);
qryAccUsr.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccountUser), aliasSystemAccount_SysAccUser);
qryAccUsr.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId, aliasSystemAccount_SysAccUser));
qryAccUsr.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_UserId, aliasSystemAccount_SysAccUser) + " IN(" + qryUsr.BuildQuery() + ")");
// Exclude users that belong to any of these accounts.
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Id, aliasSystemAccount) + " NOT IN(" + qryAccUsr.BuildQuery() + ")");
}
if (filter.Count < 1 && prm.IncludeDefaultFilter) filter.Add("1=1");
return filter;
}
// SystemAccount sort columns.
public List<String> GetSystemAccountSortColumns(SbmSystemAccount.Params prm)
{
// Sort sequence.
List<String> sortColumnCodes = new List<String>();
foreach (var columnDfn in prm.SortColumns)
{
// Need to ensure correct alias used for SystemAccount columns in case of conflict.
String sortCol = String.Empty;
if (SbmDbDfn.GetTableForColumn((SbmDbDfn.ColumnIdx)columnDfn.DbColumn) == SbmDbDfn.TableIdx.SystemAccount)
{
sortCol = SbmDbDfn.GetColumnNameQual((SbmDbDfn.ColumnIdx)columnDfn.DbColumn, aliasSystemAccount);
}
else
{
sortCol = columnDfn.DataName;
if (!StringUtil.IsEmpty(columnDfn.SortDataName))
sortCol = columnDfn.SortDataName;
}
if (!sortCol.IsEmpty())
{
DbOrderByNullMode orderByNullMode = DbOrderByNullMode.Default; // eSaOrderByDemoteNulls, eSaOrderByPromoteNulls
sortColumnCodes.Add(DbQuery.FormatForOrderBy(sortCol, columnDfn.SortAscending, orderByNullMode).Concat(", "));
}
}
sortColumnCodes.RemoveBlanks();
if (sortColumnCodes.IsEmpty())
{
// Default sort columns:
sortColumnCodes.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Type, aliasSystemAccount) + " ASC");
}
return sortColumnCodes;
}
//-------------------------------------------------------------------
// SystemAccount count.
private long GetSystemAccountCount(String filter)
{
ClearErrorState();
return Database.GetRowCount($"{SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccount)} As {aliasSystemAccount}", filter);
}
public override long GetSystemAccountCount(SbmSystemAccount.Params prm)
{
ClearErrorState();
return GetSystemAccountCount(GetSystemAccountQueryFilter(prm).Concat(" AND ", "(", ")"));
}
//-------------------------------------------------------------------
// SystemAccount list.
public override List<SbmSystemAccount> GetSystemAccountList(SbmSystemAccount.Params prm)
{
ClearErrorState();
List<SbmSystemAccount> systemAccounts = new List<SbmSystemAccount>();
DbQuery qry = new DbQuery(Database.DbmsType);
qry.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccount), aliasSystemAccount);
SystemAccount_Db dbSystemAccount = new SystemAccount_Db(DbInfo);
qry.Columns.AddRange(dbSystemAccount.ExistingColumnNames);
qry.Filters.AddRange(GetSystemAccountQueryFilter(prm));
qry.OrderBy.AddRange(GetSystemAccountSortColumns(prm));
// Query data.
using (var dbConnection = Database.CreateConnection())
using (var dbCommand = dbConnection.CreateCommand())
{
try
{
dbConnection.Open();
dbCommand.CommandText = qry.BuildQuery(prm.ResultsPaging);
using (DbDataReader dbDataReader = dbCommand.ExecuteReader())
{
while (dbDataReader.Read())
{
dbSystemAccount = new SystemAccount_Db(DbInfo);
dbSystemAccount.PopulateFrom(dbDataReader);
systemAccounts.Add(CreateSystemAccount(dbSystemAccount));
}
}
}
catch (Exception exc)
{
SetErrorState(ErrorMessage.DatabaseQueryException);
SaLogger.Warn($"{SysUtil.Fn}\n\tQuery: {dbCommand.CommandText}\n\t{ExceptionUtil.GetErrorStateMessage(exc)}");
}
}
return systemAccounts;
}
//-------------------------------------------------------------------
// SystemAccount query execution.
public override DbResultSet SystemAccountExecuteQuery(List<ColumnDfn> columns, SbmSystemAccount.Params prm)
{
ClearErrorState();
//---------------------------------------------------------------------
// Construct build database query.
DbQuery query = new DbQuery(Database.DbmsType);
query.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccount), aliasSystemAccount);
//--------------------------------------------------------------- -
// SystemAccount Properties - add columns as defined in query list.
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Id, aliasSystemAccount));
if (columns.HasId(SbmSystemAccount.Column.Type_Desc))
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Type));
if (columns.HasId(SbmSystemAccount.Column.Code))
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Code, aliasSystemAccount));
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_InterfaceId, aliasSystemAccount));
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_AssocId, aliasSystemAccount));
if (columns.HasId(SbmSystemAccount.Column.Name))
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Name, aliasSystemAccount));
//--------------------------------------------------------------- -
// Interface.
bool hasInterfaceCols = false;
if (!hasInterfaceCols) hasInterfaceCols = columns.HasId(SbmSystemAccount.Column.Interface_Code);
if (!hasInterfaceCols) hasInterfaceCols = columns.HasId(SbmSystemAccount.Column.Interface_Caption);
if (hasInterfaceCols)
{
// Join to the interface table:
DbQueryJoin joinInt = new DbQueryJoin(DbJoinType.LeftOuter);
joinInt.Table = new StringPair(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Interface), aliasSystemAccount_Interface);
joinInt.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Interface_Id, aliasSystemAccount_Interface) + "=" + SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_InterfaceId, aliasSystemAccount));
query.Joins.Add(joinInt);
// Add the Interface columns.
List<SbmDbDfn.ColumnIdx> colInterface = new List<SbmDbDfn.ColumnIdx>();
colInterface.Add(SbmDbDfn.ColumnIdx.Interface_Code);
colInterface.Add(SbmDbDfn.ColumnIdx.Interface_Caption);
foreach (SbmDbDfn.ColumnIdx dbColumn in colInterface)
query.Columns.Add(SbmDbDfn.GetColumnNameQual(dbColumn, aliasSystemAccount_Interface), prefixSystemAccount_Interface + SbmDbDfn.GetColumnName(dbColumn, false));
}
//--------------------------------------------------------------- -
// Association.
bool hasAssociationCols = false;
if (!hasAssociationCols) hasAssociationCols = columns.HasId(SbmSystemAccount.Column.Assoc_Status_Ind);
if (hasAssociationCols)
{
// Join to the Assoc table:
DbQueryJoin joinAssoc = new DbQueryJoin(DbJoinType.LeftOuter);
joinAssoc.Table = new StringPair(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Association), aliasSystemAccount_Association);
joinAssoc.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_Id, aliasSystemAccount_Association) + "=" + SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_AssocId, aliasSystemAccount));
query.Joins.Add(joinAssoc);
// Add the Assoc columns.
List<SbmDbDfn.ColumnIdx> colAssocation = new List<SbmDbDfn.ColumnIdx>();
colAssocation.Add(SbmDbDfn.ColumnIdx.Association_Id);
colAssocation.Add(SbmDbDfn.ColumnIdx.Association_Status);
foreach (SbmDbDfn.ColumnIdx dbColumn in colAssocation)
query.Columns.Add(SbmDbDfn.GetColumnNameQual(dbColumn, aliasSystemAccount_Association), prefixSystemAccount_Association + SbmDbDfn.GetColumnName(dbColumn, false));
}
//--------------------------------------------------------------- -
// Associated User.
if (columns.HasId(SbmSystemAccount.Column.UserCount))
{
DbQuery qryUsers = new DbQuery(Database.DbmsType);
qryUsers.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccountUser));
qryUsers.Columns.Add("Count(" + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccountUser_UserId) + ")");
qryUsers.Filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId) + "=" + SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Id, aliasSystemAccount));
query.Columns.Add("(" + qryUsers.BuildQuery() + ")", colnameSysAcc_UserCount);
}
//--------------------------------------------------------------- -
// Common Properties
if (columns.HasId(SbmSystemAccount.Column.Notes))
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Notes, aliasSystemAccount));
if (columns.HasId(SbmSystemAccount.Column.Flag))
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Flags, aliasSystemAccount));
if (columns.HasId(SbmSystemAccount.Column.Status) || columns.HasId(SbmSystemAccount.Column.Status_Desc) || columns.HasId(SbmSystemAccount.Column.Status_Ind))
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Status, aliasSystemAccount));
if (columns.HasId(SbmSystemAccount.Column.CreateOn_Date))
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_CreateOn, aliasSystemAccount));
if (columns.HasId(SbmSystemAccount.Column.UpdateOn_Date))
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_UpdateOn, aliasSystemAccount));
//---------------------------------------------------------------------
// Populate filter.
query.Filters.AddRange(GetSystemAccountQueryFilter(prm));
if (prm.IdsIncluded.Count != 1)
{
// Configure sort sequence.
foreach (String sortColumn in GetSystemAccountSortColumns(prm))
query.OrderBy.Add(sortColumn);
}
//---------------------------------------------------------------------
//return new DbResultSet_DataTableReader(Database, query.BuildQuery());
return DbResultSet_DataReader.ExecuteQuery(Database, query.BuildQuery(prm.ResultsPaging));
}
//-------------------------------------------------------------------
}
//=======================================================================
}