112 lines
3.4 KiB
C#
112 lines
3.4 KiB
C#
// Copyright (c) 2023- schick Informatik
|
|
// Description: Participant Port Map Entity
|
|
|
|
using System.Data;
|
|
|
|
namespace brecal.model
|
|
{
|
|
public class PortAssignment : DbEntity
|
|
{
|
|
#region Properties
|
|
|
|
public int? ParticipantId { get; set; }
|
|
|
|
public int? PortId { get; set; }
|
|
|
|
public Participant? AssignedParticipant { get; set; }
|
|
|
|
public Port? AssignedPort { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region public static methods
|
|
|
|
public static async Task<List<PortAssignment>> LoadForParticipant(Participant? p, IDBManager manager)
|
|
{
|
|
List<DbEntity> loadResultList = await manager.Load(SetLoadQuery, LoadElems, args: p);
|
|
List<PortAssignment> result = new();
|
|
foreach (PortAssignment pa in loadResultList.Cast<PortAssignment>())
|
|
{
|
|
pa.AssignedParticipant = p;
|
|
result.Add(pa);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static void SetLoadQuery(IDbCommand cmd, params object?[] args)
|
|
{
|
|
cmd.CommandText = "SELECT id, participant_id, port_id FROM participant_port_map WHERE participant_id = @PID";
|
|
if (args.Length != 1 || args[0] is not Participant)
|
|
throw new ArgumentException("loader needs single participant as argument");
|
|
IDataParameter pid = cmd.CreateParameter();
|
|
pid.ParameterName = "PID";
|
|
if (args[0] is Participant p)
|
|
pid.Value = p.Id;
|
|
cmd.Parameters.Add(pid);
|
|
}
|
|
|
|
public static List<DbEntity> LoadElems(IDataReader reader)
|
|
{
|
|
List<DbEntity> result = new();
|
|
while (reader.Read())
|
|
{
|
|
PortAssignment ra = new();
|
|
ra.Id = (uint)reader.GetInt32(0);
|
|
if (!reader.IsDBNull(1)) ra.ParticipantId = reader.GetInt32(1);
|
|
if (!reader.IsDBNull(2)) ra.PortId = reader.GetInt32(2);
|
|
result.Add(ra);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region overrides
|
|
|
|
public override void SetUpdate(IDbCommand cmd)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public override void SetCreate(IDbCommand cmd)
|
|
{
|
|
cmd.CommandText = "INSERT INTO participant_port_map (participant_id, port_id) VALUES (@PID, @PORTID)";
|
|
|
|
IDbDataParameter participantId = cmd.CreateParameter();
|
|
participantId.ParameterName = "pID";
|
|
participantId.Value = this.ParticipantId;
|
|
cmd.Parameters.Add(participantId);
|
|
|
|
IDbDataParameter portId = cmd.CreateParameter();
|
|
portId.ParameterName = "PORTID";
|
|
portId.Value = this.PortId;
|
|
cmd.Parameters.Add(portId);
|
|
}
|
|
|
|
public override void SetDelete(IDbCommand cmd)
|
|
{
|
|
cmd.CommandText = "DELETE FROM participant_port_map WHERE id = @ID";
|
|
|
|
IDataParameter idParam = cmd.CreateParameter();
|
|
idParam.ParameterName = "ID";
|
|
idParam.Value = this.Id;
|
|
cmd.Parameters.Add(idParam);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
if (this.AssignedPort == null)
|
|
{
|
|
return $"{Id}: <defunct port>";
|
|
}
|
|
else
|
|
{
|
|
return AssignedPort.Name ?? AssignedPort.Id.ToString();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|