using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Data.SqlClient; using DTS.Common.Interface.Sensors; using DTS.Common.Storage; namespace DTS.SensorDB { public class SoftwareFilter : Common.Base.BasePropertyChanged, ISoftwareFilter { public int Id { get; set; } = -1; private char _isoCode; public char ISOCode { get => _isoCode; set => SetProperty(ref _isoCode, value, "ISOCode"); } private string _description = ""; public string Description { get => _description; set => SetProperty(ref _description, value, "Description"); } private double _frequency = 0D; public double Frequency { get => _frequency; set => SetProperty(ref _frequency, value, "Frequency"); } //FB 13120 property to define wich filter is default private bool _isDefault = false; public bool IsDefault { get => _isDefault; set => SetProperty(ref _isDefault, value, "IsDefault"); } private DateTime _lastModified = DateTime.Today; public DateTime LastModified { get => _lastModified; set => SetProperty(ref _lastModified, value, "LastModified"); } private string _lastModifiedBy = ""; public string LastModifiedBy { get => _lastModifiedBy; set => SetProperty(ref _lastModifiedBy, value, "LastModifiedBy"); } public void Commit(bool updateDateTime = true, string user = "") { if (updateDateTime) { LastModifiedBy = user; LastModified = DateTime.Now; } if (Id <= 0) { var filters = GetSoftwareFilters(); if (filters.Where(p => p.Frequency == Frequency).Any()) return; Insert(); } else { Update(); } } private void Insert() { using (var sql = DbOperations.GetSQLCommand(true)) { try { sql.CommandType = CommandType.StoredProcedure; sql.CommandText = "sp_SoftwareFiltersInsert"; sql.Parameters.Add(new SqlParameter("@ISOCode", SqlDbType.Char) { Value = ISOCode }); sql.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar, 255) { Value = Description }); sql.Parameters.Add(new SqlParameter("@Frequency", SqlDbType.Float) { Value = Frequency }); sql.Parameters.Add(new SqlParameter("@LastModified", SqlDbType.DateTime) { Value = LastModified }); sql.Parameters.Add(new SqlParameter("@LastModifiedBy", SqlDbType.NVarChar, 255) { Value = LastModifiedBy }); sql.Parameters.Add(new SqlParameter("@IsDefault", SqlDbType.Bit) { Value = IsDefault }); var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output }; sql.Parameters.Add(errorNumber); var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 255) { Direction = ParameterDirection.Output }; sql.Parameters.Add(errorMessage); var newId = new SqlParameter("@new_id", SqlDbType.Int) { Direction = ParameterDirection.Output }; sql.Parameters.Add(newId); sql.ExecuteNonQuery(); if (null != errorNumber.Value && !DBNull.Value.Equals(errorNumber.Value)) { if (0 != Convert.ToInt32(errorNumber.Value)) { throw new Exception((string)errorMessage.Value); } } Id = Convert.ToInt32(newId.Value); } finally { sql.Connection.Dispose(); } } } private void Update() { using (var sql = DbOperations.GetSQLCommand(true)) { try { sql.CommandType = CommandType.StoredProcedure; sql.CommandText = "sp_SoftwareFiltersUpdate"; sql.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int) { Value = Id }); sql.Parameters.Add(new SqlParameter("@ISOCode", SqlDbType.Char) { Value = ISOCode }); sql.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar, 255) { Value = Description }); sql.Parameters.Add(new SqlParameter("@Frequency", SqlDbType.Float) { Value = Frequency }); sql.Parameters.Add(new SqlParameter("@LastModified", SqlDbType.DateTime) { Value = LastModified }); sql.Parameters.Add(new SqlParameter("@LastModifiedBy", SqlDbType.NVarChar, 255) { Value = LastModifiedBy }); sql.Parameters.Add(new SqlParameter("@IsDefault", SqlDbType.Bit) { Value = IsDefault }); var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output }; sql.Parameters.Add(errorNumber); var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 255) { Direction = ParameterDirection.Output }; sql.Parameters.Add(errorMessage); sql.ExecuteNonQuery(); if (null != errorNumber.Value && !DBNull.Value.Equals(errorNumber.Value)) { if (0 != Convert.ToInt32(errorNumber.Value)) { throw new Exception((string)errorMessage.Value); } } } finally { sql.Connection.Dispose(); } } } public void Delete() { if (Id <= 0) { return; } using (var sql = DbOperations.GetSQLCommand(true)) { sql.CommandType = CommandType.StoredProcedure; sql.CommandText = "sp_SoftwareFiltersDelete"; sql.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int) { Value = Id }); var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output }; sql.Parameters.Add(errorNumber); var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 255) { Direction = ParameterDirection.Output }; sql.Parameters.Add(errorMessage); sql.ExecuteNonQuery(); if (null != errorNumber.Value && !DBNull.Value.Equals(errorNumber.Value)) { if (0 != Convert.ToInt32(errorNumber.Value)) { throw new Exception((string)errorMessage.Value); } } } } public SoftwareFilter() { } public SoftwareFilter(int id, string description, char isoCode, DateTime lastModified, string lastModifiedBy, double frequency, bool isDefault) { Id = id; Description = description; ISOCode = isoCode; LastModified = lastModified; LastModifiedBy = lastModifiedBy; Frequency = frequency; IsDefault = isDefault; } public bool IsBlank() { if (Id > 0) { return false; } if (!string.IsNullOrWhiteSpace(Description)) { return false; } if (Frequency != 0D) { return false; } return true; } public static ISoftwareFilter[] GetSoftwareFilters() { var filters = new List(); using (var sql = DbOperations.GetSQLCommand(true)) { try { sql.CommandType = CommandType.StoredProcedure; sql.CommandText = "sp_SoftwareFiltersGet"; sql.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int) { Value = null }); sql.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar, 255) { Value = null }); sql.Parameters.Add(new SqlParameter("@ISOCode", SqlDbType.Char, 1) { Value = null }); var reader = sql.ExecuteReader(); while (reader.Read()) { var id = Convert.ToInt32(reader["Id"]); var description = Convert.ToString(reader["Description"]); var isoCode = Convert.ToChar(reader["ISOCode"]); var frequency = Convert.ToDouble(reader["Frequency"]); var lastModified = Convert.ToDateTime(reader["LastModified"]); var lastModifiedBy = Convert.ToString(reader["LastModifiedBy"]); var isDefault = Convert.ToBoolean(reader["IsDefault"]); filters.Add(new SoftwareFilter(id, description, isoCode, lastModified, lastModifiedBy, frequency, isDefault)); } } finally { sql.Connection.Dispose(); } } return filters.ToArray(); } } }