// // Class: PortOfCallLast30Days // Current CLR: 4.0.30319.34209 // System: Microsoft Visual Studio 10.0 // Author: dani // Created: 3/22/2015 8:29:02 AM // // Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved. using System; using System.Data; using System.Text; using System.Data.SqlClient; using System.Collections.Generic; namespace bsmd.database { public class PortOfCallLast30Days : DatabaseEntity, ISublistElement, ISublistContainer { private List poc30Crew = new List(); public PortOfCallLast30Days() { this.tablename = "[dbo].[PortOfCallLast30Days]"; } #region Properties public MDH MDH { get; set; } public List CrewJoinedShip { get { return this.poc30Crew; } } [ShowReport] [Validation2(ValidationCode.LOCODE)] [MaxLength(5)] [ENI2Validation] public string PortOfCallLast30DaysLocode { get; set; } [ShowReport] [Validation2(ValidationCode.NOT_NULL)] [ENI2Validation] public DateTime? PortOfCallLast30DaysDateOfDeparture { get; set; } [ShowReport] [Validation2(ValidationCode.NOT_NULL)] [ENI2Validation] public bool? PortOfCallLast30DaysCrewMembersJoined { get; set; } public string Identifier { get; set; } public string SublistCollectionKey { get { return "pocl30d"; } } /// /// Hilfsproperty, um eine kommaseparierte Liste von Crew (analog ANSW) im ENI-2 anzuzeigen, /// [ENI2Validation] public string CrewMembersJoinedText { get { StringBuilder sb = new StringBuilder(); for (int i = 0; i < this.CrewJoinedShip.Count; i++) { if (i > 0) sb.Append(", "); sb.Append(((PortOfCallLast30DaysCrewJoinedShip) this.CrewJoinedShip[i]).PortOfCallLast30DaysCrewJoinedShipName); } return sb.ToString(); } set { if (value.IsNullOrEmpty()) { foreach (PortOfCallLast30DaysCrewJoinedShip wdsp in this.CrewJoinedShip) DBManager.Instance.Delete(wdsp); this.CrewJoinedShip.Clear(); } else { string[] crewNames = value.Split(','); List foundList = new List(); for (int i = 0; i < crewNames.Length; i++) { string crewName = crewNames[i].Trim(); if (crewName.Length > 0) { PortOfCallLast30DaysCrewJoinedShip matchingCrew = null; foreach (PortOfCallLast30DaysCrewJoinedShip wdsp in this.CrewJoinedShip) { if (wdsp.PortOfCallLast30DaysCrewJoinedShipName.Equals(crewName, StringComparison.OrdinalIgnoreCase)) { matchingCrew = wdsp; break; } } if (matchingCrew != null) { foundList.Add(matchingCrew); this.CrewJoinedShip.Remove(matchingCrew); } else { PortOfCallLast30DaysCrewJoinedShip newPoC30Crew = new PortOfCallLast30DaysCrewJoinedShip(); newPoC30Crew.PortOfCallLast30Days = this; newPoC30Crew.PortOfCallLast30DaysCrewJoinedShipName = crewName; foundList.Add(newPoC30Crew); } } } // remove remaining crew (no longer valid) foreach (PortOfCallLast30DaysCrewJoinedShip remainingCrew in this.CrewJoinedShip) DBManager.Instance.Delete(remainingCrew); this.CrewJoinedShip.Clear(); // add existing and new crew this.CrewJoinedShip.AddRange(foundList); DatabaseEntity.ResetIdentifiers(this.CrewJoinedShip); } } } #endregion #region abstract class implementation public override void PrepareSave(IDbCommand cmd) { SqlCommand scmd = cmd as SqlCommand; scmd.Parameters.AddWithNullableValue("@P1", this.MDH.Id); scmd.Parameters.AddWithNullableValue("@P2", this.PortOfCallLast30DaysLocode); scmd.Parameters.AddWithNullableValue("@P3", this.PortOfCallLast30DaysDateOfDeparture); scmd.Parameters.AddWithNullableValue("@P4", this.PortOfCallLast30DaysCrewMembersJoined); scmd.Parameters.AddWithNullableValue("@P5", this.Identifier); if (this.IsNew) { this.CreateId(); scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("INSERT INTO {0} (Id, MDH_Id, PortOfCallLast30DaysLocode, PortOfCallLast30DaysDateOfDeparture, " + "PortOfCallLast30DaysCrewMembersJoined, Identifier) VALUES (@ID, @P1, @P2, @P3, @P4, @P5)", this.Tablename); } else { scmd.CommandText = string.Format("UPDATE {0} SET PortOfCallLast30DaysLocode = @P2, PortOfCallLast30DaysDateOfDeparture = @P3, " + "PortOfCallLast30DaysCrewMembersJoined = @P4, Identifier = @P5 WHERE Id = @ID", this.Tablename); scmd.Parameters.AddWithNullableValue("@ID", this.Id); } } public override void PrepareLoadCommand(IDbCommand cmd, Message.LoadFilter filter, params object[] criteria) { string query = string.Format("SELECT Id, PortOfCallLast30DaysLocode, PortOfCallLast30DaysDateOfDeparture, " + "PortOfCallLast30DaysCrewMembersJoined, Identifier FROM {0} ", this.Tablename); switch (filter) { case Message.LoadFilter.MDH_ID: query += "WHERE MDH_Id = @MDHID"; if (!cmd.Parameters.Contains("@MDHID")) ((SqlCommand)cmd).Parameters.AddWithValue("@MDHID", criteria[0]); break; case Message.LoadFilter.ALL: default: break; } query += " ORDER BY CAST(Identifier AS INT)"; cmd.CommandText = query; } public override List LoadList(IDataReader reader) { List result = new List(); while (reader.Read()) { PortOfCallLast30Days poc = new PortOfCallLast30Days(); poc.id = reader.GetGuid(0); if (!reader.IsDBNull(1)) poc.PortOfCallLast30DaysLocode = reader.GetString(1); if (!reader.IsDBNull(2)) poc.PortOfCallLast30DaysDateOfDeparture = reader.GetDateTime(2); if (!reader.IsDBNull(3)) poc.PortOfCallLast30DaysCrewMembersJoined = reader.GetBoolean(3); if (!reader.IsDBNull(4)) poc.Identifier = reader.GetString(4); result.Add(poc); } reader.Close(); return result; } #endregion #region ISublistContainer implementation public ISublistElement GetSublistElementWithIdentifier(string identifier) { foreach (ISublistElement sElem in this.CrewJoinedShip) if (sElem.Identifier.Equals(identifier)) return sElem; return null; } public int NumberOfExcelRows { get { return 1; } } public void SaveElements() { foreach (PortOfCallLast30DaysCrewJoinedShip cjs in this.CrewJoinedShip) { DBManager.Instance.Save(cjs); } } public void DeleteElements() { foreach (PortOfCallLast30DaysCrewJoinedShip cjs in this.CrewJoinedShip) { DBManager.GetSingleCon(DBManager.Instance.ConnectionString).Delete(cjs); } this.CrewJoinedShip.Clear(); } #endregion #region IMessageParagraph implementation public override List ChildParagraphs { get { List result = new List(); foreach (IMessageParagraph imp in this.CrewJoinedShip) result.Add(imp); return result; } } #endregion #region Validation public override ValidationBlock GetValidationBlock() { return ValidationBlock.BLOCK2; } public override void Validate(List errors, List violations) { if((this.PortOfCallLast30DaysCrewMembersJoined ?? false) && (this.CrewJoinedShip.Count == 0)) violations.Add(RuleEngine.CreateViolation(ValidationCode.V767, "PortOfCallLast30DaysCrewMembersJoined", null, this.Title, this.Identifier, this.MDH.Tablename)); if (this.PortOfCallLast30DaysCrewMembersJoined ?? false) { foreach (PortOfCallLast30DaysCrewJoinedShip poc30Crew in this.CrewJoinedShip) { RuleEngine.ValidateProperties(poc30Crew, errors, violations); } } } #endregion } }