//******************************************************************************************************
// Measurement.cs - Gbtc
//
// Copyright © 2012, 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:
// ----------------------------------------------------------------------------------------------------
// 04/08/2011 - Magdiel Lorenzo
// Generated original version of source code.
// 05/02/2011 - J. Ritchie Carroll
// Updated for coding consistency.
// 05/03/2011 - Mehulbhai P Thakkar
// Guid field related changes as well as static functions update.
// 05/05/2011 - Mehulbhai P Thakkar
// Added NULL handling for Save() operation.
// 05/13/2011 - Aniket Salver
// Modified the way Guid is retrieved from the Data Base.
// 05/13/2011 - Mehulbhai P Thakkar
// Modified static methods to filter data by device.
// 12/20/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using GSF.Data;
namespace GSF.TimeSeries.UI.DataModels
{
///
/// Represents a record of information as defined in the database.
///
public class Measurement : DataModelBase
{
#region [ Members ]
// Fields
private Guid m_signalID;
private int? m_historianID;
private int m_pointID;
private int? m_deviceID;
private string m_pointTag;
private string m_alternateTag;
private int m_signalTypeID;
private int? m_phasorSourceIndex;
private string m_signalReference;
private double m_adder;
private double m_multiplier;
private string m_description;
private bool m_subscribed;
private bool m_internal;
private bool m_enabled;
private string m_historianAcronym;
private string m_deviceAcronym;
private int? m_framesPerSecond;
private string m_signalName;
private string m_signalAcronym;
private string m_signalSuffix;
private string m_phasorLabel;
private DateTime m_createdOn;
private string m_createdBy;
private DateTime m_updatedOn;
private string m_updatedBy;
private string m_id;
private string m_companyAcronym;
private string m_companyName;
private bool m_selected; //This is added for the SelectMeasurement user control to provide check boxes.
#endregion
#region [ Constructors ]
///
/// Creates a new instance of the class.
///
public Measurement()
{
}
///
/// Creates a new instance of the class.
///
///
/// Determines whether to load default values into the properties using reflection.
///
public Measurement(bool loadDefaults)
: base(loadDefaults, false)
{
}
#endregion
#region [ Properties ]
///
/// Gets or sets the current 's Signal ID.
///
public Guid SignalID
{
get
{
return m_signalID;
}
set
{
m_signalID = value;
OnPropertyChanged("SignalID");
}
}
///
/// Gets or sets the current 's Historian ID.
///
// Because of database design, no validation attributes are applied
public int? HistorianID
{
get
{
return m_historianID;
}
set
{
m_historianID = value;
OnPropertyChanged("HistorianID");
}
}
///
/// Gets or sets the current 's Point ID.
///
// Field is populated by database via auto-increment and has no screen interaction, so no validation attributes are applied.
public int PointID
{
get
{
return m_pointID;
}
set
{
m_pointID = value;
}
}
///
/// Gets or sets the current 's Device ID.
///
// Because of database design, no validation attributes are applied.
public int? DeviceID
{
get
{
return m_deviceID;
}
set
{
m_deviceID = value;
OnPropertyChanged("DeviceID");
}
}
///
/// Gets or sets the current 's Point Tag.
///
[Required(ErrorMessage = "Measurement point tag is a required field, please provide value.")]
[StringLength(200, ErrorMessage = "Measurement point tag cannot exceed 200 characters.")]
public string PointTag
{
get
{
return m_pointTag;
}
set
{
if ((object)value != null && value.Length > 200)
m_pointTag = value.Substring(0, 200);
else
m_pointTag = value;
OnPropertyChanged("PointTag");
}
}
///
/// Gets or sets the current 's Alternate Tag.
///
public string AlternateTag
{
get
{
return m_alternateTag;
}
set
{
m_alternateTag = value;
OnPropertyChanged("AlternateTag");
}
}
///
/// Gets or sets the current 's Signal Type ID.
///
[Required(ErrorMessage = "Measurement signal type ID is a required field, please provide value.")]
public int SignalTypeID
{
get
{
return m_signalTypeID;
}
set
{
m_signalTypeID = value;
OnPropertyChanged("SignalTypeID");
}
}
///
/// Gets or sets the current 's Phasor Source Index.
///
// Because of database design, no validation attributes are applied
public int? PhasorSourceIndex
{
get
{
return m_phasorSourceIndex;
}
set
{
m_phasorSourceIndex = value;
OnPropertyChanged("PhasorSourceIndex");
}
}
///
/// Gets or sets the current 's Signal Reference.
///
[Required(ErrorMessage = "Measurement signal reference 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 Adder.
///
[DefaultValue(typeof(double), "0.0")]
public double Adder
{
get
{
return m_adder;
}
set
{
m_adder = value;
OnPropertyChanged("Adder");
}
}
///
/// Gets or sets the current 's Multiplier.
///
[DefaultValue(typeof(double), "1.0")]
public double Multiplier
{
get
{
return m_multiplier;
}
set
{
m_multiplier = value;
OnPropertyChanged("Multiplier");
}
}
///
/// Gets or sets the current 's Description.
///
// Because of database design, no validation attributes are applied
public string Description
{
get
{
return m_description;
}
set
{
m_description = value;
OnPropertyChanged("Description");
}
}
///
/// Gets or sets whether the current is enabled.
///
[DefaultValue(false)]
public bool Subscribed
{
get
{
return m_subscribed;
}
set
{
m_subscribed = value;
OnPropertyChanged("Subscribed");
}
}
///
/// Gets or sets whether the current is enabled.
///
[DefaultValue(true)]
public bool Internal
{
get
{
return m_internal;
}
set
{
m_internal = value;
OnPropertyChanged("Internal");
}
}
///
/// Gets or sets whether the current is enabled.
///
[DefaultValue(false)]
public bool Enabled
{
get
{
return m_enabled;
}
set
{
m_enabled = value;
OnPropertyChanged("Enabled");
}
}
///
/// Gets the current 's Historian Acronym.
///
// Because of database design, no validation attributes are supplied
public string HistorianAcronym
{
get
{
return m_historianAcronym;
}
}
///
/// Gets the current 's Device Acronym.
///
// Because of database design, no validation attributes are supplied
public string DeviceAcronym
{
get
{
return m_deviceAcronym;
}
}
///
/// Gets the current 's Frames Per Second.
///
// Because of database design, no validation attributes are supplied
public int? FramesPerSecond
{
get
{
return m_framesPerSecond;
}
}
///
/// Gets the current 's Signal Name.
///
// Because of database design, no validation attributes are supplied
public string SignalName
{
get
{
return m_signalName;
}
}
///
/// Gets the current 's Signal Acronym.
///
// Because of database design, no validation attributes are supplied
public string SignalAcronym
{
get
{
return m_signalAcronym;
}
}
///
/// Gets the current 's Signal Suffix.
///
// Because of database design, no validation attributes are supplied
public string SignalSuffix
{
get
{
return m_signalSuffix;
}
}
///
/// Gets the current 's Phasor Label.
///
// Because of database design, no validation attributes are supplied
public string PhasorLabel
{
get
{
return m_phasorLabel;
}
}
///
/// Gets or sets when the current was Created.
///
// 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 when the current updated.
///
// 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;
}
}
///
/// Gets ID of the .
///
// Field is populated via view, so no validation attributes are applied.
public string ID
{
get
{
return m_id;
}
}
///
/// Gets company acronym.
///
// Field is populated via view, so no validation attributes are applied.
public string CompanyAcronym
{
get
{
return m_companyAcronym;
}
}
///
/// Gets company name.
///
// Field is populated via view, so no validation attributes are applied.
public string CompanyName
{
get
{
return m_companyName;
}
}
///
/// Gets or sets selected flag for .
///
public bool Selected
{
get
{
return m_selected;
}
set
{
m_selected = value;
OnPropertyChanged("Selected");
}
}
#endregion
#region [ Static ]
///
/// Loads signal IDs as an .
///
/// to connection to database.
/// SQL expression by which to filter the data coming from the database.
/// The field to sort by.
/// ASC or DESC for ascending or descending respectively.
/// Collection of .
///
/// This method does not validate for SQL injection.
/// Developers should validate their inputs before entering a filter expression.
///
public static List LoadSignalIDs(AdoDataConnection database, string filterExpression = "", string sortMember = "", string sortDirection = "")
{
bool createdConnection = false;
try
{
createdConnection = CreateConnection(ref database);
List signalIDList = new List();
DataTable measurementTable;
string query;
string sortClause = string.Empty;
if (!string.IsNullOrEmpty(sortMember))
sortClause = string.Format("ORDER BY {0} {1}", sortMember, sortDirection);
if (!string.IsNullOrEmpty(filterExpression))
query = string.Format("SELECT SignalID FROM MeasurementDetail WHERE ({0}) {1}", filterExpression, sortClause);
else
query = string.Format("SELECT SignalID FROM MeasurementDetail {0}", sortClause);
measurementTable = database.Connection.RetrieveData(database.AdapterType, query);
foreach (DataRow row in measurementTable.Rows)
{
signalIDList.Add(database.Guid(row, "SignalID"));
}
return signalIDList;
}
finally
{
if (createdConnection && database != null)
database.Dispose();
}
}
///
/// Loads information as an style list.
///
/// to connection to database.
/// Keys of the measurements to be loaded from the database.
/// Collection of .
public static ObservableCollection LoadFromKeys(AdoDataConnection database, List keys)
{
bool createdConnection = false;
try
{
createdConnection = CreateConnection(ref database);
string query;
string commaSeparatedKeys;
Measurement[] measurementList = null;
DataTable measurementTable;
Guid signalID;
if ((object)keys != null && keys.Count > 0)
{
commaSeparatedKeys = keys.Select(key => "'" + key.ToString() + "'").Aggregate((str1, str2) => str1 + "," + str2);
query = string.Format("SELECT * FROM MeasurementDetail WHERE SignalID IN ({0})", commaSeparatedKeys);
measurementTable = database.Connection.RetrieveData(database.AdapterType, query);
measurementList = new Measurement[measurementTable.Rows.Count];
foreach (DataRow row in measurementTable.Rows)
{
signalID = database.Guid(row, "SignalID");
measurementList[keys.IndexOf(signalID)] = new Measurement(false)
{
SignalID = signalID,
HistorianID = row.ConvertNullableField("HistorianID"),
PointID = row.ConvertField("PointID"),
DeviceID = row.ConvertNullableField("DeviceID"),
PointTag = row.Field("PointTag"),
AlternateTag = row.Field("AlternateTag"),
SignalTypeID = row.ConvertField("SignalTypeID"),
PhasorSourceIndex = row.ConvertNullableField("PhasorSourceIndex"),
SignalReference = row.Field("SignalReference"),
Adder = row.ConvertField("Adder"),
Multiplier = row.ConvertField("Multiplier"),
Internal = Convert.ToBoolean(row.Field