using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Text; using System.Xml.Serialization; namespace DTS.Common.Storage.TestSetups { /// /// this is a class representing test history /// 3003 Attach Test History to Sensors /// [Serializable] public class TestHistory { /// /// the primary key/id for the record in the test history table /// public long TestHistoryId { get; set; } /// /// the id for the test setup in the db /// public int TestSetupId { get; set; } /// /// the name of the test setup /// public string TestSetupName { get; set; } /// /// the description for the test setup /// public string TestSetupDescription { get; set; } /// /// the user provided test id for the test run /// public string TestId { get; set; } /// /// whether the test was marked as destructive or not /// public bool Destructive { get; set; } /// /// the time the unit was armed or auto-armed /// public DateTime ArmTime { get; set; } /// /// bytes array representing the test setup xml in gzip'd compressed form /// public byte[] TestSetup { get; set; } public TestHistory() { } public TestHistory(IDataReader reader) { var o = reader["TestHistoryId"]; if (!DBNull.Value.Equals(o)) { TestHistoryId = Convert.ToInt64(o); } o = reader["TestSetupId"]; if (!DBNull.Value.Equals(o)) { TestSetupId = Convert.ToInt32(o); } o = reader["TestSetupName"]; if (!DBNull.Value.Equals(o)) { TestSetupName = Convert.ToString(o); } o = reader["TestSetupDescription"]; if (!DBNull.Value.Equals(o)) { TestSetupDescription = Convert.ToString(o); } o = reader["TestId"]; if (!DBNull.Value.Equals(o)) { TestId = Convert.ToString(o); } o = reader["Destructive"]; if (!DBNull.Value.Equals(o)) { Destructive = Convert.ToBoolean(o); } o = reader["ArmTime"]; if (!DBNull.Value.Equals(o)) { ArmTime = Convert.ToDateTime(o); } o = reader["TestSetup"]; if (!DBNull.Value.Equals(o)) { TestSetup = (byte[])o; } } /// /// retrieves all test histories matching the test name /// /// /// public static TestHistory[] GetTestHistory(string name) { var list = new List(); using (var cmd = DbOperations.GetSQLCommand(true)) { try { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "sp_TestHistoryGet"; cmd.Parameters.Add(new SqlParameter("@TestSetupName", SqlDbType.NVarChar) { Value = name }); var reader = cmd.ExecuteReader(); while (reader.Read()) { var history = new TestHistory(reader); list.Add(history); } } finally { cmd.Connection.Dispose(); } } return list.ToArray(); } /// /// returns an xml document in string form representing all the input histories /// /// /// public static string SerializeToString(TestHistory[] histories) { var serializer = new XmlSerializer(typeof(TestHistoryCollection)); using (var memoryStream = new MemoryStream()) { serializer.Serialize(memoryStream, new TestHistoryCollection(histories)); return Encoding.UTF8.GetString(memoryStream.ToArray()); } } /// /// this is just a helper class for serialization /// [Serializable] public class TestHistoryCollection { public TestHistory[] Histories { get; set; } public TestHistoryCollection(TestHistory[] histories) { Histories = histories; } public TestHistoryCollection() { } } } }