884 lines
36 KiB
C#
884 lines
36 KiB
C#
//
|
|
// Class: DBManager
|
|
// Current CLR: 4.0.30319.34209
|
|
// System: Microsoft Visual Studio 10.0
|
|
// Author: dani
|
|
// Created: 3/10/2015 7:25:55 AM
|
|
//
|
|
// Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Diagnostics;
|
|
using log4net;
|
|
|
|
namespace bsmd.database
|
|
{
|
|
public class DBManager
|
|
{
|
|
|
|
#region Fields
|
|
|
|
private SqlConnection _con;
|
|
private static DBManager _instance;
|
|
private static ILog _log = LogManager.GetLogger(typeof(DBManager));
|
|
private static Dictionary<Guid, ReportingParty> allReportingParties;
|
|
private static Dictionary<string, PortArea> allPortAreas;
|
|
|
|
#endregion
|
|
|
|
#region Enums
|
|
|
|
public enum MessageLoad
|
|
{
|
|
ALL,
|
|
EXCEL,
|
|
HE,
|
|
ENI
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public static DBManager Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
_instance = new DBManager();
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public string ConnectionString { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region public DB funcs
|
|
|
|
public bool Connect(string dbConnectionString)
|
|
{
|
|
try
|
|
{
|
|
_con = new SqlConnection(dbConnectionString);
|
|
_con.Open();
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.ErrorFormat("DBManager cannot connect:{0}", ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void Disconnect()
|
|
{
|
|
if ((this._con != null) && (this._con.State == ConnectionState.Open))
|
|
this._con.Close();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region public helper funcs
|
|
|
|
/*
|
|
public Dictionary<Guid, MessageCore> GetToSendMessageCoreList()
|
|
{
|
|
List<MessageCore> toSendList = this.GetMessageCoresByStatus(MessageCore.BSMDStatus.TOSEND);
|
|
Dictionary<Guid, MessageCore> result = new Dictionary<Guid, MessageCore>();
|
|
foreach (MessageCore core in toSendList)
|
|
result.Add(core.Id.Value, core);
|
|
return result;
|
|
}
|
|
|
|
*/
|
|
|
|
public List<MessageCore> GetMessageCoresByStatus(MessageCore.BSMDStatus status)
|
|
{
|
|
MessageCore aMessageCore = new MessageCore();
|
|
SqlCommand cmd = new SqlCommand();
|
|
aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.BSMDSTATUS, status);
|
|
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> cores = aMessageCore.LoadList(reader);
|
|
List<MessageCore> result = new List<MessageCore>();
|
|
foreach (MessageCore core in cores)
|
|
{
|
|
this.LoadCustomer(core);
|
|
result.Add(core);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public List<MessageCore> GetMessageCoresByReportStatus(MessageCore.ReportStatusEnum reportStatus)
|
|
{
|
|
MessageCore aMessageCore = new MessageCore();
|
|
SqlCommand cmd = new SqlCommand();
|
|
aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.REPORTSTATUS, reportStatus);
|
|
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> cores = aMessageCore.LoadList(reader);
|
|
List<MessageCore> result = new List<MessageCore>();
|
|
foreach (MessageCore core in cores)
|
|
{
|
|
this.LoadCustomer(core);
|
|
result.Add(core);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lädt MessageCore (=Schiffsanlauf) einer EU-NOAD Nachricht vom Fleettracker / Herberg
|
|
/// </summary>
|
|
/// <param name="herbergFormGuid"></param>
|
|
/// <returns></returns>
|
|
public MessageCore GetHerbergFormMessage(Guid herbergFormGuid)
|
|
{
|
|
MessageCore result = null;
|
|
MessageCore aMessageCore = new MessageCore();
|
|
SqlCommand cmd = new SqlCommand();
|
|
aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.HERBERG_FORMGUID, herbergFormGuid);
|
|
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> cores = aMessageCore.LoadList(reader);
|
|
if (cores.Count > 1) _log.WarnFormat("Herberg form message: {0} cores found for guid {1}",
|
|
cores.Count, herbergFormGuid);
|
|
if (cores.Count > 0)
|
|
{
|
|
result = (MessageCore)cores[0];
|
|
this.LoadCustomer(result);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public List<Message> GetMessagesForCore(MessageCore core, MessageLoad loadType)
|
|
{
|
|
Message aMessage = new Message();
|
|
SqlCommand cmd = new SqlCommand();
|
|
|
|
Message.LoadFilter loadFilter = Message.LoadFilter.BY_CORE;
|
|
switch(loadType)
|
|
{
|
|
case MessageLoad.ENI: loadFilter = Message.LoadFilter.BY_CORE_ENI; break;
|
|
case MessageLoad.EXCEL: loadFilter = Message.LoadFilter.BY_CORE_EXCEL; break;
|
|
case MessageLoad.HE: loadFilter = Message.LoadFilter.BY_CORE_HE; break;
|
|
|
|
}
|
|
|
|
aMessage.PrepareLoadCommand(cmd, loadFilter, core.Id);
|
|
IDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> messages = aMessage.LoadList(reader);
|
|
|
|
List<Message> messageList = new List<Message>();
|
|
foreach (Message message in messages)
|
|
{
|
|
message.MessageCore = core;
|
|
messageList.Add(message);
|
|
}
|
|
this.LoadMessageDependencies(messageList);
|
|
return messageList;
|
|
}
|
|
|
|
public Dictionary<Guid, ReportingParty> GetReportingPartyDict()
|
|
{
|
|
if (DBManager.allReportingParties == null)
|
|
{
|
|
ReportingParty aRep = new ReportingParty();
|
|
SqlCommand cmd = new SqlCommand();
|
|
aRep.PrepareLoadCommand(cmd, Message.LoadFilter.ALL);
|
|
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> reportingParties = aRep.LoadList(reader);
|
|
DBManager.allReportingParties = new Dictionary<Guid, ReportingParty>();
|
|
foreach (ReportingParty rp in reportingParties)
|
|
DBManager.allReportingParties.Add(rp.Id.Value, rp);
|
|
}
|
|
return DBManager.allReportingParties;
|
|
}
|
|
|
|
public Dictionary<string, PortArea> GetPortAreaDict()
|
|
{
|
|
if(DBManager.allPortAreas == null)
|
|
{
|
|
PortArea pa = new PortArea();
|
|
SqlCommand cmd = new SqlCommand();
|
|
pa.PrepareLoadCommand(cmd, Message.LoadFilter.ALL);
|
|
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> portAreas = pa.LoadList(reader);
|
|
DBManager.allPortAreas = new Dictionary<string, PortArea>();
|
|
foreach (PortArea aPa in portAreas)
|
|
DBManager.allPortAreas.Add(aPa.Locode + aPa.Code, aPa);
|
|
}
|
|
return DBManager.allPortAreas;
|
|
}
|
|
|
|
public DatabaseEntity GetMessageById(Guid id)
|
|
{
|
|
Message aMessage = new Message();
|
|
SqlCommand cmd = new SqlCommand();
|
|
Message.LoadFilter filter = Message.LoadFilter.BY_ID;
|
|
aMessage.PrepareLoadCommand(cmd, filter, id);
|
|
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> messages = aMessage.LoadList(reader);
|
|
|
|
List<Message> messageList = new List<Message>();
|
|
foreach (Message message in messages)
|
|
messageList.Add(message);
|
|
|
|
if (messageList.Count == 0) return null;
|
|
|
|
this.LoadMessageDependencies(messageList);
|
|
if(messageList[0].MessageCoreId.HasValue)
|
|
messageList[0].MessageCore = this.GetMessageCoreById(messageList[0].MessageCoreId.Value);
|
|
|
|
return messageList[0];
|
|
}
|
|
|
|
public MessageCore GetMessageCoreById(Guid id)
|
|
{
|
|
MessageCore aCore = new MessageCore();
|
|
SqlCommand cmd = new SqlCommand();
|
|
Message.LoadFilter filter = Message.LoadFilter.BY_ID;
|
|
aCore.PrepareLoadCommand(cmd, filter, id);
|
|
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> cores = aCore.LoadList(reader);
|
|
|
|
MessageCore result = null;
|
|
if (cores.Count > 0)
|
|
{
|
|
result = cores[0] as MessageCore;
|
|
if (result.CustomerId.HasValue)
|
|
{
|
|
Customer c = new Customer();
|
|
SqlCommand cCmd = new SqlCommand();
|
|
c.PrepareLoadCommand(cCmd, Message.LoadFilter.BY_ID, result.CustomerId.Value);
|
|
reader = this.PerformCommand(cCmd);
|
|
List<DatabaseEntity> customers = c.LoadList(reader);
|
|
if (customers.Count > 0)
|
|
result.Customer = customers[0] as Customer;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public MessageCore GetMessageCoreByVisitId(string visitId)
|
|
{
|
|
MessageCore aCore = new MessageCore();
|
|
SqlCommand cmd = new SqlCommand();
|
|
Message.LoadFilter filter = Message.LoadFilter.BY_VISITID;
|
|
aCore.PrepareLoadCommand(cmd, filter, visitId);
|
|
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> cores = aCore.LoadList(reader);
|
|
|
|
MessageCore result = null;
|
|
if (cores.Count > 0)
|
|
{
|
|
if (cores.Count > 1) _log.WarnFormat("more than one core in DB for VISIT ID {0}", visitId);
|
|
result = cores[0] as MessageCore;
|
|
if (result.CustomerId.HasValue)
|
|
{
|
|
Customer c = new Customer();
|
|
SqlCommand cCmd = new SqlCommand();
|
|
c.PrepareLoadCommand(cCmd, Message.LoadFilter.BY_ID, result.CustomerId.Value);
|
|
reader = this.PerformCommand(cCmd);
|
|
List<DatabaseEntity> customers = c.LoadList(reader);
|
|
if (customers.Count > 0)
|
|
result.Customer = customers[0] as Customer;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public MessageCore GetMessageCoreByTransitId(string transitId)
|
|
{
|
|
MessageCore aCore = new MessageCore();
|
|
SqlCommand cmd = new SqlCommand();
|
|
Message.LoadFilter filter = Message.LoadFilter.BY_TRANSITID;
|
|
aCore.PrepareLoadCommand(cmd, filter, transitId);
|
|
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> cores = aCore.LoadList(reader);
|
|
|
|
MessageCore result = null;
|
|
if (cores.Count > 0)
|
|
{
|
|
if (cores.Count > 1) _log.WarnFormat("more than one core in DB for TRANSIT ID {0}", transitId);
|
|
result = cores[0] as MessageCore;
|
|
if (result.CustomerId.HasValue)
|
|
{
|
|
Customer c = new Customer();
|
|
SqlCommand cCmd = new SqlCommand();
|
|
c.PrepareLoadCommand(cCmd, Message.LoadFilter.BY_ID, result.CustomerId.Value);
|
|
reader = this.PerformCommand(cCmd);
|
|
List<DatabaseEntity> customers = c.LoadList(reader);
|
|
if (customers.Count > 0)
|
|
result.Customer = customers[0] as Customer;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
public MessageCore GetMessageCoreByShipInfos(string imo, DateTime eta, string poc)
|
|
{
|
|
MessageCore aCore = new MessageCore();
|
|
SqlCommand cmd = new SqlCommand();
|
|
Message.LoadFilter filter = Message.LoadFilter.IMO_ETA_POC;
|
|
aCore.PrepareLoadCommand(cmd, filter, imo, eta, poc);
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> cores = aCore.LoadList(reader);
|
|
|
|
MessageCore result = null;
|
|
if (cores.Count > 0)
|
|
{
|
|
if (cores.Count > 1)
|
|
_log.WarnFormat("Multiple ({3}) cores found for IMO {0}, ETA {1}, PoC {2}",
|
|
imo, eta, poc, cores.Count);
|
|
result = cores[0] as MessageCore;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public void Save(DatabaseEntity entity)
|
|
{
|
|
SqlCommand cmd = new SqlCommand();
|
|
entity.PrepareSave(cmd);
|
|
int queryResult = this.PerformNonQuery(cmd);
|
|
this.LogNonQueryResult(cmd.CommandText, queryResult);
|
|
}
|
|
|
|
public void Delete(DatabaseEntity entity)
|
|
{
|
|
SqlCommand cmd = new SqlCommand();
|
|
entity.PrepareDelete(cmd);
|
|
int queryResult = this.PerformNonQuery(cmd);
|
|
this.LogNonQueryResult(cmd.CommandText, queryResult);
|
|
}
|
|
|
|
public void DeleteMessageErrors(Message message)
|
|
{
|
|
SqlCommand cmd = new SqlCommand();
|
|
cmd.CommandText = string.Format("UPDATE Error SET Deleted = 1 WHERE MessageHeaderId='{0}'", message.Id);
|
|
int queryResult = this.PerformNonQuery(cmd);
|
|
this.LogNonQueryResult(cmd.CommandText, queryResult);
|
|
}
|
|
|
|
public void DeleteMessageViolations(Message message)
|
|
{
|
|
SqlCommand cmd = new SqlCommand();
|
|
cmd.CommandText = string.Format("UPDATE Violation SET Deleted = 1 WHERE MessageHeaderId='{0}'", message.Id);
|
|
int queryResult = this.PerformNonQuery(cmd);
|
|
this.LogNonQueryResult(cmd.CommandText, queryResult);
|
|
}
|
|
|
|
public void Save(MessageCore core)
|
|
{
|
|
if (core.Customer != null)
|
|
{
|
|
SqlCommand cCmd = new SqlCommand();
|
|
core.Customer.PrepareSave(cCmd);
|
|
_log.DebugFormat("Saved Customer to Core: {0}", this.PerformNonQuery(cCmd));
|
|
core.CustomerId = core.Customer.Id;
|
|
}
|
|
|
|
SqlCommand cmd = new SqlCommand();
|
|
core.PrepareSave(cmd);
|
|
int queryResult = this.PerformNonQuery(cmd);
|
|
this.LogNonQueryResult(cmd.CommandText, queryResult);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region internal/private funcs
|
|
|
|
private void LogNonQueryResult(string query, int queryResult)
|
|
{
|
|
switch (queryResult)
|
|
{
|
|
case 0:
|
|
_log.DebugFormat("Query {0} affected no rows", query); break;
|
|
case -1:
|
|
_log.DebugFormat("Query {0} error occurred", query); break;
|
|
default:
|
|
_log.DebugFormat("Query {0} affected {1} rows", query, queryResult); break;
|
|
}
|
|
}
|
|
|
|
internal void LoadMessageDependencies(List<Message> messageList)
|
|
{
|
|
// Dictionary<Guid, MessageCore> messageCoreDict = this.GetToSendMessageCoreList();
|
|
Dictionary<Guid, ReportingParty> reportingPartyDict = this.GetReportingPartyDict();
|
|
|
|
// Zuordnung MessageCore,Zuordnung Reporting party
|
|
Message.AssignReportingParties(messageList, reportingPartyDict);
|
|
// Message.AssignMessageCores(messageList, messageCoreDict);
|
|
|
|
foreach (Message message in messageList)
|
|
{
|
|
this.LoadErrorList(message);
|
|
this.LoadViolationList(message);
|
|
|
|
SqlCommand cmd = new SqlCommand();
|
|
|
|
DatabaseEntity msgClass = DBManager.CreateMessage(message.MessageNotificationClass);
|
|
|
|
if (msgClass != null)
|
|
{
|
|
msgClass.PrepareLoadCommand(cmd, Message.LoadFilter.MESSAGEHEADER, message.Id);
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> statList = msgClass.LoadList(reader);
|
|
foreach (DatabaseEntity derivedMessage in statList)
|
|
{
|
|
message.Elements.Add(derivedMessage);
|
|
derivedMessage.MessageHeader = message;
|
|
this.LoadDependingLists(derivedMessage);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_log.DebugFormat("cannot create a message class for notification type {0}", message.MessageNotificationClass);
|
|
}
|
|
}
|
|
}
|
|
|
|
#region CreateMessage()
|
|
|
|
/// <summary>
|
|
/// factory method for messages by type
|
|
/// </summary>
|
|
internal static DatabaseEntity CreateMessage(Message.NotificationClass notificationClass)
|
|
{
|
|
DatabaseEntity result = null;
|
|
switch (notificationClass)
|
|
{
|
|
case Message.NotificationClass.NOA_NOD: result = new NOA_NOD(); break;
|
|
case Message.NotificationClass.ATA: result = new ATA(); break;
|
|
case Message.NotificationClass.ATD: result = new ATD(); break;
|
|
case Message.NotificationClass.SEC: result = new SEC(); break;
|
|
case Message.NotificationClass.POBA: result = new POBA(); break;
|
|
case Message.NotificationClass.POBD: result = new POBD(); break;
|
|
case Message.NotificationClass.NAME: result = new NAME(); break;
|
|
case Message.NotificationClass.TIEFA: result = new TIEFA(); break;
|
|
case Message.NotificationClass.TIEFD: result = new TIEFD(); break;
|
|
case Message.NotificationClass.BKRA: result = new BRKA(); break;
|
|
case Message.NotificationClass.BKRD: result = new BRKD(); break;
|
|
case Message.NotificationClass.STAT: result = new STAT(); break;
|
|
case Message.NotificationClass.LADG: result = new LADG(); break;
|
|
case Message.NotificationClass.INFO: result = new INFO(); break;
|
|
case Message.NotificationClass.SERV: result = new SERV(); break;
|
|
case Message.NotificationClass.PRE72H: result = new PRE72H(); break;
|
|
case Message.NotificationClass.MDH: result = new MDH(); break;
|
|
case Message.NotificationClass.WAS: result = new WAS(); break;
|
|
case Message.NotificationClass.CREW: result = new CREW(); break;
|
|
case Message.NotificationClass.PAS: result = new PAS(); break;
|
|
case Message.NotificationClass.BPOL: result = new BPOL(); break;
|
|
case Message.NotificationClass.TOWA: result = new TOWA(); break;
|
|
case Message.NotificationClass.TOWD: result = new TOWD(); break;
|
|
case Message.NotificationClass.HAZA: result = new HAZ(); break;
|
|
case Message.NotificationClass.HAZD: result = new HAZ(); ((HAZ)result).IsDeparture = true; break;
|
|
case Message.NotificationClass.AGNT: result = new AGNT(); break;
|
|
|
|
default:
|
|
break; // VISIT, TRANSIT
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Loads inner lists / collections
|
|
/// </summary>
|
|
internal void LoadDependingLists(DatabaseEntity databaseEntity)
|
|
{
|
|
SqlCommand cmd = new SqlCommand();
|
|
|
|
#region BPOL
|
|
|
|
if (databaseEntity.GetType().IsAssignableFrom(typeof(BPOL)))
|
|
{
|
|
BPOL bpol = databaseEntity as BPOL;
|
|
PortOfItinerary poi = new PortOfItinerary();
|
|
poi.PrepareLoadCommand(cmd, Message.LoadFilter.BPOL_ID, bpol.Id);
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> pois = poi.LoadList(reader);
|
|
foreach (PortOfItinerary aPoi in pois)
|
|
{
|
|
bpol.PortOfItineraries.Add(aPoi);
|
|
aPoi.BPOL = bpol;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region HAZ
|
|
|
|
if(databaseEntity.GetType().IsAssignableFrom(typeof(HAZ)))
|
|
{
|
|
HAZ haz = databaseEntity as HAZ;
|
|
|
|
// load all 5 position sublists
|
|
IMDGPosition imdg = new IMDGPosition();
|
|
imdg.PrepareLoadCommand(cmd, Message.LoadFilter.HAZ_ID, haz.Id);
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> imdgs = imdg.LoadList(reader);
|
|
foreach (IMDGPosition imdgPosition in imdgs)
|
|
{
|
|
haz.IMDGPositions.Add(imdgPosition);
|
|
imdgPosition.HAZ = haz;
|
|
|
|
// load SubsidiaryRisk sub-table
|
|
SqlCommand srCmd = new SqlCommand();
|
|
SubsidiaryRisks subsidiaryRisks = new SubsidiaryRisks();
|
|
subsidiaryRisks.PrepareLoadCommand(srCmd, Message.LoadFilter.IMDG_ID, imdgPosition.Id);
|
|
SqlDataReader srReader = this.PerformCommand(srCmd);
|
|
List<DatabaseEntity> sRisks = subsidiaryRisks.LoadList(srReader);
|
|
foreach (SubsidiaryRisks subsidiaryRisk in sRisks)
|
|
{
|
|
imdgPosition.SubsidiaryRiskList.Add(subsidiaryRisk);
|
|
subsidiaryRisk.IMDGPosition = imdgPosition;
|
|
}
|
|
}
|
|
|
|
cmd = new SqlCommand();
|
|
|
|
IBCPosition ibc = new IBCPosition();
|
|
ibc.PrepareLoadCommand(cmd, Message.LoadFilter.HAZ_ID, haz.Id);
|
|
reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> ibcs = ibc.LoadList(reader);
|
|
foreach (IBCPosition ibcPosition in ibcs)
|
|
{
|
|
haz.IBCPositions.Add(ibcPosition);
|
|
ibcPosition.HAZ = haz;
|
|
}
|
|
|
|
cmd = new SqlCommand();
|
|
|
|
IGCPosition igc = new IGCPosition();
|
|
igc.PrepareLoadCommand(cmd, Message.LoadFilter.HAZ_ID, haz.Id);
|
|
reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> igcs = igc.LoadList(reader);
|
|
foreach (IGCPosition igcPosition in igcs)
|
|
{
|
|
haz.IGCPositions.Add(igcPosition);
|
|
igcPosition.HAZ = haz;
|
|
}
|
|
|
|
cmd = new SqlCommand();
|
|
|
|
IMSBCPosition imsbc = new IMSBCPosition();
|
|
imsbc.PrepareLoadCommand(cmd, Message.LoadFilter.HAZ_ID, haz.Id);
|
|
reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> imsbcs = imsbc.LoadList(reader);
|
|
foreach (IMSBCPosition imsbcPosition in imsbcs)
|
|
{
|
|
haz.IMSBCPositions.Add(imsbcPosition);
|
|
imsbcPosition.HAZ = haz;
|
|
}
|
|
|
|
cmd = new SqlCommand();
|
|
MARPOL_Annex_I_Position marpol = new MARPOL_Annex_I_Position();
|
|
marpol.PrepareLoadCommand(cmd, Message.LoadFilter.HAZ_ID, haz.Id);
|
|
reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> marpols = marpol.LoadList(reader);
|
|
foreach (MARPOL_Annex_I_Position marpolPosition in marpols)
|
|
{
|
|
haz.MARPOLPositions.Add(marpolPosition);
|
|
marpolPosition.HAZ = haz;
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region MDH
|
|
|
|
if (databaseEntity.GetType().IsAssignableFrom(typeof(MDH)))
|
|
{
|
|
MDH mdh = databaseEntity as MDH;
|
|
|
|
PortOfCallLast30Days poc30 = new PortOfCallLast30Days();
|
|
poc30.PrepareLoadCommand(cmd, Message.LoadFilter.MDH_ID, mdh.Id);
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> poc30s = poc30.LoadList(reader);
|
|
foreach (PortOfCallLast30Days apoc30 in poc30s)
|
|
{
|
|
mdh.PortOfCallLast30Days.Add(apoc30);
|
|
apoc30.MDH = mdh;
|
|
this.LoadDependingLists(apoc30);
|
|
}
|
|
|
|
SanitaryMeasuresDetail smd = new SanitaryMeasuresDetail();
|
|
smd.PrepareLoadCommand(cmd, Message.LoadFilter.MDH_ID, mdh.Id);
|
|
reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> smds = smd.LoadList(reader);
|
|
foreach(SanitaryMeasuresDetail aSmd in smds)
|
|
{
|
|
mdh.SanitaryMeasuresDetails.Add(aSmd);
|
|
aSmd.MDH = mdh;
|
|
}
|
|
|
|
StowawaysJoiningLocation sjl = new StowawaysJoiningLocation();
|
|
sjl.PrepareLoadCommand(cmd, Message.LoadFilter.MDH_ID, mdh.Id);
|
|
reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> sjls = sjl.LoadList(reader);
|
|
foreach(StowawaysJoiningLocation aSjl in sjls)
|
|
{
|
|
mdh.StowawaysJoiningLocations.Add(aSjl);
|
|
aSjl.MDH = mdh;
|
|
}
|
|
|
|
InfectedArea ia = new InfectedArea();
|
|
ia.PrepareLoadCommand(cmd, Message.LoadFilter.MDH_ID, mdh.Id);
|
|
reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> ias = ia.LoadList(reader);
|
|
foreach(InfectedArea aIa in ias)
|
|
{
|
|
mdh.InfectedAreas.Add(aIa);
|
|
aIa.MDH = mdh;
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region PortOfCallLast30Days
|
|
|
|
if (databaseEntity.GetType().IsAssignableFrom(typeof(PortOfCallLast30Days)))
|
|
{
|
|
PortOfCallLast30Days poc30 = databaseEntity as PortOfCallLast30Days;
|
|
PortOfCallLast30DaysCrewJoinedShip poc30s = new PortOfCallLast30DaysCrewJoinedShip();
|
|
poc30s.PrepareLoadCommand(cmd, Message.LoadFilter.POC30_ID, poc30.Id);
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> poc30Names = poc30s.LoadList(reader);
|
|
foreach (PortOfCallLast30DaysCrewJoinedShip poc30Name in poc30Names)
|
|
{
|
|
poc30.CrewJoinedShip.Add(poc30Name);
|
|
poc30Name.PortOfCallLast30Days = poc30;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region SEC
|
|
|
|
if (databaseEntity.GetType().IsAssignableFrom(typeof(SEC)))
|
|
{
|
|
SEC sec = databaseEntity as SEC;
|
|
LastTenPortFacilitiesCalled ltp = new LastTenPortFacilitiesCalled();
|
|
ltp.PrepareLoadCommand(cmd, Message.LoadFilter.SEC_ID, sec.Id);
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> ltps = ltp.LoadList(reader);
|
|
foreach (LastTenPortFacilitiesCalled altp in ltps)
|
|
{
|
|
sec.LastTenPortFacilitesCalled.Add(altp);
|
|
altp.SEC = sec;
|
|
}
|
|
|
|
cmd = new SqlCommand();
|
|
ShipToShipActivitiesDuringLastTenPortFacilitiesCalled sts = new ShipToShipActivitiesDuringLastTenPortFacilitiesCalled();
|
|
sts.PrepareLoadCommand(cmd, Message.LoadFilter.SEC_ID, sec.Id);
|
|
reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> stss = sts.LoadList(reader);
|
|
foreach (ShipToShipActivitiesDuringLastTenPortFacilitiesCalled asts in stss)
|
|
{
|
|
sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Add(asts);
|
|
asts.SEC = sec;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region WAS
|
|
|
|
if(databaseEntity.GetType().IsAssignableFrom(typeof(WAS)))
|
|
{
|
|
WAS was = databaseEntity as WAS;
|
|
|
|
WasteDisposalServiceProvider wdsp = new WasteDisposalServiceProvider();
|
|
wdsp.PrepareLoadCommand(cmd, Message.LoadFilter.WAS_ID, was.Id);
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> wdsps = wdsp.LoadList(reader);
|
|
foreach (WasteDisposalServiceProvider awdsp in wdsps)
|
|
{
|
|
was.WasteDisposalServiceProvider.Add(awdsp);
|
|
awdsp.WAS = was;
|
|
this.LoadDependingLists(awdsp);
|
|
}
|
|
|
|
Waste waste = new Waste();
|
|
waste.PrepareLoadCommand(cmd, Message.LoadFilter.WAS_ID, was.Id);
|
|
SqlDataReader reader2 = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> wastes = waste.LoadList(reader2);
|
|
foreach (Waste aWaste in wastes)
|
|
{
|
|
was.Waste.Add(aWaste);
|
|
aWaste.WAS = was;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region NOA_NOD
|
|
|
|
if (databaseEntity.GetType().IsAssignableFrom(typeof(NOA_NOD)))
|
|
{
|
|
NOA_NOD noa_nod = databaseEntity as NOA_NOD;
|
|
|
|
CallPurpose cp = new CallPurpose();
|
|
cp.PrepareLoadCommand(cmd, Message.LoadFilter.NOA_NODID, noa_nod.Id);
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> cps = cp.LoadList(reader);
|
|
foreach (CallPurpose callPurpose in cps)
|
|
{
|
|
noa_nod.CallPurposes.Add(callPurpose);
|
|
callPurpose.NOA_NOD = noa_nod;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region WasteDisposalServiceProvider
|
|
|
|
if (databaseEntity.GetType().IsAssignableFrom(typeof(WasteDisposalServiceProvider)))
|
|
{
|
|
WasteDisposalServiceProvider wdsp = databaseEntity as WasteDisposalServiceProvider;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
internal void LoadErrorList(Message message)
|
|
{
|
|
MessageError aMessageError = new MessageError();
|
|
SqlCommand cmd = new SqlCommand();
|
|
aMessageError.PrepareLoadCommand(cmd, Message.LoadFilter.MESSAGEHEADER, message.Id);
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> errorList = aMessageError.LoadList(reader);
|
|
foreach (MessageError error in errorList)
|
|
message.ErrorList.Add(error);
|
|
}
|
|
|
|
internal void LoadViolationList(Message message)
|
|
{
|
|
MessageViolation aMessageViolation = new MessageViolation();
|
|
SqlCommand cmd = new SqlCommand();
|
|
aMessageViolation.PrepareLoadCommand(cmd, Message.LoadFilter.MESSAGEHEADER, message.Id);
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
List<DatabaseEntity> violationList = aMessageViolation.LoadList(reader);
|
|
foreach (MessageViolation violation in violationList)
|
|
message.ViolationList.Add(violation);
|
|
}
|
|
|
|
internal void LoadCustomer(MessageCore core)
|
|
{
|
|
if (core.CustomerId.HasValue)
|
|
{
|
|
Customer c = new Customer();
|
|
SqlCommand cCmd = new SqlCommand();
|
|
c.PrepareLoadCommand(cCmd, Message.LoadFilter.BY_ID, core.CustomerId.Value);
|
|
SqlDataReader reader = this.PerformCommand(cCmd);
|
|
List<DatabaseEntity> customers = c.LoadList(reader);
|
|
if (customers.Count > 0)
|
|
core.Customer = customers[0] as Customer;
|
|
}
|
|
}
|
|
|
|
internal Dictionary<int, string> LoadErrorTexts()
|
|
{
|
|
SqlCommand cmd = new SqlCommand();
|
|
cmd.CommandText = "SELECT ErrorCode, ErrorText FROM ErrorText";
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
Dictionary<int, string> result = new Dictionary<int, string>();
|
|
while (reader.Read())
|
|
{
|
|
int errorCode = reader.GetInt32(0);
|
|
string errorText = reader.GetString(1);
|
|
result[errorCode] = errorText;
|
|
}
|
|
reader.Close();
|
|
return result;
|
|
}
|
|
|
|
internal Dictionary<int, string> LoadViolationTexts()
|
|
{
|
|
SqlCommand cmd = new SqlCommand();
|
|
cmd.CommandText = "SELECT ViolationCode, ViolationText FROM ViolationText";
|
|
SqlDataReader reader = this.PerformCommand(cmd);
|
|
Dictionary<int, string> result = new Dictionary<int, string>();
|
|
while (reader.Read())
|
|
{
|
|
int violationCode = reader.GetInt32(0);
|
|
string violationText = reader.GetString(1);
|
|
result[violationCode] = violationText;
|
|
}
|
|
reader.Close();
|
|
return result;
|
|
}
|
|
|
|
#region DB access methods
|
|
|
|
internal SqlDataReader PerformCommand(SqlCommand cmd)
|
|
{
|
|
try
|
|
{
|
|
cmd.Connection = this._con;
|
|
// Stopwatch sw = new Stopwatch();
|
|
// sw.Start();
|
|
SqlDataReader reader = cmd.ExecuteReader();
|
|
// sw.Stop();
|
|
// _log.DebugFormat("{1}ms: {0}", cmd.CommandText, sw.ElapsedMilliseconds);
|
|
return reader;
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
System.Diagnostics.Trace.WriteLine("SQL Exception:" + ex.Message);
|
|
_log.Error("Error performing command", ex);
|
|
_log.DebugFormat("Query: {0}", cmd.CommandText);
|
|
_log.Debug("Parameters:");
|
|
for (int i = 0; i < cmd.Parameters.Count; i++)
|
|
{
|
|
_log.DebugFormat("{0}:{1}", cmd.Parameters[i].ParameterName, cmd.Parameters[i].Value);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
internal int PerformNonQuery(SqlCommand cmd)
|
|
{
|
|
try
|
|
{
|
|
cmd.Connection = this._con;
|
|
return cmd.ExecuteNonQuery();
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
System.Diagnostics.Trace.WriteLine("SQL Exception:" + ex.Message);
|
|
_log.Error("Error performing command", ex);
|
|
_log.DebugFormat("Query: {0}", cmd.CommandText);
|
|
_log.Debug("Parameters:");
|
|
for (int i = 0; i < cmd.Parameters.Count; i++)
|
|
{
|
|
_log.DebugFormat("{0}:{1}", cmd.Parameters[i].ParameterName, cmd.Parameters[i].Value);
|
|
}
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|