git_bsmd/nsw/Source/bsmd.database/MDH.cs

570 lines
25 KiB
C#

//
// Class: MDH
// Current CLR: 4.0.30319.34209
// System: Microsoft Visual Studio 10.0
// Author: dani
// Created: 3/21/2015 10:56:30 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.Text;
namespace bsmd.database
{
public class MDH : DatabaseEntity, ISublistContainer
{
private List<DatabaseEntity> portOfCallLast30Days = new List<DatabaseEntity>();
private List<DatabaseEntity> sanitaryMeasuresDetails = new List<DatabaseEntity>();
private List<DatabaseEntity> stowawaysJoiningLocations = new List<DatabaseEntity>();
private List<DatabaseEntity> infectedAreas = new List<DatabaseEntity>();
public MDH()
{
this.tablename = "[dbo].[MDH]";
this.MDHSimplification = false;
}
#region Properties
public List<DatabaseEntity> PortOfCallLast30Days { get { return this.portOfCallLast30Days; } }
public List<DatabaseEntity> SanitaryMeasuresDetails { get { return this.sanitaryMeasuresDetails; } }
public List<DatabaseEntity> StowawaysJoiningLocations { get { return this.stowawaysJoiningLocations; } }
public List<DatabaseEntity> InfectedAreas { get { return this.infectedAreas; } }
[ShowReport]
[Validation1(ValidationCode.NOT_NULL)]
[ENI2Validation]
public bool? MDHSimplification { get; set; }
[ShowReport]
[Validation1(ValidationCode.LOCODE_GER)]
[MaxLength(5)]
[ENI2Validation]
public string PortOfCallWhereCompleteMDHNotified { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.NonAccidentialDeathsDuringVoyage")]
[ENI2Validation]
public bool? NonAccidentalDeathsDuringVoyage { get; set; }
[ShowReport]
[LookupName("MDH.NonAccidentialDeathsDuringVoyageCount")]
[ENI2Validation]
public int? NonAccidentalDeathsDuringVoyageCount { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.SuspisionInfectiousNature")]
[ENI2Validation]
public bool? SuspisionInfectiousNature { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.NumberOfIllPersonsHigherThanExpected")]
[ENI2Validation]
public bool? NumberOfIllPersonsHigherThanExpected { get; set; }
[ShowReport]
[LookupName("MDH.NumberOfIllPersons")]
[ENI2Validation]
public int? NumberOfIllPersons { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.SickPersonsOnBoard")]
[ENI2Validation]
public bool? SickPersonsOnBoard { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.MedicalConsulted")]
[ENI2Validation]
public bool? MedicalConsulted { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.AwareOfConditionsForFurtherInfections")]
[ENI2Validation]
public bool? AwareOfFurtherInfections { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.SanitaryMeasuresApplied")]
[ENI2Validation]
public bool? SanitaryMeasuresApplied { get; set; }
[ShowReport]
[LookupName("MDH.SanitaryMeasuresType_1")] // TODO: NSW 3.0: wird ein Array
[MaxLength(100)]
[ENI2Validation]
public string SanitaryMeasuresType { get; set; }
[ShowReport]
[LookupName("MDH.SanitaryMeasuresLocation_1")]
[MaxLength(100)]
[ENI2Validation]
public string SanitaryMeasuresLocation { get; set; }
[ShowReport]
[LookupName("MDH.SanitaryMeasuresDate_1")]
[ENI2Validation]
public DateTime? SanitaryMeasuresDate { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.StowawaysDetected")]
[ENI2Validation]
public bool? StowawaysDetected { get; set; }
[ShowReport]
[LookupName("MDH.StowawaysJoiningLocation_1")] // TODO: NSW 3.0: wird ein Array
[MaxLength(100)]
[ENI2Validation]
public string StowawaysJoiningLocation { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.SickAnimalOrPetOnBoard")]
[ENI2Validation]
public bool? SickAnimalOrPetOnBoard { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.ValidSanitaryControlExemptionOrCertificateOnBoard")]
[ENI2Validation]
public bool? ValidSanitaryControlExemptionOrCertificateOnBoard { get; set; }
[ShowReport]
[LookupName("MDH.PlaceOfIssue")]
[MaxLength(100)]
[ENI2Validation]
public string PlaceOfIssue { get; set; }
[ShowReport]
[LookupName("MDH.DateOfIssue")]
[ENI2Validation]
public DateTime? DateOfIssue { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.SanitaryControlReinspectionRequired")]
[ENI2Validation]
public bool? SanitaryControlReinspectionRequired { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("MDH.InfectedAreaVisited")]
[ENI2Validation]
public bool? InfectedAreaVisited { get; set; }
[ShowReport]
[LookupName("MDH.InfectedAreaPort_1")] // TODO: NSW 3.0: wird ein Array
[MaxLength(100)]
[ENI2Validation]
public string InfectedAreaPort { get; set; }
[ShowReport]
[LookupName("MDH.InfectedAreaDate_1")]
[ENI2Validation]
public DateTime? InfectedAreaDate { get; set; }
/// <summary>
/// Hilfsproperty, um eine kommaseparierte Liste von JoiningLocation (analog ANSW) im ENI-2 anzuzeigen,
/// </summary>
[ENI2Validation]
public string StowawaysJoiningLocationText
{
get
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.StowawaysJoiningLocations.Count; i++)
{
if (i > 0)
sb.Append(", ");
sb.Append(((StowawaysJoiningLocation)this.StowawaysJoiningLocations[i]).StowawayJoiningLocation);
}
return sb.ToString();
}
set
{
if (value.IsNullOrEmpty())
{
foreach (StowawaysJoiningLocation wdsp in this.StowawaysJoiningLocations)
DBManager.Instance.Delete(wdsp);
this.StowawaysJoiningLocations.Clear();
}
else
{
string[] joiningLocations = value.Split(',');
List<StowawaysJoiningLocation> foundList = new List<database.StowawaysJoiningLocation>();
for (int i = 0; i < joiningLocations.Length; i++)
{
string jLocation = joiningLocations[i].Trim();
if (jLocation.Length > 0)
{
StowawaysJoiningLocation matchingLocation = null;
foreach (StowawaysJoiningLocation wdsp in this.StowawaysJoiningLocations)
{
if (wdsp.StowawayJoiningLocation.Equals(jLocation, StringComparison.OrdinalIgnoreCase))
{
matchingLocation = wdsp;
break;
}
}
if (matchingLocation != null)
{
foundList.Add(matchingLocation);
this.StowawaysJoiningLocations.Remove(matchingLocation);
}
else
{
StowawaysJoiningLocation newLocation = new StowawaysJoiningLocation();
newLocation.MDH = this;
newLocation.StowawayJoiningLocation = jLocation;
foundList.Add(newLocation);
}
}
}
// remove remaining location (no longer valid)
foreach (StowawaysJoiningLocation remainingLocation in this.StowawaysJoiningLocations)
DBManager.Instance.Delete(remainingLocation);
this.StowawaysJoiningLocations.Clear();
// add existing and new crew
this.StowawaysJoiningLocations.AddRange(foundList);
}
}
}
#endregion
#region public helper
internal void SetBoolsToFalse()
{
this.AwareOfFurtherInfections = false;
this.InfectedAreaVisited = false;
this.MDHSimplification = false;
this.MedicalConsulted = false;
this.NonAccidentalDeathsDuringVoyage = false;
this.NumberOfIllPersonsHigherThanExpected = false;
this.SanitaryControlReinspectionRequired = false;
this.SanitaryMeasuresApplied = false;
this.SickAnimalOrPetOnBoard = false;
this.SickPersonsOnBoard = false;
this.StowawaysDetected = false;
this.SuspisionInfectiousNature = false;
this.ValidSanitaryControlExemptionOrCertificateOnBoard = false;
}
#endregion
#region abstract method implementation
public override void PrepareSave(System.Data.IDbCommand cmd)
{
SqlCommand scmd = cmd as SqlCommand;
scmd.Parameters.AddWithNullableValue("@P1", this.MessageHeader.Id);
scmd.Parameters.AddWithNullableValue("@P2", this.MDHSimplification);
scmd.Parameters.AddWithNullableValue("@P3", this.PortOfCallWhereCompleteMDHNotified);
scmd.Parameters.AddWithNullableValue("@P4", this.NonAccidentalDeathsDuringVoyage);
scmd.Parameters.AddWithNullableValue("@P5", this.NonAccidentalDeathsDuringVoyageCount);
scmd.Parameters.AddWithNullableValue("@P6", this.SuspisionInfectiousNature);
scmd.Parameters.AddWithNullableValue("@P7", this.NumberOfIllPersonsHigherThanExpected);
scmd.Parameters.AddWithNullableValue("@P8", this.NumberOfIllPersons);
scmd.Parameters.AddWithNullableValue("@P9", this.SickPersonsOnBoard);
scmd.Parameters.AddWithNullableValue("@P10", this.MedicalConsulted);
scmd.Parameters.AddWithNullableValue("@P11", this.AwareOfFurtherInfections);
scmd.Parameters.AddWithNullableValue("@P12", this.SanitaryMeasuresApplied);
scmd.Parameters.AddWithNullableValue("@P13", this.SanitaryMeasuresType);
scmd.Parameters.AddWithNullableValue("@P14", this.SanitaryMeasuresLocation);
scmd.Parameters.AddWithNullableValue("@P15", this.SanitaryMeasuresDate);
scmd.Parameters.AddWithNullableValue("@P16", this.StowawaysDetected);
scmd.Parameters.AddWithNullableValue("@P17", this.StowawaysJoiningLocation);
scmd.Parameters.AddWithNullableValue("@P18", this.SickAnimalOrPetOnBoard);
scmd.Parameters.AddWithNullableValue("@P19", this.ValidSanitaryControlExemptionOrCertificateOnBoard);
scmd.Parameters.AddWithNullableValue("@P20", this.PlaceOfIssue);
scmd.Parameters.AddWithNullableValue("@P21", this.DateOfIssue);
scmd.Parameters.AddWithNullableValue("@P22", this.SanitaryControlReinspectionRequired);
scmd.Parameters.AddWithNullableValue("@P23", this.InfectedAreaVisited);
scmd.Parameters.AddWithNullableValue("@P24", this.InfectedAreaPort);
scmd.Parameters.AddWithNullableValue("@P25", this.InfectedAreaDate);
if (this.IsNew)
{
this.CreateId();
scmd.Parameters.AddWithValue("@ID", this.Id);
scmd.CommandText = string.Format("INSERT INTO {0} (Id, MessageHeaderId, MDHSimplification, PortOfCallWhereCompleteMDHNotified, " +
"NonAccidentalDeathsDuringVoyage, NonAccidentalDeathsDuringVoyageCount, SuspisionInfectiousNature, NumberOfIllPersonsHigherThanExpected, " +
"NumberOfIllPersons, SickPersonsOnBoard, MedicalConsulted, AwareOfFurtherInfections, SanitaryMeasuresApplied, SanitaryMeasuresType, " +
"SanitaryMeasuresLocation, SanitaryMeasuresDate, StowawaysDetected, StowawaysJoiningLocation, SickAnimalOrPetOnBoard, " +
"ValidSanitaryControlExemptionOrCertificateOnBoard, PlaceOfIssue, DateOfIssue, SanitaryControlReinspectionRequired, " +
"InfectedAreaVisited, InfectedAreaPort, InfectedAreaDate) VALUES (@ID, @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9, @P10, @P11, @P12, " +
"@P13, @P14, @P15, @P16, @P17, @P18, @P19, @P20, @P21, @P22, @P23, @P24, @P25)", this.Tablename);
}
else
{
scmd.CommandText = string.Format("UPDATE {0} SET MDHSimplification = @P2, PortOfCallWhereCompleteMDHNotified = @P3, " +
"NonAccidentalDeathsDuringVoyage = @P4, NonAccidentalDeathsDuringVoyageCount = @P5, SuspisionInfectiousNature = @P6, " +
"NumberOfIllPersonsHigherThanExpected = @P7, NumberOfIllPersons = @P8, SickPersonsOnBoard = @P9, MedicalConsulted = @P10, " +
"AwareOfFurtherInfections = @P11, SanitaryMeasuresApplied = @P12, SanitaryMeasuresType = @P13, SanitaryMeasuresLocation = @P14, " +
"SanitaryMeasuresDate = @P15, StowawaysDetected = @P16, StowawaysJoiningLocation = @P17, SickAnimalOrPetOnBoard = @P18, " +
"ValidSanitaryControlExemptionOrCertificateOnBoard = @P19, PlaceofIssue = @P20, DateOfIssue = @P21, SanitaryControlReinspectionRequired = @P22, " +
"InfectedAreaVisited = @P23, InfectedAreaPort = @P24, InfectedAreaDate = @P25 WHERE Id = @ID", this.Tablename);
scmd.Parameters.AddWithValue("@ID", this.Id);
}
}
public override void PrepareLoadCommand(System.Data.IDbCommand cmd, Message.LoadFilter filter, params object[] criteria)
{
string query = string.Format("SELECT Id, MDHSimplification, PortOfCallWhereCompleteMDHNotified, " +
"NonAccidentalDeathsDuringVoyage, NonAccidentalDeathsDuringVoyageCount, SuspisionInfectiousNature, " +
"NumberOfIllPersonsHigherThanExpected, NumberOfIllPersons, SickPersonsOnBoard, MedicalConsulted, AwareOfFurtherInfections, " +
"SanitaryMeasuresApplied, SanitaryMeasuresType, SanitaryMeasuresLocation, SanitaryMeasuresDate, StowawaysDetected, " +
"StowawaysJoiningLocation, SickAnimalOrPetOnBoard, ValidSanitaryControlExemptionOrCertificateOnBoard, " +
"PlaceOfIssue, DateOfIssue, SanitaryControlReinspectionRequired, InfectedAreaVisited, InfectedAreaPort, InfectedAreaDate " +
"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<DatabaseEntity> LoadList(System.Data.IDataReader reader)
{
List<DatabaseEntity> result = new List<DatabaseEntity>();
while (reader.Read())
{
MDH mdh = new MDH();
mdh.id = reader.GetGuid(0);
if (!reader.IsDBNull(1)) mdh.MDHSimplification = reader.GetBoolean(1);
if (!reader.IsDBNull(2)) mdh.PortOfCallWhereCompleteMDHNotified = reader.GetString(2);
if (!reader.IsDBNull(3)) mdh.NonAccidentalDeathsDuringVoyage = reader.GetBoolean(3);
if (!reader.IsDBNull(4)) mdh.NonAccidentalDeathsDuringVoyageCount = reader.GetInt32(4);
if (!reader.IsDBNull(5)) mdh.SuspisionInfectiousNature = reader.GetBoolean(5);
if (!reader.IsDBNull(6)) mdh.NumberOfIllPersonsHigherThanExpected = reader.GetBoolean(6);
if (!reader.IsDBNull(7)) mdh.NumberOfIllPersons = reader.GetInt32(7);
if (!reader.IsDBNull(8)) mdh.SickPersonsOnBoard = reader.GetBoolean(8);
if (!reader.IsDBNull(9)) mdh.MedicalConsulted = reader.GetBoolean(9);
if (!reader.IsDBNull(10)) mdh.AwareOfFurtherInfections = reader.GetBoolean(10);
if (!reader.IsDBNull(11)) mdh.SanitaryMeasuresApplied = reader.GetBoolean(11);
if (!reader.IsDBNull(12)) mdh.SanitaryMeasuresType = reader.GetString(12);
if (!reader.IsDBNull(13)) mdh.SanitaryMeasuresLocation = reader.GetString(13);
if (!reader.IsDBNull(14)) mdh.SanitaryMeasuresDate = reader.GetDateTime(14);
if (!reader.IsDBNull(15)) mdh.StowawaysDetected = reader.GetBoolean(15);
if (!reader.IsDBNull(16)) mdh.StowawaysJoiningLocation = reader.GetString(16);
if (!reader.IsDBNull(17)) mdh.SickAnimalOrPetOnBoard = reader.GetBoolean(17);
if (!reader.IsDBNull(18)) mdh.ValidSanitaryControlExemptionOrCertificateOnBoard = reader.GetBoolean(18);
if (!reader.IsDBNull(19)) mdh.PlaceOfIssue = reader.GetString(19);
if (!reader.IsDBNull(20)) mdh.DateOfIssue = reader.GetDateTime(20);
if (!reader.IsDBNull(21)) mdh.SanitaryControlReinspectionRequired = reader.GetBoolean(21);
if (!reader.IsDBNull(22)) mdh.InfectedAreaVisited = reader.GetBoolean(22);
if (!reader.IsDBNull(23)) mdh.InfectedAreaPort = reader.GetString(23);
if (!reader.IsDBNull(24)) mdh.InfectedAreaDate = reader.GetDateTime(24);
result.Add(mdh);
}
reader.Close();
return result;
}
#endregion
#region ISublistContainer implementation
public ISublistElement GetSublistElementWithIdentifier(string identifier)
{
foreach(ISublistElement sElem in this.PortOfCallLast30Days)
{
if (sElem.Identifier.Equals(identifier)) return sElem;
}
return null;
}
public int NumberOfExcelRows
{
get { return 37; }
}
public void SaveElements()
{
foreach(PortOfCallLast30Days poc30d in this.PortOfCallLast30Days)
{
DBManager.Instance.Save(poc30d);
((ISublistContainer)poc30d).SaveElements();
}
foreach(SanitaryMeasuresDetail smd in this.SanitaryMeasuresDetails)
{
DBManager.Instance.Save(smd);
}
foreach(StowawaysJoiningLocation sjl in this.stowawaysJoiningLocations)
{
DBManager.Instance.Save(sjl);
}
foreach(InfectedArea ia in this.InfectedAreas)
{
DBManager.Instance.Save(ia);
}
}
public void DeleteElements()
{
foreach (PortOfCallLast30Days poc30d in this.PortOfCallLast30Days)
{
((ISublistContainer)poc30d).DeleteElements();
DBManager.Instance.Delete(poc30d);
}
this.PortOfCallLast30Days.Clear();
foreach (SanitaryMeasuresDetail smd in this.SanitaryMeasuresDetails)
{
DBManager.Instance.Delete(smd);
}
this.sanitaryMeasuresDetails.Clear();
foreach (StowawaysJoiningLocation sjl in this.stowawaysJoiningLocations)
{
DBManager.Instance.Delete(sjl);
}
this.StowawaysJoiningLocations.Clear();
foreach (InfectedArea ia in this.InfectedAreas)
{
DBManager.Instance.Delete(ia);
}
this.InfectedAreas.Clear();
}
public SanitaryMeasuresDetail GetSanitaryMeasuresDetailWithIdentifier(string identifier)
{
foreach (SanitaryMeasuresDetail sd in this.sanitaryMeasuresDetails)
if (sd.Identifier.Equals(identifier)) return sd;
return null;
}
public StowawaysJoiningLocation GetStowawaysJoiningLocationWithIdentifier(string identifier)
{
foreach (StowawaysJoiningLocation sj in this.stowawaysJoiningLocations)
if (sj.Identifier.Equals(identifier)) return sj;
return null;
}
public InfectedArea GetInfectedAreaWithIdentifier(string identifier)
{
foreach (InfectedArea ia in this.infectedAreas)
if (ia.Identifier.Equals(identifier)) return ia;
return null;
}
#endregion
#region IMessageParagraph implementation
public override string Subtitle
{
get
{
return "Medical declaration of health";
}
}
public override List<IMessageParagraph> ChildParagraphs
{
get
{
List<IMessageParagraph> result = new List<IMessageParagraph>();
foreach (IMessageParagraph imp in this.PortOfCallLast30Days)
result.Add(imp);
return result;
}
}
#endregion
#region Validation
public override DatabaseEntity.ValidationBlock GetValidationBlock()
{
return (this.MDHSimplification ?? false) ? DatabaseEntity.ValidationBlock.BLOCK1 : DatabaseEntity.ValidationBlock.BLOCK2;
}
public override void Validate(List<MessageError> errors, List<MessageViolation> violations)
{
if (this.GetValidationBlock() == ValidationBlock.BLOCK1)
{
if (this.PortOfCallWhereCompleteMDHNotified.IsNullOrEmpty())
violations.Add(RuleEngine.CreateViolation(ValidationCode.V768, "PortOfCallWhereCompleteMDHNotified missing", null, this.Title, null));
}
else
{
if ((this.NonAccidentalDeathsDuringVoyage ?? false) && ((this.NonAccidentalDeathsDuringVoyageCount ?? 0) == 0))
violations.Add(RuleEngine.CreateViolation(ValidationCode.V761, "NonAccidentalDeathsDuringVoyageCount missing", null, this.Title, null));
if ((this.NumberOfIllPersonsHigherThanExpected ?? false) && ((this.NumberOfIllPersons ?? 0) == 0))
violations.Add(RuleEngine.CreateViolation(ValidationCode.V762, "Number of ill persons missing", null, this.Title, null));
if ((this.SanitaryMeasuresApplied ?? false) && (
this.SanitaryMeasuresType.IsNullOrEmpty() ||
!this.SanitaryMeasuresDate.HasValue ||
this.SanitaryMeasuresLocation.IsNullOrEmpty()))
violations.Add(RuleEngine.CreateViolation(ValidationCode.V763, "Sanitary measure details missing", null, this.Title, null));
if ((this.StowawaysDetected ?? false) && this.StowawaysJoiningLocation.IsNullOrEmpty())
violations.Add(RuleEngine.CreateViolation(ValidationCode.V764, "Stowaways joining location missing", null, this.Title, null));
if ((this.ValidSanitaryControlExemptionOrCertificateOnBoard ?? false) &&
(this.PlaceOfIssue.IsNullOrEmpty() || !this.DateOfIssue.HasValue))
violations.Add(RuleEngine.CreateViolation(ValidationCode.V765, "Cert. Place or Date of issue missing", null, this.Title, null));
if ((this.InfectedAreaVisited ?? false) &&
(!this.InfectedAreaDate.HasValue || this.InfectedAreaPort.IsNullOrEmpty()))
violations.Add(RuleEngine.CreateViolation(ValidationCode.V766, "Infected area date or port missing", null, this.Title, null));
if (this.portOfCallLast30Days.Count == 0)
{
errors.Add(RuleEngine.CreateError(ValidationCode.NOT_NULL, "Port of Call last 30 day list is EMPTY", null, this.Title, null));
}
foreach (PortOfCallLast30Days poc30d in this.portOfCallLast30Days)
{
RuleEngine.ValidateProperties(poc30d, errors);
poc30d.Validate(errors, violations);
}
}
}
#endregion
}
}