// Copyright (c) 2015-2017 schick Informatik // Description: Einzeldatenwert eines Excel-Imports (als String), zum Vergleichen mit Vorversionen usw. using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Reflection; namespace bsmd.database { public class ImportValue : DatabaseEntity { #region Construction public ImportValue() { this.tablename = "[dbo].[ImportValue]"; } #endregion #region Enumeration public enum ValueStatus { REJECTED, CONFIRMED }; #endregion #region Properties public Guid ImportHeaderId { get; set; } public Message.NotificationClass NotificationClass { get; set; } public string Name { get; set; } public string Value { get; set; } public int? Identifier { get; set; } public ValueStatus? Status { get; set; } #endregion #region DatabaseEntity implementation public override List LoadList(IDataReader reader) { List result = new List(); while (reader.Read()) { ImportValue iv = new ImportValue(); iv.id = reader.GetGuid(0); if (!reader.IsDBNull(1)) iv.ImportHeaderId = reader.GetGuid(1); if (!reader.IsDBNull(2)) iv.NotificationClass = (Message.NotificationClass)Enum.ToObject(typeof(Message.NotificationClass), reader.GetByte(2)); if (!reader.IsDBNull(3)) iv.Name = reader.GetString(3); if (!reader.IsDBNull(4)) iv.Value = reader.GetString(4); if (!reader.IsDBNull(5)) iv.Identifier = reader.GetInt32(5); if (!reader.IsDBNull(6)) iv.Status = (ValueStatus)Enum.ToObject(typeof(ValueStatus), reader.GetByte(6)); result.Add(iv); } reader.Close(); return result; } public override void PrepareLoadCommand(IDbCommand cmd, Message.LoadFilter filter, params object[] criteria) { string query = string.Format("SELECT Id, ImportHeaderId, NotificationClass, Name, Value, Identifier, Status FROM {0} ", this.Tablename); switch (filter) { case Message.LoadFilter.IMPORTHEADER_ID: query += " WHERE ImportHeaderId = @IHID"; ((SqlCommand)cmd).Parameters.AddWithValue("@IHID", criteria[0]); break; default: break; } cmd.CommandText = query; } public override void PrepareSave(IDbCommand cmd) { SqlCommand scmd = cmd as SqlCommand; scmd.Parameters.AddWithNullableValue("@IHID", this.ImportHeaderId); scmd.Parameters.AddWithNullableValue("@NCLASS", this.NotificationClass); scmd.Parameters.AddWithNullableValue("@NAME", this.Name); scmd.Parameters.AddWithNullableValue("@VALUE", this.Value); scmd.Parameters.AddWithNullableValue("@IDENTIFIER", this.Identifier); scmd.Parameters.AddWithNullableValue("@STATUS", this.Status); if (this.IsNew) { this.CreateId(); scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("INSERT INTO {0} (Id, ImportHeaderId, NotificationClass, Name, Value, Identifier, Status) VALUES " + "(@ID, @IHID, @NCLASS, @NAME, @VALUE, @IDENTIFIER, @STATUS)", this.Tablename); } else { scmd.Parameters.AddWithValue("@ID", this.Id); scmd.CommandText = string.Format("UPDATE {0} SET ImportHeaderId = @IHID, NotificationClass = @NCLASS, Name = @NAME, Value = @VALUE, " + "Identifier = @IDENTIFIER, Status = @STATUS WHERE Id = @ID", this.Tablename); } } #endregion #region public static methods public static void BulkSave(List importValues) { // create data table from entities DataTable table = new DataTable(); PropertyInfo[] properties = typeof(ImportValue).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); // create table columns foreach (PropertyInfo p in properties) { if (!(p.CanRead && p.CanWrite)) continue; Type propType = p.PropertyType; DataColumn dc = new DataColumn(); dc.ColumnName = p.Name; if (p.Name == "Identifier") { dc.AllowDBNull = true; dc.DataType = Type.GetType("System.Int32"); } else if(p.Name == "Status") { dc.AllowDBNull = true; dc.DataType = Type.GetType("System.Byte"); } else { dc.DataType = propType; } table.Columns.Add(dc); } // insert entity data in table columns foreach (ImportValue importValue in importValues) { DataRow row = table.NewRow(); foreach (PropertyInfo p in properties) { if (!(p.CanRead && p.CanWrite)) continue; if (p.GetValue(importValue, null) == null) row[p.Name] = DBNull.Value; else row[p.Name] = p.GetValue(importValue, null); } table.Rows.Add(row); } table.TableName = "[dbo].[ImportValue]"; DBManager.Instance.PerformBulkInsert(table); } #endregion #region overrides public override string ToString() { return string.Format("{0} - {1}", this.Name, this.Value); } #endregion } }