// Copyright (c) 2020-present schick Informatik // Description: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace bsmd.database { /// /// Extra balcony: Data stored in Maersk Excel sheets must be stored to be exported /// at a later date. In order to do that (and to be a little more flexible in the future) /// we use a generic storage class called "XtraData" that hopefully might be useful in another /// future scenario as well /// public class MaerskData : DatabaseEntity { #region Construction public MaerskData() { this.tablename = "[dbo].[XtraData]"; } #endregion #region Enums public enum MDStatus { [Description("No id")] NO_ID, [Description("Updated")] UPDATED, [Description("Requesting id")] REQUESTING_ID, [Description("Id assigned")] ID_ASSIGNED, [Description("upcoming without id")] NO_ID_AND_DUE, [Description("Has id")] ID } #endregion #region Properties /// /// reference id as set in the database /// public Guid MessageCoreId { get; set; } /// /// Core instance reference once matched / set /// public MessageCore Core { get; set; } public MDStatus Status { get; set; } public DateTime? ETA { get; set; } public string ColA { get; set; } public string ColB { get; set; } /// /// Shipping area? /// public string ColC { get; set; } public string ColD { get; set; } /// /// Vessel name /// public string ColE { get; set; } /// /// IMO /// public string ColF { get; set; } /// /// Voyage-No incoming /// public string ColG { get; set; } /// /// Voyage-No outgoing /// public string ColH { get; set; } /// /// Terminal /// public string ColI { get; set; } /// /// Operator /// public string ColJ { get; set; } /// /// ETA /// public string ColK { get; set; } /// /// ETD /// public string ColL { get; set; } /// /// Visit-Id /// public string ColM { get; set; } public string Remark { get; set; } #endregion #region abstract DatabaseEntity method implementation public override List LoadList(IDataReader reader) { List result = new List(); if (reader != null) { while (reader.Read()) { result.Add(ReadRowFromReader(reader)); } reader.Close(); } return result; } public async Task> LoadListAsync(SqlDataReader reader) { List result = new List(); if (reader != null) { while (await reader.ReadAsync()) { result.Add(ReadRowFromReader(reader)); } reader.Close(); } return result; } private MaerskData ReadRowFromReader(IDataReader reader) { MaerskData md = null; if (reader != null) { md = new MaerskData(); md.id = reader.GetGuid(0); if (!reader.IsDBNull(1)) md.MessageCoreId = reader.GetGuid(1); if (!reader.IsDBNull(2)) md.ColA = reader.GetString(2); if (!reader.IsDBNull(3)) md.ColB = reader.GetString(3); if (!reader.IsDBNull(4)) md.ColC = reader.GetString(4); if (!reader.IsDBNull(5)) md.ColD = reader.GetString(5); if (!reader.IsDBNull(6)) md.ColE = reader.GetString(6); if (!reader.IsDBNull(7)) md.ColF = reader.GetString(7); if (!reader.IsDBNull(8)) md.ColG = reader.GetString(8); if (!reader.IsDBNull(9)) md.ColH = reader.GetString(9); if (!reader.IsDBNull(10)) md.ColI = reader.GetString(10); if (!reader.IsDBNull(11)) md.ColJ = reader.GetString(11); if (!reader.IsDBNull(12)) md.ColK = reader.GetString(12); if (!reader.IsDBNull(13)) md.ColL = reader.GetString(13); if (!reader.IsDBNull(14)) md.ColM = reader.GetString(14); if (!reader.IsDBNull(15)) md.Remark = reader.GetString(15); // try parsing the column to datetime if (DateTime.TryParse(md.ColK, out DateTime eta)) ETA = eta; } return md; } public override void PrepareLoadCommand(IDbCommand cmd, Message.LoadFilter filter, params object[] criteria) { string query = string.Format("SELECT Id, ReferenceId, Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12 , Field13, Field14 FROM {0} ", this.Tablename); switch (filter) { case Message.LoadFilter.BY_ID: query += " WHERE Id = @ID"; ((SqlCommand)cmd).Parameters.AddWithValue("@ID", criteria[0]); break; case Message.LoadFilter.BY_CORE: query += " WHERE ReferenceId = @ID"; ((SqlCommand)cmd).Parameters.AddWithValue("@ID", criteria[0]); break; default: break; } cmd.CommandText = query; } public override void PrepareSave(IDbCommand cmd) { SqlCommand scmd = cmd as SqlCommand; scmd.Parameters.AddWithNullableValue("@REF", this.MessageCoreId); scmd.Parameters.AddWithNullableValue("@COLA", this.ColA); scmd.Parameters.AddWithNullableValue("@COLB", this.ColB); scmd.Parameters.AddWithNullableValue("@COLC", this.ColC); scmd.Parameters.AddWithNullableValue("@COLD", this.ColD); scmd.Parameters.AddWithNullableValue("@COLE", this.ColE); scmd.Parameters.AddWithNullableValue("@COLF", this.ColF); scmd.Parameters.AddWithNullableValue("@COLG", this.ColG); scmd.Parameters.AddWithNullableValue("@COLH", this.ColH); scmd.Parameters.AddWithNullableValue("@COLI", this.ColI); scmd.Parameters.AddWithNullableValue("@COLJ", this.ColJ); scmd.Parameters.AddWithNullableValue("@COLK", this.ColK); scmd.Parameters.AddWithNullableValue("@COLL", this.ColL); scmd.Parameters.AddWithNullableValue("@COLM", this.ColM); scmd.Parameters.AddWithNullableValue("@REMARK", this.Remark); if (this.IsNew) { this.CreateId(); scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("INSERT INTO {0} (Id, ReferenceId, Field1, Field2, Field3, Field4, Field5, Field6, Field7, Field8, Field9, Field10, Field11, Field12, Field13, Field14) " + "VALUES (@ID, @REF, @COLA, @COLB, @COLC, @COLD, @COLE, @COLF, @COLG, @COLH, @COLI, @COLJ, @COLK, @COLL, @COLM, @REMARK)", this.Tablename); } else { scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("UPDATE {0} SET Field1 = @COLA, Field2 = @COLB, Field3 = @COLC, Field4 = @COLD, Field5 = @COLE, Field6 = @COLF, @Field7 = @COLG, @Field8 = @COLH, " + "Field9 = @COLI, Field10 = @COLJ, Field11 = @COLK, Field12 = @COLL, Field13 = @COLM, Field14 = @Remark WHERE Id = @ID", this.Tablename); } } #endregion } }