git_bsmd/nsw/Source/bsmd.database/DBManager.cs

1036 lines
41 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;
private object _lock = new object();
#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();
}
public bool IsConnected
{
get { return (this._con != null) && (this._con.State == ConnectionState.Open); }
}
#endregion
#region public helper funcs
public List<MessageCore> GetMessageCoresForExcelCreate()
{
MessageCore aMessageCore = new MessageCore();
SqlCommand cmd = new SqlCommand();
aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.CREATE_EXCEL);
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> 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;
}
public List<MessageCore> GetMessageCoresWithNSWStatusFlag()
{
MessageCore aMessageCore = new MessageCore();
SqlCommand cmd = new SqlCommand();
aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.QUERY_NSW_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> GetMessageCoresWithFilters(Dictionary<MessageCore.SearchFilterType, string> filters)
{
MessageCore aMessageCore = new MessageCore();
SqlCommand cmd = new SqlCommand();
aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.SEARCH_CORE_FILTERS, filters);
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);
this.LoadSTATShipName(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 string GetShipNameFromCore(MessageCore core)
{
foreach(Message message in this.GetMessagesForCore(core, MessageLoad.ALL))
{
if (message.MessageNotificationClass == Message.NotificationClass.STAT)
{
if (message.Elements.Count > 0)
{
STAT stat = message.Elements[0] as STAT;
if (stat != null)
return stat.ShipName;
}
break;
}
}
return null;
}
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 DeleteCore(MessageCore messageCore)
{
if(messageCore == null) return;
foreach(Message message in this.GetMessagesForCore(messageCore, MessageLoad.ALL))
{
if (message is ISublistContainer)
{
((ISublistContainer)message).DeleteElements();
}
this.Delete(message);
}
this.Delete(messageCore);
}
public void Save(DatabaseEntity entity)
{
List<string> truncatedFields = new List<string>();
entity.TruncateFields(truncatedFields);
foreach(string truncatedField in truncatedFields)
{
_log.WarnFormat("Entity {0} save: Field truncated: {1}", entity.GetType(), truncatedField);
}
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);
}
public bool? GetMessageCoreQueryStatusFlag(Guid messageCoreId)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = string.Format("SELECT QueryNSWStatus FROM MessageCore WHERE Id = '{0}'", messageCoreId);
return this.PerformReadFlagQuery(cmd);
}
#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);
if (reader != null)
{
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;
case Message.NotificationClass.STO: result = new STO(); break;
case Message.NotificationClass.CREWD: result = new CREWD(); break;
case Message.NotificationClass.PASD: result = new PASD(); 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 void LoadSTATShipName(MessageCore core)
{
if (core == null) return;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT STAT.ShipName FROM STAT JOIN MessageHeader ON STAT.MessageHeaderId = MessageHeader.Id WHERE MessageHeader.MessageCoreId = @ID";
cmd.Parameters.AddWithValue("@ID", core.Id);
SqlDataReader reader = this.PerformCommand(cmd);
if(reader.Read())
{
if (reader.IsDBNull(0))
core.Shipname = "";
else
core.Shipname = reader.GetString(0);
}
reader.Close();
}
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)
{
SqlDataReader reader = null;
lock (this._lock)
{
try
{
cmd.Connection = this._con;
// Stopwatch sw = new Stopwatch();
// sw.Start();
reader = cmd.ExecuteReader();
// sw.Stop();
// _log.DebugFormat("{1}ms: {0}", cmd.CommandText, sw.ElapsedMilliseconds);
}
catch (SqlException ex)
{
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 reader;
}
internal int PerformNonQuery(SqlCommand cmd)
{
int result = -1;
lock (this._lock)
{
try
{
cmd.Connection = this._con;
result = 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 result;
}
internal bool? PerformReadFlagQuery(SqlCommand cmd)
{
bool? result = null;
lock (this._lock)
{
try
{
cmd.Connection = this._con;
result = (bool?)cmd.ExecuteScalar();
}
catch (SqlException ex)
{
_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 result;
}
#endregion
#endregion
}
}