//****************************************************************************************************** // OutputStreamMeasurement.cs - Gbtc // // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 08/22/2011 - Aniket Salver // Generated original version of source code. // 09/16/2011 - Mehulbhai P Thakkar // Modified static methods to fix bugs. // 09/19/2011 - Mehulbhai P Thakkar // Added OnPropertyChanged() on all properties to reflect changes on UI. // Fixed Load() and GetLookupList() static methods. // 09/21/2011 - Aniket Salver // Fixed issue, which helps in enabling the save button on the screen. // 09/15/2012 - Aniket Salver // Added paging and sorting technique. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel.DataAnnotations; using System.Data; using System.Linq; using GSF.Data; using GSF.TimeSeries.UI; using Measurement = GSF.TimeSeries.UI.DataModels.Measurement; namespace GSF.PhasorProtocols.UI.DataModels { /// /// Represents a record of information as defined in the database. /// public class OutputStreamMeasurement : DataModelBase { #region [ Members ] private Guid m_nodeID; private int m_adapterID; private int m_id; private int? m_historianID; private int m_pointID; private string m_signalReference; private string m_sourcePointTag; private string m_historianAcronym; private DateTime m_createdOn; private string m_createdBy; private DateTime m_updatedOn; private string m_updatedBy; #endregion #region [ Properties ] /// /// Gets or sets the current 's NodeID. /// public Guid NodeID { get { return m_nodeID; } set { m_nodeID = value; OnPropertyChanged("NodeID"); } } /// /// Gets or sets the current 's AdapterID. /// public int AdapterID { get { return m_adapterID; } set { m_adapterID = value; OnPropertyChanged("AdapterID"); } } /// /// Gets or sets the current 's ID. /// public int ID { get { return m_id; } set { m_id = value; } } /// /// Gets or sets the current 's HistorianID. /// public int? HistorianID { get { return m_historianID; } set { m_historianID = value; OnPropertyChanged("HistorianID"); } } /// /// Gets or sets the current 's PointID. /// public int PointID { get { return m_pointID; } set { m_pointID = value; OnPropertyChanged("PointID"); } } /// /// Gets or sets the current 's SignalReference. /// [StringLength(200, ErrorMessage = "OutputStreamMeasurement SignalReference cannot exceed 200 characters.")] [Required(ErrorMessage = "OutputStreamMeasurement SignalReference is a required field, please provide value.")] public string SignalReference { get { return m_signalReference; } set { m_signalReference = value; OnPropertyChanged("SignalReference"); } } /// /// Gets or sets the current 's SourcePointTag. /// public string SourcePointTag { get { return m_sourcePointTag; } } /// /// Gets or sets the current 's HistorianAcronym. /// public string HistorianAcronym { get { return m_historianAcronym; } } /// /// Gets or sets the Date or Time the current was created on. /// // Field is populated by trigger and has no screen interaction, so no validation attributes are applied public DateTime CreatedOn { get { return m_createdOn; } set { m_createdOn = value; } } /// /// Gets or sets who the current was created by. /// // Field is populated by trigger and has no screen interaction, so no validation attributes are applied public string CreatedBy { get { return m_createdBy; } set { m_createdBy = value; } } /// /// Gets or sets the Date or Time when the current was updated on. /// // Field is populated by trigger and has no screen interaction, so no validation attributes are applied public DateTime UpdatedOn { get { return m_updatedOn; } set { m_updatedOn = value; } } /// /// Gets or sets who the current was updated by. /// // Field is populated by trigger and has no screen interaction, so no validation attributes are applied public string UpdatedBy { get { return m_updatedBy; } set { m_updatedBy = value; } } #endregion #region [ Static ] // Static Methods /// /// LoadKeys information as an style list. /// /// to connection to database. /// ID of the to filter data. /// The field to sort by. /// ASC or DESC for ascending or descending respectively. /// Collection of . public static IList LoadKeys(AdoDataConnection database, int outputStreamID, string sortMember = "", string sortDirection = "") { bool createdConnection = false; try { createdConnection = CreateConnection(ref database); IList outputStreamMeasurementList = new List(); DataTable outputStreamMeasurementTable; string sortClause = string.Empty; if (!string.IsNullOrEmpty(sortMember)) sortClause = string.Format("ORDER BY {0} {1}", sortMember, sortDirection); outputStreamMeasurementTable = database.Connection.RetrieveData(database.AdapterType, string.Format("SELECT ID From OutputStreamMeasurementDetail where AdapterID = {0} {1}", outputStreamID, sortClause)); foreach (DataRow row in outputStreamMeasurementTable.Rows) { outputStreamMeasurementList.Add(row.ConvertField("ID")); } return outputStreamMeasurementList; } finally { if (createdConnection && database != null) database.Dispose(); } } /// /// Loads information as an style list. /// /// to connection to database. /// Keys of the measurement to be loaded from the database /// Collection of . public static ObservableCollection Load(AdoDataConnection database, IList keys) { bool createdConnection = false; try { createdConnection = CreateConnection(ref database); string query; string commaSeparatedKeys; OutputStreamMeasurement[] outputStreamMeasurementList = null; DataTable outputStreamMeasurementTable; int id; if ((object)keys != null && keys.Count > 0) { commaSeparatedKeys = keys.Select(key => "" + key.ToString() + "").Aggregate((str1, str2) => str1 + "," + str2); query = string.Format("SELECT NodeID, AdapterID, ID, HistorianID, PointID, SignalReference, SourcePointTag, HistorianAcronym " + "FROM OutputStreamMeasurementDetail WHERE ID IN ({0})", commaSeparatedKeys); outputStreamMeasurementTable = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout); outputStreamMeasurementList = new OutputStreamMeasurement[outputStreamMeasurementTable.Rows.Count]; foreach (DataRow row in outputStreamMeasurementTable.Rows) { id = row.ConvertField("ID"); outputStreamMeasurementList[keys.IndexOf(id)] = new OutputStreamMeasurement() { NodeID = database.Guid(row, "NodeID"), AdapterID = row.ConvertField("AdapterID"), ID = id, HistorianID = row.ConvertNullableField("HistorianID"), PointID = row.ConvertField("PointID"), SignalReference = row.Field("SignalReference"), m_sourcePointTag = row.Field("SourcePointTag"), m_historianAcronym = row.Field("HistorianAcronym") }; } } return new ObservableCollection(outputStreamMeasurementList ?? new OutputStreamMeasurement[0]); } finally { if (createdConnection && database != null) database.Dispose(); } } /// /// Gets a style list of information. /// /// to connection to database. /// Indicates if selection on UI is optional for this collection. /// ID of the output stream to filter data. /// containing ID and NodeID of OutputStreamMeasurement defined in the database. public static Dictionary GetLookupList(AdoDataConnection database, int outputStreamID, bool isOptional = false) { bool createdConnection = false; try { createdConnection = CreateConnection(ref database); Dictionary OutputStreamMeasurementList = new(); if (isOptional) OutputStreamMeasurementList.Add(0, "Select OutputStreamMeasurement"); string query = database.ParameterizedQueryString("SELECT PointID, SignalReference FROM OutputStreamMeasurement WHERE AdapterID = {0} ORDER BY LoadOrder", "adapterID"); DataTable OutputStreamMeasurementTable = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout, outputStreamID); foreach (DataRow row in OutputStreamMeasurementTable.Rows) OutputStreamMeasurementList[row.ConvertField("PointID")] = row.Field("SignalReference"); return OutputStreamMeasurementList; } finally { if (createdConnection && database != null) database.Dispose(); } } /// /// Saves information to database. /// /// to connection to database. /// Information about . /// String, for display use, indicating success. public static string Save(AdoDataConnection database, OutputStreamMeasurement outputStreamMeasurement) { bool createdConnection = false; string query; try { createdConnection = CreateConnection(ref database); if (outputStreamMeasurement.ID == 0) { query = database.ParameterizedQueryString("INSERT INTO OutputStreamMeasurement (NodeID, AdapterID, HistorianID, PointID, SignalReference, " + " UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})", "nodeID", "adapterID", "historianID", "pointID", "signalReference", "updatedBy", "updatedOn", "createdBy", "createdOn"); database.Connection.ExecuteNonQuery(query, DefaultTimeout, outputStreamMeasurement.NodeID == Guid.Empty ? database.CurrentNodeID() : database.Guid(outputStreamMeasurement.NodeID), outputStreamMeasurement.AdapterID, outputStreamMeasurement.HistorianID.ToNotNull(), outputStreamMeasurement.PointID, outputStreamMeasurement.SignalReference, CommonFunctions.CurrentUser, database.UtcNow, CommonFunctions.CurrentUser, database.UtcNow); } else { query = database.ParameterizedQueryString("UPDATE OutputStreamMeasurement SET NodeID = {0}, AdapterID = {1}, HistorianID = {2}, PointID = {3}, " + "SignalReference = {4}, UpdatedBy = {5}, UpdatedOn = {6} WHERE ID = {7}", "nodeID", "adapterID", "historianID", "pointID", "signalReference", "updatedBy", "updatedOn", "id"); database.Connection.ExecuteNonQuery(query, DefaultTimeout, database.Guid(outputStreamMeasurement.NodeID), outputStreamMeasurement.AdapterID, outputStreamMeasurement.HistorianID.ToNotNull(), outputStreamMeasurement.PointID, outputStreamMeasurement.SignalReference, CommonFunctions.CurrentUser, database.UtcNow, outputStreamMeasurement.ID); } return "OutputStreamMeasurement information saved successfully"; } finally { if (createdConnection && database != null) database.Dispose(); } } /// /// Deletes specified record from database. /// /// to connection to database. /// ID of the record to be deleted. /// String, for display use, indicating success. public static string Delete(AdoDataConnection database, int OutputStreamMeasurementID) { bool createdConnection = false; try { createdConnection = CreateConnection(ref database); // Setup current user context for any delete triggers CommonFunctions.SetCurrentUserContext(database); database.Connection.ExecuteNonQuery(database.ParameterizedQueryString("DELETE FROM OutputStreamMeasurement WHERE ID = {0}", "outputStreamMeasurementID"), DefaultTimeout, OutputStreamMeasurementID); return "OutputStreamMeasurement deleted successfully"; } finally { if (createdConnection && database != null) database.Dispose(); } } /// /// Creates and saves new into the database. /// /// to connection to database. /// ID of the output stream. /// Collection of measurements to be added. /// String, for display use, indicating success. public static string AddMeasurements(AdoDataConnection database, int outputStreamID, ObservableCollection measurements) { bool createdConnection = false; try { createdConnection = CreateConnection(ref database); foreach (Measurement measurement in measurements) { OutputStreamMeasurement outputStreamMeasurement = new() { NodeID = CommonFunctions.CurrentNodeID(), AdapterID = outputStreamID, HistorianID = measurement.HistorianID, PointID = measurement.PointID, SignalReference = measurement.SignalReference }; Save(database, outputStreamMeasurement); } return "Output stream measurements added successfully."; } finally { if (createdConnection && database != null) database.Dispose(); } } #endregion } }