//******************************************************************************************************
// Company.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:
// ----------------------------------------------------------------------------------------------------
// 03/25/2011 - Mehulbhai P Thakkar
// 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/13/2011 - Mehulbhai P Thakkar
// Added regular expression validator for Acronym.
// 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 Company : DataModelBase
{
#region [ Members ]
// Fields
private int m_id;
private string m_acronym;
private string m_mapAcronym;
private string m_name;
private string m_url;
private int m_loadOrder;
private DateTime m_createdOn;
private string m_createdBy;
private DateTime m_updatedOn;
private string m_updatedBy;
#endregion
#region [ Properties ]
///
/// Gets or sets ID.
///
// Field is populated by database via auto-increment and has no screen interaction, so no validation attributes are applied
public int ID
{
get
{
return m_id;
}
set
{
m_id = value;
}
}
///
/// Gets or sets acronym.
///
[Required(ErrorMessage = "Company acronym is a required field, please provide value.")]
[StringLength(200, ErrorMessage = "Company acronym cannot exceed 200 characters.")]
[AcronymValidation]
public string Acronym
{
get
{
return m_acronym;
}
set
{
m_acronym = value;
OnPropertyChanged("Acronym");
}
}
///
/// Gets or sets map acronym used by .
///
[Required(ErrorMessage = "Company map acronym is a required field, please provide value.")]
[StringLength(3, ErrorMessage = "Company map acronym cannot exceed 3 characters.")]
public string MapAcronym
{
get
{
return m_mapAcronym;
}
set
{
m_mapAcronym = value;
OnPropertyChanged("MapAcronym");
}
}
///
/// Gets or sets name.
///
[Required(ErrorMessage = "Company name is a required field, please provide value.")]
[StringLength(200, ErrorMessage = "Company name cannot exceed 200 characters.")]
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
OnPropertyChanged("Name");
}
}
///
/// Gets or sets URL.
///
[DataType(DataType.Url, ErrorMessage = "Company web site address URL is not formatted properly.")]
[UrlValidation]
public string URL
{
get
{
return m_url;
}
set
{
m_url = value;
OnPropertyChanged("URL");
}
}
///
/// Gets or sets desired load order of record.
///
[Required(ErrorMessage = "Company load order value is a required field, please provide value.")]
[DefaultValue(0)]
public int LoadOrder
{
get
{
return m_loadOrder;
}
set
{
m_loadOrder = value;
OnPropertyChanged("LoadOrder");
}
}
///
/// 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 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 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
///
/// Loads IDs as an .
///
/// to connection to database.
/// The field to sort by.
/// ASC or DESC for ascending or descending respectively.
/// Collection of .
public static IList LoadKeys(AdoDataConnection database, string sortMember = "", string sortDirection = "")
{
bool createdConnection = false;
try
{
createdConnection = CreateConnection(ref database);
IList companyList = new List();
string sortClause = string.Empty;
DataTable companyTable;
if (!string.IsNullOrEmpty(sortMember))
sortClause = string.Format("ORDER BY {0} {1}", sortMember, sortDirection);
companyTable = database.Connection.RetrieveData(database.AdapterType, string.Format("SELECT ID FROM Company {0}", sortClause));
foreach (DataRow row in companyTable.Rows)
{
companyList.Add(row.ConvertField("ID"));
}
return companyList;
}
finally
{
if (createdConnection && database != null)
database.Dispose();
}
}
///
/// Loads information as an style list.
///
/// to connection to database.
/// Keys of the companies 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;
Company[] companyList = null;
DataTable companyTable;
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, MapAcronym, Name, URL, LoadOrder FROM Company WHERE ID IN ({0})", commaSeparatedKeys);
companyTable = database.Connection.RetrieveData(database.AdapterType, query);
companyList = new Company[companyTable.Rows.Count];
foreach (DataRow row in companyTable.Rows)
{
id = row.ConvertField("ID");
companyList[keys.IndexOf(id)] = new Company()
{
ID = id,
Acronym = row.Field("Acronym"),
MapAcronym = row.Field("MapAcronym"),
Name = row.Field("Name"),
URL = row.Field("URL"),
LoadOrder = row.ConvertField("LoadOrder")
};
}
}
return new ObservableCollection(companyList ?? new Company[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 companies defined in the database.
public static Dictionary GetLookupList(AdoDataConnection database, bool isOptional = false)
{
bool createdConnection = false;
try
{
createdConnection = CreateConnection(ref database);
Dictionary companyList = new Dictionary();
if (isOptional)
companyList.Add(0, "Select Company");
DataTable companyTable = database.Connection.RetrieveData(database.AdapterType, "SELECT ID, Name FROM Company ORDER BY Name");
foreach (DataRow row in companyTable.Rows)
companyList[row.ConvertField("ID")] = row.Field("Name");
return companyList;
}
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, Company company)
{
bool createdConnection = false;
string query;
try
{
createdConnection = CreateConnection(ref database);
if (company.ID == 0)
{
query = database.ParameterizedQueryString("INSERT INTO Company (Acronym, MapAcronym, Name, URL, LoadOrder, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) " +
"VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})", "acronym", "mapAcronym", "name", "url", "loadOrder", "updatedBy", "updatedOn", "createdBy",
"createdOn");
database.Connection.ExecuteNonQuery(query, DefaultTimeout, company.Acronym.Replace(" ", "").ToUpper(), company.MapAcronym.Replace(" ", "").ToUpper(),
company.Name, company.URL.ToNotNull(), company.LoadOrder, CommonFunctions.CurrentUser, database.UtcNow, CommonFunctions.CurrentUser,
database.UtcNow);
}
else
{
query = database.ParameterizedQueryString("UPDATE Company SET Acronym = {0}, MapAcronym = {1}, Name = {2}, URL = {3}, LoadOrder = {4}, " +
"UpdatedBy = {5}, UpdatedOn = {6} WHERE ID = {7}", "acronym", "mapAcronym", "name", "url", "loadOrder", "updatedBy", "updatedOn", "id");
database.Connection.ExecuteNonQuery(query, DefaultTimeout, company.Acronym.Replace(" ", "").ToUpper(),
company.MapAcronym.Replace(" ", "").ToUpper(), company.Name, company.URL.ToNotNull(), company.LoadOrder, CommonFunctions.CurrentUser,
database.UtcNow, company.ID);
}
return "Company 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 companyID)
{
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 Company WHERE ID = {0}", "companyID"), DefaultTimeout, companyID);
return "Company deleted successfully";
}
finally
{
if (createdConnection && database != null)
database.Dispose();
}
}
#endregion
}
}