409 lines
15 KiB
C#
409 lines
15 KiB
C#
// Copyright (c) 2020-present schick Informatik
|
|
// Description: Container for HAZA subclass templates
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace bsmd.database
|
|
{
|
|
/// <summary>
|
|
/// Container for a dangerous goods template description which is imported from SQLite / Excel DB
|
|
/// Instances of this class will be offered to users as templates and respective DG entries created on them:
|
|
/// IGC, IMSBC, IBC and MARPOL (not! IMDG)
|
|
/// </summary>
|
|
public class HAZPosTemplate : DatabaseEntityAsync, IComparable
|
|
{
|
|
|
|
#region enums
|
|
|
|
public enum SublistType
|
|
{
|
|
IBC,
|
|
IGC,
|
|
IMSBC,
|
|
MARPOL
|
|
}
|
|
|
|
public enum PollutionCategoryEnum
|
|
{
|
|
X = 0,
|
|
Y,
|
|
Z,
|
|
OS
|
|
}
|
|
|
|
public enum HazardsEnum
|
|
{
|
|
P = 0,
|
|
S,
|
|
S_P
|
|
}
|
|
|
|
public enum FlashpointEnum
|
|
{
|
|
NF = 0,
|
|
GT60CEL,
|
|
LE60CEL
|
|
}
|
|
|
|
public enum IMO_HAZ_ClassEnum
|
|
{
|
|
A = 0,
|
|
B,
|
|
A_B
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
private static ObservableCollection<HAZPosTemplate> _templates;
|
|
private static Task _loadTask;
|
|
|
|
public static ObservableCollection<HAZPosTemplate> Templates
|
|
{
|
|
get
|
|
{
|
|
if (_templates == null)
|
|
{
|
|
_templates = new ObservableCollection<HAZPosTemplate>();
|
|
_ = EnsureLoadedAsync();
|
|
}
|
|
return _templates;
|
|
}
|
|
}
|
|
|
|
public string Description { get; set; }
|
|
|
|
public HazardsEnum? Hazard { get; set; }
|
|
|
|
[JsonIgnore]
|
|
[Browsable(false)]
|
|
public int? HazardIndex
|
|
{
|
|
get { return this.Hazard.HasValue ? (int)this.Hazard.Value : (int?)null; }
|
|
set { this.Hazard = value.HasValue ? (HazardsEnum?)value.Value : null; }
|
|
}
|
|
|
|
public string FP { get; set; }
|
|
|
|
public FlashpointEnum? Flashpoint { get; set; }
|
|
|
|
[JsonIgnore]
|
|
[Browsable(false)]
|
|
public int? FlashpointIndex
|
|
{
|
|
get { return this.Flashpoint.HasValue ? (int)this.Flashpoint.Value : (int?)null; }
|
|
set { this.Flashpoint = value.HasValue ? (FlashpointEnum?)value.Value : null; }
|
|
}
|
|
|
|
public bool? SpecRef15_19 { get; set; } = false;
|
|
|
|
public string IMSBC { get; set; }
|
|
|
|
public bool? MHB { get; set; }
|
|
|
|
public int? IMSBC_MHB { get; set; }
|
|
|
|
public string Group { get; set; }
|
|
|
|
public IMO_HAZ_ClassEnum? IMSBC_HAZ { get; set; }
|
|
|
|
[JsonIgnore]
|
|
[Browsable(false)]
|
|
public int? IMSBC_HAZ_Index
|
|
{
|
|
get { return this.IMSBC_HAZ.HasValue ? (int)this.IMSBC_HAZ.Value : (int?)null; }
|
|
set { this.IMSBC_HAZ = value.HasValue ? (IMO_HAZ_ClassEnum?)value.Value : null; }
|
|
}
|
|
public string UNNr { get; set; }
|
|
|
|
public string IMOClass { get; set; }
|
|
|
|
public string IBC { get; set; }
|
|
|
|
public string Comment { get; set; }
|
|
|
|
public SublistType TemplateType { get; set; }
|
|
|
|
public PollutionCategoryEnum? PollutionCategory { get; set; }
|
|
|
|
[JsonIgnore]
|
|
[Browsable(false)]
|
|
public int? PollutionCategoryIndex
|
|
{
|
|
get { return this.PollutionCategory.HasValue ? (int)this.PollutionCategory.Value : (int?)null; }
|
|
set { this.PollutionCategory = value.HasValue ? (PollutionCategoryEnum?)value.Value : null; }
|
|
}
|
|
public string MARPOL { get; set; }
|
|
|
|
public string IGC { get; set; }
|
|
|
|
public string FP_IBC { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region DatabaseEntity implementation
|
|
|
|
public HAZPosTemplate()
|
|
{
|
|
this.tablename = "[dbo].[HazardMaterial]";
|
|
}
|
|
|
|
public override void PrepareSave(IDbCommand cmd)
|
|
{
|
|
SqlCommand scmd = cmd as SqlCommand;
|
|
|
|
scmd.Parameters.AddWithNullableValue("@P1", this.Description);
|
|
scmd.Parameters.AddWithNullableValue("@P2", this.Hazard.HasValue ? (int?)this.Hazard.Value : null);
|
|
scmd.Parameters.AddWithNullableValue("@P3", this.FP);
|
|
scmd.Parameters.AddWithNullableValue("@P4", this.Flashpoint.HasValue ? (int?)this.Flashpoint.Value : null);
|
|
scmd.Parameters.AddWithNullableValue("@P5", this.SpecRef15_19.HasValue ? (this.SpecRef15_19.Value ? "Yes" : "No") : null);
|
|
scmd.Parameters.AddWithNullableValue("@P6", this.TemplateType.ToString());
|
|
scmd.Parameters.AddWithNullableValue("@P7", this.IMSBC);
|
|
scmd.Parameters.AddWithNullableValue("@P8", this.MHB.HasValue ? (this.MHB.Value ? "y" : "n") : null);
|
|
scmd.Parameters.AddWithNullableValue("@P9", this.IMSBC_MHB);
|
|
scmd.Parameters.AddWithNullableValue("@P10", this.Group);
|
|
scmd.Parameters.AddWithNullableValue("@P11", this.IMSBC_HAZ.HasValue ? (int?)this.IMSBC_HAZ.Value : null);
|
|
scmd.Parameters.AddWithNullableValue("@P12", this.UNNr);
|
|
scmd.Parameters.AddWithNullableValue("@P13", this.IMOClass);
|
|
scmd.Parameters.AddWithNullableValue("@P14", this.IBC);
|
|
scmd.Parameters.AddWithNullableValue("@P15", this.PollutionCategory.HasValue ? (int?)this.PollutionCategory.Value : null);
|
|
scmd.Parameters.AddWithNullableValue("@P16", this.MARPOL);
|
|
scmd.Parameters.AddWithNullableValue("@P17", this.IGC);
|
|
scmd.Parameters.AddWithNullableValue("@P18", this.FP_IBC);
|
|
scmd.Parameters.AddWithNullableValue("@P19", this.Comment);
|
|
|
|
if (this.IsNew)
|
|
{
|
|
this.CreateId();
|
|
scmd.Parameters.AddWithValue("@ID", this.Id);
|
|
cmd.CommandText = string.Format(
|
|
"INSERT INTO {0} (Id, Description, Hazard_Enum, FP, FP_Enum, [15_19], Typ, IMSBC, MHB, IMSBC_MHB, [Group], IMSBC_HAZ, UN_Nr, IMO_CL, IBC, Pollution_Category_Enum, MARPOL, IGC, FP_IBC, Remarks) " +
|
|
"VALUES (@ID, @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9, @P10, @P11, @P12, @P13, @P14, @P15, @P16, @P17, @P18, @P19)",
|
|
this.Tablename);
|
|
}
|
|
else
|
|
{
|
|
cmd.CommandText = string.Format(
|
|
"UPDATE {0} SET Description = @P1, Hazard_Enum = @P2, FP = @P3, FP_Enum = @P4, [15_19] = @P5, Typ = @P6, " +
|
|
"IMSBC = @P7, MHB = @P8, IMSBC_MHB = @P9, [Group] = @P10, IMSBC_HAZ = @P11, UN_Nr = @P12, IMO_CL = @P13, " +
|
|
"IBC = @P14, Pollution_Category_Enum = @P15, MARPOL = @P16, IGC = @P17, FP_IBC = @P18, Remarks = @P19 WHERE Id = @ID",
|
|
this.Tablename);
|
|
scmd.Parameters.AddWithValue("@ID", this.Id);
|
|
}
|
|
}
|
|
|
|
public override void PrepareLoadCommand(IDbCommand cmd, Message.LoadFilter filter, params object[] criteria)
|
|
{
|
|
string query = string.Format(
|
|
"SELECT Id, Description, Hazard_Enum, FP, FP_Enum, [15_19], Typ, IMSBC, MHB, IMSBC_MHB, [Group], IMSBC_HAZ, UN_Nr, IMO_CL, IBC, Pollution_Category_Enum, MARPOL, IGC, FP_IBC, Remarks FROM {0}",
|
|
this.Tablename);
|
|
|
|
switch (filter)
|
|
{
|
|
case Message.LoadFilter.ALL:
|
|
default:
|
|
break;
|
|
}
|
|
|
|
cmd.CommandText = query;
|
|
}
|
|
|
|
public override List<DatabaseEntity> LoadList(IDataReader reader)
|
|
{
|
|
List<DatabaseEntity> result = new List<DatabaseEntity>();
|
|
|
|
while (reader.Read())
|
|
{
|
|
HAZPosTemplate hpt = new HAZPosTemplate();
|
|
hpt.id = reader.GetGuid(0);
|
|
if (!reader.IsDBNull(1))
|
|
hpt.Description = reader.GetString(1);
|
|
if (!reader.IsDBNull(2))
|
|
hpt.Hazard = (HazardsEnum)reader.GetInt32(2);
|
|
if (!reader.IsDBNull(3))
|
|
hpt.FP = reader.GetString(3);
|
|
if (!reader.IsDBNull(4))
|
|
hpt.Flashpoint = (FlashpointEnum)reader.GetInt32(4);
|
|
if (!reader.IsDBNull(5))
|
|
{
|
|
string specRefString = reader.GetString(5);
|
|
if (specRefString.Equals("JA", StringComparison.OrdinalIgnoreCase) || specRefString.Equals("Yes", StringComparison.OrdinalIgnoreCase)) hpt.SpecRef15_19 = true;
|
|
if (specRefString.Equals("NEIN", StringComparison.OrdinalIgnoreCase) || specRefString.Equals("No", StringComparison.OrdinalIgnoreCase)) hpt.SpecRef15_19 = false;
|
|
}
|
|
if (!reader.IsDBNull(6) && Enum.TryParse<SublistType>(reader.GetString(6), out SublistType templateType))
|
|
{
|
|
hpt.TemplateType = templateType;
|
|
}
|
|
if (!reader.IsDBNull(7))
|
|
hpt.IMSBC = reader.GetString(7);
|
|
if (!reader.IsDBNull(8))
|
|
{
|
|
string mhbstring = reader.GetString(8);
|
|
if (mhbstring.Equals("y", StringComparison.OrdinalIgnoreCase))
|
|
hpt.MHB = true;
|
|
else if (mhbstring.Equals("n", StringComparison.OrdinalIgnoreCase))
|
|
hpt.MHB = false;
|
|
}
|
|
if (!reader.IsDBNull(9))
|
|
hpt.IMSBC_MHB = reader.GetInt32(9);
|
|
if (!reader.IsDBNull(10))
|
|
hpt.Group = reader.GetString(10);
|
|
if (!reader.IsDBNull(11))
|
|
hpt.IMSBC_HAZ = (IMO_HAZ_ClassEnum)reader.GetInt32(11);
|
|
if (!reader.IsDBNull(12))
|
|
hpt.UNNr = reader.GetString(12);
|
|
if (!reader.IsDBNull(13))
|
|
hpt.IMOClass = reader.GetString(13);
|
|
if (!reader.IsDBNull(14))
|
|
hpt.IBC = reader.GetString(14);
|
|
if (!reader.IsDBNull(15))
|
|
hpt.PollutionCategory = (PollutionCategoryEnum)reader.GetInt32(15);
|
|
if (!reader.IsDBNull(16))
|
|
hpt.MARPOL = reader.GetString(16);
|
|
if (!reader.IsDBNull(17))
|
|
hpt.IGC = reader.GetString(17);
|
|
if (!reader.IsDBNull(18))
|
|
hpt.FP_IBC = reader.GetString(18);
|
|
if (!reader.IsDBNull(19))
|
|
hpt.Comment = reader.GetString(19);
|
|
|
|
result.Add(hpt);
|
|
}
|
|
|
|
reader.Close();
|
|
return result;
|
|
}
|
|
|
|
protected override DatabaseEntityAsync ReadRowFromReader(IDataReader reader)
|
|
{
|
|
HAZPosTemplate hpt = null;
|
|
if (reader != null)
|
|
{
|
|
hpt = new HAZPosTemplate();
|
|
hpt.id = reader.GetGuid(0);
|
|
if (!reader.IsDBNull(1))
|
|
hpt.Description = reader.GetString(1);
|
|
if (!reader.IsDBNull(2))
|
|
hpt.Hazard = (HazardsEnum)reader.GetInt32(2);
|
|
if (!reader.IsDBNull(3))
|
|
hpt.FP = reader.GetString(3);
|
|
if (!reader.IsDBNull(4))
|
|
hpt.Flashpoint = (FlashpointEnum)reader.GetInt32(4);
|
|
if (!reader.IsDBNull(5))
|
|
{
|
|
string specRefString = reader.GetString(5);
|
|
if (specRefString.Equals("JA", StringComparison.OrdinalIgnoreCase) || specRefString.Equals("Yes", StringComparison.OrdinalIgnoreCase)) hpt.SpecRef15_19 = true;
|
|
if (specRefString.Equals("NEIN", StringComparison.OrdinalIgnoreCase) || specRefString.Equals("No", StringComparison.OrdinalIgnoreCase)) hpt.SpecRef15_19 = false;
|
|
}
|
|
if (!reader.IsDBNull(6) && Enum.TryParse<SublistType>(reader.GetString(6), out SublistType templateType))
|
|
{
|
|
hpt.TemplateType = templateType;
|
|
}
|
|
if (!reader.IsDBNull(7))
|
|
hpt.IMSBC = reader.GetString(7);
|
|
if (!reader.IsDBNull(8))
|
|
{
|
|
string mhbstring = reader.GetString(8);
|
|
if (mhbstring.Equals("y", StringComparison.OrdinalIgnoreCase))
|
|
hpt.MHB = true;
|
|
else if (mhbstring.Equals("n", StringComparison.OrdinalIgnoreCase))
|
|
hpt.MHB = false;
|
|
}
|
|
if (!reader.IsDBNull(9))
|
|
hpt.IMSBC_MHB = reader.GetInt32(9);
|
|
if (!reader.IsDBNull(10))
|
|
hpt.Group = reader.GetString(10);
|
|
if (!reader.IsDBNull(11))
|
|
hpt.IMSBC_HAZ = (IMO_HAZ_ClassEnum)reader.GetInt32(11);
|
|
if (!reader.IsDBNull(12))
|
|
hpt.UNNr = reader.GetString(12);
|
|
if (!reader.IsDBNull(13))
|
|
hpt.IMOClass = reader.GetString(13);
|
|
if (!reader.IsDBNull(14))
|
|
hpt.IBC = reader.GetString(14);
|
|
if (!reader.IsDBNull(15))
|
|
hpt.PollutionCategory = (PollutionCategoryEnum)reader.GetInt32(15);
|
|
if (!reader.IsDBNull(16))
|
|
hpt.MARPOL = reader.GetString(16);
|
|
if (!reader.IsDBNull(17))
|
|
hpt.IGC = reader.GetString(17);
|
|
if (!reader.IsDBNull(18))
|
|
hpt.FP_IBC = reader.GetString(18);
|
|
if (!reader.IsDBNull(19))
|
|
hpt.Comment = reader.GetString(19);
|
|
}
|
|
return hpt;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Static loading
|
|
|
|
public static async Task EnsureLoadedAsync()
|
|
{
|
|
if (_templates == null)
|
|
{
|
|
_templates = new ObservableCollection<HAZPosTemplate>();
|
|
}
|
|
|
|
if (_loadTask != null)
|
|
{
|
|
await _loadTask;
|
|
return;
|
|
}
|
|
|
|
_loadTask = LoadInternalAsync();
|
|
await _loadTask;
|
|
}
|
|
|
|
private static async Task LoadInternalAsync()
|
|
{
|
|
var list = await DBManagerAsync.LoadHAZTemplatesAsync();
|
|
_templates.Clear();
|
|
foreach (var item in list)
|
|
{
|
|
_templates.Add(item);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region overrides
|
|
|
|
public override string ToString()
|
|
{
|
|
if(string.IsNullOrEmpty(this.Description))
|
|
return base.ToString();
|
|
|
|
if (this.Description.Length > 75)
|
|
return string.Format("{0}...", this.Description.Substring(0, 75));
|
|
|
|
return Description;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IComparable implementation
|
|
|
|
public int CompareTo(object obj)
|
|
{
|
|
if (obj is HAZPosTemplate other)
|
|
{
|
|
return this.Description?.CompareTo(other.Description ?? "") ?? 0;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|