// // Class: Waste // Current CLR: 4.0.30319.34209 // System: Microsoft Visual Studio 10.0 // Author: dani // Created: 4/2/2015 7:44:03 PM // // Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved. using System.Data.SqlClient; using System.Collections.Generic; using Newtonsoft.Json; using System.ComponentModel; using System; namespace bsmd.database { [TypeConverter(typeof(MessageClassConverter))] [JsonConverter(typeof(NoTypeConverterJsonConverter))] public class Waste : DatabaseEntity, ISublistElement { public Waste() { this.tablename = "[dbo].[Waste]"; } #region Properties [JsonIgnore] [Browsable(false)] public WAS WAS { get; set; } [ShowReport] [JsonIgnore] public string WasteTypeDisplayV4 { get { if (!WasteType.HasValue) return string.Empty; switch(WasteType.Value) { // "old" waste codes (pre V7), used to display old data case 1100: return "Oily residues (sludge)"; case 1200: return "Oily bilge water"; case 1300: return "Waste oil - others (specify)"; case 2100: return "Food waste"; case 2200: return "Plastic"; case 2300: return "Domestic wastes"; case 2311: return "Cooking oil"; case 2308: return "Incinerator ashes"; case 2313: return "Operational wastes"; case 2600: return "Operational wastes"; case 2309: return "Animal carcass(es)"; case 3000: return "Sewage"; case 5100: return "Cargo residues - Marpol Annex I"; case 5200: return "Cargo residues - Marpol Annex II"; case 5300: return "Cargo residues - Marpol Annex V"; default: return string.Empty; } } } [JsonIgnore] public string WasteTypeDisplayGrid { get { if (!this.WasteType.HasValue) return string.Empty; string code = WasteTypeDisplayV4; if(code.IsNullOrEmpty()) { if (WAS.WasteCodeDict.ContainsKey(this.WasteType.ToString())) code = WAS.WasteCodeDict[this.WasteType.ToString()]; } return string.Format("{0} {1}", WasteType, code); } } // jetzt der "WasteCode" [Validation(ValidationCode.NOT_NULL)] [ENI2Validation] public int? WasteType { get; set; } [ShowReport] [MaxLength(99)] [ENI2Validation] public string WasteDescription { get; set; } [ShowReport] [Validation(ValidationCode.NOT_NULL)] [ENI2Validation] public double? WasteDisposalAmount_MTQ { get; set; } [ShowReport] [Validation(ValidationCode.NOT_NULL)] [ENI2Validation] public double? WasteCapacity_MTQ { get; set; } [ShowReport] [Validation(ValidationCode.NOT_NULL)] [ENI2Validation] public double? WasteAmountRetained_MTQ { get; set; } // Jetzt der "RemainingWasteDeliveryPort" [ShowReport] [MaxLength(5)] [ENI2Validation] [Validation(ValidationCode.LOCODE_SSN)] public string WasteDisposalPort { get; set; } [ShowReport] [ENI2Validation] [Validation(ValidationCode.NOT_NULL)] public double? WasteAmountGeneratedTillNextPort_MTQ { get; set; } [Obsolete] [ENI2Validation] public double? WasteDisposedAtLastPort_MTQ { get; set; } public string Identifier { get; set; } [JsonIgnore] [Browsable(false)] public string SublistCollectionKey { get { return "waste"; } } [JsonIgnore] public bool HasValidWasteCode { get { if (!this.WasteType.HasValue) return false; foreach(string wasteCode in WAS.WasteCodes) { if (wasteCode.Equals(this.WasteType.Value.ToString())) return true; } return false; } } [JsonIgnore] public bool IsDashWasteCode { get { if (!WasteType.HasValue) return false; switch (WasteType.Value) { case 999: case 201: case 202: case 203: case 204: case 510: case 511: return true; default: return false; } } } #endregion #region DatabaseEntity implementation public override void PrepareSave(System.Data.IDbCommand cmd) { SqlCommand scmd = cmd as SqlCommand; scmd.Parameters.AddWithValue("@P1", this.WAS.Id); scmd.Parameters.AddWithNullableValue("@P2", this.WasteType); scmd.Parameters.AddWithNullableValue("@P3", this.WasteDescription); scmd.Parameters.AddWithNullableValue("@P4", this.WasteDisposalAmount_MTQ); scmd.Parameters.AddWithNullableValue("@P5", this.WasteCapacity_MTQ); scmd.Parameters.AddWithNullableValue("@P6", this.WasteAmountRetained_MTQ); scmd.Parameters.AddWithNullableValue("@P7", this.WasteDisposalPort); scmd.Parameters.AddWithNullableValue("@P8", this.WasteAmountGeneratedTillNextPort_MTQ); scmd.Parameters.AddWithNullableValue("@P9", this.WasteDisposedAtLastPort_MTQ); scmd.Parameters.AddWithNullableValue("@P10", this.Identifier); if (this.IsNew) { this.CreateId(); scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("INSERT INTO {0} (Id, WASId, WasteType, WasteDescription, " + "WasteDisposalAmount_MTQ, WasteCapacity_MTQ, WasteAmountRetained_MTQ, WasteDisposalPort, " + "WasteAmountGeneratedTillNextPort_MTQ, WasteDisposedAtLastPort_MTQ, Identifier) " + " VALUES ( @ID, @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9, @P10 )", this.Tablename); } else { scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("UPDATE {0} SET WasteType = @P2, WasteDescription = @P3, " + "WasteDisposalAmount_MTQ = @P4, WasteCapacity_MTQ = @P5, WasteAmountRetained_MTQ = @P6," + "WasteDisposalPort = @P7, WasteAmountGeneratedTillNextPort_MTQ = @P8, WasteDisposedAtLastPort_MTQ = @P9, " + "Identifier = @P10 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, WasteType, WasteDescription, WasteDisposalAmount_MTQ, " + "WasteCapacity_MTQ, WasteAmountRetained_MTQ, WasteDisposalPort, WasteAmountGeneratedTillNextPort_MTQ, " + "WasteDisposedAtLastPort_MTQ, Identifier FROM {0} ", this.Tablename); switch (filter) { case Message.LoadFilter.WAS_ID: query += " WHERE WASId = @WDID"; ((SqlCommand)cmd).Parameters.AddWithValue("@WDID", 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()) { Waste waste = new Waste(); waste.id = reader.GetGuid(0); if (!reader.IsDBNull(1)) waste.WasteType = reader.GetInt32(1); if (!reader.IsDBNull(2)) waste.WasteDescription = reader.GetString(2); if (!reader.IsDBNull(3)) waste.WasteDisposalAmount_MTQ = (float) reader.GetDouble(3); if (!reader.IsDBNull(4)) waste.WasteCapacity_MTQ = (float) reader.GetDouble(4); if (!reader.IsDBNull(5)) waste.WasteAmountRetained_MTQ = (float) reader.GetDouble(5); if (!reader.IsDBNull(6)) waste.WasteDisposalPort = reader.GetString(6); if (!reader.IsDBNull(7)) waste.WasteAmountGeneratedTillNextPort_MTQ = (float) reader.GetDouble(7); if (!reader.IsDBNull(8)) waste.WasteDisposedAtLastPort_MTQ = (float)reader.GetDouble(8); if (!reader.IsDBNull(9)) waste.Identifier = reader.GetString(9); result.Add(waste); } reader.Close(); result.Sort(); return result; } #endregion #region Validation public override void Validate(List errors, List violations) { if (this.IsDashWasteCode && this.WasteDescription.IsNullOrEmpty()) violations.Add(RuleEngine.CreateViolation(ValidationCode.V781, "WasteDescription", null, this.Title, this.Identifier, this.Tablename)); if (!this.HasValidWasteCode) violations.Add(RuleEngine.CreateViolation(ValidationCode.V784, "WasteCode", null, this.Title, this.Identifier, this.Tablename)); if (this.WasteDisposalAmount_MTQ > 10000) violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Waste disposal amount", null, this.Title, this.Identifier, this.Tablename)); if (this.WasteCapacity_MTQ > 10000) violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Waste capacity", null, this.Title, this.Identifier, this.Tablename)); if (this.WasteAmountRetained_MTQ > 10000) violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Waste amount retained", null, this.Title, this.Identifier, this.Tablename)); if (this.WasteAmountGeneratedTillNextPort_MTQ > 10000) violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Waste generated till next port", null, this.Title, this.Identifier, this.Tablename)); if(this.WasteDisposalAmount_MTQ > this.WasteCapacity_MTQ) violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Disposal greater than capacity!", null, this.Title, this.Identifier, this.Tablename)); if((this.WasteAmountGeneratedTillNextPort_MTQ + this.WasteAmountRetained_MTQ) > this.WasteCapacity_MTQ) violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Waste generated+retained greater than capacity!", null, this.Title, this.Identifier, this.Tablename)); } #endregion #region IComparable implementation public int CompareTo(object obj) { if (object.ReferenceEquals(obj, null)) return 1; return this.Identifier.CompareTo(((Waste)obj).Identifier); } #endregion } }