From 9b038b3571e2ca77bc75c1059d721736f258e6e5 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Sun, 1 Sep 2024 11:33:01 +0200 Subject: [PATCH 1/6] Database extension, first step --- misc/update_1.5_to_1.6.sql | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 misc/update_1.5_to_1.6.sql diff --git a/misc/update_1.5_to_1.6.sql b/misc/update_1.5_to_1.6.sql new file mode 100644 index 0000000..dc2e736 --- /dev/null +++ b/misc/update_1.5_to_1.6.sql @@ -0,0 +1,28 @@ +CREATE TABLE `port` ( + `id` int unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(128) NOT NULL COMMENT 'Name of port', + `locode` char(5) DEFAULT NULL COMMENT 'UNECE locode', + `created` datetime DEFAULT CURRENT_TIMESTAMP, + `modified` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, + `deleted` bit(1) DEFAULT b'0', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Port as reference for shipcalls and berths'; + +-- Add default port to table +INSERT INTO port (id, name, locode) VALUES (1, 'Bremen', 'DEBRE'); + +-- Adding new ref column to berth +ALTER TABLE `bremen_calling_devel`.`berth` +ADD COLUMN `port_id` INT UNSIGNED NOT NULL DEFAULT 1 AFTER `authority_id`; +ALTER TABLE `bremen_calling_devel`.`berth` ALTER INDEX `FK_AUTHORITY_PART_idx` INVISIBLE; + +-- adding a foreign key berth.port_id -> port.id +ALTER TABLE `bremen_calling_devel`.`berth` +ADD INDEX `FK_PORT_PART_idx` (`port_id` ASC) VISIBLE; + +ALTER TABLE `bremen_calling_devel`.`berth` +ADD CONSTRAINT `FK_PORT` + FOREIGN KEY (`port_id`) + REFERENCES `bremen_calling_devel`.`port` (`id`) + ON DELETE RESTRICT + ON UPDATE RESTRICT; From bbc705cf63b530fc74aaae7734b015d440903c7b Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Tue, 3 Sep 2024 08:54:34 +0200 Subject: [PATCH 2/6] Database changes complete --- misc/update_1.5_to_1.6.sql | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/misc/update_1.5_to_1.6.sql b/misc/update_1.5_to_1.6.sql index dc2e736..74b93e9 100644 --- a/misc/update_1.5_to_1.6.sql +++ b/misc/update_1.5_to_1.6.sql @@ -26,3 +26,29 @@ ADD CONSTRAINT `FK_PORT` REFERENCES `bremen_calling_devel`.`port` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT; + +-- adding new ref column to shipcall incl. foreign key +ALTER TABLE `bremen_calling_devel`.`shipcall` +ADD COLUMN `port_id` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'Selected port for this shipcall' AFTER `evaluation_notifications_sent`, +CHANGE COLUMN `time_ref_point` `time_ref_point` INT NULL DEFAULT '0' COMMENT 'Index of a location which is the reference point for all time value entries, e.g. berth or Geeste' AFTER `port_id`, +ADD INDEX `FK_SHIPCALL_PORT_idx` (`port_id` ASC) VISIBLE; +; +ALTER TABLE `bremen_calling_devel`.`shipcall` +ADD CONSTRAINT `FK_SHIPCALL_PORT` + FOREIGN KEY (`port_id`) + REFERENCES `bremen_calling_devel`.`port` (`id`) + ON DELETE RESTRICT + ON UPDATE RESTRICT; + +CREATE TABLE `participant_port_map` ( + `id` int NOT NULL AUTO_INCREMENT, + `participant_id` int unsigned NOT NULL COMMENT 'Ref to participant', + `port_id` int unsigned NOT NULL COMMENT 'Ref to port', + `created` datetime DEFAULT CURRENT_TIMESTAMP, + `modified` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + KEY `FK_PP_PARTICIPANT` (`participant_id`), + KEY `FK_PP_PORT` (`port_id`), + CONSTRAINT `FK_PP_PARTICIPANT` FOREIGN KEY (`participant_id`) REFERENCES `participant` (`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'; From de7a9a05f28526377fc278791c338005a57c2ccb Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Tue, 3 Sep 2024 10:03:36 +0200 Subject: [PATCH 3/6] Extending the role editor pt. 1 --- src/RoleEditor/MainWindow.xaml | 57 +++++++++++++-- src/RoleEditor/MainWindow.xaml.cs | 19 +++++ src/brecal.model/Port.cs | 106 +++++++++++++++++++++++++++ src/brecal.model/PortAssignment.cs | 111 +++++++++++++++++++++++++++++ src/brecal.model/Role.cs | 8 +-- 5 files changed, 291 insertions(+), 10 deletions(-) create mode 100644 src/brecal.model/Port.cs create mode 100644 src/brecal.model/PortAssignment.cs diff --git a/src/RoleEditor/MainWindow.xaml b/src/RoleEditor/MainWindow.xaml index 27c2af2..0a30e97 100644 --- a/src/RoleEditor/MainWindow.xaml +++ b/src/RoleEditor/MainWindow.xaml @@ -6,7 +6,7 @@ xmlns:local="clr-namespace:RoleEditor" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" mc:Ignorable="d" - Title="Bremen calling admin editor" Height="670" Width="800" Icon="Resources/lock_preferences.ico" Loaded="Window_Loaded"> + Title="Bremen calling admin editor" Height="670" Width="1024" Icon="Resources/lock_preferences.ico" Loaded="Window_Loaded"> @@ -16,8 +16,9 @@ - - + + + @@ -82,6 +83,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -145,7 +192,7 @@ - + @@ -199,7 +246,7 @@ - + diff --git a/src/RoleEditor/MainWindow.xaml.cs b/src/RoleEditor/MainWindow.xaml.cs index b605eb9..cb30035 100644 --- a/src/RoleEditor/MainWindow.xaml.cs +++ b/src/RoleEditor/MainWindow.xaml.cs @@ -802,5 +802,24 @@ namespace RoleEditor #endregion + private void menuItemNewPort_Click(object sender, RoutedEventArgs e) + { + + } + + private void listBoxPort_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + + } + + private void menuItemDeletePort_Click(object sender, RoutedEventArgs e) + { + + } + + private void buttonPortSave_Click(object sender, RoutedEventArgs e) + { + + } } } diff --git a/src/brecal.model/Port.cs b/src/brecal.model/Port.cs new file mode 100644 index 0000000..5a48270 --- /dev/null +++ b/src/brecal.model/Port.cs @@ -0,0 +1,106 @@ +// Copyright (c) 2023- schick Informatik +// Description: Port entity + +using System.Data; + +namespace brecal.model +{ + public class Port : DbEntity + { + + #region Properties + + public string? Name { get; set; } + + public string? Locode { get; set; } + + public bool ? IsDeleted { get; set; } + + #endregion + + #region overrides + + public override void SetCreate(IDbCommand cmd) + { + cmd.CommandText = "INSERT INTO role (name, locode) VALUES ( @NAME, @LOCODE)"; + this.SetParameters(cmd); + } + + public override void SetDelete(IDbCommand cmd) + { + cmd.CommandText = "UPDATE port SET deleted = 1 WHERE id = @ID"; + IDataParameter idParam = cmd.CreateParameter(); + idParam.ParameterName = "ID"; + idParam.Value = this.Id; + cmd.Parameters.Add(idParam); + } + + public override void SetUpdate(IDbCommand cmd) + { + cmd.CommandText = "UPDATE port set name = @NAME, locode = @LOCODE WHERE id = @ID"; + this.SetParameters(cmd); + } + + #endregion + + #region public static methods + + public static async Task> LoadAll(IDBManager manager) + { + List loadResultList = await manager.Load(SetLoadQuery, LoadElems); + List result = new(); + foreach (Port p in loadResultList.Cast()) + result.Add(p); + return result; + } + + public static void SetLoadQuery(IDbCommand cmd, params object?[] list) + { + cmd.CommandText = "SELECT id, name, locode, created, modified, deleted FROM port"; + } + + public static List LoadElems(IDataReader reader) + { + List result = new List(); + while (reader.Read()) + { + Port p = new(); + p.Id = (uint)reader.GetInt32(0); + if (!reader.IsDBNull(1)) p.Name = reader.GetString(1); + if (!reader.IsDBNull(2)) p.Locode = reader.GetString(2); + if (!reader.IsDBNull(3)) p.Created = reader.GetDateTime(3); + if (!reader.IsDBNull(4)) p.Modified = reader.GetDateTime(4); + if (!reader.IsDBNull(5)) p.IsDeleted = reader.GetBoolean(5); + result.Add(p); + } + return result; + } + + #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 desc = cmd.CreateParameter(); + desc.ParameterName = "LOCODE"; + desc.Value = this.Locode; + cmd.Parameters.Add(desc); + + IDataParameter idParam = cmd.CreateParameter(); + idParam.ParameterName = "ID"; + idParam.Value = this.Id; + cmd.Parameters.Add(idParam); + } + + #endregion + + } + + +} diff --git a/src/brecal.model/PortAssignment.cs b/src/brecal.model/PortAssignment.cs new file mode 100644 index 0000000..17ddc22 --- /dev/null +++ b/src/brecal.model/PortAssignment.cs @@ -0,0 +1,111 @@ +// 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> LoadForParticipant(Participant? p, IDBManager manager) + { + List loadResultList = await manager.Load(SetLoadQuery, LoadElems, args: p); + List result = new(); + foreach (PortAssignment pa in loadResultList.Cast()) + { + 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 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 LoadElems(IDataReader reader) + { + List result = new List(); + 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}: "; + } + else + { + return AssignedPort.Name ?? AssignedPort.Id.ToString(); + } + } + + #endregion + + } +} diff --git a/src/brecal.model/Role.cs b/src/brecal.model/Role.cs index c85802b..c52ca1f 100644 --- a/src/brecal.model/Role.cs +++ b/src/brecal.model/Role.cs @@ -1,9 +1,7 @@ -using System; -using System.Collections.Generic; +// Copyright (c) 2023- schick Informatik +// Description: Role Entity + using System.Data; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace brecal.model { From 8b4c9e25908edc3d51fce346a3dd03be03dc35e6 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Wed, 4 Sep 2024 08:09:00 +0200 Subject: [PATCH 4/6] Extending the role editor pt.2 --- src/RoleEditor/MainWindow.xaml | 23 +++- src/RoleEditor/MainWindow.xaml.cs | 125 +++++++++++++++--- .../Properties/Settings.Designer.cs | 6 +- src/RoleEditor/Properties/Settings.settings | 2 +- src/brecal.model/Participant.cs | 2 +- src/brecal.model/Port.cs | 15 ++- src/brecal.model/PortAssignment.cs | 4 +- src/brecal.mysql/DBManager.cs | 4 +- 8 files changed, 146 insertions(+), 35 deletions(-) diff --git a/src/RoleEditor/MainWindow.xaml b/src/RoleEditor/MainWindow.xaml index 0a30e97..8cf5eed 100644 --- a/src/RoleEditor/MainWindow.xaml +++ b/src/RoleEditor/MainWindow.xaml @@ -84,7 +84,24 @@ - + + + + + + + + + + + + + + @@ -117,8 +134,8 @@