// // Class: STAT // Current CLR: 4.0.30319.34209 // System: Microsoft Visual Studio 10.0 // Author: dani // Created: 3/19/2015 8:59:21 PM // // Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved. using System; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; using Newtonsoft.Json; using System.ComponentModel; using System.Runtime.Serialization; namespace bsmd.database { [DataContract] public class STAT : DatabaseEntity { public STAT() { this.tablename = "[dbo].[STAT]"; } #region Properties [ShowReport] [Validation(ValidationCode.NOT_NULL)] [LookupName("STAT.ShipName")] [MaxLength(100)] [ENI2Validation] [DataMember] public string ShipName { get; set; } [ShowReport] [Validation(ValidationCode.FRZ)] [MaxLength(50)] [ENI2Validation] [DataMember] [LookupName("STAT.CallSign")] public string CallSign { get; set; } [ShowReport] [Validation(ValidationCode.MMSI)] [MaxLength(50)] [ENI2Validation] [DataMember] [LookupName("STAT.MMSINumber")] public string MMSINumber { get; set; } [ShowReport] [Validation(ValidationCode.FLAG_CODE)] [MaxLength(2)] [ENI2Validation] [DataMember] [LookupName("STAT.Flag")] public string Flag { get; set; } [ShowReport] [Validation(ValidationCode.DOUBLE_GT_ZERO)] [LookupName("STAT.LengthOverall_MTR")] [ENI2Validation] [DataMember] public double? LengthOverall_MTR { get; set; } [ShowReport] [Validation(ValidationCode.DOUBLE_GT_ZERO)] [LookupName("STAT.Beam_MTR")] [ENI2Validation] [DataMember] public double? Beam_MTR { get; set; } [ShowReport] [Validation(ValidationCode.DOUBLE_GT_ZERO)] [LookupName("STAT.GrossTonnage")] [ENI2Validation] [DataMember] public int? GrossTonnage { get; set; } [ShowReport] [Validation(ValidationCode.LOCODE_NOPORT)] [MaxLength(5)] [ENI2Validation] [DataMember] [LookupName("STAT.PortOfRegistry")] public string PortOfRegistry { get; set; } [ShowReport] [LookupName("STAT.InmarsatCallNumber")] [MaxLength(100)] [ENI2Validation] [Validation(ValidationCode.INVALID_NUMBER_CHARS)] [DataMember] public string InmarsatCallNumber { get; set; } /// /// UNECE Rec.19 Code (http://www.unece.org/fileadmin/DAM/cefact/recommendations/rec19/rec19_ecetrd138.pdf) /// [Validation(ValidationCode.NOT_NULL)] [MaxLength(1)] [ENI2Validation] [DataMember] public string TransportMode { get; set; } [ShowReport] [JsonIgnore] public string TransportModeDisplay { get { if (!this.TransportMode.IsNullOrEmpty()) { if (this.TransportMode == "1") return "Maritime transport"; if (this.TransportMode == "8") return "Inland water transport"; } return string.Empty; } } [ShowReport] [Validation(ValidationCode.VESSEL_TYPE)] [LookupName("STAT.ShipType")] [MaxLength(5)] [ENI2Validation] [DataMember] public string ShipType { get; set; } [ShowReport] [LookupName("STAT.ISMCompanyName")] [MaxLength(100)] [ENI2Validation] [DataMember] public string ISMCompanyName { get; set; } [ShowReport] [Validation(ValidationCode.STRING_EXACT_LEN, 7)] [MaxLength(10)] [ENI2Validation] [DataMember] [LookupName("STAT.ISMCompanyId")] public string ISMCompanyId { get; set; } [ShowReport] [LookupName("STAT.ISMCompanyStreetAndNumber")] [MaxLength(100)] [ENI2Validation] [DataMember] public string ISMCompanyStreetAndNumber { get; set; } [ShowReport] [LookupName("STAT.ISMCompanyPostalCode")] [MaxLength(24)] [ENI2Validation] [DataMember] public string ISMCompanyPostalCode { get; set; } [ShowReport] [LookupName("STAT.ISMCompanyCity")] [MaxLength(100)] [ENI2Validation] [DataMember] public string ISMCompanyCity { get; set; } [ShowReport] [LookupName("STAT.ISMCompanyCountry")] [MaxLength(100)] [ENI2Validation] [DataMember] public string ISMCompanyCountry { get; set; } [Browsable(false)] public override string Subtitle { get { return "Vessel details"; } } [LookupName("STAT.NetTonnage")] [ENI2Validation] [DataMember] public double? NetTonnage { get; set; } [LookupName("STAT.RegistryDate")] [ENI2Validation] [DataMember] public DateTime? RegistryDate { get; set; } [LookupName("STAT.CertificateOfRegistryNumber")] [MaxLength(35)] [ENI2Validation] [DataMember] public string CertificateOfRegistryNumber { get; set; } public bool? IsTanker { get { if (ShipType.IsNullOrEmpty()) return null; if (ShipType.StartsWith("53")) return true; if (ShipType.StartsWith("54")) return true; if (ShipType.StartsWith("55")) return true; return false; } } [JsonIgnore] public static Dictionary VesselTypeDict { get; set; } [JsonIgnore] public static Dictionary TransportModeDict { get; set; } #endregion #region abstract class implementation public override void PrepareLoadCommand(IDbCommand cmd, Message.LoadFilter filter, params object[] criteria) { string query = string.Format("SELECT Id, ShipName, Callsign, MMSINumber, Flag, LengthOverall_MTR, Beam_MTR, " + "GrossTonnage, PortOfRegistry, InmarsatCallNumber, ShipType, ISMCompanyName, ISMCompanyId, ISMCompanyStreetAndNumber, " + "ISMCompanyPostalCode, ISMCompanyCity, ISMCompanyCountry, TransportMode, NetTonnage, RegistryDate, " + "CertificateOfRegistryNumber FROM {0} ", this.Tablename); switch (filter) { case Message.LoadFilter.MESSAGEHEADER: query += "WHERE MessageHeaderId = @MHID"; ((SqlCommand)cmd).Parameters.AddWithValue("@MHID", 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()) { STAT stat = new STAT(); stat.id = reader.GetGuid(0); if (!reader.IsDBNull(1)) stat.ShipName = reader.GetString(1); if (!reader.IsDBNull(2)) stat.CallSign = reader.GetString(2); if (!reader.IsDBNull(3)) stat.MMSINumber = reader.GetString(3); if (!reader.IsDBNull(4)) stat.Flag = reader.GetString(4); if (!reader.IsDBNull(5)) stat.LengthOverall_MTR = (float) reader.GetDouble(5); if (!reader.IsDBNull(6)) stat.Beam_MTR = (float) reader.GetDouble(6); if (!reader.IsDBNull(7)) stat.GrossTonnage = reader.GetInt32(7); if (!reader.IsDBNull(8)) stat.PortOfRegistry = reader.GetString(8); if (!reader.IsDBNull(9)) stat.InmarsatCallNumber = reader.GetString(9); if (!reader.IsDBNull(10)) stat.ShipType = reader.GetString(10); if (!reader.IsDBNull(11)) stat.ISMCompanyName = reader.GetString(11); if (!reader.IsDBNull(12)) stat.ISMCompanyId = reader.GetString(12); if (!reader.IsDBNull(13)) stat.ISMCompanyStreetAndNumber = reader.GetString(13); if (!reader.IsDBNull(14)) stat.ISMCompanyPostalCode = reader.GetString(14); if (!reader.IsDBNull(15)) stat.ISMCompanyCity = reader.GetString(15); if (!reader.IsDBNull(16)) stat.ISMCompanyCountry = reader.GetString(16); if (!reader.IsDBNull(17)) stat.TransportMode = reader.GetString(17); if (!reader.IsDBNull(18)) stat.NetTonnage = (float) reader.GetDouble(18); if (!reader.IsDBNull(19)) stat.RegistryDate = reader.GetDateTime(19); if (!reader.IsDBNull(20)) stat.CertificateOfRegistryNumber = reader.GetString(20); result.Add(stat); } reader.Close(); return result; } public override void PrepareSave(IDbCommand cmd) { SqlCommand scmd = cmd as SqlCommand; scmd.Parameters.AddWithValue("@P1", this.MessageHeader.Id); if (this.ShipName != null) scmd.Parameters.AddWithValue("@P2", this.ShipName); else scmd.Parameters.AddWithValue("@P2", DBNull.Value); if (this.CallSign != null) scmd.Parameters.AddWithValue("@P3", this.CallSign); else scmd.Parameters.AddWithValue("@P3", DBNull.Value); if (this.MMSINumber != null) scmd.Parameters.AddWithValue("@P4", this.MMSINumber); else scmd.Parameters.AddWithValue("@P4", DBNull.Value); if (this.Flag != null) scmd.Parameters.AddWithValue("@P5", this.Flag); else scmd.Parameters.AddWithValue("@P5", DBNull.Value); if (this.LengthOverall_MTR.HasValue) scmd.Parameters.AddWithValue("@P6", (double) this.LengthOverall_MTR.Value); else scmd.Parameters.AddWithValue("@P6", DBNull.Value); if (this.Beam_MTR.HasValue) scmd.Parameters.AddWithValue("@P7", (double) this.Beam_MTR.Value); else scmd.Parameters.AddWithValue("@P7", DBNull.Value); if (this.GrossTonnage.HasValue) scmd.Parameters.AddWithValue("@P8", this.GrossTonnage.Value); else scmd.Parameters.AddWithValue("@P8", DBNull.Value); if (this.PortOfRegistry != null) scmd.Parameters.AddWithValue("@P9", this.PortOfRegistry); else scmd.Parameters.AddWithValue("@P9", DBNull.Value); if (this.InmarsatCallNumber != null) scmd.Parameters.AddWithValue("@P10", this.InmarsatCallNumber); else scmd.Parameters.AddWithValue("@P10", DBNull.Value); if (this.ShipType != null) scmd.Parameters.AddWithValue("@P11", this.ShipType); else scmd.Parameters.AddWithValue("@P11", DBNull.Value); if (this.ISMCompanyName != null) scmd.Parameters.AddWithValue("@P12", this.ISMCompanyName); else scmd.Parameters.AddWithValue("@P12", DBNull.Value); if (this.ISMCompanyId != null) scmd.Parameters.AddWithValue("@P13", this.ISMCompanyId); else scmd.Parameters.AddWithValue("@P13", DBNull.Value); if (this.ISMCompanyStreetAndNumber != null) scmd.Parameters.AddWithValue("@P14", this.ISMCompanyStreetAndNumber); else scmd.Parameters.AddWithValue("@P14", DBNull.Value); if (this.ISMCompanyPostalCode != null) scmd.Parameters.AddWithValue("@P15", this.ISMCompanyPostalCode); else scmd.Parameters.AddWithValue("@P15", DBNull.Value); if (this.ISMCompanyCity != null) scmd.Parameters.AddWithValue("@P16", this.ISMCompanyCity); else scmd.Parameters.AddWithValue("@P16", DBNull.Value); if (this.ISMCompanyCountry != null) scmd.Parameters.AddWithValue("@P17", this.ISMCompanyCountry); else scmd.Parameters.AddWithValue("@P17", DBNull.Value); if (this.TransportMode != null) scmd.Parameters.AddWithValue("@P18", this.TransportMode); else scmd.Parameters.AddWithValue("@P18", DBNull.Value); scmd.Parameters.AddWithNullableValue("@P19", this.NetTonnage); scmd.Parameters.AddWithNullableValue("@P20", this.RegistryDate); scmd.Parameters.AddWithNullableValue("@P21", this.CertificateOfRegistryNumber); if (this.IsNew) { this.CreateId(); scmd.Parameters.AddWithValue("@ID", this.Id); cmd.CommandText = string.Format("INSERT INTO {0} (Id, MessageHeaderId, ShipName, CallSign, MMSINumber, " + "Flag, LengthOverall_MTR, Beam_MTR, GrossTonnage, PortOfRegistry, InmarsatCallNumber, ShipType, " + "ISMCompanyName, ISMCompanyId, ISMCompanyStreetAndNumber, ISMCompanyPostalCode, ISMCompanyCity, " + "ISMCompanyCountry, TransportMode, NetTonnage, RegistryDate, CertificateOfRegistryNumber) " + "VALUES (@ID, @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9, @P10, @P11, " + " @P12, @P13, @P14, @P15, @P16, @P17, @P18, @P19, @P20, @P21)", this.Tablename); } else { cmd.CommandText = string.Format("UPDATE {0} SET ShipName = @P2, CallSign = @P3, MMSINumber = @P4, Flag = @P5, " + "LengthOverall_MTR = @P6, Beam_MTR = @P7, GrossTonnage = @P8, PortOfRegistry = @P9, InmarsatCallNumber = @P10, " + "ShipType = @P11, ISMCompanyName = @P12, ISMCompanyId = @P13, ISMCompanyStreetAndNumber = @P14, " + "ISMCompanyPostalCode = @P15, ISMCompanyCity = @P16, ISMCompanyCountry = @P17, TransportMode = @P18, " + "NetTonnage = @P19, RegistryDate = @P20, CertificateOfRegistryNumber = @P21 WHERE Id = @ID", this.Tablename); scmd.Parameters.AddWithValue("@ID", this.Id); } } #endregion #region Validation public override void Validate(List errors, List violations) { if (this.GrossTonnage >= 500 && (this.ISMCompanyName.IsNullOrEmpty() || this.ISMCompanyId.IsNullOrEmpty())) violations.Add(RuleEngine.CreateViolation(ValidationCode.V821, "ISMCompanyId/Name must be provided", null, this.Title, null, this.Tablename)); // Neue Regeln vom 1.4.2019 if (this.LengthOverall_MTR.HasValue && ((this.LengthOverall_MTR.Value < 10) || (this.LengthOverall_MTR.Value > 500))) violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Length valid?", null, this.Title, null, this.Tablename)); if (this.Beam_MTR.HasValue && ((this.Beam_MTR.Value < 5) || (this.Beam_MTR.Value > 65))) violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Beam valid?", null, this.Title, null, this.Tablename)); if (this.GrossTonnage.HasValue && ((this.GrossTonnage.Value < 100) || (this.GrossTonnage > 500000))) violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Gross tonnage valid?", null, this.Title, null, this.Tablename)); if((this.PortOfRegistry?.Length > 1) && (this.Flag?.Length == 2)) { if (!this.PortOfRegistry.Substring(0, 2).Equals(this.Flag)) violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Port of registration and flag do not match!", null, this.Title, null, this.Tablename)); } } #endregion } }