279 lines
11 KiB
C#
279 lines
11 KiB
C#
//
|
|
// Class: IMSBCPosition
|
|
// Current CLR: 4.0.30319.34209
|
|
// System: Microsoft Visual Studio 10.0
|
|
// Author: dani
|
|
// Created: 5/27/2015 9:43:58 AM
|
|
//
|
|
// Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Text.RegularExpressions;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace bsmd.database
|
|
{
|
|
|
|
[TypeConverter(typeof(MessageClassConverter<IMSBCPosition>))]
|
|
[JsonConverter(typeof(NoTypeConverterJsonConverter<IMSBCPosition>))]
|
|
public class IMSBCPosition : DatabaseEntity, ISublistElement
|
|
{
|
|
|
|
#region static defs
|
|
|
|
public static string[] hazardClass =
|
|
{
|
|
"A",
|
|
"B",
|
|
"A and B"
|
|
};
|
|
|
|
private static readonly Regex unNumberRegex = new Regex("^[0-9]{4}$");
|
|
private static readonly Regex imoClassRegex = new Regex(@"^[1-9]{1}(\.[1-9]{1})?$");
|
|
|
|
#endregion
|
|
|
|
public IMSBCPosition()
|
|
{
|
|
this.tablename = "[dbo].[IMSBCPosition]";
|
|
}
|
|
|
|
#region Properties
|
|
|
|
[JsonIgnore]
|
|
[Browsable(false)]
|
|
public HAZ HAZ { get; set; }
|
|
|
|
[ShowReport]
|
|
[Validation(ValidationCode.NOT_NULL)]
|
|
[MaxLength(255)]
|
|
[MaxWordLength(15)]
|
|
[ENI2Validation]
|
|
public string BulkCargoShippingName { get; set; }
|
|
|
|
[ShowReport]
|
|
[Validation(ValidationCode.NOT_NULL)]
|
|
[ENI2Validation]
|
|
public bool? MHB { get; set; }
|
|
|
|
[ShowReport]
|
|
[MaxLength(4)]
|
|
[ENI2Validation]
|
|
public string UNNumber { get; set; }
|
|
|
|
[ShowReport]
|
|
[MaxLength(10)]
|
|
[ENI2Validation]
|
|
public string IMOClass { get; set; }
|
|
|
|
[ShowReport]
|
|
[Validation(ValidationCode.DOUBLE_GT_ZERO)]
|
|
[ENI2Validation]
|
|
public double? Quantity_KGM { get; set; }
|
|
|
|
[ShowReport]
|
|
[Validation(ValidationCode.NOT_NULL)]
|
|
[MaxLength(24)]
|
|
[MaxWordLength(8)]
|
|
[ENI2Validation]
|
|
public string StowagePosition { get; set; }
|
|
|
|
[ShowReport]
|
|
[Validation(ValidationCode.LOCODE)]
|
|
[MaxLength(5)]
|
|
[ENI2Validation]
|
|
public string PortOfLoading { get; set; }
|
|
|
|
[ShowReport]
|
|
[Validation(ValidationCode.LOCODE)]
|
|
[MaxLength(5)]
|
|
[ENI2Validation]
|
|
public string PortOfDischarge { get; set; }
|
|
|
|
[ShowReport]
|
|
[MaxLength(255)]
|
|
[MaxWordLength(35)]
|
|
[ENI2Validation]
|
|
public string Remarks { get; set; }
|
|
|
|
[ShowReport]
|
|
[ENI2Validation]
|
|
public int? IMOHazardClass { get; set; }
|
|
|
|
[ShowReport]
|
|
[ReportDisplayName("IMO hazard class")]
|
|
[JsonIgnore]
|
|
public string IMOHazardClassDisplay
|
|
{
|
|
get
|
|
{
|
|
if (this.IMOHazardClass.HasValue && (this.IMOHazardClass.Value < hazardClass.Length))
|
|
return hazardClass[this.IMOHazardClass.Value];
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public string Identifier { get; set; }
|
|
|
|
[JsonIgnore]
|
|
[Browsable(false)]
|
|
public string SublistCollectionKey { get { return "imsbc"; } }
|
|
|
|
#endregion
|
|
|
|
#region DatabaseEntity implementation
|
|
|
|
public override void PrepareSave(IDbCommand cmd)
|
|
{
|
|
SqlCommand scmd = cmd as SqlCommand;
|
|
|
|
scmd.Parameters.AddWithValue("@P1", this.HAZ.Id);
|
|
scmd.Parameters.AddWithNullableValue("@P2", this.BulkCargoShippingName);
|
|
scmd.Parameters.AddWithNullableValue("@P3", this.MHB);
|
|
scmd.Parameters.AddWithNullableValue("@P4", this.UNNumber);
|
|
scmd.Parameters.AddWithNullableValue("@P5", this.IMOClass);
|
|
scmd.Parameters.AddWithNullableValue("@P6", this.Quantity_KGM);
|
|
scmd.Parameters.AddWithNullableValue("@P7", this.StowagePosition);
|
|
scmd.Parameters.AddWithNullableValue("@P8", this.PortOfLoading);
|
|
scmd.Parameters.AddWithNullableValue("@P9", this.PortOfDischarge);
|
|
scmd.Parameters.AddWithNullableValue("@P10", this.Remarks);
|
|
scmd.Parameters.AddWithNullableValue("@P11", this.Identifier);
|
|
scmd.Parameters.AddWithNullableValue("@P12", this.IMOHazardClass);
|
|
|
|
if (this.IsNew)
|
|
{
|
|
this.CreateId();
|
|
scmd.Parameters.AddWithValue("@ID", this.Id);
|
|
scmd.CommandText = string.Format("INSERT INTO {0} (Id, HAZId, BulkCargoShippingName, MHB, UNNumber, " +
|
|
"IMOClass, Quantity_KGM, StowagePosition, PortOfLoading, PortOfDischarge, Remarks, Identifier, IMOHazardClass) " +
|
|
" VALUES (@ID, @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9, @P10, @P11, @P12)", this.Tablename);
|
|
}
|
|
else
|
|
{
|
|
scmd.Parameters.AddWithValue("@ID", this.Id);
|
|
scmd.CommandText = string.Format("UPDATE {0} SET BulkCargoShippingName = @P2, MHB = @P3, UNNumber = @P4, " +
|
|
"IMOClass = @P5, Quantity_KGM = @P6, StowagePosition = @P7, PortOfLoading = @P8, PortOfDischarge = @P9, " +
|
|
"Remarks = @P10, Identifier = @P11, IMOHazardClass = @P12 WHERE Id = @ID", this.Tablename);
|
|
}
|
|
}
|
|
|
|
public override void PrepareLoadCommand(IDbCommand cmd, Message.LoadFilter filter, params object[] criteria)
|
|
{
|
|
string query = string.Format("SELECT Id, BulkCargoShippingName, MHB, " +
|
|
"UNNumber, IMOClass, Quantity_KGM, StowagePosition, PortOfLoading, PortOfDischarge, " +
|
|
"Remarks, Identifier, IMOHazardClass FROM {0} ", this.Tablename);
|
|
|
|
switch (filter)
|
|
{
|
|
case Message.LoadFilter.HAZ_ID:
|
|
query += " WHERE HAZId = @HAZID";
|
|
((SqlCommand)cmd).Parameters.AddWithValue("@HAZID", criteria[0]);
|
|
break;
|
|
case Message.LoadFilter.ALL:
|
|
default:
|
|
|
|
break;
|
|
}
|
|
|
|
cmd.CommandText = query;
|
|
}
|
|
|
|
public override List<DatabaseEntity> LoadList(IDataReader reader)
|
|
{
|
|
List<DatabaseEntity> result = new List<DatabaseEntity>();
|
|
|
|
while (reader.Read())
|
|
{
|
|
IMSBCPosition imsbc = new IMSBCPosition();
|
|
imsbc.id = reader.GetGuid(0);
|
|
if (!reader.IsDBNull(1)) imsbc.BulkCargoShippingName = reader.GetString(1);
|
|
if (!reader.IsDBNull(2)) imsbc.MHB = reader.GetBoolean(2);
|
|
if (!reader.IsDBNull(3)) imsbc.UNNumber = reader.GetString(3);
|
|
if (!reader.IsDBNull(4)) imsbc.IMOClass = reader.GetString(4);
|
|
if (!reader.IsDBNull(5)) imsbc.Quantity_KGM = reader.GetDouble(5);
|
|
if (!reader.IsDBNull(6)) imsbc.StowagePosition = reader.GetString(6);
|
|
if (!reader.IsDBNull(7)) imsbc.PortOfLoading = reader.GetString(7);
|
|
if (!reader.IsDBNull(8)) imsbc.PortOfDischarge = reader.GetString(8);
|
|
if (!reader.IsDBNull(9)) imsbc.Remarks = reader.GetString(9);
|
|
if (!reader.IsDBNull(10)) imsbc.Identifier = reader.GetString(10);
|
|
if (!reader.IsDBNull(11)) imsbc.IMOHazardClass = reader.GetByte(11);
|
|
|
|
result.Add(imsbc);
|
|
}
|
|
reader.Close();
|
|
result.Sort();
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region public methods
|
|
|
|
public void CopyFromIMSBC(IMSBCPosition selectedIMSBC)
|
|
{
|
|
if (selectedIMSBC == null) return;
|
|
|
|
this.BulkCargoShippingName = selectedIMSBC.BulkCargoShippingName;
|
|
this.IMOClass = selectedIMSBC.IMOClass;
|
|
this.MHB = selectedIMSBC.MHB;
|
|
this.PortOfDischarge = selectedIMSBC.PortOfDischarge;
|
|
this.PortOfLoading = selectedIMSBC.PortOfLoading;
|
|
this.Quantity_KGM = selectedIMSBC.Quantity_KGM;
|
|
this.Remarks = selectedIMSBC.Remarks;
|
|
this.StowagePosition = selectedIMSBC.StowagePosition;
|
|
this.UNNumber = selectedIMSBC.UNNumber;
|
|
this.IMOHazardClass = selectedIMSBC.IMOHazardClass;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Validation
|
|
|
|
public override void Validate(List<MessageError> errors, List<MessageViolation> violations)
|
|
{
|
|
if(this.Quantity_KGM >= 1000000000)
|
|
violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, null, null, this.Title, this.Identifier, this.HAZ.IsDeparture ? "HAZD" : "HAZA"));
|
|
|
|
if (!MHB ?? false)
|
|
{
|
|
if(this.UNNumber.IsNullOrEmpty())
|
|
violations.Add(RuleEngine.CreateViolation(ValidationCode.V803, "UNNumber", this.UNNumber, this.Title, this.Identifier, this.HAZ.IsDeparture ? "HAZD" : "HAZA"));
|
|
|
|
if (this.IMOClass.IsNullOrEmpty())
|
|
violations.Add(RuleEngine.CreateViolation(ValidationCode.V803, "IMOClass", this.IMOClass, this.Title, this.Identifier, this.HAZ.IsDeparture ? "HAZD" : "HAZA"));
|
|
}
|
|
|
|
if(!this.UNNumber.IsNullOrEmpty() && !unNumberRegex.IsMatch(this.UNNumber))
|
|
violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "UNNumber", null, this.Title, this.Identifier, this.HAZ.IsDeparture ? "HAZD" : "HAZA"));
|
|
|
|
if(!this.IMOClass.IsNullOrEmpty() && !imoClassRegex.IsMatch(this.IMOClass))
|
|
violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "IMOClass", null, this.Title, this.Identifier, this.HAZ.IsDeparture ? "HAZD" : "HAZA"));
|
|
|
|
if (!this.IMOHazardClass.HasValue)
|
|
{
|
|
violations.Add(RuleEngine.CreateViolation(ValidationCode.V810, "Hazards", null, this.Title, this.Identifier, this.HAZ.IsDeparture ? "HAZD" : "HAZA"));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IComparable implementation
|
|
|
|
public int CompareTo(object obj)
|
|
{
|
|
if (this.Identifier == null) return 1;
|
|
if ((obj is null) || (((IMSBCPosition)obj).Identifier == null))
|
|
return 1;
|
|
if (Int32.TryParse(((IMSBCPosition)obj).Identifier, out int i1) && Int32.TryParse(this.Identifier, out int i2))
|
|
return i2.CompareTo(i1);
|
|
return this.Identifier.CompareTo(((IMSBCPosition)obj).Identifier);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|