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

180 lines
6.8 KiB
C#

// 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.Linq;
using System.Text;
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; }
/// <summary>
/// ENI Grid helper property
/// </summary>
public string ReportingPartyName
{
get
{
Dictionary<Guid, ReportingParty> 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 FROM MessageHistory WHERE EntityId=@ENTITYID ORDER BY Timestamp";
}
public object CreateObjectFromValues()
{
if(!this.EntityType.IsNullOrEmpty())
{
Type objectType = Type.GetType(this.EntityType);
if(objectType != null)
return JsonConvert.DeserializeObject(this.EntityValues, objectType);
}
return null;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="allHistories"></param>
/// <returns></returns>
public List<DatabaseEntity> GetGroup(List<MessageHistory> allHistories)
{
LinkedList<DatabaseEntity> tmpResult = new LinkedList<DatabaseEntity>();
DatabaseEntity refEntity = this.CreateObjectFromValues() as DatabaseEntity;
if (refEntity is ISublistElement sublistElement)
{
List<MessageHistory> tmpList = new List<MessageHistory>(allHistories);
// Alle History-Elemente einer anderen Meldeklasse ausfiltern
tmpList.RemoveAll(aMessageHistory => !aMessageHistory.EntityId.Equals(this.EntityId));
int selIndex = tmpList.IndexOf(this);
tmpResult.AddLast(refEntity);
int myIdentifier = Int32.Parse((sublistElement).Identifier);
// Elemente vor dem ausgewählten Element der Linked List hinzufügen
int indexIdentifier = myIdentifier;
for (int tmpIndex = selIndex - 1; (indexIdentifier != 1) && (tmpIndex >= 0); tmpIndex--)
{
DatabaseEntity prevEntity = tmpList[tmpIndex].CreateObjectFromValues() as DatabaseEntity;
indexIdentifier = Int32.Parse(((ISublistElement)prevEntity).Identifier);
tmpResult.AddFirst(prevEntity);
}
// Elemente nach dem ausgewählten Element hinzufügen (wenn sie zur gleichen Gruppe gehören)
indexIdentifier = myIdentifier;
for(int tmpIndex = selIndex + 1; (indexIdentifier != 1) && (tmpIndex < tmpList.Count); tmpIndex++)
{
DatabaseEntity nextEntity = tmpList[tmpIndex].CreateObjectFromValues() as DatabaseEntity;
indexIdentifier = Int32.Parse(((ISublistElement)nextEntity).Identifier);
tmpResult.AddLast(nextEntity);
}
}
return new List<DatabaseEntity>(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);
cmd.CommandText = string.Format("INSERT INTO {0} (Id, ReportingPartyId, EntityId, EntityType, " +
"EntityName, EntityValues, Timestamp) VALUES (@ID, @P1, @P2, @P3, @P4, @P5, @P6)", 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);
}
internal static List<MessageHistory> LoadList(IDataReader reader)
{
List<MessageHistory> result = new List<MessageHistory>();
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);
result.Add(mh);
}
reader.Close();
return result;
}
List<DatabaseEntity> IDatabaseEntity.LoadList(IDataReader reader)
{
throw new NotImplementedException();
}
#endregion
}
}