556 lines
26 KiB
C#
556 lines
26 KiB
C#
using Sarand.Net.Standard.Dbas.Data;
|
|
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 Saxonite.Base.App;
|
|
using Saxonite.Net.Standard.Dbas.Bus;
|
|
using Saxonite.Net.Standard.Dbas.Internal;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.Text;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System.Linq;
|
|
|
|
namespace Saxonite.Net.Standard.Dbas.Mgr
|
|
{
|
|
//=======================================================================
|
|
partial class SaxAccountMgr_Dbas
|
|
{
|
|
//*******************************************************************
|
|
// Account Mgr - Organisation
|
|
//*******************************************************************
|
|
// Utilities.
|
|
private List<StringPair> GetLogProperties(SaxOrganisation organisation)
|
|
{
|
|
return new List<StringPair>()
|
|
{
|
|
new StringPair("Id", SysId.ToString(organisation.Id)),
|
|
new StringPair("Name", organisation.Name),
|
|
new StringPair("Status", organisation.StatusCode)
|
|
};
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Create item.
|
|
public override SaxOrganisation CreateOrganisation()
|
|
{
|
|
ClearErrorState();
|
|
return new SaxOrganisation_Dbas(CoreDatabase, null, 0);
|
|
}
|
|
private SaxOrganisation_Dbas CreateOrganisation(Organisation_Db dbOrganisation)
|
|
{
|
|
ClearErrorState();
|
|
SaxOrganisation_Dbas organisation = new SaxOrganisation_Dbas(CoreDatabase, new SysId(dbOrganisation._Id), (UInt16)dbOrganisation._Flags);
|
|
PopulateFrom(dbOrganisation, organisation);
|
|
return organisation;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get item.
|
|
public override SaxOrganisation GetOrganisation(SaxOrganisation.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
return GetOrganisation(GetOrganisationQueryFilter(prm).Concat(" And ", "(", ")"));
|
|
}
|
|
private SaxOrganisation GetOrganisation(String filter)
|
|
{
|
|
ClearErrorState();
|
|
long count = GetOrganisationCount(filter);
|
|
if (count == 0) return null;
|
|
if (count != 1)
|
|
{
|
|
SetErrorState("Multiple items found");
|
|
return null;
|
|
}
|
|
SaxOrganisation organisation = null;
|
|
// Query data.
|
|
using (var dbConnection = CoreDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
Organisation_Db dbOrganisation = new Organisation_Db(CoreDbInfo);
|
|
dbOrganisation.PopulateFrom(dbCommand, aliasOrganisation, filter);
|
|
organisation = CreateOrganisation(dbOrganisation);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SetErrorState("Database query exception");
|
|
SaLogger.Warn(GetType().Name + ".GetOrganisation\n\tQuery: " + dbCommand.CommandText + "\n\t" + exc.GetType().Name + ": " + exc.Message);
|
|
}
|
|
}
|
|
return organisation;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Validate item.
|
|
public override bool IsValidOrganisationData(SaxOrganisation.Data data, SaxOrganisation.ErrField errField)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.IsValidOrganisationData(data, errField)) return false;
|
|
return true;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Insert item.
|
|
public override bool InsertOrganisation(SaxOrganisation organisation, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".InsertOrganisation";
|
|
ClearErrorState();
|
|
if (!IsValidOrganisation(organisation)) return false;
|
|
if (SysId.IsValid(organisation.Id)) return SetErrorState(ErrorMessage.ValiditemId);
|
|
using (var dbConnection = CoreDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
Organisation_Db dbOrganisation = new Organisation_Db(CoreDbInfo);
|
|
PopulateTo(dbOrganisation, organisation, dbUpdateInfo);
|
|
if (Organisation_Db.FieldsNonId.Contains("Id"))
|
|
{
|
|
// Generate next Id.
|
|
// ToDo dbOrganisation._Id = CoreDatabase.GetNextIdFromTable("Organisation");
|
|
}
|
|
dbOrganisation.ExecuteInsertCommand(dbCommand);
|
|
organisation.Id = new SysId(dbOrganisation._Id);
|
|
organisation.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Inserted. " + GetLogProperties(organisation).Concat(": ").Concat("", "\n\t"));
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Update item.
|
|
public override bool UpdateOrganisation(SaxOrganisation organisation, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".UpdateOrganisation";
|
|
ClearErrorState();
|
|
if (!IsValidOrganisation(organisation)) return false;
|
|
if (!SysId.IsValid(organisation.Id)) return SetErrorState(ErrorMessage.InvalidItemId);
|
|
if (!organisation.Changed)
|
|
{
|
|
if (DebugMode) SaLogger.Debug(fn + " Data not changed. " + GetLogProperties(organisation).Concat(": ").Concat("", "\n\t"));
|
|
return true;
|
|
}
|
|
using (var dbConnection = CoreDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve item.
|
|
dbConnection.Open();
|
|
Organisation_Db dbOrganisation = new Organisation_Db(CoreDbInfo);
|
|
dbOrganisation.PopulateFrom(dbCommand, aliasOrganisation, "Id=" + SysId.ToString(organisation.Id));
|
|
// ToDo SaxUpdateLogItemMonitor_Organisation updateLogMonitor = new SaxUpdateLogItemMonitor_Organisation(SessionContext, CreateOrganisation(dbOrganisation), dbUpdateInfo);
|
|
// Update properties.
|
|
PopulateTo(dbOrganisation, organisation, dbUpdateInfo);
|
|
dbOrganisation.ExecuteUpdateCommand(dbCommand);
|
|
organisation.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Updated. " + GetLogProperties(organisation).Concat(": ").Concat("", "\n\t"));
|
|
// Record updates.
|
|
// ToDo updateLogMonitor.RecordUpdatedValues(organisation);
|
|
// ToDo updateLogMonitor.LogUpdates();
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Delete item.
|
|
public override bool OrganisationCanBeDeleted(SaxOrganisation organisation)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.OrganisationCanBeDeleted(organisation)) return false;
|
|
return true;
|
|
}
|
|
public override bool DeleteOrganisation(SaxOrganisation organisation, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".DeleteOrganisation";
|
|
ClearErrorState();
|
|
if (!OrganisationCanBeDeleted(organisation)) return false;
|
|
using (var dbConnection = CoreDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve organisation.
|
|
dbConnection.Open();
|
|
Organisation_Db dbOrganisation = new Organisation_Db(CoreDbInfo);
|
|
dbOrganisation.PopulateFrom(dbCommand, aliasOrganisation, "Id=" + organisation.Id.ToString());
|
|
// Delete organisation.
|
|
dbCommand.CommandType = System.Data.CommandType.Text;
|
|
dbCommand.CommandText = "DELETE FROM " + SaxDbDfn.GetTableName(SaxDbDfn.TableIdx.Organisation) + " WHERE Id=" + SysId.ToString(organisation.Id) + ";";
|
|
dbCommand.ExecuteNonQuery();
|
|
organisation.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Deleted. " + GetLogProperties(organisation).Concat(": ").Concat("", "\n\t"));
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Copy data properties to and from database.
|
|
private void PopulateTo(Organisation_Db dbOrganisation, SaxOrganisation organisation, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
dbOrganisation._Id = SysId.ToInt(organisation.Id);
|
|
dbOrganisation._Name = organisation.Name;
|
|
dbOrganisation._PhoneMain = organisation.PhoneMain;
|
|
dbOrganisation._PhoneAlt = organisation.PhoneAlt;
|
|
dbOrganisation._OrganisationType = OrganisationTypes.GetCodeForType(organisation.OrganisationType);
|
|
dbOrganisation._OrganisationCategory = OrganisationCategories.GetCodeForType(organisation.OrganisationCategory);
|
|
dbOrganisation._Notes = organisation.Notes;
|
|
dbOrganisation._Status = SacsSystem.GetStatusCode(organisation.Status);
|
|
dbOrganisation._Flags = (short)organisation.Flags;
|
|
if (!SysId.IsValid(organisation.Id))
|
|
{
|
|
dbOrganisation._CreateOn = DateTime.Now;
|
|
dbOrganisation._CreateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbOrganisation._CreateHost = dbUpdateInfo.Host;
|
|
}
|
|
dbOrganisation._UpdateOn = DateTime.Now;
|
|
dbOrganisation._UpdateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbOrganisation._UpdateHost = dbUpdateInfo.Host;
|
|
}
|
|
private void PopulateFrom(Organisation_Db dbOrganisation, SaxOrganisation organisation)
|
|
{
|
|
//organisation.Id = new SysId(dbOrganisation._Id);
|
|
//organisation.UserAccountId = new SysId(dbOrganisation._UserAccountId);
|
|
organisation.Name = dbOrganisation._Name;
|
|
organisation.PhoneMain = dbOrganisation._PhoneMain;
|
|
organisation.PhoneAlt = dbOrganisation._PhoneAlt;
|
|
organisation.OrganisationType = (Sax.OrganisationType)OrganisationTypes.GetTypeForCode(dbOrganisation._OrganisationType);
|
|
organisation.OrganisationCategory = (Sax.OrganisationCategory)OrganisationCategories.GetTypeForCode(dbOrganisation._OrganisationCategory);
|
|
organisation.Notes = dbOrganisation._Notes;
|
|
organisation.Status = SacsSystem.GetStatusForCode(dbOrganisation._Status);
|
|
organisation.SetCreateStamp(dbOrganisation._CreateOn, new SysId(dbOrganisation._CreateBy), dbOrganisation._CreateHost);
|
|
organisation.SetUpdateStamp(dbOrganisation._UpdateOn, new SysId(dbOrganisation._UpdateBy), dbOrganisation._UpdateHost);
|
|
organisation.AcceptChanges();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item column definitions.
|
|
private static readonly String aliasOrganisation = "Org";
|
|
private static readonly String colOrganisation_User_Count = "Org_Usr_Count";
|
|
private static readonly String colOrganisation_Member_Count = "Org_Mbr_Count";
|
|
private void SetOrganisationColDfn
|
|
(ColumnDfn col, String caption, int width, SaxDbDfn.ColumnIdx dbCol,
|
|
ColumnAlignment align = ColumnAlignment.Default, DataValue.DataType dataType = DataValue.DataType.String,
|
|
String alias = "", String prefix = "")
|
|
{
|
|
if (String.IsNullOrEmpty(alias) && SaxDbDfn.GetTableForColumn(dbCol) == SaxDbDfn.TableIdx.Organisation)
|
|
alias = aliasOrganisation;
|
|
col.Set(caption, width, (int)dbCol, prefix + SaxDbDfn.GetColumnName(dbCol, false), SaxDbDfn.GetColumnNameQual(dbCol, alias), dataType, align);
|
|
if (dataType == DataValue.DataType.String && dbCol != SaxDbDfn.ColumnIdx.Unknown)
|
|
col.MaxLength = (int)SaxDbDfn.GetColTextLen(dbCol);
|
|
}
|
|
public override ColumnDfn GetOrganisationColumnDfn(SaxOrganisation.Column columnId, string caption = "", int width = 0)
|
|
{
|
|
ColumnDfn columnDfn = new ColumnDfn((int)columnId, caption, width);
|
|
switch (columnId)
|
|
{
|
|
//-----------------------------------------------------------
|
|
case SaxOrganisation.Column.Pos:
|
|
SetOrganisationColDfn(columnDfn, "Pos", 60, SaxDbDfn.ColumnIdx.Unknown, ColumnAlignment.Right);
|
|
columnDfn.SortMode = ColumnDfn.ColumnSortMode.Off;
|
|
columnDfn.ReadOnly = true;
|
|
columnDfn.Resizable = false;
|
|
break;
|
|
case SaxOrganisation.Column.Id:
|
|
SetOrganisationColDfn(columnDfn, "Id", 80, SaxDbDfn.ColumnIdx.Organisation_Id, ColumnAlignment.Right, DataValue.DataType.Long);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SaxOrganisation.Column.Name:
|
|
SetOrganisationColDfn(columnDfn, "Name", 120, SaxDbDfn.ColumnIdx.Organisation_Name);
|
|
break;
|
|
case SaxOrganisation.Column.OrganisationType:
|
|
SetOrganisationColDfn(columnDfn, "Type", 80, SaxDbDfn.ColumnIdx.Organisation_OrganisationType, ColumnAlignment.Center, DataValue.DataType.String);
|
|
break;
|
|
case SaxOrganisation.Column.OrganisationType_Desc:
|
|
SetOrganisationColDfn(columnDfn, "Type", 80, SaxDbDfn.ColumnIdx.Organisation_OrganisationType, ColumnAlignment.Center, DataValue.DataType.String);
|
|
break;
|
|
case SaxOrganisation.Column.OrganisationCategory:
|
|
SetOrganisationColDfn(columnDfn, "Category", 80, SaxDbDfn.ColumnIdx.Organisation_OrganisationCategory, ColumnAlignment.Center, DataValue.DataType.String);
|
|
break;
|
|
case SaxOrganisation.Column.OrganisationCategory_Desc:
|
|
SetOrganisationColDfn(columnDfn, "Category", 80, SaxDbDfn.ColumnIdx.Organisation_OrganisationCategory, ColumnAlignment.Center, DataValue.DataType.String);
|
|
break;
|
|
case SaxOrganisation.Column.PhoneMain:
|
|
SetOrganisationColDfn(columnDfn, "Phone", 100, SaxDbDfn.ColumnIdx.Organisation_PhoneMain);
|
|
break;
|
|
case SaxOrganisation.Column.PhoneAlt:
|
|
SetOrganisationColDfn(columnDfn, "Alt. phone", 100, SaxDbDfn.ColumnIdx.Organisation_PhoneAlt);
|
|
break;
|
|
|
|
//-----------------------------------------------------------
|
|
// Derived properties.
|
|
case SaxOrganisation.Column.User_Count:
|
|
SetOrganisationColDfn(columnDfn, "Users", 80, SaxDbDfn.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colOrganisation_User_Count;
|
|
break;
|
|
case SaxOrganisation.Column.Member_Count:
|
|
SetOrganisationColDfn(columnDfn, "Members", 80, SaxDbDfn.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colOrganisation_Member_Count;
|
|
break;
|
|
|
|
//-----------------------------------------------------------
|
|
// Common properties.
|
|
case SaxOrganisation.Column.Notes:
|
|
SetOrganisationColDfn(columnDfn, "Notes", 240, SaxDbDfn.ColumnIdx.Organisation_Notes);
|
|
break;
|
|
case SaxOrganisation.Column.Flags:
|
|
SetOrganisationColDfn(columnDfn, "Flags", 60, SaxDbDfn.ColumnIdx.Organisation_Flags, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
break;
|
|
case SaxOrganisation.Column.Status:
|
|
SetOrganisationColDfn(columnDfn, "Status", 80, SaxDbDfn.ColumnIdx.Organisation_Status, ColumnAlignment.Center);
|
|
break;
|
|
case SaxOrganisation.Column.Status_Desc:
|
|
SetOrganisationColDfn(columnDfn, "Status", 90, SaxDbDfn.ColumnIdx.Organisation_Status, ColumnAlignment.Center);
|
|
break;
|
|
case SaxOrganisation.Column.Status_Ind:
|
|
SetOrganisationColDfn(columnDfn, "Status", 80, SaxDbDfn.ColumnIdx.Organisation_Status, ColumnAlignment.Center, DataValue.DataType.TriState);
|
|
columnDfn.TriStateFormat = ColumnTriStateFormat.Lights;
|
|
break;
|
|
case SaxOrganisation.Column.CreateOn_Date:
|
|
SetOrganisationColDfn(columnDfn, "Created", 80, SaxDbDfn.ColumnIdx.Organisation_CreateOn, ColumnAlignment.Center, DataValue.DataType.Date);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SaxOrganisation.Column.UpdateOn_Date:
|
|
SetOrganisationColDfn(columnDfn, "Updated", 80, SaxDbDfn.ColumnIdx.Organisation_UpdateOn, ColumnAlignment.Center, DataValue.DataType.Date);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
|
|
//-----------------------------------------------------------
|
|
}
|
|
if (!String.IsNullOrEmpty(caption))
|
|
columnDfn.Caption = caption;
|
|
if (width > 0)
|
|
columnDfn.Width = width;
|
|
return columnDfn;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item query filter.
|
|
public override List<string> GetOrganisationQueryFilter(SaxOrganisation.Params prm)
|
|
{
|
|
List<String> filter = new List<String>();
|
|
|
|
// Item Ids.
|
|
String idsIncluded = prm.IdsIncluded.Concat(",");
|
|
if (!idsIncluded.IsEmpty())
|
|
filter.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Id, aliasOrganisation) + " IN (" + idsIncluded + ")");
|
|
String idsExcluded = prm.IdsExcluded.Concat(",");
|
|
if (!idsExcluded.IsEmpty())
|
|
filter.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Id, aliasOrganisation) + " NOT IN (" + idsExcluded + ")");
|
|
|
|
// Name.
|
|
if (!StringUtil.IsEmpty(prm.NameFull))
|
|
filter.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Name, aliasOrganisation) + "='" + DbQuery.FormatForCondition(prm.NameFull) + "'");
|
|
if (!StringUtil.IsEmpty(prm.NamePartial))
|
|
filter.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Name, aliasOrganisation) + " Like '%" + DbQuery.FormatForLike(prm.NamePartial, true) + "%'");
|
|
|
|
// Organisation filter.
|
|
if (!StringUtil.IsEmpty(prm.OrganisationFilter))
|
|
{
|
|
// Split filter into words.
|
|
List<String> filterWords = prm.OrganisationFilter.Split((char[])null).ToList();
|
|
filterWords.RemoveDuplicatesCI();
|
|
filterWords.RemoveBlanks();
|
|
foreach (String filterWord in filterWords)
|
|
{
|
|
List<String> organisationFilter = new List<String>();
|
|
organisationFilter.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Name, aliasOrganisation) + " Like '%" + DbQuery.FormatForLike(filterWord, true) + "%'");
|
|
filter.Add(organisationFilter.Concat(" OR ", "(", ")"));
|
|
}
|
|
}
|
|
|
|
// Notes.
|
|
if (!StringUtil.IsEmpty(prm.NotesPartial))
|
|
filter.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Notes, aliasOrganisation) + " LIKE '%" + DbQuery.FormatForLike(prm.NotesPartial) + "%'");
|
|
|
|
// Status.
|
|
if (prm.StatusCat == SaxOrganisation.StatusCat.ActiveOnly)
|
|
filter.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Status, aliasOrganisation) + "='" + SacsSystem.GetStatusCode(SaStatus.Active) + "'");
|
|
else if (prm.StatusCat == SaxOrganisation.StatusCat.NotDeleted)
|
|
filter.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Status, aliasOrganisation) + "!='" + SacsSystem.GetStatusCode(SaStatus.Deleted) + "'");
|
|
|
|
if (filter.Count < 1) filter.Add("1=1");
|
|
return filter;
|
|
}
|
|
// Item sort columns.
|
|
public List<String> GetOrganisationSortColumns(SaxOrganisation.Params prm)
|
|
{
|
|
// Sort sequence:
|
|
List<String> sortColumnNames = new List<String>();
|
|
foreach (var columnDfn in prm.SortColumns)
|
|
{
|
|
// Need to ensure correct alias used for Organisation columns in case of conflict.
|
|
String sortCol = String.Empty;
|
|
if (SaxDbDfn.GetTableForColumn((SaxDbDfn.ColumnIdx)columnDfn.DbColumn) == SaxDbDfn.TableIdx.Organisation)
|
|
{
|
|
sortCol = SaxDbDfn.GetColumnNameQual((SaxDbDfn.ColumnIdx)columnDfn.DbColumn, aliasOrganisation);
|
|
}
|
|
else
|
|
{
|
|
sortCol = columnDfn.DataName;
|
|
if (!StringUtil.IsEmpty(columnDfn.SortDataName))
|
|
sortCol = columnDfn.SortDataName;
|
|
}
|
|
if (!sortCol.IsEmpty())
|
|
{
|
|
DbOrderByNullMode orderByNullMode = DbOrderByNullMode.Default; // eSaOrderByDemoteNulls, eSaOrderByPromoteNulls
|
|
sortColumnNames.Add(DbQuery.FormatForOrderBy(sortCol, columnDfn.SortAscending, orderByNullMode).Concat(", "));
|
|
}
|
|
}
|
|
sortColumnNames.RemoveBlanks();
|
|
if (sortColumnNames.IsEmpty())
|
|
{
|
|
// Default sort columns.
|
|
sortColumnNames.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Id, aliasOrganisation) + " ASC");
|
|
}
|
|
return sortColumnNames;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item count.
|
|
private long GetOrganisationCount(String filter)
|
|
{
|
|
ClearErrorState();
|
|
return CoreDatabase.GetRowCount(SaxDbDfn.GetTableName(SaxDbDfn.TableIdx.Organisation) + " As " + aliasOrganisation, filter);
|
|
}
|
|
public override long GetOrganisationCount(SaxOrganisation.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
return GetOrganisationCount(GetOrganisationQueryFilter(prm).Concat(" AND ", "(", ")"));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item list.
|
|
public override List<SaxOrganisation> GetOrganisationList(SaxOrganisation.Params prm)
|
|
{
|
|
List<SaxOrganisation> userAccounts = new List<SaxOrganisation>();
|
|
DbQuery qry = new DbQuery(CoreDatabase.DbmsType);
|
|
qry.Tables.Add(SaxDbDfn.GetTableName(SaxDbDfn.TableIdx.Organisation), aliasOrganisation);
|
|
Organisation_Db dbOrganisation = new Organisation_Db(CoreDbInfo);
|
|
qry.Columns.AddRange(dbOrganisation.ExistingColumnNames);
|
|
qry.Filters.AddRange(GetOrganisationQueryFilter(prm));
|
|
qry.OrderBy.AddRange(GetOrganisationSortColumns(prm));
|
|
// Query data.
|
|
using (var dbConnection = CoreDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
dbCommand.CommandText = qry.BuildQuery(prm.ResultsPaging);
|
|
using (DbDataReader dbDataReader = dbCommand.ExecuteReader())
|
|
{
|
|
while (dbDataReader.Read())
|
|
{
|
|
dbOrganisation = new Organisation_Db(CoreDbInfo);
|
|
dbOrganisation.PopulateFrom(dbDataReader);
|
|
userAccounts.Add(CreateOrganisation(dbOrganisation));
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn(GetType().Name + ".GetOrganisationList\n\tQuery: " + dbCommand.CommandText + "\n\t" + exc.GetType().Name + ": " + exc.Message);
|
|
}
|
|
}
|
|
return userAccounts;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item query execution.
|
|
public override DbResultSet ExecuteOrganisationQuery(List<ColumnDfn> columns, SaxOrganisation.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
|
|
//---------------------------------------------------------------------
|
|
// Construct query.
|
|
DbQuery query = new DbQuery(CoreDatabase.DbmsType);
|
|
query.Tables.Add(SaxDbDfn.GetTableName(SaxDbDfn.TableIdx.Organisation), aliasOrganisation);
|
|
|
|
// Add columns as defined in query list.
|
|
if (columns.HasId(SaxOrganisation.Column.Id))
|
|
query.Columns.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Id, aliasOrganisation));
|
|
if (columns.HasId(SaxOrganisation.Column.Name))
|
|
query.Columns.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Name, aliasOrganisation));
|
|
if (columns.HasId(SaxOrganisation.Column.PhoneMain))
|
|
query.Columns.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_PhoneMain, aliasOrganisation));
|
|
if (columns.HasId(SaxOrganisation.Column.PhoneAlt))
|
|
query.Columns.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_PhoneAlt, aliasOrganisation));
|
|
if (columns.HasId(SaxOrganisation.Column.OrganisationType) || columns.HasId(SaxOrganisation.Column.OrganisationType_Desc))
|
|
query.Columns.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_OrganisationType, aliasOrganisation));
|
|
if (columns.HasId(SaxOrganisation.Column.OrganisationCategory) || columns.HasId(SaxOrganisation.Column.OrganisationCategory_Desc))
|
|
query.Columns.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_OrganisationCategory, aliasOrganisation));
|
|
// Common properties.
|
|
if (columns.HasId(SaxOrganisation.Column.Notes))
|
|
query.Columns.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Notes, aliasOrganisation));
|
|
if (columns.HasId(SaxOrganisation.Column.Flags))
|
|
query.Columns.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Flags, aliasOrganisation));
|
|
if (columns.HasId(SaxOrganisation.Column.Status) || columns.HasId(SaxOrganisation.Column.Status_Desc) || columns.HasId(SaxOrganisation.Column.Status_Ind))
|
|
query.Columns.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Status, aliasOrganisation));
|
|
if (columns.HasId(SaxOrganisation.Column.CreateOn_Date))
|
|
query.Columns.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_CreateOn, aliasOrganisation));
|
|
if (columns.HasId(SaxOrganisation.Column.UpdateOn_Date))
|
|
query.Columns.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_UpdateOn, aliasOrganisation));
|
|
|
|
//---------------------------------------------------------------------
|
|
// User count.
|
|
if (columns.HasId(SaxOrganisation.Column.User_Count))
|
|
{
|
|
String aliasUserCount = "aUsrOrg_Count";
|
|
DbQuery queryUserCount = new DbQuery(CoreDatabase.DbmsType);
|
|
queryUserCount.Tables.Add(SaxDbDfn.GetTableName(SaxDbDfn.TableIdx.UserOrganisation), aliasUserCount);
|
|
queryUserCount.Columns.Add("Count(*)");
|
|
queryUserCount.Filters.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.UserOrganisation_OrganisationId, aliasUserCount) + "=" + SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Id, aliasOrganisation));
|
|
queryUserCount.Filters.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.UserOrganisation_Status, aliasUserCount) + "='" + SacsSystem.GetStatusCode(SaStatus.Active) + "'");
|
|
query.Columns.Add("(" + queryUserCount.BuildQuery() + ")", colOrganisation_Member_Count);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
// Member count.
|
|
if (columns.HasId(SaxOrganisation.Column.Member_Count))
|
|
{
|
|
String aliasMemberCount = "aUsrOrg_Count";
|
|
DbQuery queryMemberCount = new DbQuery(CoreDatabase.DbmsType);
|
|
queryMemberCount.Tables.Add(SaxDbDfn.GetTableName(SaxDbDfn.TableIdx.UserOrganisation), aliasMemberCount);
|
|
queryMemberCount.Columns.Add("Count(*)");
|
|
queryMemberCount.Filters.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.UserOrganisation_OrganisationId, aliasMemberCount) + "=" + SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.Organisation_Id, aliasOrganisation));
|
|
queryMemberCount.Filters.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.UserOrganisation_OrganisationRoleCount, aliasMemberCount) + ">0");
|
|
queryMemberCount.Filters.Add(SaxDbDfn.GetColumnNameQual(SaxDbDfn.ColumnIdx.UserOrganisation_Status, aliasMemberCount) + "='" + SacsSystem.GetStatusCode(SaStatus.Active) + "'");
|
|
query.Columns.Add("(" + queryMemberCount.BuildQuery() + ")", colOrganisation_Member_Count);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
// Populate filter.
|
|
query.Filters.AddRange(GetOrganisationQueryFilter(prm));
|
|
|
|
if (prm.IdsIncluded.Count != 1)
|
|
{
|
|
// Configure sort sequence.
|
|
foreach (String sortColumn in GetOrganisationSortColumns(prm))
|
|
query.OrderBy.Add(sortColumn);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
//return new DbResultSet_DataTableReader(db, query.BuildQuery(prm.ResultsPaging));
|
|
return DbResultSet_DataReader.ExecuteQuery(CoreDatabase, query.BuildQuery(prm.ResultsPaging));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|