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 MMEPositions : AbstractOLEDbWrapper { public string S_GUID { get; } public string Position { 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 DateTime Last_Change { get; } public string Last_Change_Text { get; } public string History { get; } public MMEPossibleChannels.MMEChannelTypes RecordType { get; } = MMEPossibleChannels.MMEChannelTypes.ISO13499_106; public MMEPositions(string sGuid, string position, string textL1, string textL2, long version, DateTime date, string remarks, bool expired, string sortKey, DateTime lastChange, string lastChangeText, string history, MMEPossibleChannels.MMEChannelTypes type) { RecordType = type; S_GUID = sGuid; Position = position; Text_L1 = textL1; Text_L2 = textL2; Version = version; Date = date; Remarks = remarks; Expired = expired; SortKey = sortKey; Last_Change = lastChange; Last_Change_Text = lastChangeText; History = history; } public static MMEPositions[] GetPositions() { var positions = new List(); using (var cmd = DbOperations.GetISOCommand()) { cmd.CommandText = "SELECT * FROM MMEPositions"; cmd.CommandType = CommandType.Text; try { using (var ISOReader = cmd.ExecuteReader()) { while (ISOReader.Read()) { try { string sGuid = ISOReader["s_GUID"].ToString(); string position = ISOReader["POSITION"].ToString(); string textL1 = ISOReader["TEXT_L1"].ToString(); string textL2 = ISOReader["TEXT_L2"].ToString(); long version = GetLong(ISOReader, "VERSION"); DateTime date = (DateTime)ISOReader["DATE"]; string remarks = ISOReader["REMARKS"].ToString(); bool expired = (bool)ISOReader["EXPIRED"]; string sortkey = ISOReader["SORTKEY"].ToString(); DateTime lastChange = GetDate(ISOReader, "LAST_CHANGE"); string lastChangeText = ISOReader["LAST_CHANGE_TEXT"].ToString(); string history = ISOReader["HISTORY"].ToString(); positions.Add(new MMEPositions(sGuid, position, textL1, textL2, version, date, remarks, expired, sortkey, lastChange, lastChangeText, history, MMEPossibleChannels.MMEChannelTypes.ISO13499_106)); } catch (Exception ex) { APILogger.Log("Failed to process position", ex); } } } } finally { cmd.Connection.Dispose(); } } return positions.ToArray(); } /// /// imports a singular position /// /// /// /// public static MMEPositions ReadXML(XmlElement node) { var fields = Enum.GetValues(typeof(PositionFields)).Cast().ToArray(); string sGuid = "", position = "", textL1 = "", textL2 = "", remarks = "", sortKey = "", lastChangeText = "", history = ""; long version = 0; var expired = false; DateTime date = DateTime.Now, lastChange = DateTime.Now; foreach (var field in fields) { switch (field) { case PositionFields.Date: date = DateTime.Parse(node.GetAttribute(field.ToString()), System.Globalization.CultureInfo.InvariantCulture); break; case PositionFields.Expired: expired = Convert.ToBoolean(node.GetAttribute(field.ToString())); break; case PositionFields.History: history = node.GetAttribute(field.ToString()); break; case PositionFields.Last_Change: lastChange = DateTime.Parse(node.GetAttribute(field.ToString()), System.Globalization.CultureInfo.InvariantCulture); break; case PositionFields.Last_Change_Text: lastChangeText = node.GetAttribute(field.ToString()); break; case PositionFields.Position: position = node.GetAttribute(field.ToString()); break; case PositionFields.RecordType: break; case PositionFields.Remarks: remarks = node.GetAttribute(field.ToString()); break; case PositionFields.S_GUID: sGuid = node.GetAttribute(field.ToString()); break; case PositionFields.SortKey: sortKey = node.GetAttribute(field.ToString()); break; case PositionFields.Text_L1: textL1 = node.GetAttribute(field.ToString()); break; case PositionFields.Text_L2: textL2 = node.GetAttribute(field.ToString()); break; case PositionFields.Version: version = long.Parse(node.GetAttribute(field.ToString()), System.Globalization.CultureInfo.InvariantCulture); break; default: throw new NotSupportedException("ImportTestSetup::ImportCustomPosition unsupported field: " + field.ToString()); } } return new MMEPositions(sGuid, position, textL1, textL2, version, date, remarks, expired, sortKey, lastChange, lastChangeText, history, MMEPossibleChannels.MMEChannelTypes.SQL); } } }