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

164 lines
5.7 KiB
C#

//
// Class: Util
// Current CLR: 4.0.30319.34209
// System: Microsoft Visual Studio 10.0
// Author: dani
// Created: 3/21/2015 10:36:56 AM
//
// Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved.
using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace bsmd.database
{
public static class Util
{
private static Regex regexVisit = new Regex("(DE)([A-Z]{3})-([0-9]{4})-([A-Z]{6})", RegexOptions.IgnoreCase);
private static Regex regexTransit = new Regex("(ZZNOK)-([0-9]{4})-([A-Z]{6})", RegexOptions.IgnoreCase);
/// <summary>
/// Extension helper to add values that can be null:
/// http://stackoverflow.com/questions/13451085/exception-when-addwithvalue-parameter-is-null
/// </summary>
public static SqlParameter AddWithNullableValue(this SqlParameterCollection collection, string parameterName, object value)
{
if (value == null)
return collection.AddWithValue(parameterName, DBNull.Value);
else
return collection.AddWithValue(parameterName, value);
}
public static string GetGenderDisplay(byte? val)
{
if (val.HasValue)
{
switch (val)
{
case 0: return "Male";
case 1: return "Female";
default: return "Other";
}
}
else
{
return "Unknown";
}
}
public static string GetIdentityDocumentTypeDisplay(byte? val)
{
if(val.HasValue)
{
switch (val)
{
case 0: return "Identity card";
case 1: return "Passport";
case 2: return "Muster book";
case 3: return "Picture ID";
case 4: return "Residantal permit";
default: return "Other legal identity document";
}
}
else
{
return "Unknown";
}
}
public static string GetISSCTypeDisplay(byte? val)
{
if (val.HasValue)
{
switch(val)
{
case 0: return "FULL";
case 1: return "INTERIM";
default:
return "UNKNOWN";
}
}
else
{
return "Unknown";
}
}
public static string GetISSCIssuerTypeDisplay(byte? val)
{
if (val.HasValue)
{
switch (val)
{
case 0: return "ADMINISTRATION";
case 1: return "RSO";
default:
return "UNKNOWN";
}
}
else
{
return "Unknown";
}
}
public static bool IsVisitId(string val)
{
if (val.IsNullOrEmpty()) return false;
return regexVisit.IsMatch(val);
}
public static bool IsTransitId(string val)
{
if (val.IsNullOrEmpty()) return false;
return regexTransit.IsMatch(val);
}
/// <summary>
/// Hilfsfunktion für "manuelle" Anlage eines Schiffsanlaufs. Die Objekte sind bereits gespeichert.
/// </summary>
public static List<Message> CreateMessagesForCore(MessageCore core)
{
List<Message> result = new List<Message>();
bool isDE, isDK;
if(core != null)
{
isDE = core.PoC.Equals("ZZNOK") || core.PoC.StartsWith("DE");
isDK = core.PoC.StartsWith("DK");
foreach (Message.NotificationClass notificationClass in Enum.GetValues(typeof(Message.NotificationClass)))
{
if(isDE)
{
if ((notificationClass == Message.NotificationClass.CREWD) ||
(notificationClass == Message.NotificationClass.PASD) ||
(notificationClass == Message.NotificationClass.STO)) continue;
}
if(isDK)
{
// gibt es hier etwas, das nicht gebraucht wird? (siehe Mail von Christin, 29.5.17
if ((notificationClass == Message.NotificationClass.MDH) ||
(notificationClass == Message.NotificationClass.BKRA) ||
(notificationClass == Message.NotificationClass.BKRD) ||
(notificationClass == Message.NotificationClass.TOWA) ||
(notificationClass == Message.NotificationClass.TOWD)) continue;
}
Message message = new Message();
message.MessageCoreId = core.Id;
message.MessageNotificationClass = notificationClass;
DatabaseEntity classElement = DBManager.CreateMessage(notificationClass);
DBManager.Instance.Save(classElement);
message.Elements.Add(classElement);
message.SaveElements();
result.Add(message);
}
}
return result;
}
}
}