//****************************************************************************************************** // Vendor.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. // 04/14/2011 - Mehulbhai P Thakkar // Added static methods for database operations. // 05/02/2011 - J. Ritchie Carroll // Updated for coding consistency. // 05/05/2011 - Mehulbhai P Thakkar // Added NULL handling for Save() operation. // 12/20/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.Collections.ObjectModel; 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 Vendor : DataModelBase { #region [ Members ] // Fields private int m_id; private string m_acronym; private string m_name; private string m_phoneNumber; private string m_contactEmail; private string m_url; private DateTime m_createdOn; private string m_createdBy; private DateTime m_updatedOn; private string m_updatedBy; #endregion #region [ Properties ] /// /// Gets or sets the 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 Acronym. /// [StringLength(200, ErrorMessage = "Vendor acronym cannot exceed 200 characters")] [AcronymValidation] public string Acronym { get { return m_acronym; } set { m_acronym = value; OnPropertyChanged("Acronym"); } } /// /// Gets or sets the Name. /// [Required(ErrorMessage = "The Vendor name is a required field, please provide value")] [StringLength(200, ErrorMessage = "Vendor name cannot exceed 200 characters.")] public string Name { get { return m_name; } set { m_name = value; OnPropertyChanged("Name"); } } /// /// Gets or sets Phone Number. /// [StringLength(200, ErrorMessage = "Vendor phone number cannot exceed 200 characters.")] public string PhoneNumber { get { return m_phoneNumber; } set { m_phoneNumber = value; OnPropertyChanged("PhoneNumber"); } } /// /// Gets or sets the Contact Email. /// [StringLength(200, ErrorMessage = "Vendor contact e-mail cannot exceed 200 characters.")] [EmailValidation] public string ContactEmail { get { return m_contactEmail; } set { m_contactEmail = value; OnPropertyChanged("ContactEmail"); } } /// /// Gets or sets the URL. /// [DataType(DataType.Url, ErrorMessage = "Vendor URL is not formatted properly.")] [UrlValidation] public string URL { get { return m_url; } set { m_url = value; OnPropertyChanged("URL"); } } /// /// Gets or sets the Date or Time this 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 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 Time this 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 this 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. /// The field to sort by. /// ASC or DESC for ascending or descending respectively. /// Collection of vendor IDs. public static IList LoadKeys(AdoDataConnection database, string sortMember, string sortDirection) { bool createdConnection = false; try { createdConnection = CreateConnection(ref database); IList vendorList = new List(); DataTable vendorTable; 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 vendorTable = database.Connection.RetrieveData(database.AdapterType, string.Format("SELECT ID From VendorDetail {0}", sortClause)); foreach (DataRow row in vendorTable.Rows) { vendorList.Add(row.ConvertField("ID")); } return vendorList; } 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; Vendor[] vendorList = null; DataTable vendorTable; int id; if ((object)keys != null && keys.Count > 0) { commaSeparatedKeys = keys.Select(key => key.ToString()).Aggregate((str1, str2) => str1 + "," + str2); query = string.Format("SELECT ID, Acronym, Name, PhoneNumber, ContactEmail, URL FROM VendorDetail WHERE ID IN ({0})", commaSeparatedKeys); vendorTable = database.Connection.RetrieveData(database.AdapterType, query); vendorList = new Vendor[vendorTable.Rows.Count]; foreach (DataRow row in vendorTable.Rows) { id = row.ConvertField("ID"); vendorList[keys.IndexOf(id)] = new Vendor() { ID = id, Acronym = row.Field("Acronym"), Name = row.Field("Name"), PhoneNumber = row.Field("PhoneNumber"), ContactEmail = row.Field("ContactEmail"), URL = row.Field("URL") }; } } return new ObservableCollection(vendorList ?? new Vendor[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 vendors defined in the database. public static Dictionary GetLookupList(AdoDataConnection database, bool isOptional = false) { bool createdConnection = false; try { createdConnection = CreateConnection(ref database); Dictionary vendorList = new Dictionary(); if (isOptional) vendorList.Add(0, "Select Vendor"); DataTable vendorTable = database.Connection.RetrieveData(database.AdapterType, "SELECT ID, Name FROM Vendor ORDER BY Name"); foreach (DataRow row in vendorTable.Rows) vendorList[row.ConvertField("ID")] = row.Field("Name"); return vendorList; } 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, Vendor vendor) { bool createdConnection = false; string query; try { createdConnection = CreateConnection(ref database); if (vendor.ID == 0) { query = database.ParameterizedQueryString("INSERT INTO Vendor (Acronym, Name, PhoneNumber, ContactEmail, URL, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) " + "VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})", "acronym", "name", "phoneNumber", "contactEmail", "url", "updatedBy", "updatedOn", "createdBy", "createdOn"); database.Connection.ExecuteNonQuery(query, DefaultTimeout, vendor.Acronym.Replace(" ", "").ToUpper(), vendor.Name, vendor.PhoneNumber.ToNotNull(), vendor.ContactEmail.ToNotNull(), vendor.URL.ToNotNull(), CommonFunctions.CurrentUser, database.UtcNow, CommonFunctions.CurrentUser, database.UtcNow); } else { query = database.ParameterizedQueryString("UPDATE Vendor SET Acronym = {0}, Name = {1}, PhoneNumber = {2}, ContactEmail = {3}, " + "URL = {4}, UpdatedBy = {5}, UpdatedOn = {6} WHERE ID = {7}", "acronym", "name", "phoneNumber", "contactEmail", "url", "updatedBy", "updatedOn", "id"); database.Connection.ExecuteNonQuery(query, DefaultTimeout, vendor.Acronym.Replace(" ", "").ToUpper(), vendor.Name, vendor.PhoneNumber.ToNotNull(), vendor.ContactEmail.ToNotNull(), vendor.URL.ToNotNull(), CommonFunctions.CurrentUser, database.UtcNow, vendor.ID); } return "Vendor 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 vendorID) { bool createdConnection = false; try { if (database == null) { database = new AdoDataConnection(CommonFunctions.DefaultSettingsCategory); createdConnection = true; } // Setup current user context for any delete triggers CommonFunctions.SetCurrentUserContext(database); database.Connection.ExecuteNonQuery(database.ParameterizedQueryString("DELETE FROM Vendor WHERE ID = {0}", "vendorID"), DefaultTimeout, vendorID); return "Vendor deleted successfully"; } finally { if (createdConnection && database != null) database.Dispose(); } } #endregion } }