Files
DP44/Common/DTS.Common.ISO/MMETransducerMainLocation.cs
2026-04-17 14:55:32 -04:00

348 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Data.SqlClient;
using System.Xml;
using DTS.Common.Enums.DBExport;
using DTS.Common.Storage;
using DTS.Common.Utilities.Logging;
namespace DTS.Common.ISO
{
public class MMETransducerMainLocation : AbstractOLEDbWrapper
{
public string S_GUID { get; }
public string Type { get; }
public string Trans_Main_Loc { get; }
public string Text_L1 { get; }
public string Text_L2 { get; }
public long Version { get; }
public DateTime Date { get; }
public string Remarks { get; }
public bool Expired { get; }
public string SortKey { get; }
public string Picture_ShortName { get; }
public DateTime Last_Change { get; }
public string Last_Change_Text { get; }
public string History { get; }
public MMEPossibleChannels.MMEChannelTypes RecordType { get; }
/// <summary>
/// imports a singular transducer main location
/// </summary>
/// <param name="node"></param>
/// <param name="setStatus"></param>
/// <param name="page"></param>
public static MMETransducerMainLocation ReadXML(XmlElement node)
{
var fields = Enum.GetValues(typeof(MainLocationFields)).Cast<MainLocationFields>().ToArray();
string sGuid = "",
type = "",
transMainLoc = "",
textL1 = "",
textL2 = "",
remarks = "",
sortkey = "",
pictureShortName = "",
lastChangeText = "",
history = "";
long version = 0;
var expired = false;
DateTime date = DateTime.Now, lastChange = DateTime.Now;
foreach (var field in fields)
{
switch (field)
{
case MainLocationFields.Date:
date = DateTime.Parse(node.GetAttribute(field.ToString()),
System.Globalization.CultureInfo.InvariantCulture);
break;
case MainLocationFields.Expired:
expired = Convert.ToBoolean(node.GetAttribute(field.ToString()));
break;
case MainLocationFields.History:
history = node.GetAttribute(field.ToString());
break;
case MainLocationFields.Last_Change:
lastChange = DateTime.Parse(node.GetAttribute(field.ToString()),
System.Globalization.CultureInfo.InvariantCulture);
break;
case MainLocationFields.Last_Change_Text:
lastChangeText = node.GetAttribute(field.ToString());
break;
case MainLocationFields.Picture_ShortName:
pictureShortName = node.GetAttribute(field.ToString());
break;
case MainLocationFields.Remarks:
remarks = node.GetAttribute(field.ToString());
break;
case MainLocationFields.S_GUID:
sGuid = node.GetAttribute(field.ToString());
break;
case MainLocationFields.SortKey:
sortkey = node.GetAttribute(field.ToString());
break;
case MainLocationFields.Text_L1:
textL1 = node.GetAttribute(field.ToString());
break;
case MainLocationFields.Text_L2:
textL2 = node.GetAttribute(field.ToString());
break;
case MainLocationFields.Trans_Main_Loc:
transMainLoc = node.GetAttribute(field.ToString());
break;
case MainLocationFields.Type:
type = node.GetAttribute(field.ToString());
break;
case MainLocationFields.Version:
version = long.Parse(node.GetAttribute(field.ToString()),
System.Globalization.CultureInfo.InvariantCulture);
break;
default:
throw new NotSupportedException("ImportTestSetup::ImportCustomMainLoc unsupported field: " +
field.ToString());
}
}
return new MMETransducerMainLocation(sGuid, type, transMainLoc, textL1, textL2, version, date, remarks,
expired, sortkey, pictureShortName, lastChange, lastChangeText, history,
MMEPossibleChannels.MMEChannelTypes.SQL);
}
public static void WriteXML(ref XmlWriter writer, MMETransducerMainLocation[] locs)
{
writer.WriteStartElement(TopLevelFields.CustomMainLocs.ToString());
foreach (var main in locs)
{
writer.Flush();
main.WriteXML(ref writer);
writer.Flush();
}
writer.WriteEndElement();
}
/// <summary>
/// writes an ISODll.MMETransducerMainLocation to xml
/// </summary>
/// <param name="m"></param>
/// <param name="writer"></param>
public void WriteXML(ref XmlWriter writer)
{
writer.WriteStartElement("TransducerMainLocation");
var fields = Enum.GetValues(typeof(MainLocationFields)).Cast<MainLocationFields>().ToArray();
foreach (var field in fields)
{
var sVal = "";
switch (field)
{
case MainLocationFields.Date: sVal = Date.ToString(System.Globalization.CultureInfo.InvariantCulture); break;
case MainLocationFields.Expired: sVal = Expired.ToString(); break;
case MainLocationFields.History: sVal = History; break;
case MainLocationFields.Last_Change: sVal = Last_Change.ToString(System.Globalization.CultureInfo.InvariantCulture); break;
case MainLocationFields.Last_Change_Text: sVal = Last_Change_Text; break;
case MainLocationFields.Picture_ShortName: sVal = Picture_ShortName; break;
case MainLocationFields.Remarks: sVal = Remarks; break;
case MainLocationFields.S_GUID: sVal = S_GUID; break;
case MainLocationFields.SortKey: sVal = SortKey; break;
case MainLocationFields.Text_L1: sVal = Text_L1; break;
case MainLocationFields.Text_L2: sVal = Text_L2; break;
case MainLocationFields.Trans_Main_Loc: sVal = Trans_Main_Loc; break;
case MainLocationFields.Type: sVal = Type; break;
case MainLocationFields.Version: sVal = Version.ToString(System.Globalization.CultureInfo.InvariantCulture); break;
default: throw new NotSupportedException("ExportTestSetup::WriteMainLoc unsupported field: " + field.ToString());
}
writer.WriteAttributeString(field.ToString(), sVal);
}
writer.WriteEndElement();
}
public MMETransducerMainLocation(string textL1)
{
Text_L1 = textL1;
}
public MMETransducerMainLocation(string sGuid, string type, string transMainLoc, string textL1, string textL2,
long version, DateTime date, string remarks, bool expired, string sortkey, string pictureShortName,
DateTime lastChange, string lastChangeText, string history, MMEPossibleChannels.MMEChannelTypes recordType)
{
RecordType = recordType;
S_GUID = sGuid;
Type = type;
Trans_Main_Loc = transMainLoc;
Text_L1 = textL1;
Text_L2 = textL2;
Version = version;
Date = date;
Remarks = remarks;
Expired = expired;
SortKey = sortkey;
Picture_ShortName = pictureShortName;
Last_Change = lastChange;
Last_Change_Text = lastChangeText;
History = history;
}
public static void DeleteTransducerMainLocations()
{
try
{
using (var cmd = DbOperations.GetSQLCommand(true))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = DbOperationsEnum.StoredProcedure.sp_MMEMainLocationsDelete.ToString();
cmd.Parameters.Add(new SqlParameter("@s_GUID", SqlDbType.UniqueIdentifier) { Value = null });
var errorNumberParam =
new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(errorNumberParam);
var errorMessageParam =
new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250)
{
Direction = ParameterDirection.Output
};
cmd.Parameters.Add(errorMessageParam);
cmd.ExecuteNonQuery();
if (int.Parse(errorNumberParam.Value.ToString()) != 0)
{
//errorMessageParam.Value
}
}
finally
{
cmd.Connection.Dispose();
}
}
}
catch (Exception ex) { APILogger.Log("failed to delete main locations, ", ex); }
}
public static MMETransducerMainLocation[] GetTransducerMainLocations()
{
var transducerMainLocations = new List<MMETransducerMainLocation>();
using (var cmd = DbOperations.GetISOCommand())
{
cmd.CommandText = "SELECT * FROM MMEMainLocations";
cmd.CommandType = CommandType.Text;
try
{
using (var ISOReader = cmd.ExecuteReader())
{
while (ISOReader.Read())
{
try
{
string sGuid = ISOReader["s_GUID"].ToString();
string type = ISOReader["TYPE"].ToString();
string transMainLoc = ISOReader["TRANS_MAIN_LOC"].ToString();
string textL1 = ISOReader["TEXT_L1"].ToString();
string textL2 = ISOReader["TEXT_L2"].ToString();
long version = GetLong(ISOReader, "VERSION");
DateTime date = GetDate(ISOReader, "DATE");
string remarks = ISOReader["REAMRKS"].ToString();
bool expired = (bool)ISOReader["EXPIRED"];
string sortkey = ISOReader["SORTKEY"].ToString();
string pictureShortName = ISOReader["PICTURE_SHORTNAME"].ToString();
DateTime lastChange = GetDate(ISOReader, "LAST_CHANGE");
string lastChangeText = ISOReader["LAST_CHANGE_TEXT"].ToString();
string history = ISOReader["HISTORY"].ToString();
transducerMainLocations.Add(new MMETransducerMainLocation(sGuid, type, transMainLoc,
textL1, textL2, version,
date, remarks, expired, sortkey, pictureShortName, lastChange, lastChangeText,
history, MMEPossibleChannels.MMEChannelTypes.ISO13499_106));
}
catch (Exception ex)
{
APILogger.Log("Failed to process main location", ex);
}
}
}
}
finally
{
cmd.Connection.Dispose();
}
}
return transducerMainLocations.ToArray();
}
public void Commit()
{
using (var cmd = DbOperations.GetSQLCommand(true))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = DbOperationsEnum.StoredProcedure.sp_MMEMainLocationsUpdateInsert.ToString();
#region params
cmd.Parameters.Add(
new SqlParameter("@s_GUID", SqlDbType.UniqueIdentifier) { Value = Guid.Parse(S_GUID) });
cmd.Parameters.Add(new SqlParameter("@TYPE", SqlDbType.NVarChar, 50) { Value = Type });
cmd.Parameters.Add(
new SqlParameter("@TRANS_MAIN_LOC", SqlDbType.NVarChar, 50) { Value = Trans_Main_Loc });
cmd.Parameters.Add(new SqlParameter("@TEXT_L1", SqlDbType.NVarChar, 50) { Value = Text_L1 });
cmd.Parameters.Add(new SqlParameter("@TEXT_L2", SqlDbType.NVarChar, 50) { Value = Text_L1 });
cmd.Parameters.Add(new SqlParameter("@VERSION", SqlDbType.Int) { Value = Version });
cmd.Parameters.Add(new SqlParameter("@DATE", SqlDbType.DateTime) { Value = Date });
cmd.Parameters.Add(new SqlParameter("@REMARKS", SqlDbType.NVarChar, 50) { Value = Remarks });
cmd.Parameters.Add(new SqlParameter("@EXPIRED", SqlDbType.Bit) { Value = Expired });
cmd.Parameters.Add(new SqlParameter("@SORTKEY", SqlDbType.NVarChar, 50) { Value = SortKey });
cmd.Parameters.Add(
new SqlParameter("@PICTURE_SHORTNAME", SqlDbType.NVarChar, 50) { Value = Picture_ShortName });
cmd.Parameters.Add(new SqlParameter("@LAST_CHANGE", SqlDbType.DateTime) { Value = Last_Change });
cmd.Parameters.Add(
new SqlParameter("@LAST_CHANGE_TEXT", SqlDbType.NVarChar, 50) { Value = Last_Change_Text });
cmd.Parameters.Add(new SqlParameter("@HISTORY", SqlDbType.NVarChar, 50) { Value = History });
var errorNumberParam =
new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(errorNumberParam);
var errorMessageParam =
new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250)
{
Direction = ParameterDirection.Output
};
cmd.Parameters.Add(errorMessageParam);
#endregion params
cmd.ExecuteNonQuery();
if (int.Parse(errorNumberParam.Value.ToString()) != 0)
{
//errorMessageParam.Value
}
}
finally
{
cmd.Connection.Dispose();
}
}
}
}
}