206 lines
6.2 KiB
C#
206 lines
6.2 KiB
C#
// Copyright (c) 2023- schick Informatik
|
|
// Description: Container for participants (Teilnehmer)
|
|
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
|
|
namespace brecal.model
|
|
{
|
|
public class Participant : DbEntity
|
|
{
|
|
#region Enumerations
|
|
|
|
// TODO: should localize the descriptions
|
|
[Flags]
|
|
public enum ParticipantType
|
|
{
|
|
[Description("not assigned")]
|
|
NONE = 0,
|
|
[Description("BSMD")]
|
|
BSMD = 1,
|
|
[Description("Terminal")]
|
|
TERMINAL = 2,
|
|
[Description("Flusslotsen")]
|
|
PILOT = 4,
|
|
[Description("Agentur")]
|
|
AGENCY = 8,
|
|
[Description("Festmacher")]
|
|
MOORING = 16,
|
|
[Description("Hafenamt")]
|
|
PORT_ADMINISTRATION = 32,
|
|
[Description("Schlepper")]
|
|
TUG = 64,
|
|
}
|
|
|
|
[Flags]
|
|
public enum ParticipantFlags
|
|
{
|
|
[Description("allow BSMD initial info")]
|
|
ALLOW_BSMD = 1,
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public string? Name { get; set; }
|
|
|
|
public string? Street { get; set; }
|
|
|
|
public string? PostalCode { get; set; }
|
|
|
|
public string? City { get; set; }
|
|
|
|
public uint Type { get; set;}
|
|
|
|
public uint Flags { get; set; }
|
|
|
|
public bool Deleted { get; set; } = false;
|
|
|
|
#endregion
|
|
|
|
#region public static methods
|
|
|
|
public static async Task<List<Participant>> LoadAll(IDBManager manager)
|
|
{
|
|
List<DbEntity> loadResultList = await manager.Load(SetLoadQuery, LoadElems);
|
|
List<Participant> result = new();
|
|
foreach (Participant p in loadResultList.Cast<Participant>())
|
|
result.Add(p);
|
|
return result;
|
|
}
|
|
|
|
public static List<DbEntity> LoadElems(IDataReader reader)
|
|
{
|
|
List<DbEntity> result = new();
|
|
while (reader.Read())
|
|
{
|
|
Participant p = new();
|
|
p.Id = (uint)reader.GetInt32(0);
|
|
if (!reader.IsDBNull(1)) p.Name = reader.GetString(1);
|
|
if (!reader.IsDBNull(2)) p.Street = reader.GetString(2);
|
|
if (!reader.IsDBNull(3)) p.PostalCode = reader.GetString(3);
|
|
if (!reader.IsDBNull(4)) p.City = reader.GetString(4);
|
|
if (!reader.IsDBNull(5)) p.Type = (uint) reader.GetInt32(5);
|
|
if (!reader.IsDBNull(6)) p.Flags = (uint)reader.GetInt32(6);
|
|
if (!reader.IsDBNull(7)) p.Created = reader.GetDateTime(7);
|
|
if (!reader.IsDBNull(8)) p.Modified = reader.GetDateTime(8);
|
|
if (!reader.IsDBNull(9)) p.Deleted = reader.GetBoolean(9);
|
|
result.Add(p);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static void SetLoadQuery(IDbCommand cmd, params object?[] list)
|
|
{
|
|
cmd.CommandText = "SELECT id, name, street, postal_code, city, type, flags, created, modified, deleted FROM participant";
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region abstract method implementation
|
|
|
|
public override void SetUpdate(IDbCommand cmd)
|
|
{
|
|
cmd.CommandText = "UPDATE participant set name = @NAME, street = @STREET, postal_code = @POSTAL_CODE, type = @TYPE, city = @CITY, flags = @FLAGS WHERE id = @ID";
|
|
this.SetParameters(cmd);
|
|
}
|
|
|
|
public override void SetCreate(IDbCommand cmd)
|
|
{
|
|
cmd.CommandText = "INSERT INTO participant (name, street, postal_code, city, type, flags) VALUES ( @NAME, @STREET, @POSTAL_CODE, @CITY, @TYPE, @FLAGS)";
|
|
this.SetParameters(cmd);
|
|
}
|
|
|
|
public override void SetDelete(IDbCommand cmd)
|
|
{
|
|
cmd.CommandText = "UPDATE participant SET deleted = 1 WHERE id = @ID";
|
|
|
|
IDataParameter idParam = cmd.CreateParameter();
|
|
idParam.ParameterName = "ID";
|
|
idParam.Value = this.Id;
|
|
cmd.Parameters.Add(idParam);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region private methods
|
|
|
|
private void SetParameters(IDbCommand cmd)
|
|
{
|
|
IDbDataParameter name = cmd.CreateParameter();
|
|
name.ParameterName = "NAME";
|
|
name.Value = this.Name;
|
|
cmd.Parameters.Add(name);
|
|
|
|
IDbDataParameter street = cmd.CreateParameter();
|
|
street.ParameterName = "STREET";
|
|
street.Value = this.Street;
|
|
cmd.Parameters.Add(street);
|
|
|
|
IDataParameter postal_code = cmd.CreateParameter();
|
|
postal_code.ParameterName = "POSTAL_CODE";
|
|
postal_code.Value = this.PostalCode;
|
|
cmd.Parameters.Add(postal_code);
|
|
|
|
IDataParameter city = cmd.CreateParameter();
|
|
city.ParameterName = "CITY";
|
|
city.Value = this.City;
|
|
cmd.Parameters.Add(city);
|
|
|
|
IDataParameter flags = cmd.CreateParameter();
|
|
flags.ParameterName = "FLAGS";
|
|
flags.Value = this.Flags;
|
|
cmd.Parameters.Add(flags);
|
|
|
|
IDataParameter idParam = cmd.CreateParameter();
|
|
idParam.ParameterName = "ID";
|
|
idParam.Value = this.Id;
|
|
cmd.Parameters.Add(idParam);
|
|
|
|
IDataParameter typeParam = cmd.CreateParameter();
|
|
typeParam.ParameterName = "TYPE";
|
|
typeParam.Value = (int) this.Type;
|
|
cmd.Parameters.Add(typeParam);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region overrides
|
|
|
|
public override string ToString()
|
|
{
|
|
return this.Name ?? $"{base.Id} - {this.GetType().Name}";
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region public type flag funcs
|
|
|
|
public bool IsTypeFlagSet(ParticipantType flag)
|
|
{
|
|
return (this.Type & (uint)flag) != 0;
|
|
}
|
|
|
|
public void SetTypeFlag(bool value, ParticipantType flag)
|
|
{
|
|
if (value) this.Type |= (uint)flag;
|
|
else this.Type &= (uint)~flag;
|
|
}
|
|
|
|
public bool IsFlagSet(ParticipantFlags flag)
|
|
{
|
|
return (this.Flags & (uint)flag) != 0;
|
|
}
|
|
|
|
public void SetFlag(bool value, ParticipantFlags flag)
|
|
{
|
|
if (value) this.Flags |= (uint)flag;
|
|
else this.Flags &= (uint)~flag;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|