init
This commit is contained in:
262
DataPRO/Modules/Channels/ChannelCodes/Model/ChannelCode.cs
Normal file
262
DataPRO/Modules/Channels/ChannelCodes/Model/ChannelCode.cs
Normal file
@@ -0,0 +1,262 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.OleDb;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using System.Windows.Input;
|
||||
using DTS.Common.Classes.ChannelCodes;
|
||||
using DTS.Common.Enums;
|
||||
using DTS.Common.Enums.Channels;
|
||||
using DTS.Common.Interface.Channels.ChannelCodes;
|
||||
using DTS.Common.Storage;
|
||||
|
||||
namespace ChannelCodes.Model
|
||||
{
|
||||
public class ChannelCode : DTS.Common.Classes.ChannelCodes.ChannelCode, IChannelCode
|
||||
{
|
||||
//in case this object is held onto longer before cleanup, attempt to empty
|
||||
//private storage
|
||||
~ChannelCode()
|
||||
{
|
||||
_code = null;
|
||||
_name = null;
|
||||
PossibleChannels?.Clear();
|
||||
PasteCommand = null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// whether the current id is valid or not
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool HasValidId()
|
||||
{
|
||||
return Id >= 0;
|
||||
}
|
||||
|
||||
public bool IsBlank()
|
||||
{
|
||||
return string.IsNullOrEmpty(Name) && string.IsNullOrEmpty(Code);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// this dates back from when there was a dropdown to select the type of code,
|
||||
/// I'm not sure it's used anymore (2019-04-11)
|
||||
/// </summary>
|
||||
public int SelectedChannelType
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (CodeType)
|
||||
{
|
||||
case ChannelEnumsAndConstants.ChannelCodeType.ISO:
|
||||
return 0;
|
||||
case ChannelEnumsAndConstants.ChannelCodeType.User:
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case 0: CodeType = ChannelEnumsAndConstants.ChannelCodeType.ISO; break;
|
||||
case 1: CodeType = ChannelEnumsAndConstants.ChannelCodeType.User; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RegisterCommands()
|
||||
{
|
||||
PasteCommand = new PasteCommandClass(PASTE_ID);
|
||||
CommandManager.RegisterClassCommandBinding(GetType(),
|
||||
new CommandBinding(PasteCommand, Paste));
|
||||
}
|
||||
public ChannelCode(IChannelCode channelCode)
|
||||
: base(channelCode)
|
||||
{
|
||||
RegisterCommands();
|
||||
}
|
||||
|
||||
private void Paste(object sender, ExecutedRoutedEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
public ChannelCode()
|
||||
{
|
||||
CodeType = ChannelEnumsAndConstants.ChannelCodeType.ISO;
|
||||
RegisterCommands();
|
||||
}
|
||||
|
||||
public ChannelCode(IDataRecord sqlReader, IDictionary<short, string> channelCodeLookup)
|
||||
{
|
||||
RegisterCommands();
|
||||
Id = Convert.ToInt32(sqlReader["Id"]);
|
||||
Code = (string)sqlReader["Code"];
|
||||
Name = (string)sqlReader["Name"];
|
||||
var codeType = Convert.ToInt16(sqlReader["CodeTypeInt"]);
|
||||
if (channelCodeLookup.ContainsKey(codeType))
|
||||
{
|
||||
var key = channelCodeLookup[codeType];
|
||||
switch (key)
|
||||
{
|
||||
case ChannelEnumsAndConstants.UserCodeTypeString:
|
||||
CodeType = ChannelEnumsAndConstants.ChannelCodeType.User;
|
||||
break;
|
||||
case ChannelEnumsAndConstants.IsoCodeTypeString:
|
||||
CodeType = ChannelEnumsAndConstants.ChannelCodeType.ISO;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// get all existing channel codes from the db
|
||||
/// </summary>
|
||||
/// <param name="lookup">a lookup from channel code type (short) to channel code type string as in the db</param>
|
||||
/// <returns></returns>
|
||||
public static ChannelCode[] GetExistingChannelCodes(IDictionary<short, string> lookup)
|
||||
{
|
||||
var channelCodes = new List<ChannelCode>();
|
||||
using (var cmd = DbOperations.GetSQLCommand(true))
|
||||
{
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
cmd.CommandText = @"sp_ChannelCodesGet";
|
||||
cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int) { Value = null });
|
||||
cmd.Parameters.Add(new SqlParameter("@Code", SqlDbType.NVarChar, 255) { Value = null });
|
||||
cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 255) { Value = null });
|
||||
cmd.Parameters.Add(new SqlParameter("@CodeType", SqlDbType.SmallInt) { Value = null });
|
||||
|
||||
var reader = cmd.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
channelCodes.Add(new ChannelCode(reader, lookup));
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Connection.Dispose();
|
||||
}
|
||||
}
|
||||
return channelCodes.ToArray();
|
||||
}
|
||||
/// <summary>
|
||||
/// retrieves all channel codes and returns as a list
|
||||
/// </summary>
|
||||
public static IList<IChannelCode> ChannelCodes
|
||||
{
|
||||
get
|
||||
{
|
||||
var lookup = ChannelCodeType.GetChannelCodeTypeLookup();
|
||||
var channelCodes = new List<IChannelCode>();
|
||||
channelCodes.AddRange(GetExistingChannelCodes(lookup));
|
||||
channelCodes.AddRange(ISOChannelCodes);
|
||||
|
||||
return channelCodes;
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly object RefreshLock = new object();
|
||||
|
||||
public Dictionary<string, string> PossibleChannels { get; private set; }
|
||||
|
||||
|
||||
private static IList<IChannelCode> _isoChannelCodes;
|
||||
|
||||
/// <summary>
|
||||
/// retrieves a list of iso channel codes from the db
|
||||
/// </summary>
|
||||
public static IList<IChannelCode> ISOChannelCodes
|
||||
{
|
||||
get
|
||||
{
|
||||
if (null != _isoChannelCodes) return _isoChannelCodes;
|
||||
//we only need to fetch these values once, they're not going to change.
|
||||
var channelCodes = new List<IChannelCode>();
|
||||
|
||||
lock (RefreshLock)
|
||||
{
|
||||
var sourceFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ISOPossibleChannels.txt");
|
||||
using (StreamReader sr = File.OpenText(sourceFile))
|
||||
{
|
||||
string s = string.Empty;
|
||||
while ((s = sr.ReadLine()) != null)
|
||||
{
|
||||
var splitString = s.Split(',');
|
||||
var isoCode = splitString[0];
|
||||
var textL1 = splitString[1];
|
||||
var newChannelCode = new ChannelCode()
|
||||
{
|
||||
Code = isoCode,
|
||||
Name = textL1,
|
||||
CodeType = ChannelEnumsAndConstants.ChannelCodeType.ISO
|
||||
};
|
||||
if (!channelCodes.Contains(newChannelCode))
|
||||
{
|
||||
channelCodes.Add(newChannelCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_isoChannelCodes = channelCodes;
|
||||
return _isoChannelCodes;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// not used? maybe was a part of migration?
|
||||
/// </summary>
|
||||
/// <param name="reader"></param>
|
||||
/// <param name="field"></param>
|
||||
/// <returns></returns>
|
||||
protected static long GetLong(OleDbDataReader reader, string field)
|
||||
{
|
||||
return DBNull.Value == reader[field] ? long.MinValue : Convert.ToInt64(reader[field]);
|
||||
}
|
||||
/// <summary>
|
||||
/// not used, maybe was a part of migration?
|
||||
/// </summary>
|
||||
/// <param name="reader"></param>
|
||||
/// <param name="field"></param>
|
||||
/// <returns></returns>
|
||||
protected static DateTime GetDate(OleDbDataReader reader, string field)
|
||||
{
|
||||
return DBNull.Value != reader[field] ? (DateTime)reader[field] : DateTime.MinValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// deletes channel code
|
||||
/// deleting a channel code only removes it from the lookup, doesn't remove it from any existing channels
|
||||
/// </summary>
|
||||
public void Delete()
|
||||
{
|
||||
_ = DbOperations.ChannelCodesDelete(Id, null, null, null);
|
||||
}
|
||||
/// <summary>
|
||||
/// commits any channel codes to the db
|
||||
/// this only affects the channel code lookup, not any existing channels
|
||||
/// </summary>
|
||||
/// <param name="lookup"></param>
|
||||
public void Save(IDictionary<ChannelEnumsAndConstants.ChannelCodeType, short> lookup)
|
||||
{
|
||||
var restrictedLookup = (IReadOnlyDictionary<ChannelEnumsAndConstants.ChannelCodeType, short>)lookup;
|
||||
_ = DbOperations.ChannelCodesUpdate(restrictedLookup, this);
|
||||
}
|
||||
/// <summary>
|
||||
/// inserts a new channel code into the db
|
||||
/// </summary>
|
||||
/// <param name="lookup"></param>
|
||||
public void Insert(IDictionary<ChannelEnumsAndConstants.ChannelCodeType, short> lookup)
|
||||
{
|
||||
var restrictedLookup = (IReadOnlyDictionary<ChannelEnumsAndConstants.ChannelCodeType, short>)lookup;
|
||||
var hr = DbOperations.ChannelCodesInsert(restrictedLookup, this, out var id);
|
||||
if (0 == hr)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using DTS.Common.Storage;
|
||||
|
||||
namespace ChannelCodes.Model
|
||||
{
|
||||
public abstract class ChannelCodeType
|
||||
{
|
||||
/// <summary>
|
||||
/// retrieves all possible channel code types
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IDictionary<short, string> GetChannelCodeTypeLookup()
|
||||
{
|
||||
var lookup = new Dictionary<short, string>();
|
||||
using (var cmd = DbOperations.GetSQLCommand(true))
|
||||
{
|
||||
try
|
||||
{
|
||||
cmd.CommandType = CommandType.StoredProcedure;
|
||||
cmd.CommandText = @"sp_ChannelCodeTypeGet";
|
||||
cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.TinyInt) { Value = null });
|
||||
cmd.Parameters.Add(new SqlParameter("@CodeType", SqlDbType.NVarChar, 50) { Value = null });
|
||||
|
||||
var reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
var id = Convert.ToInt16(reader["Id"]);
|
||||
var codeType = (string)reader["CodeType"];
|
||||
lookup[id] = codeType;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
cmd.Connection.Dispose();
|
||||
}
|
||||
}
|
||||
return lookup;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user