// // 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; 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; } [JsonIgnore] [Browsable(false)] [ShowReport] public string WasteTypeDisplay { get { if (!WasteType.HasValue) return ""; switch (WasteType.Value) { case 1: return "WASTE_OILS-SLUDGE"; case 2: return "WASTE_OILS-BILGE_WATER"; case 3: return "WASTE_OILS-OTHERS"; case 4: return "GARBAGE-FOOD_WASTE"; case 5: return "GARBATE-PLASTIC"; case 6: return "GARBAGE-OTHER"; case 7: return "SEWAGE"; case 8: return "CARGO-ASSOCIATED_WASTE"; case 9: return "CARGO-RESIDUES"; default: return ""; } } } [ShowReport] [JsonIgnore] public string WasteTypeDisplayV4 { get { if (!WasteType.HasValue) return ""; switch(WasteType.Value) { 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 "Others (specify)"; } } } [JsonIgnore] public string WasteTypeDisplayGrid { get { return string.Format("{0} {1}", WasteType, WasteTypeDisplayV4); } } [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; } [ShowReport] [MaxLength(5)] [ENI2Validation] [Validation(ValidationCode.LOCODE)] public string WasteDisposalPort { get; set; } [ShowReport] [ENI2Validation] [Validation(ValidationCode.NOT_NULL)] public double? WasteAmountGeneratedTillNextPort_MTQ { get; set; } [ENI2Validation] [Validation(ValidationCode.NOT_NULL)] 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; switch(this.WasteType.Value) { case 1100: case 1200: case 1300: case 2100: case 2200: case 2300: case 2311: case 2308: case 2313: case 2600: case 2309: case 3000: case 5100: case 5200: case 5300: 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; } query += " ORDER BY CAST(Identifier AS INT)"; 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(); return result; } #endregion #region Validation public override void Validate(List errors, List violations) { if (this.WasteType.HasValue && (this.WasteType.Value == 1300) && 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)); } #endregion } }