// // Class: NOA_NOD // Current CLR: 4.0.30319.34209 // System: Microsoft Visual Studio 10.0 // Author: dani // Created: 3/21/2015 8:56:24 AM // // Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved. using System; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using Newtonsoft.Json; using System.Runtime.Serialization; namespace bsmd.database { [TypeConverter(typeof(MessageClassConverter))] [JsonConverter(typeof(NoTypeConverterJsonConverter))] [DataContract] public class NOA_NOD : DatabaseEntity, ISublistContainer { public NOA_NOD() { this.tablename = "[dbo].[NOA_NOD]"; } #region Properties [ShowReport] [Validation1(ValidationCode.NOT_NULL)] [ENI2Validation] [DataMember] public DateTime? ETAToPortOfCall { get; set; } [ShowReport] [Validation1(ValidationCode.NOT_NULL)] [ENI2Validation] [DataMember] public DateTime? ETDFromPortOfCall { get; set; } [Validation1(ValidationCode.LIST_EMPTY)] public ObservableCollection CallPurposes { get; set; } = new ObservableCollection(); [ShowReport] [Validation2(ValidationCode.NOT_NULL)] [ENI2Validation] [DataMember] public DateTime? ETAToKielCanal { get; set; } [ShowReport] [Validation2(ValidationCode.NOT_NULL)] [ENI2Validation] [DataMember] public DateTime? ETDFromKielCanal { get; set; } [ShowReport] [Validation(ValidationCode.NOT_NULL)] [MaxLength(5)] [ENI2Validation] [DataMember] public string LastPort { get; set; } [ShowReport] [ENI2Validation] [DataMember] public DateTime? ETDFromLastPort { get; set; } [ShowReport] [Validation(ValidationCode.LOCODE_SSN)] [MaxLength(5)] [ENI2Validation] [DataMember] public string NextPort { get; set; } [ShowReport] [ENI2Validation] [DataMember] public DateTime? ETAToNextPort { get; set; } [ENI2Validation] public bool? IsAnchored { get; set; } #endregion #region abstract class implementation public override void PrepareLoadCommand(System.Data.IDbCommand cmd, Message.LoadFilter filter, params object[] criteria) { string query = string.Format("SELECT Id, ETAToPortOfCall, ETDFromPortOfCall, " + "ETAToKielCanal, ETDFromKielCanal, LastPort, ETDFromLastPort, NextPort, ETAToNextPort, IsAnchored 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()) { NOA_NOD nn = new NOA_NOD(); nn.id = reader.GetGuid(0); if (!reader.IsDBNull(1)) nn.ETAToPortOfCall = DateTime.SpecifyKind(reader.GetDateTime(1), DateTimeKind.Utc); if (!reader.IsDBNull(2)) nn.ETDFromPortOfCall = DateTime.SpecifyKind(reader.GetDateTime(2), DateTimeKind.Utc); if (!reader.IsDBNull(3)) nn.ETAToKielCanal = DateTime.SpecifyKind(reader.GetDateTime(3), DateTimeKind.Utc); if (!reader.IsDBNull(4)) nn.ETDFromKielCanal = DateTime.SpecifyKind(reader.GetDateTime(4), DateTimeKind.Utc); if (!reader.IsDBNull(5)) nn.LastPort = reader.GetString(5); if (!reader.IsDBNull(6)) nn.ETDFromLastPort = DateTime.SpecifyKind(reader.GetDateTime(6), DateTimeKind.Utc); if (!reader.IsDBNull(7)) nn.NextPort = reader.GetString(7); if (!reader.IsDBNull(8)) nn.ETAToNextPort = DateTime.SpecifyKind(reader.GetDateTime(8), DateTimeKind.Utc); if (!reader.IsDBNull(9)) nn.IsAnchored = reader.GetBoolean(9); result.Add(nn); } reader.Close(); return result; } public override void PrepareSave(IDbCommand cmd) { SqlCommand scmd = cmd as SqlCommand; scmd.Parameters.AddWithNullableValue("@P1", this.MessageHeader.Id); scmd.Parameters.AddWithNullableValue("@P2", this.ETAToPortOfCall); scmd.Parameters.AddWithNullableValue("@P3", this.ETDFromPortOfCall); scmd.Parameters.AddWithNullableValue("@P4", this.ETAToKielCanal); scmd.Parameters.AddWithNullableValue("@P5", this.ETDFromKielCanal); scmd.Parameters.AddWithNullableValue("@P6", this.LastPort); scmd.Parameters.AddWithNullableValue("@P7", this.ETDFromLastPort); scmd.Parameters.AddWithNullableValue("@P8", this.NextPort); scmd.Parameters.AddWithNullableValue("@P9", this.ETAToNextPort); scmd.Parameters.AddWithNullableValue("@P10", this.IsAnchored); if (this.IsNew) { this.CreateId(); scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("INSERT INTO {0} ( Id, MessageHeaderId, ETAToPortOfCall, ETDFromPortOfCall, " + "ETAToKielCanal, ETDFromKielCanal, LastPort, ETDFromLastPort, " + "NextPort, ETAToNextPort, IsAnchored) VALUES (@ID, @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9, @P10) ", this.Tablename); } else { ((SqlCommand)cmd).Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("UPDATE {0} SET ETAToPortOfCall = @P2, ETDFromPortOfCall = @P3, " + "ETAToKielCanal = @P4, ETDFromKielCanal = @P5, LastPort = @P6, ETDFromLastPort = @P7, " + "NextPort = @P8, ETAToNextPort = @P9, IsAnchored = @P10 WHERE Id = @ID", this.Tablename); } } #endregion #region overrides public override void OverwriteWith(DatabaseEntity otherEntity) { if (otherEntity is NOA_NOD otherNOA_NOD) { base.OverwriteWith(otherEntity); foreach (CallPurpose cp in this.CallPurposes) DBManager.Instance.Delete(cp); this.CallPurposes.Clear(); foreach (CallPurpose cp in otherNOA_NOD.CallPurposes) { CallPurpose newCP = cp.Clone() as CallPurpose; newCP.NOA_NOD = this; DBManager.Instance.Save(newCP); this.CallPurposes.Add(newCP); } } } #endregion #region ISublistContainer implementation public ISublistElement GetSublistElementWithIdentifier(string identifier) { foreach (ISublistElement sublistElement in this.CallPurposes) if (identifier.Equals(sublistElement.Identifier)) return sublistElement; return null; } [Browsable(false)] [JsonIgnore] public int NumberOfExcelRows { get { return 3; } } public void SaveElements() { foreach(CallPurpose cp in this.CallPurposes) { DBManager.Instance.Save(cp); } } public void DeleteElements() { foreach (CallPurpose cp in this.CallPurposes) { DBManager.Instance.Delete(cp); } this.CallPurposes.Clear(); } #endregion #region IMessageParagraph implementation [Browsable(false)] public override string Subtitle { get { return "Arrival and departure notification"; } } /* table now created manually [Browsable(false)] public override List ChildParagraphs { get { List result = new List(); foreach (IMessageParagraph imp in this.CallPurposes) result.Add(imp); return result; } } */ #endregion #region Validation public override void Validate(List errors, List violations) { if (this.GetValidationBlock() == ValidationBlock.BLOCK1) { if(this.ETDFromPortOfCall.HasValue && this.ETAToPortOfCall.HasValue && (this.ETDFromPortOfCall < this.ETAToPortOfCall)) errors.Add(RuleEngine.CreateError(ValidationCode.E121, "ETDFromPortOfCall implausible", this.ETDFromPortOfCall?.ToLocalTime().ToString(), this.Title, null, this.Tablename)); // 8.11.20 Validierung gegen ETA /ETD in der Vergangenheit if((this.ETAToPortOfCall < DateTime.Now) || (this.ETAToPortOfCall.HasValue && this.ETAToPortOfCall.Value.ToLocalTime().IsTimeEmpty())) { violations.Add(RuleEngine.CreateViolation(ValidationCode.TIME_IMPLAUSIBLE, "Check ETAToPortOfCall: might be implausible", this.ETAToPortOfCall.Value.ToLocalTime().ToString(), this.Title, null, this.Tablename)); } if((this.ETDFromLastPort < DateTime.Now.AddDays(-14)) || (this.ETDFromLastPort.HasValue && this.ETDFromLastPort.Value.ToLocalTime().IsTimeEmpty())) { violations.Add(RuleEngine.CreateViolation(ValidationCode.TIME_IMPLAUSIBLE, "Check ETDFromLastPort: might be implausible", this.ETDFromLastPort.Value.ToLocalTime().ToString(), this.Title, null, this.Tablename)); } if ((this.ETDFromPortOfCall < DateTime.Now.AddDays(-14)) || (this.ETDFromPortOfCall > DateTime.Now.AddDays(14)) || (this.ETDFromPortOfCall.HasValue && this.ETDFromPortOfCall.Value.ToLocalTime().IsTimeEmpty())) { violations.Add(RuleEngine.CreateViolation(ValidationCode.TIME_IMPLAUSIBLE, "Check ETDFromPortOfCall: might be implausible", this.ETDFromPortOfCall.Value.ToLocalTime().ToString(), this.Title, null, this.Tablename)); } if ((this.ETAToNextPort < DateTime.Now) || (this.ETAToNextPort > DateTime.Now.AddDays(14)) || (this.ETAToNextPort.HasValue && this.ETAToNextPort.Value.ToLocalTime().IsTimeEmpty())) { violations.Add(RuleEngine.CreateViolation(ValidationCode.TIME_IMPLAUSIBLE, "Check ETAToNextPort: might be implausible", this.ETAToNextPort.Value.ToLocalTime().ToString(), this.Title, null, this.Tablename)); } if (this.CallPurposes.IsNullOrEmpty()) { errors.Add(RuleEngine.CreateError(ValidationCode.LIST_EMPTY, "CallPurposes", "CallPurpose", this.Title, null, this.Tablename)); } else { foreach (CallPurpose cp in this.CallPurposes) { if (cp.CallPurposeCode == 0) errors.Add(RuleEngine.CreateError(ValidationCode.INT_GT_ZERO, "CallPurposeCode", "0", this.Title, cp.Identifier, this.Tablename)); if(!cp.CallPurposeDescription.IsNullOrEmpty() && (cp.CallPurposeDescription.Length > 94)) violations.Add(RuleEngine.CreateViolation(ValidationCode.TRUNCATE, "CallPurposeDescription", cp.CallPurposeDescription, this.Title, cp.Identifier, this.Tablename)); } } } else { if (this.ETDFromKielCanal.HasValue && this.ETAToKielCanal.HasValue && (this.ETDFromKielCanal < this.ETAToKielCanal)) errors.Add(RuleEngine.CreateError(ValidationCode.E121, "ETDFromKielCanal implausible", this.ETDFromKielCanal.Value.ToLocalTime().ToString(), this.Title, null, this.Tablename)); if(this.ETDFromKielCanal.HasValue && this.ETDFromKielCanal.Value.ToLocalTime().IsTimeEmpty()) violations.Add(RuleEngine.CreateViolation(ValidationCode.TIME_IMPLAUSIBLE, "Check ETDFromKielCanal: might be implausible", this.ETDFromKielCanal.Value.ToLocalTime().ToString(), this.Title, null, this.Tablename)); if(this.ETAToKielCanal.HasValue && this.ETAToKielCanal.Value.ToLocalTime().IsTimeEmpty()) violations.Add(RuleEngine.CreateViolation(ValidationCode.TIME_IMPLAUSIBLE, "Check ETAToKielCanal: might be implausible", this.ETAToKielCanal.Value.ToLocalTime().ToString(), this.Title, null, this.Tablename)); } if((this.LastPort?.Equals("ZZUKN") == false) && !this.ETDFromLastPort.HasValue) errors.Add(RuleEngine.CreateError(ValidationCode.NOT_NULL, "ETDFromLastPort missing", null, this.Title, null, this.Tablename)); if((this.LastPort?.Equals("ZZUKN") == true) && this.ETDFromLastPort.HasValue) errors.Add(RuleEngine.CreateError(ValidationCode.IMPLAUSIBLE_ZZUKN, "ETDFromLastPort missing", null, this.Title, null, this.Tablename)); if (this.ETDFromPortOfCall.HasValue && this.ETAToNextPort.HasValue && (this.NextPort?.Equals("ZZUKN") == false) && (this.ETAToNextPort <= ETDFromPortOfCall)) errors.Add(RuleEngine.CreateError(ValidationCode.E122, "ETAToNextPort", null, this.Title, null, this.Tablename)); if ((this.NextPort?.Equals("ZZUKN") == false) && !this.ETAToNextPort.HasValue) errors.Add(RuleEngine.CreateError(ValidationCode.NOT_NULL, "ETAToNextPort", null, this.Title, null, this.Tablename)); if((this.NextPort?.Equals("ZZUKN") == true) && this.ETAToNextPort.HasValue) errors.Add(RuleEngine.CreateError(ValidationCode.IMPLAUSIBLE_ZZUKN, "ETAToNextPort", null, this.Title, null, this.Tablename)); if (this.ETDFromLastPort.HasValue && this.ETAToPortOfCall.HasValue && (this.ETDFromLastPort >= this.ETAToPortOfCall)) errors.Add(RuleEngine.CreateError(ValidationCode.IMPLAUSIBLE, "ETDFromLastPort", null, this.Title, null, this.Tablename)); } public override DatabaseEntity.ValidationBlock GetValidationBlock() { if (this.MessageCore.IsTransit) return ValidationBlock.BLOCK2; return ValidationBlock.BLOCK1; } #endregion #region public methods /// /// Hilfsmethode (Heuristik) wenn nur der Text verfügbar ist (Excel) /// Quelle: http://www.unece.org/trade/untdid/d01a/tred/tred8025.htm /// Nach Nachricht von Christin doch nicht erforderlich, da der Code führend ist (wird vom Melder /// eingetragen) /// /// Code nach NSW Spec, null on failure public static int? getCallPurposeCodeFromDescription(string description) { if (description.IsNullOrEmpty()) return null; if (description.Contains("cargo", StringComparison.CurrentCultureIgnoreCase)) return 1; if (description.Contains("passenger", StringComparison.CurrentCultureIgnoreCase)) return 2; if (description.Contains("bunker", StringComparison.CurrentCultureIgnoreCase)) return 3; if (description.Contains("crew", StringComparison.CurrentCultureIgnoreCase)) return 4; if (description.Contains("visit", StringComparison.CurrentCultureIgnoreCase)) return 5; if (description.Contains("supplies", StringComparison.CurrentCultureIgnoreCase)) return 6; if (description.Contains("repair", StringComparison.CurrentCultureIgnoreCase)) return 7; if (description.Contains("laid-up", StringComparison.CurrentCultureIgnoreCase)) return 8; if (description.Contains("order", StringComparison.CurrentCultureIgnoreCase)) return 9; if (description.Contains("miscellaneous", StringComparison.CurrentCultureIgnoreCase)) return 10; if (description.Contains("movement", StringComparison.CurrentCultureIgnoreCase)) return 11; if (description.Contains("cruise", StringComparison.CurrentCultureIgnoreCase)) return 12; if (description.Contains("government", StringComparison.CurrentCultureIgnoreCase)) return 13; if (description.Contains("quarantine", StringComparison.CurrentCultureIgnoreCase)) return 14; if (description.Contains("refuge", StringComparison.CurrentCultureIgnoreCase)) return 15; return 0; } #endregion #region ICloneable implementation public override object Clone() { // NOA_NOD noanod = this.MemberwiseClone() as NOA_NOD; NOA_NOD noanod = new NOA_NOD(); noanod.LastPort = this.LastPort; noanod.NextPort = this.NextPort; noanod.IsAnchored = this.IsAnchored; noanod.id = null; foreach (CallPurpose cp in this.CallPurposes) { CallPurpose clonedCP = cp.Clone() as CallPurpose; clonedCP.NOA_NOD = noanod; noanod.CallPurposes.Add(clonedCP); } return noanod; } #endregion } }