git_bsmd/bsmd.database/SEC.cs

511 lines
23 KiB
C#

//
// Class: SEC
// Current CLR: 4.0.30319.34209
// System: Microsoft Visual Studio 10.0
// Author: dani
// Created: 4/3/2015 10:11:02 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.Collections.ObjectModel;
using System.ComponentModel;
using Newtonsoft.Json;
namespace bsmd.database
{
[TypeConverter(typeof(MessageClassConverter<SEC>))]
[JsonConverter(typeof(NoTypeConverterJsonConverter<SEC>))]
public class SEC : DatabaseEntity, ISublistContainer
{
public SEC()
{
this.tablename = "[dbo].[SEC]";
this.SECSimplification = false;
}
#region Properties
[ShowReport]
[Validation1(ValidationCode.NOT_NULL)]
[ENI2Validation]
public bool? SECSimplification { get; set; }
[ShowReport]
[Validation1(ValidationCode.LOCODE_GER)]
[MaxLength(5)]
[ENI2Validation]
public string PortOfCallWhereCompleteSECNotified { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("SEC.CSOLastName")]
[MaxLength(100)]
[ENI2Validation]
public string CSOLastName { get; set; }
[ShowReport]
[LookupName("SEC.CSOFirstName")]
[MaxLength(100)]
[ENI2Validation]
public string CSOFirstName { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("SEC.CSOPhone")]
[MaxLength(100)]
[ENI2Validation]
public string CSOPhone { get; set; }
[ShowReport]
[LookupName("SEC.CSOFax")]
[MaxLength(100)]
[ENI2Validation]
public string CSOFax { get; set; }
[ShowReport]
[LookupName("SEC.CSOEMail")]
[MaxLength(100)]
[ENI2Validation]
public string CSOEMail { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("SEC.ValidISSCOnBoard")]
[ENI2Validation]
public bool? ValidISSCOnBoard { get; set; }
[ShowReport]
[LookupName("SEC.ReasonsForNoValidISSC")]
[MaxLength(255)]
[ENI2Validation]
public string ReasonsForNoValidISSC { get; set; }
[Validation2(ValidationCode.NOT_NULL)]
[ENI2Validation]
public byte? ISSCType { get; set; }
[JsonIgnore]
[ShowReport]
[ReportDisplayName("ISSC type")]
public string ISSCTypeDisplay { get { return Util.GetISSCTypeDisplay(this.ISSCType); } }
[Validation2(ValidationCode.NOT_NULL)]
[ENI2Validation]
// [LookupName("SEC.ISSCIssuerType")] // wird manuell gescannt und konvertiert
public byte? ISSCIssuerType { get; set; }
[JsonIgnore]
[ShowReport]
[ReportDisplayName("ISSC issuer type")]
public string ISSCIssuerTypeDisplay { get { return Util.GetISSCIssuerTypeDisplay(this.ISSCIssuerType); } }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("SEC.ISSCIssuerName")]
[MaxLength(100)]
[ENI2Validation]
public string ISSCIssuerName { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("SEC.ISSCDateOfExpiration")]
[ENI2Validation]
[DateOnly]
public DateTime? ISSCDateOfExpiration { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[LookupName("SEC.ApprovedSecurityPlanOnBoard")]
[ENI2Validation]
public bool? ApprovedSecurityPlanOnBoard { get; set; }
[ShowReport]
[Validation(ValidationCode.NOT_NULL)]
[ENI2Validation]
public byte? CurrentShipSecurityLevel { get; set; }
[ShowReport]
[MaxLength(100)]
[ENI2Validation]
public string PortFacilityOfArrival { get; set; }
[ShowReport]
[Validation2(ValidationCode.NOT_NULL)]
[ENI2Validation]
public byte? GeneralDescriptionOfCargo { get; set; }
[ENI2Validation]
public bool? AreMatterToReport { get; set; }
[LookupName("SEC.MatterToReport")]
[MaxLength(1024)]
[ENI2Validation]
public string MatterToReport { get; set; }
[ENI2Validation]
public bool? KielCanalPassagePlanned { get; set; }
[ENI2Validation]
public DateTime? KielCanalPassagePlannedIncomming { get; set; }
[ENI2Validation]
public DateTime? KielCanalPassagePlannedOutgoing { get; set; }
[Validation2(ValidationCode.LIST_EMPTY)]
public ObservableCollection<LastTenPortFacilitiesCalled> LastTenPortFacilitesCalled { get; private set; } = new ObservableCollection<LastTenPortFacilitiesCalled>();
public ObservableCollection<ShipToShipActivitiesDuringLastTenPortFacilitiesCalled> ShipToShipActivitiesDuringLastTenPortFacilitiesCalled { get; private set; } = new ObservableCollection<ShipToShipActivitiesDuringLastTenPortFacilitiesCalled>();
#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.SECSimplification);
scmd.Parameters.AddWithNullableValue("@P3", this.PortOfCallWhereCompleteSECNotified);
scmd.Parameters.AddWithNullableValue("@P4", this.CSOLastName);
scmd.Parameters.AddWithNullableValue("@P5", this.CSOFirstName);
scmd.Parameters.AddWithNullableValue("@P6", this.CSOPhone);
scmd.Parameters.AddWithNullableValue("@P7", this.CSOEMail);
scmd.Parameters.AddWithNullableValue("@P8", this.ValidISSCOnBoard);
scmd.Parameters.AddWithNullableValue("@P9", this.ReasonsForNoValidISSC);
scmd.Parameters.AddWithNullableValue("@P10", this.ISSCType);
scmd.Parameters.AddWithNullableValue("@P11", this.ISSCIssuerType);
scmd.Parameters.AddWithNullableValue("@P12", this.ISSCIssuerName);
scmd.Parameters.AddWithNullableValue("@P13", this.ISSCDateOfExpiration);
scmd.Parameters.AddWithNullableValue("@P14", this.ApprovedSecurityPlanOnBoard);
scmd.Parameters.AddWithNullableValue("@P15", this.CurrentShipSecurityLevel);
scmd.Parameters.AddWithNullableValue("@P16", this.PortFacilityOfArrival);
scmd.Parameters.AddWithNullableValue("@P17", this.GeneralDescriptionOfCargo);
scmd.Parameters.AddWithNullableValue("@P18", this.CSOFax);
scmd.Parameters.AddWithNullableValue("@P19", this.MatterToReport);
scmd.Parameters.AddWithNullableValue("@P20", this.KielCanalPassagePlanned);
scmd.Parameters.AddWithNullableValue("@P21", this.KielCanalPassagePlannedIncomming);
scmd.Parameters.AddWithNullableValue("@P22", this.KielCanalPassagePlannedOutgoing);
scmd.Parameters.AddWithNullableValue("@P23", this.AreMatterToReport);
if (this.IsNew)
{
this.CreateId();
scmd.Parameters.AddWithValue("@ID", this.Id);
scmd.CommandText = string.Format("INSERT INTO {0} (Id, MessageHeaderId, SECSimplification, PortOfCallWhereCompleteSECNotified, " +
"CSOLastName, CSOFirstName, CSOPhone, CSOEMail, ValidISSCOnBoard, ReasonsForNoValidISSC, " +
"ISSCType, ISSCIssuerType, ISSCIssuerName,ISSCDateOfExpiration, ApprovedSecurityPlanOnBoard, " +
"CurrentShipSecurityLevel, PortFacilityOfArrival, GeneralDescriptionOfCargo, CSOFax, MatterToReport, " +
"KielCanalPassagePlanned, KielCanalPassagePlannedIncomming, KielCanalPassagePlannedOutgoing, AreMatterToReport) " +
"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 )", this.Tablename);
}
else
{
scmd.Parameters.AddWithValue("@ID", this.Id);
scmd.CommandText = string.Format("UPDATE {0} SET SECSimplification = @P2, PortOfCallWhereCompleteSECNotified = @P3, " +
"CSOLastName = @P4, CSOFirstName = @P5, CSOPhone = @P6," +
"CSOEMail = @P7, ValidISSCOnBoard = @P8, ReasonsForNoValidISSC = @P9, " +
"ISSCType = @P10, ISSCIssuerType = @P11, ISSCIssuerName = @P12, ISSCDateOfExpiration = @P13, " +
"ApprovedSecurityPlanOnBoard = @P14, CurrentShipSecurityLevel = @P15, PortFacilityOfArrival = @P16, " +
"GeneralDescriptionOfCargo = @P17, CSOFax = @P18, MatterToReport = @P19, KielCanalPassagePlanned = @P20, " +
"KielCanalPassagePlannedIncomming = @P21, KielCanalPassagePlannedOutgoing = @P22, AreMatterToReport = @P23 " +
"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, SECSimplification, PortOfCallWhereCompleteSECNotified, CSOLastName, " +
"CSOFirstName, CSOPhone, CSOEMail, ValidISSCOnBoard, ReasonsForNoValidISSC, " +
"ISSCType, ISSCIssuerType, ISSCIssuerName, ISSCDateOfExpiration, ApprovedSecurityPlanOnBoard, " +
"CurrentShipSecurityLevel, PortFacilityOfArrival, GeneralDescriptionOfCargo, CSOFax, MatterToReport, " +
"KielCanalPassagePlanned, KielCanalPassagePlannedIncomming, KielCanalPassagePlannedOutgoing, AreMatterToReport " +
"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())
{
SEC sec = new SEC();
sec.id = reader.GetGuid(0);
if (!reader.IsDBNull(1)) sec.SECSimplification = reader.GetBoolean(1);
if (!reader.IsDBNull(2)) sec.PortOfCallWhereCompleteSECNotified = reader.GetString(2);
if (!reader.IsDBNull(3)) sec.CSOLastName = reader.GetString(3);
if (!reader.IsDBNull(4)) sec.CSOFirstName = reader.GetString(4);
if (!reader.IsDBNull(5)) sec.CSOPhone = reader.GetString(5);
if (!reader.IsDBNull(6)) sec.CSOEMail = reader.GetString(6);
if (!reader.IsDBNull(7)) sec.ValidISSCOnBoard = reader.GetBoolean(7);
if (!reader.IsDBNull(8)) sec.ReasonsForNoValidISSC = reader.GetString(8);
if (!reader.IsDBNull(9)) sec.ISSCType = reader.GetByte(9);
if (!reader.IsDBNull(10)) sec.ISSCIssuerType = reader.GetByte(10);
if (!reader.IsDBNull(11)) sec.ISSCIssuerName = reader.GetString(11);
if (!reader.IsDBNull(12)) sec.ISSCDateOfExpiration = reader.GetDateTime(12);
if (!reader.IsDBNull(13)) sec.ApprovedSecurityPlanOnBoard = reader.GetBoolean(13);
if (!reader.IsDBNull(14)) sec.CurrentShipSecurityLevel = reader.GetByte(14);
if (!reader.IsDBNull(15)) sec.PortFacilityOfArrival = reader.GetString(15);
if (!reader.IsDBNull(16)) sec.GeneralDescriptionOfCargo = reader.GetByte(16);
if (!reader.IsDBNull(17)) sec.CSOFax = reader.GetString(17);
if (!reader.IsDBNull(18)) sec.MatterToReport = reader.GetString(18);
if (!reader.IsDBNull(19)) sec.KielCanalPassagePlanned = reader.GetBoolean(19);
if (!reader.IsDBNull(20)) sec.KielCanalPassagePlannedIncomming = reader.GetDateTime(20);
if (!reader.IsDBNull(21)) sec.KielCanalPassagePlannedOutgoing = reader.GetDateTime(21);
if (!reader.IsDBNull(22)) sec.AreMatterToReport = reader.GetBoolean(22);
result.Add(sec);
}
reader.Close();
return result;
}
#endregion
#region overrides
public override void OverwriteWith(DatabaseEntity otherEntity)
{
if (otherEntity is SEC otherSEC)
{
base.OverwriteWith(otherEntity);
foreach (LastTenPortFacilitiesCalled lc in this.LastTenPortFacilitesCalled)
DBManager.Instance.Delete(lc);
this.LastTenPortFacilitesCalled.Clear();
foreach (LastTenPortFacilitiesCalled lc in otherSEC.LastTenPortFacilitesCalled)
{
LastTenPortFacilitiesCalled newLC = lc.Clone() as LastTenPortFacilitiesCalled;
newLC.SEC = this;
DBManager.Instance.Save(newLC);
this.LastTenPortFacilitesCalled.Add(newLC);
}
foreach (ShipToShipActivitiesDuringLastTenPortFacilitiesCalled s2s in this.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled)
DBManager.Instance.Delete(s2s);
this.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Clear();
foreach (ShipToShipActivitiesDuringLastTenPortFacilitiesCalled s2s in otherSEC.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled)
{
ShipToShipActivitiesDuringLastTenPortFacilitiesCalled newS2S = s2s.Clone() as ShipToShipActivitiesDuringLastTenPortFacilitiesCalled;
newS2S.SEC = this;
DBManager.Instance.Save(newS2S);
this.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Add(newS2S);
}
}
}
#endregion
#region sublist lookup implementation
public LastTenPortFacilitiesCalled GetPortFacilityWithIdentifier(string identifier)
{
foreach(ISublistElement lp in this.LastTenPortFacilitesCalled) {
if (lp.Identifier.Equals(identifier))
return lp as LastTenPortFacilitiesCalled;
}
return null;
}
public ShipToShipActivitiesDuringLastTenPortFacilitiesCalled GetShipToShipWithIdentifier(string identifier)
{
foreach (ISublistElement lp in this.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled)
{
if (lp.Identifier.Equals(identifier))
return lp as ShipToShipActivitiesDuringLastTenPortFacilitiesCalled;
}
return null;
}
#endregion
#region IMessageParagraph implementation
[Browsable(false)]
public override string Subtitle
{
get
{
return "Sea security notification";
}
}
public override List<IMessageParagraph> ChildParagraphs
{
get
{
List<IMessageParagraph> result = new List<IMessageParagraph>();
foreach (IMessageParagraph imp in this.LastTenPortFacilitesCalled)
result.Add(imp);
foreach (IMessageParagraph imp in this.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled)
result.Add(imp);
return result;
}
}
[Browsable(false)]
[JsonIgnore]
int ISublistContainer.NumberOfExcelRows
{
get
{
return 1;
}
}
#endregion
#region Validation
public override DatabaseEntity.ValidationBlock GetValidationBlock()
{
return (this.SECSimplification ?? false) ? DatabaseEntity.ValidationBlock.BLOCK1 : DatabaseEntity.ValidationBlock.BLOCK2;
}
public override void Validate(List<MessageError> errors, List<MessageViolation> violations)
{
if(this.MessageCore.IsTransit && (this.KielCanalPassagePlanned ?? false))
{
violations.Add(RuleEngine.CreateViolation(ValidationCode.V705, "KielCanalPassagePlanned", null, this.Title, null, this.Tablename));
}
if (this.GetValidationBlock() == ValidationBlock.BLOCK1)
{
if (this.PortOfCallWhereCompleteSECNotified.IsNullOrEmpty())
violations.Add(RuleEngine.CreateViolation(ValidationCode.V704, "PortOfCallWhereCompleteSECNotified", null, this.Title, null, this.Tablename));
}
else
{
if ((!this.ValidISSCOnBoard ?? true) && this.ReasonsForNoValidISSC.IsNullOrEmpty())
violations.Add(RuleEngine.CreateViolation(ValidationCode.V702, "ReasonsForNoValidISSC", null, this.Title, null, this.Tablename));
if (this.ISSCDateOfExpiration.HasValue && (this.ISSCDateOfExpiration < DateTime.Now))
violations.Add(RuleEngine.CreateViolation(ValidationCode.TIME_IMPLAUSIBLE, "ISSC Expiration date is in the past", null, this.Title, null, this.Tablename));
if ((this.LastTenPortFacilitesCalled.Count == 0) || (this.LastTenPortFacilitesCalled.Count > 10))
errors.Add(RuleEngine.CreateError(ValidationCode.IMPLAUSIBLE, "LastTenPortFacilitiesCalled", null, this.Title, null, this.Tablename));
if (!(this.ApprovedSecurityPlanOnBoard ?? false))
violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "No valid security plan on board?", null, this.Title, null, this.Tablename));
if(this.KielCanalPassagePlanned ?? false)
{
if(!KielCanalPassagePlannedIncomming.HasValue && !KielCanalPassagePlannedOutgoing.HasValue)
violations.Add(RuleEngine.CreateViolation(ValidationCode.V706, null, null, this.Title, null, this.Tablename));
if (this.KielCanalPassagePlannedIncomming.HasValue && this.KielCanalPassagePlannedIncomming.Value.ToLocalTime().IsTimeEmpty())
violations.Add(RuleEngine.CreateViolation(ValidationCode.TIME_IMPLAUSIBLE, "Check ETA NOK incoming: might be implausible", this.KielCanalPassagePlannedIncomming.Value.ToLocalTime().ToString(), this.Title, null, this.Tablename));
if (this.KielCanalPassagePlannedOutgoing.HasValue && this.KielCanalPassagePlannedOutgoing.Value.ToLocalTime().IsTimeEmpty())
violations.Add(RuleEngine.CreateViolation(ValidationCode.TIME_IMPLAUSIBLE, "Check ETA NOK outgoing: might be implausible", this.KielCanalPassagePlannedOutgoing.Value.ToLocalTime().ToString(), this.Title, null, this.Tablename));
}
foreach (LastTenPortFacilitiesCalled L10Called in this.LastTenPortFacilitesCalled)
{
RuleEngine.ValidateProperties(L10Called, errors, violations);
L10Called.Validate(errors, violations);
}
if(this.LastTenPortFacilitesCalled.Count != 10)
{
violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Number of last port facilities != 10", null, this.Title, null, this.Tablename));
}
foreach (ShipToShipActivitiesDuringLastTenPortFacilitiesCalled s2s in this.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled)
{
RuleEngine.ValidateProperties(s2s, errors, violations);
s2s.Validate(errors, violations);
}
}
}
ISublistElement ISublistContainer.GetSublistElementWithIdentifier(string identifier)
{
// sinnlos, weil es zwei sublisten gibt
foreach (ISublistElement sElem in this.LastTenPortFacilitesCalled)
{
if (sElem.Identifier.Equals(identifier)) return sElem;
}
return null;
}
void ISublistContainer.SaveElements()
{
foreach (LastTenPortFacilitiesCalled l10c in this.LastTenPortFacilitesCalled)
DBManager.Instance.Save(l10c);
foreach(ShipToShipActivitiesDuringLastTenPortFacilitiesCalled s2s in this.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled)
DBManager.Instance.Save(s2s);
}
void ISublistContainer.DeleteElements()
{
foreach (LastTenPortFacilitiesCalled l10c in this.LastTenPortFacilitesCalled)
DBManager.Instance.Delete(l10c);
this.LastTenPortFacilitesCalled.Clear();
foreach (ShipToShipActivitiesDuringLastTenPortFacilitiesCalled s2s in this.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled)
DBManager.Instance.Delete(s2s);
this.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Clear();
}
#endregion
#region ICloneable implementation
public override object Clone()
{
SEC sec = this.MemberwiseClone() as SEC;
sec.id = null;
sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled = new ObservableCollection<ShipToShipActivitiesDuringLastTenPortFacilitiesCalled>();
sec.LastTenPortFacilitesCalled = new ObservableCollection<LastTenPortFacilitiesCalled>();
foreach (LastTenPortFacilitiesCalled ltp in this.LastTenPortFacilitesCalled)
{
bsmd.database.LastTenPortFacilitiesCalled clonedLTP = ltp.Clone() as bsmd.database.LastTenPortFacilitiesCalled;
clonedLTP.SEC = sec;
sec.LastTenPortFacilitesCalled.Add(clonedLTP);
}
foreach (ShipToShipActivitiesDuringLastTenPortFacilitiesCalled s2s in this.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled)
{
ShipToShipActivitiesDuringLastTenPortFacilitiesCalled clonedS2S = s2s.Clone() as ShipToShipActivitiesDuringLastTenPortFacilitiesCalled;
clonedS2S.SEC = sec;
sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Add(clonedS2S);
}
return sec;
}
#endregion
}
}