git_bsmd/bsmd.database/PortOfCallLast30Days.cs

312 lines
12 KiB
C#

//
// 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;
using Newtonsoft.Json;
using System.ComponentModel;
namespace bsmd.database
{
[TypeConverter(typeof(MessageClassConverter<PortOfCallLast30Days>))]
[JsonConverter(typeof(NoTypeConverterJsonConverter<PortOfCallLast30Days>))]
public class PortOfCallLast30Days : DatabaseEntity, ISublistElement, ISublistContainer
{
public PortOfCallLast30Days()
{
this.tablename = "[dbo].[PortOfCallLast30Days]";
}
#region Properties
[JsonIgnore]
[Browsable(false)]
public MDH MDH { get; set; }
public List<PortOfCallLast30DaysCrewJoinedShip> CrewJoinedShip { get; private set; } = new List<PortOfCallLast30DaysCrewJoinedShip>();
[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; }
[JsonIgnore]
[Browsable(false)]
public string SublistCollectionKey { get { return "pocl30d"; } }
/// <summary>
/// Hilfsproperty, um eine kommaseparierte Liste von Crew (analog ANSW) im ENI-2 anzuzeigen,
/// </summary>
[JsonIgnore]
[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<PortOfCallLast30DaysCrewJoinedShip> foundList = new List<database.PortOfCallLast30DaysCrewJoinedShip>();
for (int i = 0; i < crewNames.Length; i++)
{
string crewName = crewNames[i].Trim();
if (crewName.Length > 0)
{
if (crewName.Length > 99) crewName = crewName.Substring(0, 99); // Maximale Länge lt. Spec 99 Zeichen
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(new List<DatabaseEntity>(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;
}
cmd.CommandText = query;
}
public override List<DatabaseEntity> LoadList(IDataReader reader)
{
List<DatabaseEntity> result = new List<DatabaseEntity>();
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();
result.Sort();
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;
}
[Browsable(false)]
[JsonIgnore]
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<IMessageParagraph> ChildParagraphs
{
get
{
List<IMessageParagraph> result = new List<IMessageParagraph>();
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<MessageError> errors, List<MessageViolation> 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
#region ICloneable implementation
public override object Clone()
{
PortOfCallLast30Days p30 = this.MemberwiseClone() as PortOfCallLast30Days;
p30.id = null;
p30.CrewJoinedShip = new List<PortOfCallLast30DaysCrewJoinedShip>();
foreach (PortOfCallLast30DaysCrewJoinedShip p30Crew in this.CrewJoinedShip)
{
PortOfCallLast30DaysCrewJoinedShip clonedP30Crew = p30Crew.Clone() as PortOfCallLast30DaysCrewJoinedShip;
clonedP30Crew.PortOfCallLast30Days = p30;
p30.CrewJoinedShip.Add(clonedP30Crew);
}
return p30;
}
#endregion
#region IComparable implementation
public int CompareTo(object obj)
{
if (obj is null)
return 1;
return this.Identifier.CompareTo(((PortOfCallLast30Days)obj).Identifier);
}
#endregion
}
}