580 lines
27 KiB
C#
580 lines
27 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 e m U s e r s
|
|
//*******************************************************************
|
|
// Utilities.
|
|
private List<StringPair> GetLogProperties(SbmSystemUser systemUser)
|
|
{
|
|
return new List<StringPair>()
|
|
{
|
|
new StringPair("Id",SysId.ToString(systemUser.Id)),
|
|
new StringPair("Status",systemUser.StatusCode)
|
|
};
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Create SystemUser.
|
|
public override SbmSystemUser CreateSystemUser(SysId interfaceId, SysId affiliateId)
|
|
{
|
|
ClearErrorState();
|
|
SbmSystemUser systemUser = null;
|
|
if (SysId.IsValid(AffiliateId_Owner))
|
|
systemUser = new SbmSystemUser_Dbas(Internals, new SysId(), AffiliateId_Owner, interfaceId, affiliateId, 0);
|
|
else SetErrorState("Owner not selected");
|
|
return systemUser;
|
|
}
|
|
private SbmSystemUser_Dbas CreateSystemUser(SystemUser_Db dbSystemUser)
|
|
{
|
|
ClearErrorState();
|
|
SbmSystemUser_Dbas systemUser = new SbmSystemUser_Dbas(Internals, new SysId(dbSystemUser._Id), new SysId(dbSystemUser._AffIdOwner), new SysId(dbSystemUser._InterfaceId ?? 0), new SysId(dbSystemUser._AffiliateId ?? 0), (ushort)dbSystemUser._Flags);
|
|
PopulateFrom(dbSystemUser, systemUser);
|
|
return systemUser;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get SystemUser.
|
|
public override SbmSystemUser GetSystemUser(SbmSystemUser.Params prm)
|
|
{
|
|
return GetSystemUser(GetSystemUserQueryFilter(prm).Concat(" AND ", "(", ")"));
|
|
}
|
|
private SbmSystemUser GetSystemUser(String filter)
|
|
{
|
|
ClearErrorState();
|
|
long count = GetSystemUserCount(filter);
|
|
if (count == 0) return null;
|
|
if (count != 1)
|
|
{
|
|
SetErrorState("Multiple System Users found");
|
|
return null;
|
|
}
|
|
SbmSystemUser SystemUser = null;
|
|
// Query data.
|
|
using (var dbConnection = db.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
SystemUser_Db dbSystemUser = new SystemUser_Db(DbInfo);
|
|
dbSystemUser.PopulateFrom(dbCommand, aliasSystemUser, filter);
|
|
SystemUser = CreateSystemUser(dbSystemUser);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SetErrorState("Database query exception");
|
|
SaLogger.Warn($"{SysUtil.Fn}\n\tQuery: {dbCommand.CommandText}\n\t{ExceptionUtil.GetErrorStateMessage(exc)}");
|
|
}
|
|
}
|
|
return SystemUser;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Validate SystemUser.
|
|
public override bool IsValidSystemUserData(SbmSystemUser.Data data, SbmSystemUser.ErrField errField)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.IsValidSystemUserData(data, errField)) return false;
|
|
return true;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Insert SystemUser.
|
|
public override bool InsertSystemUser(SbmSystemUser systemUser, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
ClearErrorState();
|
|
if (!IsValidSystemUser(systemUser)) return false;
|
|
if (SysId.IsValid(systemUser.Id)) return SetErrorState(ErrorMessage.ValiditemId);
|
|
using (var dbConnection = db.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
SystemUser_Db dbSystemUser = new SystemUser_Db(DbInfo);
|
|
PopulateTo(dbSystemUser, systemUser, dbUpdateInfo);
|
|
if (SystemUser_Db.FieldsNonId.Contains("Id"))
|
|
{
|
|
// Generate next Id.
|
|
dbSystemUser._Id = SysId.ToInt(db.GetNextId("SystemUser", dbUpdateInfo));
|
|
}
|
|
dbSystemUser.ExecuteInsertCommand(dbCommand);
|
|
systemUser.Id = new SysId(dbSystemUser._Id);
|
|
systemUser.Changed = false;
|
|
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Inserted. {GetLogProperties(systemUser).Concat(": ").Concat("", "\n\t")}");
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Update SystemUser.
|
|
public override bool UpdateSystemUser(SbmSystemUser systemUser, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
ClearErrorState();
|
|
if (!IsValidSystemUser(systemUser)) return false;
|
|
if (!SysId.IsValid(systemUser.Id)) return SetErrorState(ErrorMessage.InvalidItemId);
|
|
if (!systemUser.Changed)
|
|
{
|
|
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Data not changed. {GetLogProperties(systemUser).Concat(": ").Concat("", "\n\t")}");
|
|
return true;
|
|
}
|
|
using (var dbConnection = db.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve prmSystemUser.
|
|
dbConnection.Open();
|
|
SystemUser_Db dbSystemUser = new SystemUser_Db(DbInfo);
|
|
dbSystemUser.PopulateFrom(dbCommand, aliasSystemUser, "Id=" + SysId.ToString(systemUser.Id));
|
|
//SbmUpdateLogItemMonitor_SystemUser updateLogMonitor = new SbmUpdateLogItemMonitor_SystemUser(SbmGlobals.SessionProfile.SessionContext, CreateSystemUser(dbSystemUser), dbUpdateInfo);
|
|
|
|
// Update properties.
|
|
PopulateTo(dbSystemUser, systemUser, dbUpdateInfo);
|
|
dbSystemUser.ExecuteUpdateCommand(dbCommand);
|
|
systemUser.Changed = false;
|
|
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Updated. {GetLogProperties(systemUser).Concat(": ").Concat("", "\n\t")}");
|
|
|
|
// Record updates.
|
|
/*updateLogMonitor.RecordUpdatedValues(systemUser);
|
|
updateLogMonitor.LogUpdates();*/
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Delete SystemUser.
|
|
public override bool SystemUserCanBeDeleted(SbmSystemUser systemUser)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.SystemUserCanBeDeleted(systemUser)) return false;
|
|
return true;
|
|
}
|
|
public override bool DeleteSystemUser(SbmSystemUser systemUser, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
ClearErrorState();
|
|
if (!SystemUserCanBeDeleted(systemUser)) return false;
|
|
using (var dbConnection = db.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve prmSystemUser.
|
|
dbConnection.Open();
|
|
SystemUser_Db dbSystemUser = new SystemUser_Db(DbInfo);
|
|
dbSystemUser.PopulateFrom(dbCommand, aliasSystemUser, "Id=" + systemUser.Id.ToString());
|
|
// Delete prmSystemUser.
|
|
dbCommand.CommandType = System.Data.CommandType.Text;
|
|
dbCommand.CommandText = "DELETE FROM " + SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemUser) + " WHERE Id=" + SysId.ToString(systemUser.Id) + ";";
|
|
dbCommand.ExecuteNonQuery();
|
|
systemUser.Changed = false;
|
|
if (DebugMode) SaLogger.Debug($"{SysUtil.Fn} Deleted. {GetLogProperties(systemUser).Concat(": ").Concat("", "\n\t")}");
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Copy data properties to and from database.
|
|
private void PopulateTo(SystemUser_Db dbSystemUser, SbmSystemUser systemUser, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
dbSystemUser._Id = systemUser.Id.ToInt();
|
|
dbSystemUser._AffIdOwner = systemUser.AffiliateIdOwner.ToInt();
|
|
dbSystemUser._InterfaceId = systemUser.InterfaceId.ToInt();
|
|
dbSystemUser._AffiliateId = systemUser.AffiliateId.ToInt();
|
|
dbSystemUser._Code = systemUser.Code;
|
|
dbSystemUser._AuthCodeEnc = systemUser.AuthCodeEnc;
|
|
// Common properties.
|
|
dbSystemUser._Notes = systemUser.Notes;
|
|
dbSystemUser._Status = SacsSystem.GetStatusCode(systemUser.Status);
|
|
dbSystemUser._Flags = (short)systemUser.Flags;
|
|
if (!SysId.IsValid(systemUser.Id))
|
|
{
|
|
dbSystemUser._CreateOn = DateTime.UtcNow;
|
|
dbSystemUser._CreateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbSystemUser._CreateHost = dbUpdateInfo.Host;
|
|
}
|
|
dbSystemUser._UpdateOn = DateTime.UtcNow;
|
|
dbSystemUser._UpdateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbSystemUser._UpdateHost = dbUpdateInfo.Host;
|
|
}
|
|
private void PopulateFrom(SystemUser_Db dbSystemUser, SbmSystemUser systemUser)
|
|
{
|
|
systemUser.Id = new SysId(dbSystemUser._Id);
|
|
systemUser.AffiliateIdOwner = new SysId(dbSystemUser._AffIdOwner);
|
|
systemUser.InterfaceId = new SysId(dbSystemUser._InterfaceId ?? 0);
|
|
systemUser.AffiliateId = new SysId(dbSystemUser._AffiliateId ?? 0);
|
|
systemUser.Code = dbSystemUser._Code;
|
|
systemUser.AuthCodeEnc = dbSystemUser._AuthCodeEnc;
|
|
// Common properties.
|
|
systemUser.Notes = dbSystemUser._Notes;
|
|
systemUser.Status = SacsSystem.GetStatusForCode(dbSystemUser._Status);
|
|
systemUser.SetCreateStamp(dbSystemUser._CreateOn ?? default(DateTime), new SysId(dbSystemUser._CreateBy ?? 0), dbSystemUser._CreateHost);
|
|
systemUser.SetUpdateStamp(dbSystemUser._UpdateOn ?? default(DateTime), new SysId(dbSystemUser._UpdateBy ?? 0), dbSystemUser._UpdateHost);
|
|
systemUser.AcceptChanges();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Column definitions.
|
|
private static readonly String aliasSystemUser = "aSysUsr";
|
|
private static readonly String aliasSystemUser_Affiliate = "SysUsr_Aff";
|
|
private static readonly String prefixSystemUser_Affiliate = aliasSystemUser_Affiliate + "_";
|
|
private static readonly String aliasSystemUser_SystemAccount = "SysUsr_SysAcc";
|
|
private static readonly String prefixSystemUser_SystemAccount = aliasSystemUser_SystemAccount + "_";
|
|
private static readonly String aliasSystemUser_SystemAccountUser = "SysUsr_SysAccUsr";
|
|
private static readonly String prefixSystemUser_SystemAccountUser = aliasSystemUser_SystemAccountUser + "_";
|
|
// Column names.
|
|
private static readonly String colnameSysUsr_AccountCount = "SysUsrAccCount";
|
|
|
|
// Column definition population.
|
|
private void SetSystemUserColDfn
|
|
(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.SystemUser)
|
|
alias = aliasSystemUser;
|
|
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 SystemUser column definition.
|
|
public override ColumnDfn GetSystemUserColumnDfn(SbmSystemUser.Column columnId, string caption = "", int width = 0)
|
|
{
|
|
ColumnDfn columnDfn = new ColumnDfn((int)columnId, caption, width);
|
|
switch (columnId)
|
|
{
|
|
// SystemUser Properties.
|
|
case SbmSystemUser.Column.Pos:
|
|
SetSystemUserColDfn(columnDfn, "Pos", 45, SbmDbDfn.ColumnIdx.Unknown, ColumnAlignment.Right);
|
|
columnDfn.SortMode = ColumnDfn.ColumnSortMode.Off;
|
|
columnDfn.ReadOnly = true;
|
|
columnDfn.Resizable = false;
|
|
break;
|
|
case SbmSystemUser.Column.Id:
|
|
SetSystemUserColDfn(columnDfn, "Id", 60, SbmDbDfn.ColumnIdx.SystemUser_Id, ColumnAlignment.Right, DataValue.DataType.Long);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SbmSystemUser.Column.Code:
|
|
SetSystemUserColDfn(columnDfn, "Code", 120, SbmDbDfn.ColumnIdx.SystemUser_Code, ColumnAlignment.Default, DataValue.DataType.String);
|
|
break;
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Affiliate details.
|
|
case SbmSystemUser.Column.Contact_Name:
|
|
SetSystemUserColDfn(columnDfn, "Name", 120, SbmDbDfn.ColumnIdx.Affiliate_Name, ColumnAlignment.Default, DataValue.DataType.String, aliasSystemUser_Affiliate, prefixSystemUser_Affiliate);
|
|
break;
|
|
case SbmSystemUser.Column.Contact_Email:
|
|
SetSystemUserColDfn(columnDfn, "Email", 120, SbmDbDfn.ColumnIdx.Affiliate_Email, ColumnAlignment.Default, DataValue.DataType.String, aliasSystemUser_Affiliate, prefixSystemUser_Affiliate);
|
|
break;
|
|
case SbmSystemUser.Column.Contact_PhoneMain:
|
|
SetSystemUserColDfn(columnDfn, "Phone", 85, SbmDbDfn.ColumnIdx.Affiliate_PhoneMain, ColumnAlignment.Default, DataValue.DataType.String, aliasSystemUser_Affiliate, prefixSystemUser_Affiliate);
|
|
break;
|
|
case SbmSystemUser.Column.Contact_PhoneMobile:
|
|
SetSystemUserColDfn(columnDfn, "Mobile", 85, SbmDbDfn.ColumnIdx.Affiliate_PhoneMobile, ColumnAlignment.Default, DataValue.DataType.String, aliasSystemUser_Affiliate, prefixSystemUser_Affiliate);
|
|
break;
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Associated details.
|
|
case SbmSystemUser.Column.AccountCount:
|
|
SetSystemUserColDfn(columnDfn, "Accounts", 80, SbmDbDfn.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colnameSysUsr_AccountCount;
|
|
break;
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Common properties.
|
|
case SbmSystemUser.Column.Notes:
|
|
SetSystemUserColDfn(columnDfn, "Notes", 200, SbmDbDfn.ColumnIdx.SystemUser_Notes);
|
|
break;
|
|
case SbmSystemUser.Column.Flag:
|
|
SetSystemUserColDfn(columnDfn, "Flags", 40, SbmDbDfn.ColumnIdx.SystemUser_Flags, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
break;
|
|
case SbmSystemUser.Column.Status:
|
|
SetSystemUserColDfn(columnDfn, "Status", 50, SbmDbDfn.ColumnIdx.SystemUser_Status, ColumnAlignment.Center);
|
|
break;
|
|
case SbmSystemUser.Column.Status_Desc:
|
|
SetSystemUserColDfn(columnDfn, "Status", 50, SbmDbDfn.ColumnIdx.SystemUser_Status);
|
|
break;
|
|
case SbmSystemUser.Column.Status_Ind:
|
|
SetSystemUserColDfn(columnDfn, "Status", 50, SbmDbDfn.ColumnIdx.SystemUser_Status, ColumnAlignment.Center, DataValue.DataType.TriState);
|
|
columnDfn.TriStateFormat = ColumnTriStateFormat.Lights;
|
|
break;
|
|
case SbmSystemUser.Column.CreateOn_Date:
|
|
SetSystemUserColDfn(columnDfn, "Created", 65, SbmDbDfn.ColumnIdx.SystemUser_CreateOn, ColumnAlignment.Center, DataValue.DataType.Date);
|
|
columnDfn.DateFormat = "d-MMM-yy";
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SbmSystemUser.Column.UpdateOn_Date:
|
|
SetSystemUserColDfn(columnDfn, "Updated", 65, SbmDbDfn.ColumnIdx.SystemUser_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;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// SystemUser query filter.
|
|
public override List<string> GetSystemUserQueryFilter(SbmSystemUser.Params prm)
|
|
{
|
|
List<String> filter = new List<String>();
|
|
|
|
// SystemUser Id.
|
|
String includedIds = prm.IdsIncluded.Concat(",");
|
|
String colSystemUser_Id = SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Id, aliasSystemUser);
|
|
if (!includedIds.IsEmpty())
|
|
filter.Add(colSystemUser_Id + " IN (" + includedIds + ")");
|
|
String excludedIds = prm.IdsExcluded.Concat(",");
|
|
if (!excludedIds.IsEmpty())
|
|
filter.Add(colSystemUser_Id + " NOT IN (" + excludedIds + ")");
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Owner.
|
|
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_AffIdOwner, aliasSystemUser) + "=" + AffiliateId_Owner.ToString());
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Code Partial.
|
|
if (!StringUtil.IsEmpty(prm.CodePartial))
|
|
{
|
|
;
|
|
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Code, aliasSystemAccount) + " LIKE '" + DbQuery.FormatForLike(prm.CodePartial) + "%'");
|
|
}
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Email Partial - link to affiliate.
|
|
if (!StringUtil.IsEmpty(prm.EmailPartial))
|
|
{
|
|
// Query contacts (affiliates) by partial email.
|
|
DbQuery qryAff = new DbQuery(db.DbmsType);
|
|
qryAff.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Affiliate), aliasSystemUser_Affiliate);
|
|
qryAff.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_Id, aliasSystemUser_Affiliate));
|
|
qryAff.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_Email, aliasSystemUser_Affiliate) + " LIKE '" + DbQuery.FormatForLike(prm.EmailPartial) + "%'");
|
|
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_AffiliateId) + " IN(" + qryAff.BuildQuery() + ")");
|
|
}
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Exclude Users that are already associated with the specified System Account by Interface & Association.
|
|
if (SysId.IsValid(prm.AssocationExcludeIdUserAccounts))
|
|
{
|
|
// Query system account.
|
|
DbQuery qrySysAcc = new DbQuery(db.DbmsType);
|
|
qrySysAcc.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccount), aliasSystemUser_SystemAccount);
|
|
qrySysAcc.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_Id, aliasSystemUser_SystemAccount));
|
|
qrySysAcc.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_AssocId, aliasSystemUser_SystemAccount) + "=" + prm.AssocationExcludeIdUserAccounts.ToString());
|
|
if (SysId.IsValid(prm.InterfaceId))
|
|
qrySysAcc.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccount_InterfaceId, aliasSystemUser_SystemAccount) + "=" + prm.InterfaceId.ToString());
|
|
|
|
// Query users that belong to this account.
|
|
DbQuery qrySysAccUsr = new DbQuery(db.DbmsType);
|
|
qrySysAccUsr.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccountUser), aliasSystemUser_SystemAccountUser);
|
|
qrySysAccUsr.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_UserId, aliasSystemUser_SystemAccountUser));
|
|
qrySysAccUsr.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId, aliasSystemUser_SystemAccountUser) + " IN(" + qrySysAcc.BuildQuery() + ")");
|
|
|
|
// Exclude users that belong to any of these accounts:
|
|
filter.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Id) + " NOT IN(" + qrySysAccUsr.BuildQuery() + ")");
|
|
}
|
|
|
|
if (filter.Count < 1 && prm.IncludeDefaultFilter) filter.Add("1=1");
|
|
return filter;
|
|
}
|
|
|
|
// SystemUser sort columns.
|
|
public List<String> GetSystemUserSortColumns(SbmSystemUser.Params prm)
|
|
{
|
|
// Sort sequence.
|
|
List<String> sortColumnCodes = new List<String>();
|
|
foreach (var columnDfn in prm.SortColumns)
|
|
{
|
|
// Need to ensure correct alias used for SystemUser columns in case of conflict.
|
|
String sortCol = String.Empty;
|
|
if (SbmDbDfn.GetTableForColumn((SbmDbDfn.ColumnIdx)columnDfn.DbColumn) == SbmDbDfn.TableIdx.SystemUser)
|
|
{
|
|
sortCol = SbmDbDfn.GetColumnNameQual((SbmDbDfn.ColumnIdx)columnDfn.DbColumn, aliasSystemUser);
|
|
}
|
|
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.SystemUser_AffIdOwner, aliasSystemUser) + " ASC");
|
|
}
|
|
return sortColumnCodes;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// SystemUser count.
|
|
private long GetSystemUserCount(String filter)
|
|
{
|
|
ClearErrorState();
|
|
return db.GetRowCount(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemUser) + " As " + aliasSystemUser, filter);
|
|
}
|
|
public override long GetSystemUserCount(SbmSystemUser.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
return GetSystemUserCount(GetSystemUserQueryFilter(prm).Concat(" AND ", "(", ")"));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// SystemUser list.
|
|
public override List<SbmSystemUser> GetSystemUserList(SbmSystemUser.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
List<SbmSystemUser> systemUsers = new List<SbmSystemUser>();
|
|
DbQuery qry = new DbQuery(db.DbmsType);
|
|
qry.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemUser), aliasSystemUser);
|
|
SystemUser_Db dbSystemUser = new SystemUser_Db(DbInfo);
|
|
qry.Columns.AddRange(dbSystemUser.ExistingColumnNames);
|
|
qry.Filters.AddRange(GetSystemUserQueryFilter(prm));
|
|
qry.OrderBy.AddRange(GetSystemUserSortColumns(prm));
|
|
// Query data.
|
|
using (var dbConnection = db.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
dbCommand.CommandText = qry.BuildQuery(prm.ResultsPaging);
|
|
using (DbDataReader dbDataReader = dbCommand.ExecuteReader())
|
|
{
|
|
while (dbDataReader.Read())
|
|
{
|
|
dbSystemUser = new SystemUser_Db(DbInfo);
|
|
dbSystemUser.PopulateFrom(dbDataReader);
|
|
systemUsers.Add(CreateSystemUser(dbSystemUser));
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SetErrorState("Database query exception");
|
|
SaLogger.Warn($"{SysUtil.Fn}\n\tQuery: {dbCommand.CommandText}\n\t{ExceptionUtil.GetErrorStateMessage(exc)}");
|
|
}
|
|
}
|
|
return systemUsers;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// SystemUser query execution.
|
|
public override DbResultSet SystemUserExecuteQuery(List<ColumnDfn> columns, SbmSystemUser.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
//---------------------------------------------------------------------
|
|
// Construct build database query.
|
|
DbQuery query = new DbQuery(db.DbmsType);
|
|
query.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemUser), aliasSystemUser);
|
|
|
|
// SystemUser Properties - add columns as defined in query list.
|
|
if (columns.HasId((int)SbmSystemUser.Column.Id))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Id, aliasSystemUser));
|
|
if (columns.HasId((int)SbmSystemUser.Column.Code))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Code, aliasSystemUser));
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Contact details.
|
|
bool hasCol_ContactEmail = columns.HasId((int)SbmSystemUser.Column.Contact_Email);
|
|
bool hasCol_ContactName = columns.HasId((int)SbmSystemUser.Column.Contact_Name);
|
|
bool hasCol_ContactPhoneMain = columns.HasId((int)SbmSystemUser.Column.Contact_PhoneMain);
|
|
bool hasCol_ContactPhoneMobile = columns.HasId((int)SbmSystemUser.Column.Contact_PhoneMobile);
|
|
if (hasCol_ContactEmail || hasCol_ContactName || hasCol_ContactPhoneMain || hasCol_ContactPhoneMobile)
|
|
{
|
|
// Join to the contact (affiliate) table.
|
|
DbQueryJoin joinAff = new DbQueryJoin(DbJoinType.LeftOuter);
|
|
joinAff.Table = new StringPair(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Affiliate), aliasSystemUser_Affiliate);
|
|
joinAff.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_Id, aliasSystemUser_Affiliate) + "=" + SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_AffiliateId, aliasSystemUser));
|
|
query.Joins.Add(joinAff);
|
|
|
|
// Add the user columns to the query.
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_Id, aliasSystemUser_Affiliate), prefixSystemUser_Affiliate + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Affiliate_Id));
|
|
if (hasCol_ContactName)
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_Name, aliasSystemUser_Affiliate), prefixSystemUser_Affiliate + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Affiliate_Name));
|
|
if (hasCol_ContactEmail)
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_Email, aliasSystemUser_Affiliate), prefixSystemUser_Affiliate + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Affiliate_Email));
|
|
if (hasCol_ContactPhoneMain)
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_PhoneMain, aliasSystemUser_Affiliate), prefixSystemUser_Affiliate + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Affiliate_PhoneMain));
|
|
if (hasCol_ContactPhoneMobile)
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Affiliate_PhoneMobile, aliasSystemUser_Affiliate), prefixSystemUser_Affiliate + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Affiliate_PhoneMobile));
|
|
}
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Number of associated accounts:
|
|
if (columns.HasId((int)SbmSystemUser.Column.AccountCount))
|
|
{
|
|
DbQuery qryAccounts = new DbQuery(db.DbmsType);
|
|
qryAccounts.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.SystemAccountUser), aliasSystemUser_SystemAccountUser);
|
|
qryAccounts.Columns.Add("Count(" + SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccountUser_AccountId) + ")", aliasSystemUser_SystemAccountUser);
|
|
qryAccounts.Filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.SystemAccountUser_UserId) + "=" + SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Id, aliasSystemUser));
|
|
query.Columns.Add("(" + qryAccounts.BuildQuery() + ")", colnameSysUsr_AccountCount);
|
|
}
|
|
|
|
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
// Common Properties
|
|
if (columns.HasId((int)SbmSystemUser.Column.Notes))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Notes, aliasSystemUser));
|
|
if (columns.HasId((int)SbmSystemUser.Column.Flag))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Flags, aliasSystemUser));
|
|
if (columns.HasId((int)SbmSystemUser.Column.Status) || columns.HasId((int)SbmSystemUser.Column.Status_Desc) || columns.HasId((int)SbmSystemUser.Column.Status_Ind))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_Status, aliasSystemUser));
|
|
if (columns.HasId((int)SbmSystemUser.Column.CreateOn_Date))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_CreateOn, aliasSystemUser));
|
|
if (columns.HasId((int)SbmSystemUser.Column.UpdateOn_Date))
|
|
query.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.SystemUser_UpdateOn, aliasSystemUser));
|
|
|
|
//---------------------------------------------------------------------
|
|
// Populate filter.
|
|
query.Filters.AddRange(GetSystemUserQueryFilter(prm));
|
|
|
|
if (prm.IdsIncluded.Count != 1)
|
|
{
|
|
// Configure sort sequence.
|
|
foreach (String sortColumn in GetSystemUserSortColumns(prm))
|
|
query.OrderBy.Add(sortColumn);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
//return new DbResultSet_DataTableReader(db, query.BuildQuery());
|
|
return DbResultSet_DataReader.ExecuteQuery(db, query.BuildQuery(prm.ResultsPaging));
|
|
}
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|