588 lines
27 KiB
C#
588 lines
27 KiB
C#
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 Saxonite.Base.App;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using Saxonite.Net.Standard.Base.Bus.Reference;
|
|
using Saxonite.Net.Standard.Dbas.Bus.Reference;
|
|
using Saxonite.Net.Standard.Dbas.Internal.Reference;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.Linq;
|
|
|
|
namespace Saxonite.Net.Standard.Dbas.Mgr.Reference
|
|
{
|
|
//=======================================================================
|
|
partial class SaxReferenceGolfMgr_Dbas
|
|
{
|
|
//*******************************************************************
|
|
// Support Mgr - User Report
|
|
//*******************************************************************
|
|
// Utilities.
|
|
private List<StringPair> GetLogProperties(SaxGolfUsgaCountry usgaCountry)
|
|
{
|
|
return new List<StringPair>()
|
|
{
|
|
new StringPair("Id",SysId.ToString(usgaCountry.Id)),
|
|
new StringPair("CountryCode",usgaCountry.CountryCode),
|
|
new StringPair("CountryName",usgaCountry.CountryName),
|
|
new StringPair("Status",usgaCountry.StatusCode)
|
|
};
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Create item.
|
|
public override SaxGolfUsgaCountry CreateUsgaCountry()
|
|
{
|
|
ClearErrorState();
|
|
return new SaxGolfUsgaCountry_Dbas(ReferenceDatabase, null, 0);
|
|
}
|
|
private SaxGolfUsgaCountry_Dbas CreateUsgaCountry(Golf_UsgaCountry_Db dbUsgaCountry)
|
|
{
|
|
ClearErrorState();
|
|
SaxGolfUsgaCountry_Dbas usgaCountry = new SaxGolfUsgaCountry_Dbas(ReferenceDatabase, new SysId(dbUsgaCountry._Id), (ushort)dbUsgaCountry._Flags);
|
|
PopulateFrom(dbUsgaCountry, usgaCountry);
|
|
return usgaCountry;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get item.
|
|
public override SaxGolfUsgaCountry GetUsgaCountry(SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
return GetUsgaCountry(GetUsgaCountryQueryFilter(prm).Concat(" And ", "(", ")"));
|
|
}
|
|
private SaxGolfUsgaCountry GetUsgaCountry(String filter)
|
|
{
|
|
ClearErrorState();
|
|
long count = GetUsgaCountryCount(filter);
|
|
if (count == 0) return null;
|
|
if (count != 1)
|
|
{
|
|
SetErrorState("Multiple items found");
|
|
return null;
|
|
}
|
|
SaxGolfUsgaCountry usgaCountry = null;
|
|
// Query data.
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
Golf_UsgaCountry_Db dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
dbUsgaCountry.PopulateFrom(dbCommand, aliasUsgaCountry, filter);
|
|
usgaCountry = CreateUsgaCountry(dbUsgaCountry);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SetErrorState("Database query exception");
|
|
SaLogger.Warn(GetType().Name + ".GetUsgaCountry\n\tQuery: " + dbCommand.CommandText + "\n\t" + ExceptionUtil.GetLogMessage(exc));
|
|
}
|
|
}
|
|
return usgaCountry;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Validate item.
|
|
public override bool IsValidUsgaCountryData(SaxGolfUsgaCountry.Data data, SaxGolfUsgaCountry.ErrField errField)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.IsValidUsgaCountryData(data, errField)) return false;
|
|
return true;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Insert item.
|
|
public override bool InsertUsgaCountry(SaxGolfUsgaCountry usgaCountry, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".InsertUsgaCountry";
|
|
ClearErrorState();
|
|
if (!IsValidUsgaCountry(usgaCountry)) return false;
|
|
if (SysId.IsValid(usgaCountry.Id)) return SetErrorState(ErrorMessage.ValiditemId);
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
Golf_UsgaCountry_Db dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
PopulateTo(dbUsgaCountry, usgaCountry, dbUpdateInfo);
|
|
if (Golf_UsgaCountry_Db.FieldsNonId.Contains("Id"))
|
|
{
|
|
// Generate next Id.
|
|
dbUsgaCountry._Id = ReferenceDatabase.GetNextIdFromTable("UsgaCountry");
|
|
}
|
|
dbUsgaCountry.ExecuteInsertCommand(dbCommand);
|
|
usgaCountry.Id = new SysId(dbUsgaCountry._Id);
|
|
usgaCountry.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Inserted. " + GetLogProperties(usgaCountry).Concat(": ").Concat("", "\n\t"));
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Update item.
|
|
public override bool UpdateUsgaCountry(SaxGolfUsgaCountry usgaCountry, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".UpdateUsgaCountry";
|
|
ClearErrorState();
|
|
if (!IsValidUsgaCountry(usgaCountry)) return false;
|
|
if (!SysId.IsValid(usgaCountry.Id)) return SetErrorState(ErrorMessage.InvalidItemId);
|
|
if (!usgaCountry.Changed)
|
|
{
|
|
if (DebugMode) SaLogger.Debug(fn + " Data not changed. " + GetLogProperties(usgaCountry).Concat(": ").Concat("", "\n\t"));
|
|
return true;
|
|
}
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve item.
|
|
dbConnection.Open();
|
|
Golf_UsgaCountry_Db dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
dbUsgaCountry.PopulateFrom(dbCommand, aliasUsgaCountry, "Id=" + SysId.ToString(usgaCountry.Id));
|
|
// ToDo SaxUpdateLogItemMonitor_UsgaCountry updateLogMonitor = new SaxUpdateLogItemMonitor_UsgaCountry(SessionContext, CreateUsgaCountry(dbUsgaCountry), dbUpdateInfo);
|
|
// Update item.
|
|
PopulateTo(dbUsgaCountry, usgaCountry, dbUpdateInfo);
|
|
dbUsgaCountry.ExecuteUpdateCommand(dbCommand);
|
|
usgaCountry.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Updated. " + GetLogProperties(usgaCountry).Concat(": ").Concat("", "\n\t"));
|
|
// Record updates.
|
|
// ToDo updateLogMonitor.RecordUpdatedValues(usgaCountry);
|
|
// ToDo updateLogMonitor.LogUpdates();
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Delete item.
|
|
public override bool UsgaCountryCanBeDeleted(SaxGolfUsgaCountry usgaCountry)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.UsgaCountryCanBeDeleted(usgaCountry)) return false;
|
|
return true;
|
|
}
|
|
public override bool DeleteUsgaCountry(SaxGolfUsgaCountry usgaCountry, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".DeleteUsgaCountry";
|
|
ClearErrorState();
|
|
if (!UsgaCountryCanBeDeleted(usgaCountry)) return false;
|
|
|
|
// User Roles are logically deleted by changing the status.
|
|
usgaCountry.Status = SaStatus.Deleted;
|
|
return UpdateUsgaCountry(usgaCountry, dbUpdateInfo);
|
|
|
|
/*
|
|
* Physical deletion.
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve item.
|
|
dbConnection.Open();
|
|
Golf_UsgaCountry_Db dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
dbUsgaCountry.PopulateFrom(dbCommand, aliasUsgaCountry, "Id=" + usgaCountry.Id.ToString());
|
|
// Delete item.
|
|
dbCommand.CommandType = System.Data.CommandType.Text;
|
|
dbCommand.CommandText = "DELETE FROM " + SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry) + " WHERE Id=" + SysId.ToString(usgaCountry.Id) + ";";
|
|
dbCommand.ExecuteNonQuery();
|
|
usgaCountry.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Deleted. " + GetLogProperties(usgaCountry).Concat(": ").Concat("", "\n\t"));
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
*/
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Copy data properties to and from database.
|
|
private void PopulateTo(Golf_UsgaCountry_Db dbUsgaCountry, SaxGolfUsgaCountry usgaCountry, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
dbUsgaCountry._Id = SysId.ToLong(usgaCountry.Id);
|
|
dbUsgaCountry._CountryCode = usgaCountry.CountryCode;
|
|
dbUsgaCountry._EntCountryCode = usgaCountry.EntCountryCode;
|
|
dbUsgaCountry._CountryName = usgaCountry.CountryName;
|
|
// Common.
|
|
dbUsgaCountry._Notes = usgaCountry.Notes;
|
|
dbUsgaCountry._Status = SacsSystem.GetStatusCode(usgaCountry.Status);
|
|
dbUsgaCountry._Flags = (short)usgaCountry.Flags;
|
|
if (!SysId.IsValid(usgaCountry.Id))
|
|
{
|
|
dbUsgaCountry._CreateOn = DateTime.Now;
|
|
dbUsgaCountry._CreateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbUsgaCountry._CreateHost = dbUpdateInfo.Host;
|
|
}
|
|
dbUsgaCountry._UpdateOn = DateTime.Now;
|
|
dbUsgaCountry._UpdateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbUsgaCountry._UpdateHost = dbUpdateInfo.Host;
|
|
}
|
|
private void PopulateFrom(Golf_UsgaCountry_Db dbUsgaCountry, SaxGolfUsgaCountry usgaCountry)
|
|
{
|
|
//usgaCountry.Id = new SysId(dbUsgaCountry._Id);
|
|
usgaCountry.CountryCode = dbUsgaCountry._CountryCode;
|
|
usgaCountry.EntCountryCode = dbUsgaCountry._EntCountryCode;
|
|
usgaCountry.CountryName = dbUsgaCountry._CountryName;
|
|
// Common.
|
|
usgaCountry.Notes = dbUsgaCountry._Notes;
|
|
usgaCountry.Status = SacsSystem.GetStatusForCode(dbUsgaCountry._Status);
|
|
usgaCountry.SetCreateStamp(dbUsgaCountry._CreateOn, new SysId(dbUsgaCountry._CreateBy), dbUsgaCountry._CreateHost);
|
|
usgaCountry.SetUpdateStamp(dbUsgaCountry._UpdateOn, new SysId(dbUsgaCountry._UpdateBy), dbUsgaCountry._UpdateHost);
|
|
usgaCountry.AcceptChanges();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Bulk updates.
|
|
/// <summary>
|
|
/// Bulk update USGA Countries.
|
|
/// <para>- Country code must be supplied to act as identifier.</para>
|
|
/// <para>- Other properties are validated as per country rules - check error state.</para>
|
|
/// <para>- Other properties are left as is if null.</para>
|
|
/// </summary>
|
|
public override bool BulkUpdateUsgaCountries(List<UsgaCountryData> countries, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
ClearErrorState();
|
|
List<UsgaCountryData> countriesToUpdate = new List<UsgaCountryData>();
|
|
|
|
// Validate supplied data.
|
|
foreach (UsgaCountryData country in countries)
|
|
{
|
|
// Validate country properties.
|
|
// Code is mandatory, other values only validated if non-null.
|
|
if (!IsValidValue(country.CountryCode, "code", (long)SaxDbDfn_Reference.GetColTextLen(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryCode), true)) return false;
|
|
if (country.EntCountryCode != null && !IsValidValue(country.EntCountryCode, "ent code", (long)SaxDbDfn_Reference.GetColTextLen(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_EntCountryCode))) return false;
|
|
if (country.CountryName != null && !IsValidValue(country.CountryName, "name", (long)SaxDbDfn_Reference.GetColTextLen(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName))) return false;
|
|
countriesToUpdate.Add(country);
|
|
}
|
|
int MaxBulkUpdateBatchSize = 64;
|
|
int batchSize = countriesToUpdate.Any() ? (int)Math.Ceiling(countriesToUpdate.Count() / Math.Ceiling((double)countriesToUpdate.Count() / MaxBulkUpdateBatchSize)) : MaxBulkUpdateBatchSize;
|
|
while (countriesToUpdate.Any())
|
|
{
|
|
List<String> query = new List<string>();
|
|
query.Add($"Declare @statusActive VarChar(3) = '{SacsSystem.GetStatusCode(SaxGolfUsgaCountry.DefaultStatus)}';");
|
|
query.Add($"Declare @updateOn DateTime = GetUtcDate();");
|
|
query.Add($"Declare @updateBy Int = {SysId.ToLong(dbUpdateInfo.UserId)};");
|
|
query.Add($"Declare @updateHost VarChar(100) = '{DbQuery.FormatForCondition(dbUpdateInfo.Host)}';");
|
|
query.Add("Merge");
|
|
query.Add($"Into {SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry)} uCty");
|
|
query.Add("Using");
|
|
query.Add("(");
|
|
query.Add("Select '' CountryCode, '' EntCountryCode, '' CountryName");
|
|
foreach (UsgaCountryData country in countriesToUpdate.ExtractRange(0, batchSize))
|
|
query.Add($"Union Select '{DbQuery.FormatForCondition(country.CountryCode)}', " + (country.EntCountryCode != null ? $"'{DbQuery.FormatForCondition(country.EntCountryCode)}'" : "Null") + ", " + (country.CountryName != null ? $"'{DbQuery.FormatForCondition(country.CountryName)}'" : "Null"));
|
|
query.Add(") dCty");
|
|
query.Add("On uCty.CountryCode=dCty.CountryCode");
|
|
query.Add("When Matched And ((dCty.EntCountryCode Is Not Null And uCty.EntCountryCode!=dCty.EntCountryCode) Or (dCty.CountryName Is Not Null And uCty.CountryName!=dCty.CountryName) Or uCty.Status!=@statusActive)");
|
|
query.Add("Then Update Set");
|
|
query.Add(" EntCountryCode=Coalesce(dCty.EntCountryCode,uCty.EntCountryCode),");
|
|
query.Add(" CountryName=Coalesce(dCty.CountryName,uCty.CountryName),");
|
|
query.Add(" Status=@statusActive,");
|
|
query.Add(" UpdateOn=@updateOn,");
|
|
query.Add(" UpdateBy=@updateBy,");
|
|
query.Add(" UpdateHost=@updateHost");
|
|
query.Add("When Not Matched And Len(IsNull(dCty.CountryName,''))>0");
|
|
query.Add("Then Insert (CountryCode,EntCountryCode,CountryName,Notes,Flags,Status,CreateOn,CreateBy,CreateHost,UpdateOn,UpdateBy,UpdateHost)");
|
|
query.Add($"Values (dCty.CountryCode,Coalesce(dCty.EntCountryCode,''),dCty.CountryName,'',0,@statusActive,@updateOn,@updateBy,@updateHost,@updateOn,@updateBy,@updateHost)");
|
|
query.Add(";");
|
|
//SaLogger.Info($"{SysUtil.Fn} Bulk USGA Country update query:\n\t{query.Concat("\n\t")}");
|
|
|
|
try
|
|
{
|
|
int result = ReferenceDatabase.ExecuteNonQuery(query.Concat("\n"));
|
|
if (ReferenceDatabase.HasErrorState())
|
|
SaLogger.Warn($"{SysUtil.Fn} Error executing bulk USGA Country update - {ReferenceDatabase.ErrorStateMessage}");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn(SysUtil.Fn + " " + ExceptionUtil.GetLogMessage(exc, " executing bulk USGA Country update"));
|
|
}
|
|
}
|
|
return HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item column definitions.
|
|
private static readonly String aliasUsgaCountry = "UsgPrv";
|
|
private static readonly String aliasUsgaCountry_Dummy = "UsgPrv_Cty";
|
|
private static readonly String prefixUsgaCountry_Dummy = aliasUsgaCountry_Dummy + "_";
|
|
private void SetUsgaCountryColDfn
|
|
(ColumnDfn col, String caption, int width, SaxDbDfn_Reference.ColumnIdx dbCol,
|
|
ColumnAlignment align = ColumnAlignment.Default, DataValue.DataType dataType = DataValue.DataType.String,
|
|
String alias = "", String prefix = "")
|
|
{
|
|
if (String.IsNullOrEmpty(alias) && SaxDbDfn_Reference.GetTableForColumn(dbCol) == SaxDbDfn_Reference.TableIdx.UsgaCountry)
|
|
alias = aliasUsgaCountry;
|
|
col.Set(caption, width, (int)dbCol, prefix + SaxDbDfn_Reference.GetColumnName(dbCol, false), SaxDbDfn_Reference.GetColumnNameQual(dbCol, alias), dataType, align);
|
|
if (dataType == DataValue.DataType.String && dbCol != SaxDbDfn_Reference.ColumnIdx.Unknown)
|
|
col.MaxLength = (int)SaxDbDfn_Reference.GetColTextLen(dbCol);
|
|
}
|
|
public override ColumnDfn GetUsgaCountryColumnDfn(SaxGolfUsgaCountry.Column columnId, string caption = "", int width = 0)
|
|
{
|
|
ColumnDfn columnDfn = new ColumnDfn((int)columnId, caption, width);
|
|
switch (columnId)
|
|
{
|
|
//-----------------------------------------------------------
|
|
case SaxGolfUsgaCountry.Column.Pos:
|
|
SetUsgaCountryColDfn(columnDfn, "Pos", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Right);
|
|
columnDfn.SortMode = ColumnDfn.ColumnSortMode.Off;
|
|
columnDfn.ReadOnly = true;
|
|
columnDfn.Resizable = false;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.Id:
|
|
SetUsgaCountryColDfn(columnDfn, "Id", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, ColumnAlignment.Right, DataValue.DataType.Long);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
|
|
//-------------------------------------------------------
|
|
case SaxGolfUsgaCountry.Column.CountryCode:
|
|
SetUsgaCountryColDfn(columnDfn, "Code", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryCode, ColumnAlignment.Center);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.CountryName:
|
|
SetUsgaCountryColDfn(columnDfn, "Name", 120, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName, ColumnAlignment.Center);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
|
|
//-----------------------------------------------------------
|
|
// Derived properties.
|
|
|
|
//-----------------------------------------------------------
|
|
// Common properties.
|
|
case SaxGolfUsgaCountry.Column.Notes:
|
|
SetUsgaCountryColDfn(columnDfn, "Notes", 240, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Notes);
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.Flags:
|
|
SetUsgaCountryColDfn(columnDfn, "Flags", 60, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Flags, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.Status:
|
|
SetUsgaCountryColDfn(columnDfn, "Status", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Status, ColumnAlignment.Center);
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.Status_Desc:
|
|
SetUsgaCountryColDfn(columnDfn, "Status", 90, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Status, ColumnAlignment.Center);
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.Status_Ind:
|
|
SetUsgaCountryColDfn(columnDfn, "Status", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Status, ColumnAlignment.Center, DataValue.DataType.TriState);
|
|
columnDfn.TriStateFormat = ColumnTriStateFormat.Lights;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.CreateOn_Date:
|
|
SetUsgaCountryColDfn(columnDfn, "Created", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CreateOn, ColumnAlignment.Center, DataValue.DataType.Date);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.UpdateOn_Date:
|
|
SetUsgaCountryColDfn(columnDfn, "Updated", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_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> GetUsgaCountryQueryFilter(SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
List<String> filter = new List<String>();
|
|
|
|
// Item Ids.
|
|
String idsIncluded = prm.IdsIncluded.Concat(",");
|
|
if (!idsIncluded.IsEmpty())
|
|
filter.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry) + " IN (" + idsIncluded + ")");
|
|
String idsExcluded = prm.IdsExcluded.Concat(",");
|
|
if (!idsExcluded.IsEmpty())
|
|
filter.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry) + " NOT IN (" + idsExcluded + ")");
|
|
|
|
if (!StringUtil.IsBlank(prm.CountryFilter))
|
|
{
|
|
// Split filter into words.
|
|
List<String> filterWords = prm.CountryFilter.Split((char[])null).ToList();
|
|
filterWords.RemoveDuplicatesCI();
|
|
filterWords.RemoveBlanks();
|
|
foreach (String filterWord in filterWords)
|
|
{
|
|
List<String> articleFilter = new List<String>();
|
|
articleFilter.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryCode, aliasUsgaCountry) + " Like '%" + DbQuery.FormatForLike(filterWord, true) + "%'");
|
|
articleFilter.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName, aliasUsgaCountry) + " Like '%" + DbQuery.FormatForLike(filterWord, true) + "%'");
|
|
filter.Add(articleFilter.Concat(" OR ", "(", ")"));
|
|
}
|
|
}
|
|
|
|
// Name.
|
|
if (!StringUtil.IsEmpty(prm.CountryNameFull))
|
|
filter.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName, aliasUsgaCountry) + "='" + DbQuery.FormatForCondition(prm.CountryNameFull) + "'");
|
|
if (!StringUtil.IsEmpty(prm.CountryNamePartial))
|
|
filter.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName, aliasUsgaCountry) + " LIKE '%" + DbQuery.FormatForLike(prm.CountryNamePartial) + "%'");
|
|
|
|
// Notes.
|
|
if (!StringUtil.IsEmpty(prm.NotesPartial))
|
|
filter.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Notes, aliasUsgaCountry) + " LIKE '%" + DbQuery.FormatForLike(prm.NotesPartial) + "%'");
|
|
|
|
// Status.
|
|
if (prm.StatusCat != SaxGolfUsgaCountry.StatusCat.Any)
|
|
filter.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Status, aliasUsgaCountry) + GetUsgaCountryStatusCatFilterCondition(prm.StatusCat));
|
|
|
|
if (filter.Count < 1) filter.Add("1=1");
|
|
return filter;
|
|
}
|
|
internal static String GetUsgaCountryStatusCatFilterCondition(SaxGolfUsgaCountry.StatusCat statusCat)
|
|
{
|
|
if (statusCat == SaxGolfUsgaCountry.StatusCat.ActiveOnly)
|
|
return "='" + SacsSystem.GetStatusCode(SaStatus.Active) + "'";
|
|
if (statusCat == SaxGolfUsgaCountry.StatusCat.InactiveOnly)
|
|
return "='" + SacsSystem.GetStatusCode(SaStatus.Inactive) + "'";
|
|
if (statusCat == SaxGolfUsgaCountry.StatusCat.NotDeleted)
|
|
return "!='" + SacsSystem.GetStatusCode(SaStatus.Deleted) + "'";
|
|
if (statusCat == SaxGolfUsgaCountry.StatusCat.DeletedOnly)
|
|
return "='" + SacsSystem.GetStatusCode(SaStatus.Deleted) + "'";
|
|
return "";
|
|
}
|
|
// Item sort columns.
|
|
public List<String> GetUsgaCountrySortColumns(SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
// Sort sequence:
|
|
List<String> sortColumnNames = new List<String>();
|
|
foreach (var columnDfn in prm.SortColumns)
|
|
{
|
|
// Need to ensure correct alias used for UsgaCountry columns in case of conflict.
|
|
String sortCol = String.Empty;
|
|
if (SaxDbDfn_Reference.GetTableForColumn((SaxDbDfn_Reference.ColumnIdx)columnDfn.DbColumn) == SaxDbDfn_Reference.TableIdx.UsgaCountry)
|
|
{
|
|
sortCol = SaxDbDfn_Reference.GetColumnNameQual((SaxDbDfn_Reference.ColumnIdx)columnDfn.DbColumn, aliasUsgaCountry);
|
|
}
|
|
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_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry) + " ASC");
|
|
}
|
|
return sortColumnNames;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item count.
|
|
private long GetUsgaCountryCount(String filter)
|
|
{
|
|
ClearErrorState();
|
|
return ReferenceDatabase.GetRowCount(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry) + " As " + aliasUsgaCountry, filter);
|
|
}
|
|
public override long GetUsgaCountryCount(SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
return GetUsgaCountryCount(GetUsgaCountryQueryFilter(prm).Concat(" AND ", "(", ")"));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item list.
|
|
public override List<SaxGolfUsgaCountry> GetUsgaCountryList(SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
List<SaxGolfUsgaCountry> userRoles = new List<SaxGolfUsgaCountry>();
|
|
DbQuery qry = new DbQuery(ReferenceDatabase.DbmsType);
|
|
qry.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry), aliasUsgaCountry);
|
|
Golf_UsgaCountry_Db dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
qry.Columns.AddRange(dbUsgaCountry.ExistingColumnNames);
|
|
qry.Filters.AddRange(GetUsgaCountryQueryFilter(prm));
|
|
qry.OrderBy.AddRange(GetUsgaCountrySortColumns(prm));
|
|
// Query data.
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
dbCommand.CommandText = qry.BuildQuery(prm.ResultsPaging);
|
|
using (DbDataReader dbDataReader = dbCommand.ExecuteReader())
|
|
{
|
|
while (dbDataReader.Read())
|
|
{
|
|
dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
dbUsgaCountry.PopulateFrom(dbDataReader);
|
|
userRoles.Add(CreateUsgaCountry(dbUsgaCountry));
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn(GetType().Name + ".GetUsgaCountryList\n\tQuery: " + dbCommand.CommandText + "\n\t" + ExceptionUtil.GetLogMessage(exc));
|
|
}
|
|
}
|
|
return userRoles;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item query execution.
|
|
public override DbResultSet ExecuteUsgaCountryQuery(List<ColumnDfn> columns, SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
|
|
//---------------------------------------------------------------------
|
|
// Construct query.
|
|
DbQuery query = new DbQuery(ReferenceDatabase.DbmsType);
|
|
query.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry), aliasUsgaCountry);
|
|
|
|
// Add columns as defined in query list.
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.Id))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.CountryCode))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryCode, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.CountryName))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName, aliasUsgaCountry));
|
|
// Common properties.
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.Notes))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Notes, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.Flags))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Flags, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.Status) || columns.HasId(SaxGolfUsgaCountry.Column.Status_Desc) || columns.HasId(SaxGolfUsgaCountry.Column.Status_Ind))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Status, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.CreateOn_Date))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CreateOn, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.UpdateOn_Date))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_UpdateOn, aliasUsgaCountry));
|
|
|
|
//---------------------------------------------------------------------
|
|
// Populate filter.
|
|
query.Filters.AddRange(GetUsgaCountryQueryFilter(prm));
|
|
|
|
if (prm.IdsIncluded.Count != 1)
|
|
{
|
|
// Configure sort sequence.
|
|
foreach (String sortColumn in GetUsgaCountrySortColumns(prm))
|
|
query.OrderBy.Add(sortColumn);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
//return new DbResultSet_DataTableReader(db, query.BuildQuery(prm.ResultsPaging));
|
|
return DbResultSet_DataReader.ExecuteQuery(ReferenceDatabase, query.BuildQuery(prm.ResultsPaging));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|