--- source_files: - DataPRO/Modules/Channels/ChannelCodes/Model/ChannelCodeType.cs - DataPRO/Modules/Channels/ChannelCodes/Model/ChannelCode.cs generated_at: "2026-04-16T04:56:25.345352+00:00" model: "Qwen/Qwen3-Coder-Next-FP8" schema_version: 1 sha256: "8041292bb801532f" --- # Model ## Documentation: ChannelCode Module --- ### 1. Purpose This module provides data access and modeling for *channel codes*—lookup values used to categorize or classify channels in the system (e.g., ISO-standardized codes vs. user-defined codes). It defines a base abstract class `ChannelCodeType` for retrieving static type metadata and a concrete class `ChannelCode` that represents individual channel code entries, including persistence operations (insert/update/delete) and caching of ISO codes from a static text file. The module serves as the data layer for channel code management, bridging the database (`sp_ChannelCodeTypeGet`, `sp_ChannelCodesGet`) and file-based ISO code definitions. --- ### 2. Public Interface #### `ChannelCodeType.GetChannelCodeTypeLookup()` ```csharp public static IDictionary GetChannelCodeTypeLookup() ``` Retrieves all channel code types from the database via stored procedure `sp_ChannelCodeTypeGet`. Returns a dictionary mapping `Id` (short) to `CodeType` (string), representing the type categories (e.g., ISO, User). Parameters passed to the stored procedure (`@Id`, `@CodeType`) are null, indicating no filtering. #### `ChannelCode` class *Inherits from `DTS.Common.Classes.ChannelCodes.ChannelCode` and implements `IChannelCode`.* - **Constructors** - `ChannelCode()` Initializes a new `ChannelCode` with `CodeType = ISO` and registers the `PasteCommand`. - `ChannelCode(IChannelCode channelCode)` Copies properties from an existing `IChannelCode` instance and registers `PasteCommand`. - `ChannelCode(IDataRecord sqlReader, IDictionary channelCodeLookup)` Populates the instance from a database row (`sqlReader`) using the provided `channelCodeLookup`. Maps `CodeTypeInt` (short) to a `ChannelCodeType` enum value (`ISO` or `User`) via string comparison against `ChannelEnumsAndConstants.IsoCodeTypeString` and `ChannelEnumsAndConstants.UserCodeTypeString`. - **Properties** - `bool HasValidId()` Returns `true` if `Id >= 0`. - `bool IsBlank()` Returns `true` if both `Name` and `Code` are `null` or empty. - `int SelectedChannelType` Gets/sets a legacy dropdown index (0 = ISO, 1 = User) based on `CodeType`. *Deprecated per inline comment.* - `Dictionary PossibleChannels { get; private set; }` Publicly readable dictionary (currently unused in source; likely intended for channel mappings). - `static IList ChannelCodes { get; }` Returns a combined list of *all* channel codes: user-defined codes (from DB via `GetExistingChannelCodes`) + ISO codes (from `ISOChannelCodes`). Uses a shared lookup from `GetChannelCodeTypeLookup()`. - `static IList ISOChannelCodes { get; }` Lazily loads ISO codes from `ISOPossibleChannels.txt` (comma-separated: `Code,Name`). Caches in `_isoChannelCodes` after first access. Thread-safe via `lock(RefreshLock)`. - **Methods** - `static ChannelCode[] GetExistingChannelCodes(IDictionary lookup)` Fetches all user-defined channel codes from DB via `sp_ChannelCodesGet`, returning an array of `ChannelCode` instances. Parameters passed to the stored procedure (`@Id`, `@Code`, `@Name`, `@CodeType`) are null (no filtering). - `void Delete()` Calls `DbOperations.ChannelCodesDelete(Id, null, null, null)` to remove the channel code *from the lookup table only* (does not affect existing channel instances referencing it). - `void Save(IDictionary lookup)` Calls `DbOperations.ChannelCodesUpdate(restrictedLookup, this)` to persist changes to the channel code *lookup table*. - `void Insert(IDictionary lookup)` Calls `DbOperations.ChannelCodesInsert(restrictedLookup, this, out id)` to insert a new channel code. On success (`hr == 0`), sets `Id` to the returned value. - `protected static long GetLong(OleDbDataReader reader, string field)` Helper for legacy OleDb access; returns `long.MinValue` on `DBNull`. *Not used in current code.* - `protected static DateTime GetDate(OleDbDataReader reader, string field)` Helper for legacy OleDb access; returns `DateTime.MinValue` on `DBNull`. *Not used in current code.* #### Command - `PasteCommand` (public property, type `ICommand`) Initialized as `PasteCommandClass(PASTE_ID)` in `RegisterCommands()`. A no-op `Paste` handler is defined but empty. Registered via `CommandManager.RegisterClassCommandBinding`. --- ### 3. Invariants - **`Id` validity**: `HasValidId()` requires `Id >= 0`. Negative IDs are considered invalid. - **Code type mapping**: `CodeType` must be either `ChannelEnumsAndConstants.ChannelCodeType.ISO` or `ChannelEnumsAndConstants.ChannelCodeType.User`. Mapping from DB `CodeTypeInt` relies on exact string matches against `IsoCodeTypeString` and `UserCodeTypeString`. - **ISO codes are static**: `ISOChannelCodes` are loaded once and cached in `_isoChannelCodes`. They are *not* persisted to the database. - **Persistence scope**: `Insert`, `Save`, and `Delete` operations affect *only the channel code lookup table*, not any channels that may reference the code. - **Thread safety**: ISO code loading uses `lock(RefreshLock)` to ensure single initialization. - **Null handling**: `IsBlank()` treats `null` and empty strings identically. --- ### 4. Dependencies #### **Dependencies *of* this module** - **Database**: - Stored procedures: `sp_ChannelCodeTypeGet`, `sp_ChannelCodesGet` - Table columns: `Id`, `Code`, `Name`, `CodeTypeInt` - **File system**: - `ISOPossibleChannels.txt` (comma-separated: `Code,Name`) in app base directory. - **External types**: - `DTS.Common.Storage.DbOperations` (provides `GetSQLCommand`, `ChannelCodesDelete`, `ChannelCodesUpdate`, `ChannelCodesInsert`) - `DTS.Common.Classes.ChannelCodes.ChannelCode` (base class) - `DTS.Common.Interface.Channels.ChannelCodes.IChannelCode` (interface) - `DTS.Common.Enums.Channels.ChannelEnumsAndConstants` (defines `ChannelCodeType`, `IsoCodeTypeString`, `UserCodeTypeString`) - `System.Data.SqlClient`, `System.Data`, `System.IO`, `System.Windows.Input` #### **Dependencies *on* this module** - Any module needing channel code metadata or management (e.g., UI dropdowns, channel assignment logic). - `ISOChannelCodes` implies a hard dependency on the presence and format of `ISOPossibleChannels.txt`. --- ### 5. Gotchas - **`SelectedChannelType` is legacy/deprecated**: The property is tied to a dropdown that may no longer exist. Its use is discouraged. - **`PasteCommand` is non-functional**: The `Paste` handler is empty; the command is registered but does nothing. - **`PossibleChannels` is unused**: The property is declared but never populated in the source. - **ISO codes are *not* persisted**: Changes to `ISOChannelCodes` (e.g., via `Insert`/`Save`) will not affect `ISOPossibleChannels.txt`. This file is read-only at runtime. - **`Delete`/`Save`/`Insert` do not cascade**: Operations affect only the channel code *definition* (lookup), not any existing channels that reference the code. - **Thread-safety limitation**: ISO code loading is thread-safe for initialization, but no caching invalidation mechanism exists (codes are assumed immutable). - **`GetLong`/`GetDate` are dead code**: OleDb-specific helpers with no callers in this module. Likely remnants of legacy migration code. - **`ChannelCodes` property is expensive**: Each access calls `GetChannelCodeTypeLookup()` and `GetExistingChannelCodes()`, which hit the database. Caching externally is recommended if used repeatedly. - **No validation on `Code`/`Name`**: The module does not enforce uniqueness or non-empty values beyond `IsBlank()`. Business rules may be enforced elsewhere. - **`Id` assignment is conditional**: `Insert` only sets `Id` if `hr == 0`; callers must check the return value or handle failure. *None identified from source alone.* (Note: The above are inferred from explicit comments and code structure—not hallucinated.)