// // Class: WAS // Current CLR: 4.0.30319.34209 // System: Microsoft Visual Studio 10.0 // Author: dani // Created: 4/2/2015 7:38:45 PM // // Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved. using System; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; namespace bsmd.database { public class WAS : DatabaseEntity, ISublistContainer { private List wdsp = new List(); private List waste = new List(); private static readonly int[] dkWasteCodes = { 1100, 1200, 1300, 2100, 2200, 2300, 2311, 2308, 2300, 2309, 3000, 5100, 5200, 5300, 2300 }; private static readonly string[] dkWasteTypes = { "Waste oils - Sludge", "Waste oils - Bilge water", "Waste oils - Other", "Garbage - Food waste", "Garbage - Plastic", "Garbage - Other", "Garbage - Other - Cooking oil", "Garbage - Other - Incinerator ashes and clinkers", "Garbage - Other", "Garbage - Other - Animal carcasses", "Sewage", "Cargo residues - Marpol Annex I - Other", "Cargo residues - Marpol Annex II - Other", "Cargo residues - Marpol Annex V - Other", "Garbage - Other" }; public WAS() { this.tablename = "[dbo].[WAS]"; } #region Properties public static int[] DKWasteCodes { get { return dkWasteCodes; } } public static string[] DKWasteTypes { get { return dkWasteTypes; } } [ShowReport] public bool? WasteDisposalValidExemption { get; set; } [ShowReport] [Validation2(ValidationCode.NOT_NULL)] [LookupName("WAS.LastWasteDisposalPort")] [MaxLength(5)] public string LastWasteDisposalPort { get; set; } [ShowReport] [Validation2(ValidationCode.NOT_NULL)] [LookupName("WAS.ConfirmationOfCorrectness")] public bool? ConfirmationOfCorrectness { get; set; } [ShowReport] [Validation2(ValidationCode.NOT_NULL)] [LookupName("WAS.LastWasteDisposalDate")] public DateTime? LastWasteDisposalDate { get; set; } [ShowReport] [Validation2(ValidationCode.NOT_NULL)] public byte? WasteDisposalDelivery { get; set; } [LookupName("WAS.ConfirmationOfSufficiency")] public bool? ConfirmationOfSufficiency { get; set; } public List Waste { get { return this.waste; } } public List WasteDisposalServiceProvider { get { return this.wdsp; } } #endregion #region DatabaseEntity implementation public override void PrepareSave(System.Data.IDbCommand cmd) { SqlCommand scmd = cmd as SqlCommand; scmd.Parameters.AddWithValue("@P1", this.MessageHeader.Id); scmd.Parameters.AddWithNullableValue("@P2", this.WasteDisposalValidExemption); scmd.Parameters.AddWithNullableValue("@P3", this.LastWasteDisposalPort); scmd.Parameters.AddWithNullableValue("@P4", this.ConfirmationOfCorrectness); scmd.Parameters.AddWithNullableValue("@P5", this.LastWasteDisposalDate); scmd.Parameters.AddWithNullableValue("@P6", this.WasteDisposalDelivery); scmd.Parameters.AddWithNullableValue("@P7", this.ConfirmationOfSufficiency); if (this.IsNew) { this.CreateId(); scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("INSERT INTO {0} (Id, MessageHeaderId, WasteDisposalValidExemption, " + "LastWasteDisposalPort, ConfirmationOfCorrectness, LastWasteDisposalDate, WasteDisposalDelivery, " + "ConfirmationOfSufficiency) VALUES ( @ID, @P1, @P2, @P3, @P4, @P5, @P6, @P7 )", this.Tablename); } else { scmd.Parameters.AddWithValue(@"ID", this.Id); scmd.CommandText = string.Format("UPDATE {0} SET WasteDisposalValidExemption = @P2, LastWasteDisposalPort = @P3, " + "ConfirmationOfCorrectness = @P4, LastWasteDisposalDate = @P5, WasteDisposalDelivery = @P6, ConfirmationOfSufficiency = @P7 " + "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, WasteDisposalValidExemption, LastWasteDisposalPort, ConfirmationOfCorrectness, " + "LastWasteDisposalDate, WasteDisposalDelivery, ConfirmationOfSufficiency " + "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()) { WAS was = new WAS(); was.id = reader.GetGuid(0); if (!reader.IsDBNull(1)) was.WasteDisposalValidExemption = reader.GetBoolean(1); if (!reader.IsDBNull(2)) was.LastWasteDisposalPort = reader.GetString(2); if (!reader.IsDBNull(3)) was.ConfirmationOfCorrectness = reader.GetBoolean(3); if (!reader.IsDBNull(4)) was.LastWasteDisposalDate = reader.GetDateTime(4); if (!reader.IsDBNull(5)) was.WasteDisposalDelivery = reader.GetByte(5); if (!reader.IsDBNull(6)) was.ConfirmationOfSufficiency = reader.GetBoolean(6); result.Add(was); } reader.Close(); return result; } #endregion #region ISublistContainer implementation public ISublistElement GetSublistElementWithIdentifier(string identifier) { foreach (Waste waste in this.Waste) { if (waste.Identifier.Equals(identifier)) return waste; } return null; } public int NumberOfExcelRows { get { return 15; } } public void SaveElements() { foreach(Waste waste in this.Waste) { DBManager.Instance.Save(waste); } foreach(WasteDisposalServiceProvider wdsp in this.WasteDisposalServiceProvider) { DBManager.Instance.Save(wdsp); } } public void DeleteElements() { foreach (Waste waste in this.Waste) { DBManager.Instance.Delete(waste); } this.Waste.Clear(); foreach (WasteDisposalServiceProvider wdsp in this.WasteDisposalServiceProvider) { DBManager.Instance.Delete(wdsp); } this.WasteDisposalServiceProvider.Clear(); } #endregion #region IMessageParagraph implementation public override string Subtitle { get { return "Waste/cargo residue"; } } public override List ChildParagraphs { get { List result = new List(); foreach (IMessageParagraph imp in this.Waste) result.Add(imp); return result; } } #endregion #region Validation public override DatabaseEntity.ValidationBlock GetValidationBlock() { return (this.WasteDisposalValidExemption ?? false) ? ValidationBlock.BLOCK1 : ValidationBlock.BLOCK2; } public override void Validate(List errors, List violations) { if(this.GetValidationBlock() == ValidationBlock.BLOCK2) { bool allWasteTypesPresent = true; for(int i=1;i<=9;i++) { bool foundIndex = false; foreach(Waste waste in this.Waste) { if((waste.WasteType ?? 0) == i) { foundIndex = true; break; } } if (!foundIndex) { allWasteTypesPresent = false; break; } } if (!allWasteTypesPresent) { violations.Add(RuleEngine.CreateViolation(ValidationCode.V783, "not all waste types present!", null)); } foreach (Waste waste in this.Waste) { RuleEngine.ValidateProperties(waste, errors); waste.Validate(errors, violations); } } } #endregion #region public convenience method public List AddRemainingWasteTypes() { List result = new List(); for (byte wType = 1; wType <= 9; wType++ ) { bool wasteTypeFound = false; foreach (Waste w in this.Waste) { if ((w.WasteType ?? 0) == wType) wasteTypeFound = true; } if (!wasteTypeFound) { Waste newWaste = new Waste(); newWaste.WasteType = wType; newWaste.WasteAmountGeneratedTillNextPort_MTQ = 0; newWaste.WasteAmountRetained_MTQ = 0; newWaste.WasteCapacity_MTQ = 0; newWaste.WasteDisposalAmount_MTQ = 0; newWaste.Identifier = ""; newWaste.WasteDisposalPort = "ZZUKN"; newWaste.WasteDescription = "-"; newWaste.WAS = this; this.Waste.Add(newWaste); result.Add(newWaste); } } return result; } #endregion } }