git_bsmd/bsmd.database/Waste.cs

294 lines
12 KiB
C#

//
// Class: Waste
// Current CLR: 4.0.30319.34209
// System: Microsoft Visual Studio 10.0
// Author: dani
// Created: 4/2/2015 7:44:03 PM
//
// Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved.
using System.Data.SqlClient;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.ComponentModel;
using System;
namespace bsmd.database
{
[TypeConverter(typeof(MessageClassConverter<Waste>))]
[JsonConverter(typeof(NoTypeConverterJsonConverter<Waste>))]
public class Waste : DatabaseEntity, ISublistElement
{
public Waste()
{
this.tablename = "[dbo].[Waste]";
}
#region Properties
[JsonIgnore]
[Browsable(false)]
public WAS WAS { get; set; }
[ShowReport]
[JsonIgnore]
public string WasteTypeDisplayV4
{
get
{
if (!WasteType.HasValue) return string.Empty;
switch(WasteType.Value)
{
// "old" waste codes (pre V7), used to display old data
case 1100: return "Oily residues (sludge)";
case 1200: return "Oily bilge water";
case 1300: return "Waste oil - others (specify)";
case 2100: return "Food waste";
case 2200: return "Plastic";
case 2300: return "Domestic wastes";
case 2311: return "Cooking oil";
case 2308: return "Incinerator ashes";
case 2313: return "Operational wastes";
case 2600: return "Operational wastes";
case 2309: return "Animal carcass(es)";
case 3000: return "Sewage";
case 5100: return "Cargo residues - Marpol Annex I";
case 5200: return "Cargo residues - Marpol Annex II";
case 5300: return "Cargo residues - Marpol Annex V";
default: return string.Empty;
}
}
}
[JsonIgnore]
public string WasteTypeDisplayGrid
{
get
{
if (!this.WasteType.HasValue) return string.Empty;
string code = WasteTypeDisplayV4;
if(code.IsNullOrEmpty())
{
if (WAS.WasteCodeDict.ContainsKey(this.WasteType.ToString()))
code = WAS.WasteCodeDict[this.WasteType.ToString()];
}
return string.Format("{0} {1}", WasteType, code);
}
}
// jetzt der "WasteCode"
[Validation(ValidationCode.NOT_NULL)]
[ENI2Validation]
public int? WasteType { get; set; }
[ShowReport]
[MaxLength(99)]
[ENI2Validation]
public string WasteDescription { get; set; }
[ShowReport]
[Validation(ValidationCode.NOT_NULL)]
[ENI2Validation]
public double? WasteDisposalAmount_MTQ { get; set; }
[ShowReport]
[Validation(ValidationCode.NOT_NULL)]
[ENI2Validation]
public double? WasteCapacity_MTQ { get; set; }
[ShowReport]
[Validation(ValidationCode.NOT_NULL)]
[ENI2Validation]
public double? WasteAmountRetained_MTQ { get; set; }
// Jetzt der "RemainingWasteDeliveryPort"
[ShowReport]
[MaxLength(5)]
[ENI2Validation]
[Validation(ValidationCode.LOCODE_SSN)]
public string WasteDisposalPort { get; set; }
[ShowReport]
[ENI2Validation]
[Validation(ValidationCode.NOT_NULL)]
public double? WasteAmountGeneratedTillNextPort_MTQ { get; set; }
public string Identifier { get; set; }
[JsonIgnore]
[Browsable(false)]
public string SublistCollectionKey { get { return "waste"; } }
[JsonIgnore]
public bool HasValidWasteCode
{
get
{
if (!this.WasteType.HasValue) return false;
foreach(string wasteCode in WAS.WasteCodes)
{
if (wasteCode.Equals(this.WasteType.Value.ToString()))
return true;
}
return false;
}
}
[JsonIgnore]
public bool IsDashWasteCode
{
get
{
if (!WasteType.HasValue) return false;
switch (WasteType.Value)
{
case 999:
case 201:
case 202:
case 203:
case 204:
case 510:
case 511:
return true;
default:
return false;
}
}
}
#endregion
#region DatabaseEntity implementation
public override void PrepareSave(System.Data.IDbCommand cmd)
{
SqlCommand scmd = cmd as SqlCommand;
scmd.Parameters.AddWithValue("@P1", this.WAS.Id);
scmd.Parameters.AddWithNullableValue("@P2", this.WasteType);
scmd.Parameters.AddWithNullableValue("@P3", this.WasteDescription);
scmd.Parameters.AddWithNullableValue("@P4", this.WasteDisposalAmount_MTQ);
scmd.Parameters.AddWithNullableValue("@P5", this.WasteCapacity_MTQ);
scmd.Parameters.AddWithNullableValue("@P6", this.WasteAmountRetained_MTQ);
scmd.Parameters.AddWithNullableValue("@P7", this.WasteDisposalPort);
scmd.Parameters.AddWithNullableValue("@P8", this.WasteAmountGeneratedTillNextPort_MTQ);
scmd.Parameters.AddWithNullableValue("@P9", this.Identifier);
if (this.IsNew)
{
this.CreateId();
scmd.Parameters.AddWithValue("@ID", this.Id);
scmd.CommandText = string.Format("INSERT INTO {0} (Id, WASId, WasteType, WasteDescription, " +
"WasteDisposalAmount_MTQ, WasteCapacity_MTQ, WasteAmountRetained_MTQ, WasteDisposalPort, " +
"WasteAmountGeneratedTillNextPort_MTQ, Identifier) " +
" VALUES ( @ID, @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9 )", this.Tablename);
}
else
{
scmd.Parameters.AddWithValue("@ID", this.Id);
scmd.CommandText = string.Format("UPDATE {0} SET WasteType = @P2, WasteDescription = @P3, " +
"WasteDisposalAmount_MTQ = @P4, WasteCapacity_MTQ = @P5, WasteAmountRetained_MTQ = @P6," +
"WasteDisposalPort = @P7, WasteAmountGeneratedTillNextPort_MTQ = @P8, " +
"Identifier = @P9 WHERE Id = @ID", this.Tablename);
}
}
public override void PrepareLoadCommand(System.Data.IDbCommand cmd, Message.LoadFilter filter, params object[] criteria)
{
string query = string.Format("SELECT Id, WasteType, WasteDescription, WasteDisposalAmount_MTQ, " +
"WasteCapacity_MTQ, WasteAmountRetained_MTQ, WasteDisposalPort, WasteAmountGeneratedTillNextPort_MTQ, " +
"Identifier FROM {0} ", this.Tablename);
switch (filter)
{
case Message.LoadFilter.WAS_ID:
query += " WHERE WASId = @WDID";
((SqlCommand)cmd).Parameters.AddWithValue("@WDID", criteria[0]);
break;
case Message.LoadFilter.ALL:
default:
break;
}
cmd.CommandText = query;
}
public override List<DatabaseEntity> LoadList(System.Data.IDataReader reader)
{
List<DatabaseEntity> result = new List<DatabaseEntity>();
while (reader.Read())
{
Waste waste = new Waste();
waste.id = reader.GetGuid(0);
if (!reader.IsDBNull(1)) waste.WasteType = reader.GetInt32(1);
if (!reader.IsDBNull(2)) waste.WasteDescription = reader.GetString(2);
if (!reader.IsDBNull(3)) waste.WasteDisposalAmount_MTQ = (float) reader.GetDouble(3);
if (!reader.IsDBNull(4)) waste.WasteCapacity_MTQ = (float) reader.GetDouble(4);
if (!reader.IsDBNull(5)) waste.WasteAmountRetained_MTQ = (float) reader.GetDouble(5);
if (!reader.IsDBNull(6)) waste.WasteDisposalPort = reader.GetString(6);
if (!reader.IsDBNull(7)) waste.WasteAmountGeneratedTillNextPort_MTQ = (float) reader.GetDouble(7);
if (!reader.IsDBNull(8)) waste.Identifier = reader.GetString(8);
result.Add(waste);
}
reader.Close();
result.Sort();
return result;
}
#endregion
#region Validation
public override void Validate(List<MessageError> errors, List<MessageViolation> violations)
{
if (this.IsDashWasteCode && this.WasteDescription.IsNullOrEmpty())
violations.Add(RuleEngine.CreateViolation(ValidationCode.V781, "WasteDescription", null, this.Title, this.Identifier, this.Tablename));
if (!this.HasValidWasteCode)
violations.Add(RuleEngine.CreateViolation(ValidationCode.V784, "WasteCode", null, this.Title, this.Identifier, this.Tablename));
if (this.WasteDisposalAmount_MTQ >= 10000)
violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Waste disposal amount too high", null, this.Title, this.Identifier, this.Tablename));
if (this.WasteCapacity_MTQ >= 10000)
violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Waste capacity too high", null, this.Title, this.Identifier, this.Tablename));
if (this.WasteAmountRetained_MTQ >= 10000)
violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Waste amount retained too high", null, this.Title, this.Identifier, this.Tablename));
if (this.WasteAmountGeneratedTillNextPort_MTQ >= 10000)
violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Waste generated till next port too high", null, this.Title, this.Identifier, this.Tablename));
if(this.WasteDisposalAmount_MTQ > this.WasteCapacity_MTQ)
violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Disposal greater than capacity!", null, this.Title, this.Identifier, this.Tablename));
if((this.WasteAmountGeneratedTillNextPort_MTQ + this.WasteAmountRetained_MTQ) > this.WasteCapacity_MTQ)
violations.Add(RuleEngine.CreateViolation(ValidationCode.IMPLAUSIBLE, "Waste generated+retained greater than capacity!", null, this.Title, this.Identifier, this.Tablename));
}
#endregion
#region IComparable implementation
public int CompareTo(object obj)
{
if (this.Identifier == null) return 1;
if ((obj is null) || (((Waste)obj).Identifier == null))
return 1;
if (Int32.TryParse(((Waste)obj).Identifier, out int i1) && Int32.TryParse(this.Identifier, out int i2))
return i2.CompareTo(i1);
return this.Identifier.CompareTo(((Waste)obj).Identifier);
}
#endregion
}
}