Added the port reference to management of berths incl. excel import

This commit is contained in:
Daniel Schick 2024-09-04 09:50:53 +02:00
parent 6cfd311bbb
commit 76995a84e4
7 changed files with 58 additions and 25 deletions

View File

@ -13,7 +13,7 @@ INSERT INTO port (id, name, locode) VALUES (1, 'Bremen', 'DEBRE');
-- Adding new ref column to berth -- Adding new ref column to berth
ALTER TABLE `bremen_calling_devel`.`berth` ALTER TABLE `bremen_calling_devel`.`berth`
ADD COLUMN `port_id` INT UNSIGNED NOT NULL DEFAULT 1 AFTER `authority_id`; ADD COLUMN `port_id` INT UNSIGNED DEFAULT NULL AFTER `authority_id`;
ALTER TABLE `bremen_calling_devel`.`berth` ALTER INDEX `FK_AUTHORITY_PART_idx` INVISIBLE; ALTER TABLE `bremen_calling_devel`.`berth` ALTER INDEX `FK_AUTHORITY_PART_idx` INVISIBLE;
-- adding a foreign key berth.port_id -> port.id -- adding a foreign key berth.port_id -> port.id
@ -52,3 +52,6 @@ CREATE TABLE `participant_port_map` (
CONSTRAINT `FK_PP_PARTICIPANT` FOREIGN KEY (`participant_id`) REFERENCES `participant` (`id`), CONSTRAINT `FK_PP_PARTICIPANT` FOREIGN KEY (`participant_id`) REFERENCES `participant` (`id`),
CONSTRAINT `FK_PP_PORT` FOREIGN KEY (`port_id`) REFERENCES `port` (`id`) CONSTRAINT `FK_PP_PORT` FOREIGN KEY (`port_id`) REFERENCES `port` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Mapping table that assigns participants to a port'; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Mapping table that assigns participants to a port';
-- all existing berths shall default to "bremen"
UPDATE berth SET port_id = 1;

View File

@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RoleEditor" xmlns:local="clr-namespace:RoleEditor"
mc:Ignorable="d" mc:Ignorable="d"
Title="Edit berth" Height="188" Width="450" Loaded="Window_Loaded"> Title="Edit berth" Height="216" Width="450" Loaded="Window_Loaded">
<Grid x:Name="berthGrid"> <Grid x:Name="berthGrid">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width=".3*" /> <ColumnDefinition Width=".3*" />
@ -16,6 +16,7 @@
<RowDefinition Height="28" /> <RowDefinition Height="28" />
<RowDefinition Height="28" /> <RowDefinition Height="28" />
<RowDefinition Height="28" /> <RowDefinition Height="28" />
<RowDefinition Height="28" />
<RowDefinition Height="*" /> <RowDefinition Height="*" />
<RowDefinition Height="28" /> <RowDefinition Height="28" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
@ -45,7 +46,9 @@
</Grid> </Grid>
<Label Content="Uses lock" HorizontalAlignment="Right" Grid.Row="3" /> <Label Content="Uses lock" HorizontalAlignment="Right" Grid.Row="3" />
<CheckBox x:Name="checkBoxLock" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" Margin="2" IsChecked="{Binding Path=Lock, Mode=OneWay}"/> <CheckBox x:Name="checkBoxLock" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" Margin="2" IsChecked="{Binding Path=Lock, Mode=OneWay}"/>
<StackPanel Grid.Column="1" Grid.Row="5" Orientation="Horizontal" FlowDirection="RightToLeft"> <Label Content="Port" HorizontalAlignment="Right" Grid.Row="4" />
<ComboBox Name="comboBoxPorts" Margin="2" Grid.Column="1" Grid.Row="4" SelectedItem="{Binding Port, Mode=OneWay}" />
<StackPanel Grid.Column="1" Grid.Row="6" Orientation="Horizontal" FlowDirection="RightToLeft">
<Button x:Name="buttonCancel" Width="80" Content="Cancel" Margin="2" Click="buttonCancel_Click" /> <Button x:Name="buttonCancel" Width="80" Content="Cancel" Margin="2" Click="buttonCancel_Click" />
<Button x:Name="buttonOK" Width="80" Content="OK" Margin="2" Click="buttonOK_Click"/> <Button x:Name="buttonOK" Width="80" Content="OK" Margin="2" Click="buttonOK_Click"/>
</StackPanel> </StackPanel>

View File

@ -20,6 +20,8 @@ namespace RoleEditor
public List<Participant> Authorities { get; } = new List<Participant>(); public List<Participant> Authorities { get; } = new List<Participant>();
public List<Port> Ports { get; } = new List<Port>();
private void buttonCancel_Click(object sender, RoutedEventArgs e) private void buttonCancel_Click(object sender, RoutedEventArgs e)
{ {
this.DialogResult = false; this.DialogResult = false;
@ -52,6 +54,7 @@ namespace RoleEditor
this.DataContext = this.Berth; this.DataContext = this.Berth;
this.comboBoxParticipants.ItemsSource = this.Owners; this.comboBoxParticipants.ItemsSource = this.Owners;
this.comboBoxAuthorities.ItemsSource = this.Authorities; this.comboBoxAuthorities.ItemsSource = this.Authorities;
this.comboBoxPorts.ItemsSource = this.Ports;
} }
private void buttonResetParticipant_Click(object sender, RoutedEventArgs e) private void buttonResetParticipant_Click(object sender, RoutedEventArgs e)

View File

@ -340,6 +340,7 @@
<DataGridCheckBoxColumn Header="Lock" Binding="{Binding Path=Lock}" IsReadOnly="True"/> <DataGridCheckBoxColumn Header="Lock" Binding="{Binding Path=Lock}" IsReadOnly="True"/>
<DataGridTextColumn Header="Terminal" Binding="{Binding Path=Terminal, Mode=OneWay}" IsReadOnly="True"/> <DataGridTextColumn Header="Terminal" Binding="{Binding Path=Terminal, Mode=OneWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Authority" Binding="{Binding Path=Authority_Text, Mode=OneWay}" IsReadOnly="True" /> <DataGridTextColumn Header="Authority" Binding="{Binding Path=Authority_Text, Mode=OneWay}" IsReadOnly="True" />
<DataGridTextColumn Header="Port" Binding="{Binding Path=Port}" IsReadOnly="True" />
<DataGridTextColumn Header="Deleted" Binding="{Binding Path=Deleted, Mode=OneWay}" IsReadOnly="True" /> <DataGridTextColumn Header="Deleted" Binding="{Binding Path=Deleted, Mode=OneWay}" IsReadOnly="True" />
</DataGrid.Columns> </DataGrid.Columns>
</local:ENIDataGrid> </local:ENIDataGrid>

View File

@ -79,6 +79,10 @@ namespace RoleEditor
_securables.Add(s); _securables.Add(s);
this.listBoxSecurables.ItemsSource = _securables; this.listBoxSecurables.ItemsSource = _securables;
// load all ports
foreach (Port port in await Port.LoadAll(_dbManager)) _ports.Add(port);
this.listBoxPort.ItemsSource = _ports;
// load all berths // load all berths
foreach (Berth b in await Berth.LoadAll(_dbManager)) foreach (Berth b in await Berth.LoadAll(_dbManager))
{ {
@ -91,6 +95,10 @@ namespace RoleEditor
{ {
b.Authority = participants.Where(p => p.Id == b.Authority_Id).FirstOrDefault(); b.Authority = participants.Where(p => p.Id == b.Authority_Id).FirstOrDefault();
} }
if (b.Port_Id != null)
{
b.Port = _ports.Where(p => p.Id == b.Port_Id).FirstOrDefault();
}
} }
this.dataGridBerths.Initialize(); this.dataGridBerths.Initialize();
this.dataGridBerths.ItemsSource = _berths; this.dataGridBerths.ItemsSource = _berths;
@ -114,11 +122,7 @@ namespace RoleEditor
this.dataGridShips.CreateRequested += DataGridShips_CreateRequested; this.dataGridShips.CreateRequested += DataGridShips_CreateRequested;
this.dataGridShips.EditRequested += DataGridShips_EditRequested; this.dataGridShips.EditRequested += DataGridShips_EditRequested;
this.dataGridShips.DeleteRequested += DataGridShips_DeleteRequested; this.dataGridShips.DeleteRequested += DataGridShips_DeleteRequested;
// load all ports
foreach(Port port in await Port.LoadAll(_dbManager)) _ports.Add(port);
this.listBoxPort.ItemsSource = _ports;
// set other item sources (filled later after selection) // set other item sources (filled later after selection)
@ -200,6 +204,7 @@ namespace RoleEditor
ebd.Berth = b; ebd.Berth = b;
ebd.Owners.AddRange(this._terminals); ebd.Owners.AddRange(this._terminals);
ebd.Authorities.AddRange(this._authorities); ebd.Authorities.AddRange(this._authorities);
ebd.Ports.AddRange(this._ports.Where(p => !p.IsDeleted));
if (ebd.ShowDialog() ?? false) if (ebd.ShowDialog() ?? false)
{ {
await b.Save(_dbManager); await b.Save(_dbManager);
@ -216,6 +221,7 @@ namespace RoleEditor
ebd.Berth = b; ebd.Berth = b;
ebd.Owners.AddRange(this._terminals); ebd.Owners.AddRange(this._terminals);
ebd.Authorities.AddRange(this._authorities); ebd.Authorities.AddRange(this._authorities);
ebd.Ports.AddRange(_ports.Where(p => !p.IsDeleted));
if (ebd.ShowDialog() ?? false) if (ebd.ShowDialog() ?? false)
{ {
_berths.Add(b); _berths.Add(b);
@ -738,7 +744,7 @@ namespace RoleEditor
{ {
if (reader.FieldCount < 2) if (reader.FieldCount < 2)
{ {
throw new InvalidDataException("Sheet must have at least 2 Columns of data"); throw new InvalidDataException("Sheet must have at least 3 Columns of data");
} }
if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue; if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue;
@ -753,8 +759,20 @@ namespace RoleEditor
if (_berths.Any(predicate: x => (x.Name != null) && x.Name.Equals(berth_name, StringComparison.OrdinalIgnoreCase))) if (_berths.Any(predicate: x => (x.Name != null) && x.Name.Equals(berth_name, StringComparison.OrdinalIgnoreCase)))
continue; continue;
string port_name = "";
if (!reader.IsDBNull(2)) port_name = reader.GetString(2);
// find port in list
if(!_ports.Any(x => (x.Name != null) && x.Name.Equals(port_name, StringComparison.OrdinalIgnoreCase)))
continue;
Port port = _ports.First(x => (x.Name != null) && x.Name.Equals(port_name, StringComparison.OrdinalIgnoreCase));
Berth b = new Berth(); Berth b = new Berth();
b.Name = berth_name; b.Name = berth_name;
b.Port = port;
b.Port_Id = port.Id;
bool found_participant = false; bool found_participant = false;
foreach(Participant p in this._participants) foreach(Participant p in this._participants)

View File

@ -1,12 +1,7 @@
// Copyright (c) 2023- schick Informatik // Copyright (c) 2023- schick Informatik
// Description: Model class for berth entity // Description: Model class for berth entity
using System;
using System.Collections.Generic;
using System.Data; using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model namespace brecal.model
{ {
@ -23,10 +18,14 @@ namespace brecal.model
public uint? Authority_Id { get; set; } public uint? Authority_Id { get; set; }
public uint? Port_Id { get; set; }
public Participant? Owner { get; set; } public Participant? Owner { get; set; }
public Participant? Authority { get; set; } public Participant? Authority { get; set; }
public Port? Port { get; set; }
public string? Terminal { get { if (Owner != null) return Owner.Name; else return "n/a"; } } public string? Terminal { get { if (Owner != null) return Owner.Name; else return "n/a"; } }
public string? Authority_Text { get { if (Authority != null) return Authority.Name; else return "n/a"; } } public string? Authority_Text { get { if (Authority != null) return Authority.Name; else return "n/a"; } }
@ -48,12 +47,12 @@ namespace brecal.model
public static void SetLoadQuery(IDbCommand cmd, params object?[] list) public static void SetLoadQuery(IDbCommand cmd, params object?[] list)
{ {
cmd.CommandText = "SELECT id, name, owner_id, authority_id, `lock`, created, modified, deleted FROM berth"; cmd.CommandText = "SELECT id, name, owner_id, authority_id, port_id, `lock`, created, modified, deleted FROM berth";
} }
public static List<DbEntity> LoadElems(IDataReader reader) public static List<DbEntity> LoadElems(IDataReader reader)
{ {
List<DbEntity> result = new List<DbEntity>(); List<DbEntity> result = new();
while (reader.Read()) while (reader.Read())
{ {
Berth b = new(); Berth b = new();
@ -61,10 +60,11 @@ namespace brecal.model
if (!reader.IsDBNull(1)) b.Name = reader.GetString(1); if (!reader.IsDBNull(1)) b.Name = reader.GetString(1);
if (!reader.IsDBNull(2)) b.Owner_Id = (uint) reader.GetInt32(2); if (!reader.IsDBNull(2)) b.Owner_Id = (uint) reader.GetInt32(2);
if (!reader.IsDBNull(3)) b.Authority_Id = (uint) reader.GetInt32(3); if (!reader.IsDBNull(3)) b.Authority_Id = (uint) reader.GetInt32(3);
if (!reader.IsDBNull(4)) b.Lock = reader.GetBoolean(4); if (!reader.IsDBNull(4)) b.Port_Id = (uint) reader.GetInt32(4);
if (!reader.IsDBNull(5)) b.Created = reader.GetDateTime(5); if (!reader.IsDBNull(5)) b.Lock = reader.GetBoolean(5);
if (!reader.IsDBNull(6)) b.Modified = reader.GetDateTime(6); if (!reader.IsDBNull(6)) b.Created = reader.GetDateTime(6);
if (!reader.IsDBNull(7)) b.Deleted = reader.GetInt16(7); if (!reader.IsDBNull(7)) b.Modified = reader.GetDateTime(7);
if (!reader.IsDBNull(8)) b.Deleted = reader.GetInt16(8);
result.Add(b); result.Add(b);
} }
return result; return result;
@ -76,7 +76,7 @@ namespace brecal.model
public override void SetCreate(IDbCommand cmd) public override void SetCreate(IDbCommand cmd)
{ {
cmd.CommandText = "INSERT INTO berth (owner_id, authority_id, name, `lock`) VALUES ( @PID, @AID, @NAME, @LOCK)"; cmd.CommandText = "INSERT INTO berth (owner_id, authority_id, port_id, name, `lock`) VALUES ( @PID, @AID, @PO_ID, @NAME, @LOCK)";
this.SetParameters(cmd); this.SetParameters(cmd);
} }
@ -92,7 +92,7 @@ namespace brecal.model
public override void SetUpdate(IDbCommand cmd) public override void SetUpdate(IDbCommand cmd)
{ {
cmd.CommandText = "UPDATE berth SET name = @NAME, owner_id = @PID, authority_id = @AID, `lock` = @LOCK WHERE id = @ID"; cmd.CommandText = "UPDATE berth SET name = @NAME, owner_id = @PID, authority_id = @AID, port_id = @PO_ID, `lock` = @LOCK WHERE id = @ID";
this.SetParameters(cmd); this.SetParameters(cmd);
} }
@ -109,14 +109,19 @@ namespace brecal.model
IDbDataParameter pid = cmd.CreateParameter(); IDbDataParameter pid = cmd.CreateParameter();
pid.ParameterName = "PID"; pid.ParameterName = "PID";
pid.Value = this.Owner_Id; pid.Value = this.Owner_Id.HasValue ? this.Owner_Id.Value : DBNull.Value;
cmd.Parameters.Add(pid); cmd.Parameters.Add(pid);
IDbDataParameter aid = cmd.CreateParameter(); IDbDataParameter aid = cmd.CreateParameter();
aid.ParameterName = "AID"; aid.ParameterName = "AID";
aid.Value = this.Authority_Id; aid.Value = this.Authority_Id.HasValue ? this.Authority_Id.Value : DBNull.Value;
cmd.Parameters.Add(aid); cmd.Parameters.Add(aid);
IDbDataParameter poid = cmd.CreateParameter();
poid.ParameterName = "PO_ID";
poid.Value = this.Port_Id.HasValue ? this.Port_Id.Value : DBNull.Value;
cmd.Parameters.Add(poid);
IDbDataParameter name = cmd.CreateParameter(); IDbDataParameter name = cmd.CreateParameter();
name.ParameterName = "NAME"; name.ParameterName = "NAME";
name.Value = this.Name; name.Value = this.Name;

View File

@ -14,7 +14,7 @@ namespace brecal.model
public string? Locode { get; set; } public string? Locode { get; set; }
public bool ? IsDeleted { get; set; } public bool IsDeleted { get; set; } = false;
#endregion #endregion