using System.Collections.Generic; using DbAPI.Connections; using DbAPI.Errors; using DTS.Common.Classes.TestSetups; using DTS.Common.Interface.Database; using DTS.Common.Interface.TestSetups; using System.Data; using System.Data.SqlClient; using System; using DbAPI.Logging; using System.Diagnostics; using DTS.Common; namespace DbAPI.TestSetups { /// /// Handles RegionsOfInterest functions /// internal class RegionsOfInterest : IRegionsOfInterest { /// /// Removes records from the TestSetupROIs and ROIPeriodChannels tables /// /// /// /// The value that matches the Primary key of the TestSetups table /// public ulong RegionsOfInterestDelete(IUserDbRecord user, IConnectionDetails connection, int testSetupId) { return TestSetupROIsDelete(user, connection, testSetupId); } /// /// Inserts records into the TestSetupROIs and ROIPeriodChannels tables /// /// /// /// The value that matches the Primary key of the TestSetups table /// The class that is split between the TestSetupROIs and ROIPeriodChannels tables /// public ulong RegionsOfInterestInsert(IUserDbRecord user, IConnectionDetails connection, int clientDbVersion, int testSetupId, DTS.Common.Interface.RegionOfInterest.IRegionOfInterest regionOfInterest) { var hResult = TestSetupROIsInsert(user, connection, testSetupId, regionOfInterest, out int testSetupROIId); if (hResult == ErrorCodes.ERROR_SUCCESS && testSetupROIId > 0) { var channelIndex = 0; foreach (var channelId in regionOfInterest.ChannelIds) { if( channelIndex >= regionOfInterest.ChannelNames.Length) { continue; } var channelName = regionOfInterest.ChannelNames[channelIndex]; hResult = ROIPeriodChannelsInsert(user, connection, clientDbVersion, testSetupROIId, channelName, channelId); if (hResult != ErrorCodes.ERROR_SUCCESS) { return hResult; } channelIndex++; } return ErrorCodes.ERROR_SUCCESS; } else { return ErrorCodes.ERROR_UNKNOWN; } } /// /// Gets records from the TestSetupROIs and ROIPeriodChannels tables /// /// /// /// The value that matches the Primary key of the TestSetups table /// The array of records combined from the TetSetupROIs and ROIPeriodChannels tables /// public ulong RegionsOfInterestGet(IUserDbRecord user, IConnectionDetails connection, int clientDbVersion, int testSetupId, out DTS.Common.Interface.RegionOfInterest.IRegionOfInterest[] records) { records = new DTS.Common.Interface.RegionOfInterest.IRegionOfInterest[0]; var list = new List(); TestSetupROIsGet(user, connection, testSetupId, out var testSetupROIRecords); foreach (var testSetupROIRecord in testSetupROIRecords) { ROIPeriodChannelsGet(user, connection, clientDbVersion, testSetupROIRecord.TestSetupROIId, out var roiPeriodChannelRecords); var channelNameList = new List(); var channelIdList = new List(); foreach (var roiPeriodChannelRecord in roiPeriodChannelRecords) { var serialNumberIndex = roiPeriodChannelRecord.ChannelName.LastIndexOf("\\"); if (serialNumberIndex > -1) { var serialNumber = roiPeriodChannelRecord.ChannelName.Substring(serialNumberIndex + 1); var hardwareChannelName = roiPeriodChannelRecord.ChannelName.Substring(0, serialNumberIndex); var newChannelName = RegionOfInterest.GetChanName(serialNumber, hardwareChannelName); channelNameList.Add(newChannelName); } else { channelNameList.Add(roiPeriodChannelRecord.ChannelName); } channelIdList.Add(roiPeriodChannelRecord.ChannelId); } list.Add(new RegionOfInterest() { Suffix = testSetupROIRecord.Suffix, Start = testSetupROIRecord.ROIStart, End = testSetupROIRecord.ROIEnd, IsEnabled = testSetupROIRecord.IsEnabled, IsDefault = testSetupROIRecord.IsDefault, ChannelNames = channelNameList.ToArray(), ChannelIds = channelIdList.ToArray() }); } records = list.ToArray(); return ErrorCodes.ERROR_SUCCESS; } /// /// Removes records from the TestSetupROIs and ROIPeriodChannels tables /// /// /// /// The value that matches the Primary key of the TestSetups table /// public ulong TestSetupROIsDelete(IUserDbRecord user, IConnectionDetails connection, int testSetupId) { if (!DbAPI.Connections.IsUserLoggedIn(user, connection)) { return ErrorCodes.ERROR_ACCESS_DENIED; } var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_TestSetupROIsDelete"); if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; } try { cmd.CommandType = CommandType.StoredProcedure; #region params cmd.Parameters.Add(new SqlParameter("@TestSetupId", SqlDbType.Int) { Value = testSetupId }); var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output }; cmd.Parameters.Add(errorNumber); var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250) { Direction = ParameterDirection.Output }; cmd.Parameters.Add(errorMessage); #endregion params cmd.ExecuteNonQuery(); if (!DBNull.Value.Equals(errorNumber.Value) && 0 != Convert.ToInt32(errorNumber.Value)) { LogManager.Log(TraceEventType.Error, LogManager.LogEvents.RegionsOfInterest, $"sp_TestSetupROIsDelete failed: {errorNumber.Value} : {errorMessage.Value}"); return ErrorCodes.ERROR_UNKNOWN; } return ErrorCodes.ERROR_SUCCESS; } catch (Exception ex) { LogManager.Log(TraceEventType.Error, LogManager.LogEvents.RegionsOfInterest, $"sp_TestSetupROIsDelete failed: {ex.Message}"); return ErrorCodes.ERROR_UNKNOWN; } finally { cmd.Connection.Dispose(); cmd.Dispose(); } } /// /// Inserts records into the TestSetupROIs table /// /// /// /// The value that matches the Primary key of the TestSetups table /// The class that will have a portion stored in the TestSetupROIs table /// The new value of the Primary key of the TestSetupROIs table /// public ulong TestSetupROIsInsert(IUserDbRecord user, IConnectionDetails connection, int testSetupId, DTS.Common.Interface.RegionOfInterest.IRegionOfInterest regionOfInterest, out int testSetupROIId) { testSetupROIId = 0; if (!DbAPI.Connections.IsUserLoggedIn(user, connection)) { return ErrorCodes.ERROR_ACCESS_DENIED; } if (null == regionOfInterest) { return ErrorCodes.ERROR_MISSING_PARAMETER; } var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_TestSetupROIsInsert"); if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; } try { cmd.CommandType = CommandType.StoredProcedure; #region params cmd.Parameters.Add(new SqlParameter("@TestSetupId", SqlDbType.Int) { Value = testSetupId }); cmd.Parameters.Add(new SqlParameter("@Suffix", SqlDbType.NVarChar, 50) { Value = regionOfInterest.Suffix }); cmd.Parameters.Add(new SqlParameter("@ROIStart", SqlDbType.Float) { Value = regionOfInterest.Start }); cmd.Parameters.Add(new SqlParameter("@ROIEnd", SqlDbType.Float) { Value = regionOfInterest.End }); cmd.Parameters.Add(new SqlParameter("@IsEnabled", SqlDbType.Bit) { Value = regionOfInterest.IsEnabled }); cmd.Parameters.Add(new SqlParameter("@IsDefault", SqlDbType.Bit) { Value = regionOfInterest.IsDefault }); var newIdParam = new SqlParameter("@new_id", SqlDbType.Int) { Direction = ParameterDirection.Output }; cmd.Parameters.Add(newIdParam); var errorNumberParam = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output }; cmd.Parameters.Add(errorNumberParam); var errorMessageParam = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250) { Direction = ParameterDirection.Output }; cmd.Parameters.Add(errorMessageParam); #endregion params cmd.ExecuteNonQuery(); if (int.Parse(errorNumberParam.Value.ToString()) != 0) { LogManager.Log(TraceEventType.Error, LogManager.LogEvents.RegionsOfInterest, errorMessageParam.Value.ToString()); } else { //Return the new id so that it can be used to enter this ROI period's channels into the ROIPeriodChannels table testSetupROIId = int.Parse(newIdParam.Value.ToString()); } } catch (Exception ex) { LogManager.Log(TraceEventType.Error, LogManager.LogEvents.RegionsOfInterest, ex.Message); return ErrorCodes.ERROR_UNKNOWN; } finally { cmd.Connection.Dispose(); cmd.Dispose(); } return ErrorCodes.ERROR_SUCCESS; } /// /// Gets records from the TestSetupROIs table /// /// /// /// The value that matches the Primary key of the TestSetups table /// The array of records from the TestSetupROIs table /// public ulong TestSetupROIsGet(IUserDbRecord user, IConnectionDetails connection, int testSetupId, out ITestSetupROIRecord[] records) { records = new ITestSetupROIRecord[0]; if (!DbAPI.Connections.IsUserLoggedIn(user, connection)) { return ErrorCodes.ERROR_ACCESS_DENIED; } var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_TestSetupROIsGet"); if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; } try { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@TestSetupId", SqlDbType.BigInt) { Value = testSetupId }); var reader = cmd.ExecuteReader(); var list = new List(); while (reader.Read()) { list.Add(new TestSetupROIsRecord(reader)); } records = list.ToArray(); return ErrorCodes.ERROR_SUCCESS; } catch (Exception ex) { LogManager.Log(TraceEventType.Error, LogManager.LogEvents.RegionsOfInterest, $"sp_TestSetupROIsGet failed: {ex.Message}"); return ErrorCodes.ERROR_UNKNOWN; } finally { cmd.Connection.Dispose(); cmd.Dispose(); } } /// /// Inserts records into the ROIPeriodChannels table /// /// /// /// The value of the Primary key of the TestSetupROIs table /// The name of a channel to be stored in the ROIPeriodChannels table /// The id of a channel to be stored in the ROIPeriodChannels table /// public ulong ROIPeriodChannelsInsert(IUserDbRecord user, IConnectionDetails connection, int clientDbVersion, int testSetupROIId, string channelName, long channelId) { if (!DbAPI.Connections.IsUserLoggedIn(user, connection)) { return ErrorCodes.ERROR_ACCESS_DENIED; } if (testSetupROIId == 0 || null == channelName) { return ErrorCodes.ERROR_MISSING_PARAMETER; } SqlCommand cmd; var storedProcedureVersionToUse = 0; var ret = Database.Database.PrepareForDbAccess(user, connection, clientDbVersion, "sp_ROIPeriodChannelsInsert", out storedProcedureVersionToUse, out cmd); if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; } try { cmd.CommandType = CommandType.StoredProcedure; #region params cmd.Parameters.Add(new SqlParameter("@TestSetupROIId", SqlDbType.Int) { Value = testSetupROIId }); cmd.Parameters.Add(new SqlParameter("@ChannelName", SqlDbType.NVarChar, 4000) { Value = channelName }); if (storedProcedureVersionToUse >= Constants.ROIPERIODCHANNELS_CHANNELID_DB_VERSION) { cmd.Parameters.Add(new SqlParameter("@ChannelId", SqlDbType.BigInt) { Value = channelId }); } var errorNumberParam = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output }; cmd.Parameters.Add(errorNumberParam); var errorMessageParam = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250) { Direction = ParameterDirection.Output }; cmd.Parameters.Add(errorMessageParam); #endregion params cmd.ExecuteNonQuery(); if (int.Parse(errorNumberParam.Value.ToString()) != 0) { LogManager.Log(TraceEventType.Error, LogManager.LogEvents.RegionsOfInterest, errorMessageParam.Value.ToString()); } } catch (Exception ex) { LogManager.Log(TraceEventType.Error, LogManager.LogEvents.RegionsOfInterest, ex.Message); return ErrorCodes.ERROR_UNKNOWN; } finally { cmd.Connection.Dispose(); cmd.Dispose(); } return ErrorCodes.ERROR_SUCCESS; } /// /// Gets records from the ROIPeriodChannels table /// /// /// /// The value of the Primary key of the TestSetupROIs table /// The array of records from the ROIPeriodChannels table /// public ulong ROIPeriodChannelsGet(IUserDbRecord user, IConnectionDetails connection, int clientDbVersion, int testSetupROIId, out IROIPeriodChannelRecord[] roiPeriodChannelRecords) { roiPeriodChannelRecords = new IROIPeriodChannelRecord[0]; if (!DbAPI.Connections.IsUserLoggedIn(user, connection)) { return ErrorCodes.ERROR_ACCESS_DENIED; } SqlCommand cmd; var storedProcedureVersionToUse = 0; var ret = Database.Database.PrepareForDbAccess(user, connection, clientDbVersion, "sp_ROIPeriodChannelsGet", out storedProcedureVersionToUse, out cmd); if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; } try { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@TestSetupROIId", SqlDbType.BigInt) { Value = testSetupROIId }); var reader = cmd.ExecuteReader(); var list = new List(); while (reader.Read()) { var newROIPeriodChannelRecord = new ROIPeriodChannelRecord(reader, storedProcedureVersionToUse); var cleanROIPeriodChannelName = RegionOfInterest.RemoveAssignedByIDFromHardwareString(newROIPeriodChannelRecord.ChannelName); cleanROIPeriodChannelName = RegionOfInterest.RemoveParentDASName(cleanROIPeriodChannelName); newROIPeriodChannelRecord.ChannelName = cleanROIPeriodChannelName; list.Add(newROIPeriodChannelRecord); } roiPeriodChannelRecords = list.ToArray(); return ErrorCodes.ERROR_SUCCESS; } catch (Exception ex) { LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Graphs, $"sp_TestSetupROIsGet failed: {ex.Message}"); return ErrorCodes.ERROR_UNKNOWN; } finally { cmd.Connection.Dispose(); cmd.Dispose(); } } } }