//****************************************************************************************************** // VendorDevice.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. // 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.ComponentModel.DataAnnotations; using GSF.Data; using DataType = System.ComponentModel.DataAnnotations.DataType; namespace GSF.TimeSeries.UI.DataModels { /// /// Represents a record of information as defined in the database. /// public class VendorDevice : DataModelBase { #region [ Members ] // Fields private int m_id; private int m_vendorID; private string m_name; private string m_description; private string m_url; private string m_vendorName; private DateTime m_createdOn; private string m_createdBy; private DateTime m_updatedOn; private string m_updatedBy; #endregion #region [ Properties ] /// /// Gets or sets the 's ID. /// // Field is populated by database via auto-increment , so no validation attributes are applied. public int ID { get { return m_id; } set { m_id = value; } } /// /// Gets or sets the VendorID. /// [DefaultValue(10)] public int VendorID { get { return m_vendorID; } set { m_vendorID = value; OnPropertyChanged("VendorID"); } } /// /// Gets or sets 's Name. /// [Required(ErrorMessage = "Vendor device name is a required field, please provide value.")] [StringLength(200, ErrorMessage = "Vendor device name cannot exceed 200 characters.")] public string Name { get { return m_name; } set { m_name = value; OnPropertyChanged("Name"); } } /// /// Gets or sets the 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 the URL. /// [DataType(DataType.Url, ErrorMessage = "Vendor device URL is not formatted properly.")] [UrlValidation] public string URL { get { return m_url; } set { m_url = value; OnPropertyChanged("URL"); } } /// /// Gets the VendorName of this . /// public string VendorName { get { return m_vendorName; } } /// /// Gets or sets the Date or Time this 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 this 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 this was 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 this VenderDevice 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 ] /// /// LoadKeys information as an style list. /// /// to connection to database. /// The field to sort by. /// ASC or DESC for ascending or descending respectively. /// Collection of vendor device IDs. public static IList LoadKeys(AdoDataConnection database, string sortMember, string sortDirection) { bool createdConnection = false; try { createdConnection = CreateConnection(ref database); IList vendorDeviceList = new List(); DataTable vendorDeviceTable; string sortClause = string.Empty; if (!string.IsNullOrEmpty(sortMember)) sortClause = string.Format("ORDER BY {0} {1}", sortMember, sortDirection); // check the query once again , Does it have to be details or somethng else vendorDeviceTable = database.Connection.RetrieveData(database.AdapterType, string.Format("SELECT ID From VendorDeviceDetail {0}", sortClause)); foreach (DataRow row in vendorDeviceTable.Rows) { vendorDeviceList.Add(row.ConvertField("ID")); } return vendorDeviceList; } 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; VendorDevice[] vendorDeviceList = null; DataTable vendorDeviceTable; int id; if ((object)keys != null && keys.Count > 0) { commaSeparatedKeys = keys.Select(key => key.ToString()).Aggregate((str1, str2) => str1 + "," + str2); query = string.Format("SELECT * FROM VendorDeviceDetail WHERE ID IN ({0})", commaSeparatedKeys); vendorDeviceTable = database.Connection.RetrieveData(database.AdapterType, query); vendorDeviceList = new VendorDevice[vendorDeviceTable.Rows.Count]; foreach (DataRow row in vendorDeviceTable.Rows) { id = row.ConvertField("ID"); vendorDeviceList[keys.IndexOf(id)] = new VendorDevice() { ID = id, VendorID = row.ConvertField("VendorID"), Name = row.Field("Name"), Description = row.Field("Description"), URL = row.Field("URL"), m_vendorName = row.Field("VendorName") }; } } return new ObservableCollection(vendorDeviceList ?? new VendorDevice[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. /// containing ID and Name of vendor devices defined in the database. public static Dictionary GetLookupList(AdoDataConnection database, bool isOptional = false) { bool createdConnection = false; try { createdConnection = CreateConnection(ref database); Dictionary vendorDeviceList = new Dictionary(); if (isOptional) vendorDeviceList.Add(0, "Select Vendor Device"); DataTable vendorDeviceTable = database.Connection.RetrieveData(database.AdapterType, "SELECT ID, Name FROM VendorDevice ORDER BY Name"); foreach (DataRow row in vendorDeviceTable.Rows) vendorDeviceList[row.ConvertField("ID")] = row.Field("Name"); return vendorDeviceList; } 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, VendorDevice vendorDevice) { bool createdConnection = false; string query; try { createdConnection = CreateConnection(ref database); if (vendorDevice.ID == 0) { query = database.ParameterizedQueryString("INSERT INTO VendorDevice (VendorID, Name, Description, URL, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) " + "Values ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})", "vendorID", "name", "description", "url", "updatedBy", "updatedOn", "createdBy", "createdOn"); database.Connection.ExecuteNonQuery(query, DefaultTimeout, vendorDevice.VendorID, vendorDevice.Name, vendorDevice.Description.ToNotNull(), vendorDevice.URL.ToNotNull(), CommonFunctions.CurrentUser, database.UtcNow, CommonFunctions.CurrentUser, database.UtcNow); } else { query = database.ParameterizedQueryString("UPDATE VendorDevice SET VendorID = {0}, Name = {1}, Description = {2}, URL = {3}, UpdatedBy = {4}, " + "UpdatedOn = {5} WHERE ID = {6}", "vendorID", "name", "description", "url", "updatedBy", "updatedOn", "id"); database.Connection.ExecuteNonQuery(query, DefaultTimeout, vendorDevice.VendorID, vendorDevice.Name, vendorDevice.Description.ToNotNull(), vendorDevice.URL.ToNotNull(), CommonFunctions.CurrentUser, database.UtcNow, vendorDevice.ID); } return "Vendor Device 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 vendorDeviceID) { 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 VendorDevice WHERE ID = {0}", "vendorDeviceID"), DefaultTimeout, vendorDeviceID); return "Vendor Device deleted successfully"; } finally { if (createdConnection && database != null) database.Dispose(); } } #endregion } }