using System; using System.Collections.Generic; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Dbas; using Sarand.Net.Standard.Base.Interfaces; using Sarand.Net.Standard.Dbas.Base; using Sarand.Net.Standard.Base.Utils; namespace Sarand.Net.Standard.Dbas.Interfaces { //======================================================================= public abstract class SaInterfaceConsole_Dbas : SaInterfaceConsole { //------------------------------------------------------------------- // Data members. private SaInterfaceMgr_Dbas interfaceMgr = null; //------------------------------------------------------------------- // Static initialisation. static SaInterfaceConsole_Dbas () { } //------------------------------------------------------------------- // Construction. protected SaInterfaceConsole_Dbas (SacsDatabase database) { Database = database; interfaceMgr = new SaInterfaceMgr_Dbas(this); } protected override void Dispose(bool disposing) { if (disposing) { if (interfaceMgr != null) interfaceMgr.Dispose(); interfaceMgr = null; } base.Dispose(disposing); } //------------------------------------------------------------------- // Properties. internal SacsDatabase Database { get; set; } public override bool DatabaseIsValid { get { return interfaceMgr.DatabaseIsValid; } } public override bool CanUpdateDatabase { get { return interfaceMgr.CanUpdateDatabase; } } //------------------------------------------------------------------- // M a n a g e I n t e r f a c e s //------------------------------------------------------------------- // Manage interfaces - returns true if changes made. public override bool ManageInterfaces (String interfaceContext, bool userCanManageInterfaces, bool userCanProcessInterfaces, DbUpdateInfo dbUpdateInfo) { ClearErrorState(); MaintainInterfacesFn?.Invoke(this, interfaceContext, userCanManageInterfaces, userCanProcessInterfaces, dbUpdateInfo); return false; } // Method to display Interface management dialog. protected delegate bool MaintainInterfacesFunction (SaInterfaceConsole codeTypeConsole, String interfaceContext, bool userCanManageInterfaces, bool userCanProcessInterfaces, DbUpdateInfo dbUpdateInfo); protected abstract MaintainInterfacesFunction MaintainInterfacesFn { get; } //------------------------------------------------------------------- // I n t e r f a c e M a n a g e m e n t //------------------------------------------------------------------- // Interface management. public override SysId CreateInterface (String context, SaInterfaceCategory interfaceCategory, short interfaceType, String code, String caption, DbUpdateInfo dbUpdateInfo) { ClearErrorState(); SaInterface interfaceObj = interfaceMgr.CreateInterface(context,interfaceCategory,interfaceType); interfaceObj.Code = code; interfaceObj.Caption = caption; if (!interfaceMgr.InsertInterface(interfaceObj,dbUpdateInfo)) { SetErrorState(interfaceMgr.ErrorStateMessage); return null; } return interfaceObj.Id; } // Clear configuration options, data items, processes etc. public override bool ClearDependentData (SaInterface interfaceObj, DbUpdateInfo dbUpdateInfo) { ClearErrorState(); throw new NotImplementedException(); //return !HasErrorState(); } //--------------------------------------------------------------- // Retrieve interfaces. public override SaInterface GetInterface (SaInterface.Params prmInterface) { return interfaceMgr.GetInterface(prmInterface); } public override SaInterface GetInterfaceById (SysId interfaceId) { return interfaceMgr.GetInterfaceById(interfaceId); } public override List GetInterfaceList (SaInterface.Params prmInterface) { return interfaceMgr.GetInterfaceList(prmInterface); } //--------------------------------------------------------------- // Conversion between Interface Category & Type and the equivalent database Id (if only one exists). public override SysId GetInterfaceIdForContextCategoryType (String context, SaInterfaceCategory interfaceCategory, short interfaceType) { ClearErrorState(); SaInterface.Params prmInterface = new SaInterface.Params(); prmInterface.Context = context; prmInterface.InterfaceCategory = interfaceCategory; prmInterface.IncludedInterfaceTypes.Add(interfaceType); prmInterface.ActiveOnly = false; prmInterface.StatusCat = SaInterface.StatusCat.Any; SaInterface interfaceObj = interfaceMgr.GetInterface(prmInterface); if (interfaceObj != null) return interfaceObj.Id; return null; } // Conversion between Interface Category & Code and the equivalent database Id (if only one exists). public override SysId GetInterfaceIdForContextCategoryCode (String context, SaInterfaceCategory interfaceCategory, String code) { ClearErrorState(); SaInterface.Params prmInterface = new SaInterface.Params(); prmInterface.Context = context; prmInterface.InterfaceCategory = interfaceCategory; prmInterface.Code = code; prmInterface.ActiveOnly = false; prmInterface.StatusCat = SaInterface.StatusCat.Any; SaInterface interfaceObj = interfaceMgr.GetInterface(prmInterface); if (interfaceObj != null) return interfaceObj.Id; return null; } public override SaInterfaceCategory GetCategoryForInterfaceId (SysId interfaceId) { SaInterface interfaceObj = interfaceMgr.GetInterfaceById(interfaceId); if (interfaceObj != null) return interfaceObj.Category; return SaInterfaceCategory.Unknown; } public override String GetCodeForInterfaceId (SysId interfaceId) { SaInterface interfaceObj = interfaceMgr.GetInterfaceById(interfaceId); if (interfaceObj != null) return interfaceObj.Code; return String.Empty; } //--------------------------------------------------------------- // I n t e r f a c e P r o c e s s e s //--------------------------------------------------------------- // Identify the highest sequence currently in use for an interface. public override int GetMaxProcessSequence (SysId interfaceId, SaInterfaceProcessDirection processDirection, bool completedOnly = true) { ClearErrorState(); int sequence = interfaceMgr.GetMaxProcessSequenceForInterface(interfaceId, processDirection); if (interfaceMgr.HasErrorState()) SetErrorState(interfaceMgr.ErrorStateMessage); return sequence; } //--------------------------------------------------------------- // Create new process and imediately store details on database (status set to 'In Progress'). public override SaInterfaceProcess StartInterfaceProcess (SysId interfaceId, SaInterfaceProcessDirection eDirection, String batchRef, DateTime batchAt, int sequence, DbUpdateInfo dbUpdateInfo) { ClearErrorState(); if (!interfaceMgr.InterfaceExistsById(interfaceId)) { SetErrorState("Unable to create process - interface does not exist"); return null; } SaInterfaceProcess interfaceProcess = interfaceMgr.CreateInterfaceProcess(interfaceId, eDirection, batchRef, sequence); interfaceProcess.ProcessAt = DateTime.Now; interfaceProcess.BatchAt = batchAt; interfaceProcess.Status = SaStatus.InProgress; if (!interfaceMgr.InsertInterfaceProcess(interfaceProcess,dbUpdateInfo)) { interfaceProcess = null; SetErrorState(interfaceMgr.ErrorStateMessage); } return interfaceProcess; } //--------------------------------------------------------------- // Retrieve a process using its identity (restricted by interface). public override SaInterfaceProcess GetInterfaceProcess (SysId interfaceId, SysId interfaceProcessId) { ClearErrorState(); SaInterfaceProcess interfaceProcess = interfaceMgr.GetInterfaceProcessById(interfaceProcessId); if (interfaceMgr.HasErrorState()) { SetErrorState(interfaceMgr.ErrorStateMessage); interfaceProcess = null; } if (interfaceProcess != null && !interfaceProcess.InterfaceId.Equals(interfaceId)) { SetErrorState("Process does not match interface"); interfaceProcess = null; } return interfaceProcess; } //--------------------------------------------------------------- // Update the details for a process. public override bool StoreInterfaceProcess (SaInterfaceProcess interfaceProcess, DbUpdateInfo dbUpdateInfo) { ClearErrorState(); if (!SysId.IsValid(interfaceProcess.Id)) { // Insert interface process. if (!interfaceMgr.InsertInterfaceProcess(interfaceProcess, dbUpdateInfo)) return SetErrorState("Failed to insert process - "+interfaceMgr.ErrorStateMessage); } else { // Update interface process. if (!interfaceMgr.UpdateInterfaceProcess(interfaceProcess, dbUpdateInfo)) return SetErrorState("Failed to update process - " + interfaceMgr.ErrorStateMessage); } return !HasErrorState(); } //--------------------------------------------------------------- // I n t e r f a c e P r o c e s s M e s s a g e s //--------------------------------------------------------------- public override bool LogInterfaceProcessMessage (SysId interfaceProcessId, DbUpdateInfo dbUpdateInfo, String message, SaMessageLevel messageLevel = SaMessageLevel.Info, String filename = "", int lineNumber = 0, ItemId itemId = null) { ClearErrorState(); if (!interfaceMgr.InterfaceProcessExistsById(interfaceProcessId)) return SetErrorState("Invalid interface process id supplied"); if (StringUtil.IsEmpty(message)) return true; // Create and store process message. SaInterfaceProcessMessage interfaceProcessMessage = interfaceMgr.CreateInterfaceProcessMessage(interfaceProcessId); interfaceProcessMessage.Message = message; interfaceProcessMessage.Level = messageLevel; interfaceProcessMessage.Filename = filename; interfaceProcessMessage.LineNumber = lineNumber; interfaceProcessMessage.ItemId = itemId; if (!interfaceMgr.InsertInterfaceProcessMessage(interfaceProcessMessage, dbUpdateInfo)) return SetErrorState(interfaceMgr.ErrorStateMessage); return !HasErrorState(); } //--------------------------------------------------------------- // I n t e r f a c e D a t a I t e m s //--------------------------------------------------------------- public override bool ItemHasDataItems (ItemId itemId) { ClearErrorState(); SaInterfaceDataItem.Params prmInterfaceDataItem = new SaInterfaceDataItem.Params(); prmInterfaceDataItem.IncludedItemIds.Add(itemId); return (interfaceMgr.GetInterfaceDataItemCount(prmInterfaceDataItem) > 0); } public override bool InterfaceDataItemExistsByRef (SysId interfaceId, SaInterfaceProcessDirection processDirection, short itemType, String externalReference) { ClearErrorState(); SaInterfaceDataItem.Params prmInterfaceDataItem = new SaInterfaceDataItem.Params(); prmInterfaceDataItem.InterfaceId = interfaceId; prmInterfaceDataItem.Direction = processDirection; prmInterfaceDataItem.ItemType = itemType; prmInterfaceDataItem.ExternalReference = externalReference; return (interfaceMgr.GetInterfaceDataItemCount(prmInterfaceDataItem) > 0); } public override SaInterfaceDataItem CreateInterfaceDataItem (SysId interfaceId, SaInterfaceProcessDirection processDirection, ItemId itemId, String externalReference) { ClearErrorState(); return interfaceMgr.CreateInterfaceDataItem(interfaceId, processDirection, itemId, externalReference); } public override SaInterfaceDataItem GetInterfaceDataItem (SysId interfaceDataItemId) { ClearErrorState(); return interfaceMgr.GetInterfaceDataItemById(interfaceDataItemId); } public override SaInterfaceDataItem GetInterfaceDataItemByItemRef (SysId interfaceId, SaInterfaceProcessDirection processDirection, ItemId itemId, String externalReference) { ClearErrorState(); SaInterfaceDataItem.Params prmInterfaceDataItem = new SaInterfaceDataItem.Params(); prmInterfaceDataItem.InterfaceId = interfaceId; prmInterfaceDataItem.Direction = processDirection; prmInterfaceDataItem.IncludedItemIds.Add(itemId); prmInterfaceDataItem.ExternalReference = externalReference; return interfaceMgr.GetInterfaceDataItem(prmInterfaceDataItem); } public override SaInterfaceDataItem GetInterfaceDataItemByRef (SysId interfaceId, SaInterfaceProcessDirection processDirection, short itemType, String externalReference) { // Fails if more than 1 matching data item. ClearErrorState(); SaInterfaceDataItem.Params prmInterfaceDataItem = new SaInterfaceDataItem.Params(); prmInterfaceDataItem.InterfaceId = interfaceId; prmInterfaceDataItem.Direction = processDirection; prmInterfaceDataItem.ItemType = itemType; prmInterfaceDataItem.ExternalReference = externalReference; return interfaceMgr.GetInterfaceDataItem(prmInterfaceDataItem); } public override SaInterfaceDataItem GetInterfaceDataItemByItem (SysId interfaceId, SaInterfaceProcessDirection processDirection, ItemId itemId) { // Fails if more than 1 matching data item. ClearErrorState(); SaInterfaceDataItem.Params prmInterfaceDataItem = new SaInterfaceDataItem.Params(); prmInterfaceDataItem.InterfaceId = interfaceId; prmInterfaceDataItem.Direction = processDirection; prmInterfaceDataItem.IncludedItemIds.Add(itemId); return interfaceMgr.GetInterfaceDataItem(prmInterfaceDataItem); } public override List GetInterfaceDataItemsByItem (SysId interfaceId, SaInterfaceProcessDirection processDirection, ItemId itemId) { ClearErrorState(); SaInterfaceDataItem.Params prmInterfaceDataItem = new SaInterfaceDataItem.Params(); prmInterfaceDataItem.InterfaceId = interfaceId; prmInterfaceDataItem.Direction = processDirection; prmInterfaceDataItem.IncludedItemIds.Add(itemId); return interfaceMgr.GetInterfaceDataItemList(prmInterfaceDataItem); } public override List GetInterfaceDataItemsByRef (SysId interfaceId, SaInterfaceProcessDirection processDirection, short itemType, String externalReference) { ClearErrorState(); SaInterfaceDataItem.Params prmInterfaceDataItem = new SaInterfaceDataItem.Params(); prmInterfaceDataItem.InterfaceId = interfaceId; prmInterfaceDataItem.Direction = processDirection; prmInterfaceDataItem.ItemType = itemType; prmInterfaceDataItem.ExternalReference = externalReference; return interfaceMgr.GetInterfaceDataItemList(prmInterfaceDataItem); } public override bool StoreInterfaceDataItem (SaInterfaceDataItem interfaceDataItem, DbUpdateInfo dbUpdateInfo) { ClearErrorState(); if (interfaceDataItem == null) return SetErrorState("data item must be supplied"); if (!SysId.IsValid(interfaceDataItem.Id)) { if (!interfaceMgr.InsertInterfaceDataItem(interfaceDataItem, dbUpdateInfo)) return SetErrorState(interfaceMgr.ErrorStateMessage); } else { if (!interfaceMgr.UpdateInterfaceDataItem(interfaceDataItem, dbUpdateInfo)) return SetErrorState(interfaceMgr.ErrorStateMessage); } return true; } public override bool StoreInterfaceDataItems (List interfaceDataItems, DbUpdateInfo dbUpdateInfo) { ClearErrorState(); foreach (SaInterfaceDataItem interfaceDataItem in interfaceDataItems) { StoreInterfaceDataItem(interfaceDataItem, dbUpdateInfo); if (HasErrorState()) return false; } return true; } public override bool ClearInterfaceDataItem (SaInterfaceDataItem interfaceDataItem, DbUpdateInfo dbUpdateInfo) { ClearErrorState(); if (!interfaceMgr.DeleteInterfaceDataItem(interfaceDataItem, dbUpdateInfo)) return SetErrorState(interfaceMgr.ErrorStateMessage); return true; } public override bool ClearInterfaceDataItems (ItemId itemId, DbUpdateInfo dbUpdateInfo) { ClearErrorState(); SaInterfaceDataItem.Params prmInterfaceDataItem = new SaInterfaceDataItem.Params(); prmInterfaceDataItem.IncludedItemIds.Add(itemId); foreach (SaInterfaceDataItem interfaceDataItem in interfaceMgr.GetInterfaceDataItemList(prmInterfaceDataItem)) { if (!ClearInterfaceDataItem(interfaceDataItem, dbUpdateInfo)) return SetErrorState(interfaceMgr.ErrorStateMessage); } return true; } //------------------------------------------------------------------- } //======================================================================= }