// Copyright (c) 2015-2017 schick Informatik // Description: using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Windows.Media; namespace bsmd.database { public class MessageHistory : IDatabaseEntity { private const string tableName = "[MessageHistory]"; #region Properties public string Tablename { get { return tableName; } } public Guid? Id { get; set; } public Guid? ReportingPartyId { get; set; } public ReportingParty CreatedBy { get; set; } public Guid EntityId { get; internal set; } public string EntityType { get; internal set; } public string EntityName { get; internal set; } public string EntityValues { get; internal set; } public DateTime Created { get; private set; } public int Identifier { get; set; } /// /// Helper property for display grid /// public Brush GroupColorBrush { get; set; } /// /// ENI Grid helper property /// public string ReportingPartyName { get { Dictionary repPartyDict = DBManager.Instance.GetReportingPartyDict(); if (ReportingPartyId.HasValue && repPartyDict.ContainsKey(ReportingPartyId.Value)) return repPartyDict[ReportingPartyId.Value].Logon; return ""; } } #endregion #region public methods internal static string GetLoadCommand() { return "SELECT Id, ReportingPartyId, EntityId, EntityType, EntityName, EntityValues, Timestamp, Identifier FROM MessageHistory WHERE EntityId = @ENTITYID ORDER BY Timestamp, Identifier"; } public object CreateObjectFromValues() { if(!this.EntityType.IsNullOrEmpty()) { Type objectType = Type.GetType(this.EntityType); if(objectType != null) return JsonConvert.DeserializeObject(this.EntityValues, objectType); } return null; } /// /// Liefert alle History-Elemente zu dem Parameter Bezugselement, wenn es ISublistElement Objekte sind /// Die Elemente werden über den Index gefunden.. also alles was von 1..n geht in dem Abschnitt, in dem der Benutzer /// einen Eintrag ausgewählt hat. /// /// /// public List GetGroup(List allHistories) { LinkedList tmpResult = new LinkedList(); DatabaseEntity refEntity = this.CreateObjectFromValues() as DatabaseEntity; if (refEntity is ISublistElement sublistElement) { List tmpList = new List(allHistories); // Alle History-Elemente einer anderen Meldeklasse ausfiltern tmpList.RemoveAll(aMessageHistory => !aMessageHistory.EntityId.Equals(this.EntityId)); int selIndex = tmpList.IndexOf(this); tmpResult.AddLast(refEntity); // Elemente vor dem ausgewählten Element der Linked List hinzufügen int indexIdentifier = Util.GetNumericIdentifier(sublistElement).Value; int refIdentifier = indexIdentifier; for (int tmpIndex = selIndex - 1; tmpIndex >= 0; tmpIndex--) { DatabaseEntity prevEntity = tmpList[tmpIndex].CreateObjectFromValues() as DatabaseEntity; int nextIdentifier = Util.GetNumericIdentifier((ISublistElement)prevEntity).Value; if (nextIdentifier >= indexIdentifier) break; indexIdentifier = nextIdentifier; tmpResult.AddFirst(prevEntity); } // Elemente nach dem ausgewählten Element hinzufügen (wenn sie zur gleichen Gruppe gehören) if ((tmpList.Count - 1) > selIndex) { indexIdentifier = refIdentifier; for (int tmpIndex = selIndex + 2; tmpIndex < tmpList.Count; tmpIndex++) { DatabaseEntity nextEntity = tmpList[tmpIndex].CreateObjectFromValues() as DatabaseEntity; int nextIdentifier = Util.GetNumericIdentifier((ISublistElement)nextEntity).Value; if (nextIdentifier <= indexIdentifier) break; indexIdentifier = nextIdentifier; tmpResult.AddLast(nextEntity); } } } return new List(tmpResult); } #endregion #region IDatabaseEntity implementation public void PrepareSave(IDbCommand cmd) { SqlCommand scmd = cmd as SqlCommand; // es gibt nur insert this.Id = Guid.NewGuid(); scmd.Parameters.AddWithValue("@ID", this.Id); scmd.Parameters.AddWithNullableValue("@P1", this.ReportingPartyId); scmd.Parameters.AddWithValue("@P2", this.EntityId); scmd.Parameters.AddWithValue("@P3", this.EntityType); scmd.Parameters.AddWithValue("@P4", this.EntityName); scmd.Parameters.AddWithValue("@P5", this.EntityValues); scmd.Parameters.AddWithValue("@P6", DateTime.Now); scmd.Parameters.AddWithValue("@P7", this.Identifier); cmd.CommandText = string.Format("INSERT INTO {0} (Id, ReportingPartyId, EntityId, EntityType, " + "EntityName, EntityValues, Timestamp, Identifier) VALUES (@ID, @P1, @P2, @P3, @P4, @P5, @P6, @P7)", this.Tablename); } public void PrepareDelete(IDbCommand cmd) { SqlCommand scmd = cmd as SqlCommand; scmd.CommandText = string.Format("DELETE FROM {0} WHERE Id = @ID", Tablename); scmd.Parameters.AddWithValue("@ID", this.Id); } public static List LoadList(IDataReader reader) { List result = new List(); while (reader.Read()) { MessageHistory mh = new MessageHistory(); mh.Id = reader.GetGuid(0); if (!reader.IsDBNull(1)) mh.ReportingPartyId = reader.GetGuid(1); if (!reader.IsDBNull(2)) mh.EntityId = reader.GetGuid(2); if (!reader.IsDBNull(3)) mh.EntityType = reader.GetString(3); if (!reader.IsDBNull(4)) mh.EntityName = reader.GetString(4); if (!reader.IsDBNull(5)) mh.EntityValues = reader.GetString(5); if (!reader.IsDBNull(6)) mh.Created = reader.GetDateTime(6); if (!reader.IsDBNull(7)) mh.Identifier = reader.GetInt32(7); result.Add(mh); } reader.Close(); return result; } List IDatabaseEntity.LoadList(IDataReader reader) { throw new NotImplementedException(); } #endregion } }