599 lines
30 KiB
C#
599 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
|
|
{
|
|
//=======================================================================
|
|
partial class SbmAccountMgr_Dbas
|
|
{
|
|
//*******************************************************************
|
|
// S y s t em A c c o u n t U s e r s
|
|
//*******************************************************************
|
|
// Utilities.
|
|
private List<StringPair> GetLogProperties(SbmSystemAccountUser systemAccountUser)
|
|
{
|
|
return new List<StringPair>()
|
|
{
|
|
new StringPair("Id",SysId.ToString(systemAccountUser.Id)),
|
|
new StringPair("Status",systemAccountUser.StatusCode)
|
|
};
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Create SystemAccountUser.
|
|
public override SbmSystemAccountUser CreateSystemAccountUser(SysId accountId, SysId userId)
|
|
{
|
|
ClearErrorState();
|
|
SbmSystemAccountUser systemAccountUser = new SbmSystemAccountUser_Dbas(Internals, accountId, userId, 0);
|
|
return systemAccountUser;
|
|
}
|
|
private SbmSystemAccountUser_Dbas CreateSystemAccountUser(SystemAccountUser_Db dbSystemAccountUser)
|
|
{
|
|
ClearErrorState();
|
|
SbmSystemAccountUser_Dbas systemAccountUser = new SbmSystemAccountUser_Dbas(Internals, new SysId(dbSystemAccountUser._AccountId), new SysId(dbSystemAccountUser._UserId), (ushort)dbSystemAccountUser._Flags);
|
|
PopulateFrom(dbSystemAccountUser, systemAccountUser);
|
|
return systemAccountUser;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get SystemAccountUser.
|
|
public override SbmSystemAccountUser GetSystemAccountUser(SbmSystemAccountUser.Params prm)
|
|
{
|
|
return GetSystemAccountUser(GetSystemAccountUserQueryFilter(prm).Concat(" AND ", "(", ")"));
|
|
}
|
|
private SbmSystemAccountUser GetSystemAccountUser(String filter)
|
|
{
|
|
ClearErrorState();
|
|
long count = GetSystemAccountUserCount(filter);
|
|
if (count == 0) return null;
|
|
if (count != 1)
|
|
{
|
|
SetErrorState("Multiple System Account Users found");
|
|
return null;
|
|
}
|
|
SbmSystemAccountUser SystemAccountUser = null;
|
|
// Query data.
|
|
using (var dbConnection = Database.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
SystemAccountUser_Db dbSystemAccountUser = new SystemAccountUser_Db(DbInfo);
|
|
dbSystemAccountUser.PopulateFrom(dbCommand, aliasSystemAccountUser, filter);
|
|
SystemAccountUser = CreateSystemAccountUser(dbSystemAccountUser);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SetErrorState("Database query exception");
|
|
SaLogger.Warn($"{SysUtil.Fn}\n\tQuery: {dbCommand.CommandText}\n\t{ExceptionUtil.GetErrorStateMessage(exc)}");
|
|
}
|
|
}
|
|
return SystemAccountUser;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Validate SystemAccountUser.
|
|
public override bool IsValidSystemAccountUserData(SbmSystemAccountUser.Data data, SbmSystemAccountUser.ErrField errField)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.IsValidSystemAccountUserData(data, errField)) return false;
|
|
return true;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Insert SystemAccountUser.
|
|
public override bool InsertSystemAccountUser(SbmSystemAccountUser systemAccountUser, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
ClearErrorState();
|
|
if (!IsValidSystemAccountUser(systemAccountUser)) return false;
|
|
if (SysId.IsValid(systemAccountUser.Id)) return SetErrorState(ErrorMessage.ValiditemId);
|
|
using (var dbConnection = Database.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
SystemAccountUser_Db dbSystemAccountUser = new SystemAccountUser_Db(DbInfo);
|
|
PopulateTo(dbSystemAccountUser, systemAccountUser, dbUpdateInfo);
|
|
if (SystemAccountUser_Db.FieldsNonId.Contains("Id"))
|
|
{
|
|
// Generate next Id.
|
|
dbSystemAccountUser._AccountId = SysId.ToInt(Database.GetNextId("SystemAccountUser", dbUpdateInfo));
|
|
}
|
|
dbSystemAccountUser.ExecuteInsertCommand(dbCommand);
|
|
systemAccountUser.Id = new SysId(dbSystemAccountUser._AccountId);
|
|
systemAccountUser.Changed = false;
|
|
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Inserted. {GetLogProperties(systemAccountUser).Concat(": ").Concat("", "\n\t")}");
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Update SystemAccountUser.
|
|
public override bool UpdateSystemAccountUser(SbmSystemAccountUser systemAccountUser, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
ClearErrorState();
|
|
if (!IsValidSystemAccountUser(systemAccountUser)) return false;
|
|
if (!SysId.IsValid(systemAccountUser.AccountId)) return SetErrorState(ErrorMessage.InvalidItemId);
|
|
if (!SysId.IsValid(systemAccountUser.UserId)) return SetErrorState(ErrorMessage.InvalidItemId);
|
|
if (!systemAccountUser.Changed)
|
|
{
|
|
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Data not changed. {GetLogProperties(systemAccountUser).Concat(": ").Concat("", "\n\t")}");
|
|
return true;
|
|
}
|
|
using (var dbConnection = Database.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve SystemAccountUser.
|
|
dbConnection.Open();
|
|
SystemAccountUser_Db dbSystemAccountUser = new SystemAccountUser_Db(DbInfo);
|
|
dbSystemAccountUser.PopulateFrom(dbCommand, aliasSystemAccountUser, "UserId=" + SysId.ToString(systemAccountUser.UserId) + "And AccountId=" + SysId.ToString(systemAccountUser.AccountId));
|
|
//SbmUpdateLogItemMonitor_SystemAccountUser updateLogMonitor = new SbmUpdateLogItemMonitor_SystemAccountUser(SbmGlobals.SessionProfile.SessionContext, CreateSystemAccountUser(dbSystemAccountUser), dbUpdateInfo);
|
|
|
|
// Update properties.
|
|
PopulateTo(dbSystemAccountUser, systemAccountUser, dbUpdateInfo);
|
|
dbSystemAccountUser.ExecuteUpdateCommand(dbCommand);
|
|
systemAccountUser.Changed = false;
|
|
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Updated. {GetLogProperties(systemAccountUser).Concat(": ").Concat("", "\n\t")}");
|
|
|
|
// Record updates.
|
|
/*updateLogMonitor.RecordUpdatedValues(systemAccountUser);
|
|
updateLogMonitor.LogUpdates();*/
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Delete SystemAccountUser.
|
|
public override bool SystemAccountUserCanBeDeleted(SbmSystemAccountUser systemAccountUser)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.SystemAccountUserCanBeDeleted(systemAccountUser)) return false;
|
|
return true;
|
|
}
|
|
public override bool DeleteSystemAccountUser(SbmSystemAccountUser systemAccountUser, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
ClearErrorState();
|
|
if (!SystemAccountUserCanBeDeleted(systemAccountUser)) return false;
|
|
using (var dbConnection = Database.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve SystemAccountUser.
|
|
dbConnection.Open();
|
|
SystemAccountUser_Db dbSystemAccountUser = new SystemAccountUser_Db(DbInfo);
|
|
dbSystemAccountUser.PopulateFrom(dbCommand, aliasSystemAccountUser, "Id=" + systemAccountUser.Id.ToString());
|
|
// Delete SystemAccountUser.
|
|
dbCommand.CommandType = System.Data.CommandType.Text;
|
|
dbCommand.CommandText = "DELETE FROM " + SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccountUser) + " WHERE Id=" + SysId.ToString(systemAccountUser.Id) + ";";
|
|
dbCommand.ExecuteNonQuery();
|
|
systemAccountUser.Changed = false;
|
|
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Deleted. {GetLogProperties(systemAccountUser).Concat(": ").Concat("", "\n\t")}");
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Copy data properties to and from database.
|
|
private void PopulateTo(SystemAccountUser_Db dbSystemAccountUser, SbmSystemAccountUser systemAccountUser, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
dbSystemAccountUser._AccountId = systemAccountUser.AccountId.ToInt();
|
|
dbSystemAccountUser._UserId = systemAccountUser.UserId.ToInt();
|
|
// Common properties.
|
|
dbSystemAccountUser._Notes = systemAccountUser.Notes;
|
|
dbSystemAccountUser._Status = SacsSystem.GetStatusCode(systemAccountUser.Status);
|
|
dbSystemAccountUser._Flags = (short)systemAccountUser.Flags;
|
|
if (!SysId.IsValid(systemAccountUser.UserId) && !SysId.IsValid(systemAccountUser.AccountId))
|
|
{
|
|
dbSystemAccountUser._CreateOn = DateTime.UtcNow;
|
|
dbSystemAccountUser._CreateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbSystemAccountUser._CreateHost = dbUpdateInfo.Host;
|
|
}
|
|
dbSystemAccountUser._UpdateOn = DateTime.UtcNow;
|
|
dbSystemAccountUser._UpdateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbSystemAccountUser._UpdateHost = dbUpdateInfo.Host;
|
|
}
|
|
private void PopulateFrom(SystemAccountUser_Db dbSystemAccountUser, SbmSystemAccountUser systemAccountUser)
|
|
{
|
|
//systemAccountUser.Id = new SysId(dbSystemAccountUser._Id);
|
|
systemAccountUser.AccountId = new SysId(dbSystemAccountUser._AccountId);
|
|
systemAccountUser.UserId = new SysId(dbSystemAccountUser._UserId);
|
|
// Common properties.
|
|
systemAccountUser.Notes = dbSystemAccountUser._Notes;
|
|
systemAccountUser.Status = SacsSystem.GetStatusForCode(dbSystemAccountUser._Status);
|
|
systemAccountUser.SetCreateStamp(dbSystemAccountUser._CreateOn ?? default(DateTime), new SysId(dbSystemAccountUser._CreateBy ?? 0), dbSystemAccountUser._CreateHost);
|
|
systemAccountUser.SetUpdateStamp(dbSystemAccountUser._UpdateOn ?? default(DateTime), new SysId(dbSystemAccountUser._UpdateBy ?? 0), dbSystemAccountUser._UpdateHost);
|
|
systemAccountUser.AcceptChanges();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Column definitions.
|
|
private static readonly String aliasSystemAccountUser = "aSysAccUsr";
|
|
private static readonly String aliasSystemAccountUser_SystemAccount = "Sau_SysAcc";
|
|
private static readonly String prefixSystemAccountUser_SystemAccount = aliasSystemAccountUser_SystemAccount + "_";
|
|
private static readonly String aliasSystemAccountUser_SystemUser = "Sau_SysUsr";
|
|
private static readonly String prefixSystemAccountUser_SystemUser = aliasSystemAccountUser_SystemUser + "_";
|
|
private static readonly String aliasSystemAccountUser_Affiliate = "Sau_Aff";
|
|
private static readonly String prefixSystemAccountUser_Affiliate = aliasSystemAccountUser_Affiliate + "_";
|
|
|
|
// Column definition population.
|
|
private void SetSystemAccountUserColDfn
|
|
(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.SystemAccountUser)
|
|
alias = aliasSystemAccountUser;
|
|
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)SbmDbDfn.GetColTextLen(dbCol);
|
|
}
|
|
//-------------------------------------------------------------------
|
|
// Getting SystemAccountUser column definition.
|
|
public override ColumnDfn GetSystemAccountUserColumnDfn(SbmSystemAccountUser.Column columnId, string caption = "", int width = 0)
|
|
{
|
|
ColumnDfn columnDfn = new ColumnDfn((int)columnId, caption, width);
|
|
switch (columnId)
|
|
{
|
|
// SystemAccountUser Properties.
|
|
case SbmSystemAccountUser.Column.Pos:
|
|
SetSystemAccountUserColDfn(columnDfn, "Pos", 45, SbmDbDfn.ColumnIdx.Unknown, ColumnAlignment.Right);
|
|
columnDfn.SortMode = ColumnDfn.ColumnSortMode.Off;
|
|
columnDfn.ReadOnly = true;
|
|
columnDfn.Resizable = false;
|
|
break;
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Account Details.
|
|
case SbmSystemAccountUser.Column.AccountId:
|
|
SetSystemAccountUserColDfn(columnDfn, "Account Id", 60, SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId, ColumnAlignment.Right, DataValue.DataType.Long);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SbmSystemAccountUser.Column.Account_Code:
|
|
SetSystemAccountUserColDfn(columnDfn, "Account Code", 80, SbmDbDfn.ColumnIdx.SystemAccount_Code, ColumnAlignment.Default, DataValue.DataType.String, aliasSystemAccountUser_SystemAccount, prefixSystemAccountUser_SystemAccount);
|
|
break;
|
|
case SbmSystemAccountUser.Column.Account_Name:
|
|
SetSystemAccountUserColDfn(columnDfn, "Account Name", 160, SbmDbDfn.ColumnIdx.SystemAccount_Name, ColumnAlignment.Default, DataValue.DataType.String, aliasSystemAccountUser_SystemAccount, prefixSystemAccountUser_SystemAccount);
|
|
break;
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// User Details.
|
|
case SbmSystemAccountUser.Column.UserId:
|
|
SetSystemAccountUserColDfn(columnDfn, "User Id", 60, SbmDbDfn.ColumnIdx.SystemAccountUser_UserId, ColumnAlignment.Right, DataValue.DataType.Long);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SbmSystemAccountUser.Column.User_Code:
|
|
SetSystemAccountUserColDfn(columnDfn, "User Code", 120, SbmDbDfn.ColumnIdx.SystemUser_Code, ColumnAlignment.Default, DataValue.DataType.String, aliasSystemAccountUser_SystemUser, prefixSystemAccountUser_SystemUser);
|
|
break;
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Affiliate-Contact Details.
|
|
case SbmSystemAccountUser.Column.User_Contact_Email:
|
|
SetSystemAccountUserColDfn(columnDfn, "Email", 170, SbmDbDfn.ColumnIdx.Affiliate_Email, ColumnAlignment.Default, DataValue.DataType.String, aliasSystemAccountUser_Affiliate, prefixSystemAccountUser_Affiliate);
|
|
break;
|
|
case SbmSystemAccountUser.Column.User_Contact_Name:
|
|
SetSystemAccountUserColDfn(columnDfn, "Name", 170, SbmDbDfn.ColumnIdx.Affiliate_Name, ColumnAlignment.Default, DataValue.DataType.String, aliasSystemAccountUser_Affiliate, prefixSystemAccountUser_Affiliate);
|
|
break;
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Common properties.
|
|
case SbmSystemAccountUser.Column.Notes:
|
|
SetSystemAccountUserColDfn(columnDfn, "Notes", 200, SbmDbDfn.ColumnIdx.SystemAccountUser_Notes);
|
|
break;
|
|
case SbmSystemAccountUser.Column.Flag:
|
|
SetSystemAccountUserColDfn(columnDfn, "Flags", 40, SbmDbDfn.ColumnIdx.SystemAccountUser_Flags, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
break;
|
|
case SbmSystemAccountUser.Column.Status:
|
|
SetSystemAccountUserColDfn(columnDfn, "Status", 50, SbmDbDfn.ColumnIdx.SystemAccountUser_Status, ColumnAlignment.Center);
|
|
break;
|
|
case SbmSystemAccountUser.Column.Status_Desc:
|
|
SetSystemAccountUserColDfn(columnDfn, "Status", 50, SbmDbDfn.ColumnIdx.SystemAccountUser_Status);
|
|
break;
|
|
case SbmSystemAccountUser.Column.Status_Ind:
|
|
SetSystemAccountUserColDfn(columnDfn, "Status", 50, SbmDbDfn.ColumnIdx.SystemAccountUser_Status, ColumnAlignment.Center, DataValue.DataType.TriState);
|
|
columnDfn.TriStateFormat = ColumnTriStateFormat.Lights;
|
|
break;
|
|
case SbmSystemAccountUser.Column.CreateOn_Date:
|
|
SetSystemAccountUserColDfn(columnDfn, "Created", 65, SbmDbDfn.ColumnIdx.SystemAccountUser_CreateOn, ColumnAlignment.Center, DataValue.DataType.Date);
|
|
columnDfn.DateFormat = "d-MMM-yy";
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SbmSystemAccountUser.Column.UpdateOn_Date:
|
|
SetSystemAccountUserColDfn(columnDfn, "Updated", 65, SbmDbDfn.ColumnIdx.SystemAccountUser_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;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// SystemAccountUser query filter.
|
|
public override List<string> GetSystemAccountUserQueryFilter(SbmSystemAccountUser.Params prm)
|
|
{
|
|
List<String> filter = new List<String>();
|
|
|
|
// SystemAccountUser Id.
|
|
String includedIds = prm.IdsIncluded.Concat(",");
|
|
String colSystemAccountUser_Id = SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId, aliasSystemAccountUser);
|
|
if (!includedIds.IsEmpty())
|
|
filter.Add(colSystemAccountUser_Id + " IN (" + includedIds + ")");
|
|
String excludedIds = prm.IdsExcluded.Concat(",");
|
|
if (!excludedIds.IsEmpty())
|
|
filter.Add(colSystemAccountUser_Id + " NOT IN (" + excludedIds + ")");
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
// Interface Id.
|
|
if (SysId.IsValid(prm.InterfaceId) && !SysId.IsValid(prm.AssocId) && !SysId.IsValid(prm.AffiliateId))
|
|
{
|
|
DbQuery qryAccount = new DbQuery(Database.DbmsType);
|
|
qryAccount.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccount));
|
|
qryAccount.Columns.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccount_Id));
|
|
qryAccount.Filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccount_InterfaceId) + "=" + prm.InterfaceId.ToString());
|
|
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId, aliasSystemAccountUser) + " IN (" + qryAccount.BuildQuery() + ")");
|
|
}
|
|
|
|
// Account Id.
|
|
if (SysId.IsValid(prm.AccountId))
|
|
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId, aliasSystemAccountUser) + "=" + prm.AccountId.ToString());
|
|
|
|
// Association Id.
|
|
if (SysId.IsValid(prm.AssocId))
|
|
{
|
|
DbQuery qryAccount = new DbQuery(Database.DbmsType);
|
|
qryAccount.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccount));
|
|
qryAccount.Columns.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccount_Id));
|
|
qryAccount.Filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccount_AssocId) + "=" + prm.AssocId.ToString());
|
|
if (SysId.IsValid(prm.InterfaceId))
|
|
qryAccount.Filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccount_InterfaceId) + "=" + prm.InterfaceId.ToString());
|
|
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId, aliasSystemAccountUser) + " IN (" + qryAccount.BuildQuery() + ")");
|
|
}
|
|
|
|
// User Id.
|
|
if (SysId.IsValid(prm.UserId))
|
|
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_UserId, aliasSystemAccountUser) + "=" + prm.UserId.ToString());
|
|
|
|
// Affiliate Id.
|
|
if (SysId.IsValid(prm.AffiliateId))
|
|
{
|
|
DbQuery qryUser = new DbQuery(Database.DbmsType);
|
|
qryUser.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemUser));
|
|
qryUser.Columns.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemUser_Id));
|
|
qryUser.Filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemUser_AffiliateId) + "=" + prm.AffiliateId.ToString());
|
|
if (SysId.IsValid(prm.InterfaceId))
|
|
qryUser.Filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemUser_InterfaceId) + "=" + prm.InterfaceId.ToString());
|
|
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_UserId, aliasSystemAccountUser) + " IN (" + qryUser.BuildQuery() + ")");
|
|
}
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Status.
|
|
if (prm.RestrictToActive)
|
|
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_Status, aliasSystemAccountUser) + "='" + StatusActive + "'");
|
|
|
|
|
|
if (filter.Count < 1 && prm.IncludeDefaultFilter) filter.Add("1=1");
|
|
return filter;
|
|
}
|
|
|
|
// SystemAccountUser sort columns.
|
|
public List<String> GetSystemAccountUserSortColumns(SbmSystemAccountUser.Params prm)
|
|
{
|
|
// Sort sequence:
|
|
List<String> sortColumnCodes = new List<String>();
|
|
foreach (var columnDfn in prm.SortColumns)
|
|
{
|
|
// Need to ensure correct alias used for SystemAccountUser columns in case of conflict.
|
|
String sortCol = String.Empty;
|
|
if (SbmDbDfn.GetTableForColumn((SbmDbDfn.ColumnIdx)columnDfn.DbColumn) == SbmDbDfn.TableIdx.SystemAccountUser)
|
|
{
|
|
sortCol = SbmDbDfn.GetColumnNameQual((SbmDbDfn.ColumnIdx)columnDfn.DbColumn, aliasSystemAccountUser);
|
|
}
|
|
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.SystemAccountUser_UserId, aliasSystemAccountUser) + " ASC");
|
|
}
|
|
return sortColumnCodes;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// SystemAccountUser count.
|
|
private long GetSystemAccountUserCount(String filter)
|
|
{
|
|
ClearErrorState();
|
|
return Database.GetRowCount(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccountUser) + " As " + aliasSystemAccountUser, filter);
|
|
}
|
|
public override long GetSystemAccountUserCount(SbmSystemAccountUser.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
return GetSystemAccountUserCount(GetSystemAccountUserQueryFilter(prm).Concat(" AND ", "(", ")"));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// SystemAccountUser list.
|
|
public override List<SbmSystemAccountUser> GetSystemAccountUserList(SbmSystemAccountUser.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
List<SbmSystemAccountUser> systemAccountUsers = new List<SbmSystemAccountUser>();
|
|
DbQuery qry = new DbQuery(Database.DbmsType);
|
|
qry.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccountUser), aliasSystemAccountUser);
|
|
SystemAccountUser_Db dbSystemAccountUser = new SystemAccountUser_Db(DbInfo);
|
|
qry.Columns.AddRange(dbSystemAccountUser.ExistingColumnNames);
|
|
qry.Filters.AddRange(GetSystemAccountUserQueryFilter(prm));
|
|
qry.OrderBy.AddRange(GetSystemAccountUserSortColumns(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())
|
|
{
|
|
dbSystemAccountUser = new SystemAccountUser_Db(DbInfo);
|
|
dbSystemAccountUser.PopulateFrom(dbDataReader);
|
|
systemAccountUsers.Add(CreateSystemAccountUser(dbSystemAccountUser));
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SetErrorState("Database query exception");
|
|
SaLogger.Warn($"{SysUtil.Fn}\n\tQuery: {dbCommand.CommandText}\n\t{ExceptionUtil.GetErrorStateMessage(exc)}");
|
|
}
|
|
}
|
|
return systemAccountUsers;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// SystemAccountUser query execution.
|
|
public override DbResultSet SystemAccountUserExecuteQuery(List<ColumnDfn> columns, SbmSystemAccountUser.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
//---------------------------------------------------------------------
|
|
// Construct build database query.
|
|
DbQuery query = new DbQuery(Database.DbmsType);
|
|
query.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccountUser), aliasSystemAccountUser);
|
|
|
|
// SystemAccountUser Properties - add columns as defined in query list.
|
|
|
|
//--------------------------------------------------------------- -
|
|
// Account details.
|
|
if (columns.HasId((int)SbmSystemAccountUser.Column.AccountId))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId, aliasSystemAccountUser));
|
|
|
|
bool colRequired_Account_Code = (columns.HasId((int)SbmSystemAccountUser.Column.Account_Code));
|
|
bool colRequired_Account_Name = (columns.HasId((int)SbmSystemAccountUser.Column.Account_Name));
|
|
if (colRequired_Account_Code || colRequired_Account_Name)
|
|
{
|
|
// Join to the system Account table.
|
|
DbQueryJoin joinSysUsr = new DbQueryJoin(DbJoinType.LeftOuter);
|
|
joinSysUsr.Table = new StringPair(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccount), aliasSystemAccountUser_SystemAccount);
|
|
joinSysUsr.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Id, aliasSystemAccountUser_SystemAccount) + "=" + SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId, aliasSystemAccountUser));
|
|
query.Joins.Add(joinSysUsr);
|
|
|
|
// Add the Account columns to the query.
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Id, aliasSystemAccountUser_SystemAccount), prefixSystemAccountUser_SystemAccount + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccount_Id));
|
|
if (colRequired_Account_Code)
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Code, aliasSystemAccountUser_SystemAccount), prefixSystemAccountUser_SystemAccount + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccount_Code));
|
|
if (colRequired_Account_Name)
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Name, aliasSystemAccountUser_SystemAccount), prefixSystemAccountUser_SystemAccount + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccount_Name));
|
|
//bHasSysAccountTable = true;
|
|
}
|
|
|
|
//--------------------------------------------------------------- -
|
|
// User details.
|
|
if (columns.HasId((int)SbmSystemAccountUser.Column.UserId))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_UserId, aliasSystemAccountUser));
|
|
|
|
bool colRequired_User_Code = (columns.HasId((int)SbmSystemAccountUser.Column.User_Code));
|
|
bool colRequired_User_Email = (columns.HasId((int)SbmSystemAccountUser.Column.User_Contact_Email));
|
|
bool colRequired_User_Name = (columns.HasId((int)SbmSystemAccountUser.Column.User_Contact_Name));
|
|
if (colRequired_User_Code || colRequired_User_Email || colRequired_User_Name)
|
|
{
|
|
// Join to the system user table.
|
|
DbQueryJoin joinSysUsr = new DbQueryJoin(DbJoinType.LeftOuter);
|
|
joinSysUsr.Table = new StringPair(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemUser), aliasSystemAccountUser_SystemUser);
|
|
joinSysUsr.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Id, aliasSystemAccountUser_SystemUser) + "=" + SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_UserId, aliasSystemAccountUser));
|
|
query.Joins.Add(joinSysUsr);
|
|
|
|
// Add the user columns to the query.
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Id, aliasSystemAccountUser_SystemUser), prefixSystemAccountUser_SystemUser + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemUser_Id));
|
|
if (colRequired_User_Code)
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Code, aliasSystemAccountUser_SystemUser), prefixSystemAccountUser_SystemUser + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemUser_Code));
|
|
|
|
// Are any of the contact's details required.
|
|
if ((colRequired_User_Email || colRequired_User_Name))
|
|
{
|
|
// Join to the user's affiliate.
|
|
DbQueryJoin joinAff = new DbQueryJoin(DbJoinType.LeftOuter);
|
|
joinAff.Table = new StringPair(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Affiliate), aliasSystemAccountUser_Affiliate);
|
|
joinAff.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_Id, aliasSystemAccountUser_Affiliate) + "=" + SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_AffiliateId, aliasSystemAccountUser_SystemUser));
|
|
query.Joins.Add(joinAff);
|
|
|
|
// Add the affiliate columns to the query.
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_Id, aliasSystemAccountUser_Affiliate), prefixSystemAccountUser_Affiliate + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Affiliate_Id));
|
|
if (colRequired_User_Email)
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_Email, aliasSystemAccountUser_Affiliate), prefixSystemAccountUser_Affiliate + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Affiliate_Email));
|
|
if (colRequired_User_Name)
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_Name, aliasSystemAccountUser_Affiliate), prefixSystemAccountUser_Affiliate + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Affiliate_Name));
|
|
}
|
|
//bHasSysUserTable = true;
|
|
}
|
|
|
|
//--------------------------------------------------------------- -
|
|
// Common Properties
|
|
if (columns.HasId((int)SbmSystemAccountUser.Column.Notes))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_Notes, aliasSystemAccountUser));
|
|
if (columns.HasId((int)SbmSystemAccountUser.Column.Flag))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_Flags, aliasSystemAccountUser));
|
|
if (columns.HasId((int)SbmSystemAccountUser.Column.Status) || columns.HasId((int)SbmSystemAccountUser.Column.Status_Desc) || columns.HasId((int)SbmSystemAccountUser.Column.Status_Ind))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_Status, aliasSystemAccountUser));
|
|
if (columns.HasId((int)SbmSystemAccountUser.Column.CreateOn_Date))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_CreateOn, aliasSystemAccountUser));
|
|
if (columns.HasId((int)SbmSystemAccountUser.Column.UpdateOn_Date))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_UpdateOn, aliasSystemAccountUser));
|
|
|
|
//---------------------------------------------------------------------
|
|
// Populate filter.
|
|
query.Filters.AddRange(GetSystemAccountUserQueryFilter(prm));
|
|
|
|
if (prm.IdsIncluded.Count != 1)
|
|
{
|
|
// Configure sort sequence.
|
|
foreach (String sortColumn in GetSystemAccountUserSortColumns(prm))
|
|
query.OrderBy.Add(sortColumn);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
//return new DbResultSet_DataTableReader(Database, query.BuildQuery());
|
|
return DbResultSet_DataReader.ExecuteQuery(Database, query.BuildQuery(prm.ResultsPaging));
|
|
}
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|