// // Class: IBCPosition // Current CLR: 4.0.30319.34209 // System: Microsoft Visual Studio 10.0 // Author: dani // Created: 5/27/2015 8:55:22 AM // // Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Reflection; using System.Text.RegularExpressions; using Newtonsoft.Json; namespace bsmd.database { [TypeConverter(typeof(MessageClassConverter))] [JsonConverter(typeof(NoTypeConverterJsonConverter))] public class IBCPosition : DatabaseEntity, ISublistElement { #region Construction public IBCPosition() { this.tablename = "[dbo].[IBCPosition]"; } #endregion #region static lookup values public static string[] pollutionCategories = { "X", "Y", "Z", "OS" }; public static string[] hazards = { "P", "S", "S/P" }; public static string[] flashpointInformations = { "Not flammable", "Above 60°", "Less than 60°" }; #endregion #region Properties [JsonIgnore] [Browsable(false)] public HAZ HAZ { get; set; } [ShowReport] [Validation(ValidationCode.NOT_NULL)] [MaxLength(255)] [MaxWordLength(14)] [ENI2Validation] public string ProductName { get; set; } [Validation(ValidationCode.NOT_NULL)] [ENI2Validation] public byte? PollutionCategory { get; set; } [ShowReport] [ReportDisplayName("PollutionCategory")] [JsonIgnore] public string PollutionCategoryDisplay { get { if (this.PollutionCategory.HasValue && (this.PollutionCategory.Value < pollutionCategories.Length)) return pollutionCategories[this.PollutionCategory.Value]; return ""; } } [ENI2Validation] public byte? Hazards { get; set; } [ShowReport] [ReportDisplayName("Hazards")] [JsonIgnore] public string HazardsDisplay { get { if (this.Hazards.HasValue && (this.Hazards.Value < hazards.Length)) return hazards[this.Hazards.Value]; return ""; } } [Validation(ValidationCode.NOT_NULL)] [ENI2Validation] public byte? FlashpointInformation { get; set; } [ShowReport] [ReportDisplayName("FlashpointInformation")] [JsonIgnore] public string FlashpointInformationDisplay { get { if (this.FlashpointInformation.HasValue && (this.FlashpointInformation.Value < flashpointInformations.Length)) return flashpointInformations[this.FlashpointInformation.Value]; return ""; } } [ShowReport] [MaxLength(11)] [ENI2Validation] [Validation(ValidationCode.DOT_NO_COMMA)] public string Flashpoint_CEL { get; set; } [ShowReport] [Validation(ValidationCode.DOUBLE_GT_ZERO)] [ENI2Validation] public double? Quantity_KGM { get; set; } [ShowReport] [Validation(ValidationCode.NOT_NULL)] [MaxLength(24)] [MaxWordLength(6)] [ENI2Validation] public string StowagePosition { get; set; } [ShowReport] [Validation(ValidationCode.LOCODE)] [MaxLength(5)] [ENI2Validation] public string PortOfLoading { get; set; } [ShowReport] [Validation(ValidationCode.LOCODE)] [MaxLength(5)] [ENI2Validation] public string PortOfDischarge { get; set; } [ShowReport] [ENI2Validation] public bool? SpecRef15_19 { get; set; } [ShowReport] [MaxLength(255)] [MaxWordLength(35)] [ENI2Validation] public string Remarks { get; set; } public string Identifier { get; set; } [JsonIgnore] [Browsable(false)] public string SublistCollectionKey { get { return "ibc"; } } #endregion #region DatabaseEntity implementation public override void PrepareSave(System.Data.IDbCommand cmd) { SqlCommand scmd = cmd as SqlCommand; scmd.Parameters.AddWithValue("@P1", this.HAZ.Id); scmd.Parameters.AddWithNullableValue("@P2", this.ProductName); scmd.Parameters.AddWithNullableValue("@P3", this.PollutionCategory); scmd.Parameters.AddWithNullableValue("@P4", this.Hazards); scmd.Parameters.AddWithNullableValue("@P5", this.FlashpointInformation); scmd.Parameters.AddWithNullableValue("@P6", this.Flashpoint_CEL); scmd.Parameters.AddWithNullableValue("@P7", this.Quantity_KGM); scmd.Parameters.AddWithNullableValue("@P8", this.StowagePosition); scmd.Parameters.AddWithNullableValue("@P9", this.PortOfLoading); scmd.Parameters.AddWithNullableValue("@P10", this.PortOfDischarge); scmd.Parameters.AddWithNullableValue("@P11", this.SpecRef15_19); scmd.Parameters.AddWithNullableValue("@P12", this.Remarks); scmd.Parameters.AddWithNullableValue("@P13", this.Identifier); if (this.IsNew) { this.CreateId(); scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("INSERT INTO {0} (Id, HAZId, ProductName, PollutionCategory, Hazards, " + "FlashpointInformation, Flashpoint_CEL, Quantity_KGM, StowagePosition, PortOfLoading, PortOfDischarge, " + "SpecRef15_19, Remarks, Identifier) VALUES (@ID, @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9, @P10, " + "@P11, @P12, @P13)", this.Tablename); } else { scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("UPDATE {0} SET ProductName = @P2, PollutionCategory = @P3, Hazards = @P4, " + "FlashpointInformation = @P5, Flashpoint_CEL = @P6, Quantity_KGM = @P7, StowagePosition = @P8, " + "PortOfLoading = @P9, PortOfDischarge = @P10, SpecRef15_19 = @P11, Remarks = @P12, Identifier = @P13 " + "WHERE ID = @ID", this.Tablename); } } public override void PrepareLoadCommand(System.Data.IDbCommand cmd, Message.LoadFilter filter, params object[] criteria) { string query = string.Format("SELECT Id, ProductName, PollutionCategory, " + "Hazards, FlashpointInformation, Flashpoint_CEL, Quantity_KGM, StowagePosition, PortOfLoading, " + "PortOfDischarge, SpecRef15_19, Remarks, Identifier FROM {0} ", this.Tablename); switch (filter) { case Message.LoadFilter.HAZ_ID: query += " WHERE HAZId = @HAZID"; ((SqlCommand)cmd).Parameters.AddWithValue("@HAZID", criteria[0]); break; case Message.LoadFilter.ALL: default: break; } cmd.CommandText = query; } public override List LoadList(System.Data.IDataReader reader) { List result = new List(); while (reader.Read()) { IBCPosition ibc = new IBCPosition(); ibc.id = reader.GetGuid(0); if (!reader.IsDBNull(1)) ibc.ProductName = reader.GetString(1); if (!reader.IsDBNull(2)) ibc.PollutionCategory = reader.GetByte(2); if (!reader.IsDBNull(3)) ibc.Hazards = reader.GetByte(3); if (!reader.IsDBNull(4)) ibc.FlashpointInformation = reader.GetByte(4); if (!reader.IsDBNull(5)) ibc.Flashpoint_CEL = reader.GetString(5); if (!reader.IsDBNull(6)) ibc.Quantity_KGM = reader.GetDouble(6); if (!reader.IsDBNull(7)) ibc.StowagePosition = reader.GetString(7); if (!reader.IsDBNull(8)) ibc.PortOfLoading = reader.GetString(8); if (!reader.IsDBNull(9)) ibc.PortOfDischarge = reader.GetString(9); if (!reader.IsDBNull(10)) ibc.SpecRef15_19 = reader.GetBoolean(10); if (!reader.IsDBNull(11)) ibc.Remarks = reader.GetString(11); if (!reader.IsDBNull(12)) ibc.Identifier = reader.GetString(12); result.Add(ibc); } reader.Close(); result.Sort(); return result; } #endregion #region Validation public override void Validate(List errors, List violations) { if(this.Quantity_KGM >= 1000000000) errors.Add(RuleEngine.CreateError(ValidationCode.IMPLAUSIBLE, null, null, this.Title, this.Identifier, this.HAZ.IsDeparture ? "HAZD" : "HAZA")); if (this.FlashpointInformation.HasValue && (this.FlashpointInformation.Value == 2) && this.Flashpoint_CEL.IsNullOrEmpty()) violations.Add(RuleEngine.CreateViolation(ValidationCode.V804, "Flashpoint_CEL", null, this.Title, this.Identifier, this.HAZ.IsDeparture ? "HAZD" : "HAZA")); if(!this.Flashpoint_CEL.IsNullOrEmpty()) { const string pattern = @"^[<>]?\-?[0-9]+(\.[0-9]+)?$"; Regex regex = new Regex(pattern); if(!regex.IsMatch(this.Flashpoint_CEL)) errors.Add(RuleEngine.CreateError(ValidationCode.IMPLAUSIBLE, "Flashpoint_CEL", null, this.Title, this.Identifier, this.HAZ.IsDeparture ? "HAZD" : "HAZA")); } if(!this.Hazards.HasValue) { violations.Add(RuleEngine.CreateViolation(ValidationCode.V810, "Hazards", null, this.Title, this.Identifier, this.HAZ.IsDeparture ? "HAZD" : "HAZA")); } } #endregion #region public methods public void CopyFromIBC(IBCPosition selectedIBC) { if (selectedIBC == null) return; this.FlashpointInformation = selectedIBC.FlashpointInformation; this.Flashpoint_CEL = selectedIBC.Flashpoint_CEL; this.Hazards = selectedIBC.Hazards; this.PollutionCategory = selectedIBC.PollutionCategory; this.PortOfDischarge = selectedIBC.PortOfDischarge; this.PortOfLoading = selectedIBC.PortOfLoading; this.ProductName = selectedIBC.ProductName; this.Quantity_KGM = selectedIBC.Quantity_KGM; this.Remarks = selectedIBC.Remarks; this.SpecRef15_19 = selectedIBC.SpecRef15_19; this.StowagePosition = selectedIBC.StowagePosition; } #endregion #region IComparable implementation public int CompareTo(object obj) { if (this.Identifier == null) return 1; if ((obj is null) || (((IBCPosition)obj).Identifier == null)) return 1; if (Int32.TryParse(((IBCPosition)obj).Identifier, out int i1) && Int32.TryParse(this.Identifier, out int i2)) return i2.CompareTo(i1); return this.Identifier.CompareTo(((IBCPosition)obj).Identifier); } #endregion } }