//******************************************************************************************************
// ErrorLog.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/13/2011 - Aniket Salver
// Generated original version of source code.
// 05/02/2011 - J. Ritchie Carroll
// Updated for coding consistency.
// 09/10/2012 - Aniket Salver
// Added paging technique and implemented sorting.
// 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.Data;
namespace GSF.TimeSeries.UI.DataModels
{
///
/// Represents a record of information as defined in the database.
///
public class ErrorLog : DataModelBase
{
#region [ Members ]
// Fields
private int m_id;
private string m_source;
private string m_type;
private string m_message;
private string m_detail;
private DateTime m_createdOn;
#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 Source.
///
[Required(ErrorMessage = "Error log source is a required field, please provide value.")]
[StringLength(200, ErrorMessage = "Error log source cannot exceed 200 characters.")]
public string Source
{
get
{
return m_source;
}
set
{
m_source = value;
OnPropertyChanged("Source");
}
}
///
/// Gets or sets Type.
///
public string Type
{
get
{
return m_type;
}
set
{
m_type = value;
OnPropertyChanged("Type");
}
}
///
/// Gets or sets Message.
///
[Required(ErrorMessage = "Error log message is a required field, please provide value.")]
[StringLength(1024, ErrorMessage = "Error log message cannot exceed 1024 characters.")]
public string Message
{
get
{
return m_message;
}
set
{
m_message = value;
OnPropertyChanged("Message");
}
}
///
/// Gets or sets Detail.
///
public string Detail
{
get
{
return m_detail;
}
set
{
m_detail = value;
OnPropertyChanged("Detail");
}
}
///
/// Gets or sets CreatedOn.
///
// Field is populated by database via trigger and has no screen interaction, so no validation attributes are applied
public DateTime CreatedOn
{
get
{
return m_createdOn;
}
set
{
m_createdOn = value;
}
}
#endregion
#region[ Static ]
///
/// 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 ErrorLogList = new List();
string sortClause = string.Empty;
DataTable ErrorLogTable;
if (!string.IsNullOrEmpty(sortMember))
sortClause = string.Format("ORDER BY {0} {1}", sortMember, sortDirection);
ErrorLogTable = database.Connection.RetrieveData(database.AdapterType, string.Format("SELECT ID FROM ErrorLog {0} ", sortClause));
foreach (DataRow row in ErrorLogTable.Rows)
{
ErrorLogList.Add(row.ConvertField("ID"));
}
return ErrorLogList;
}
finally
{
if (createdConnection && database != null)
database.Dispose();
}
}
///
/// Loads information as an style list.
///
/// to connection to database.
/// Keys of the ErrorLogs 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;
ErrorLog[] errorLogList = null;
DataTable errorLogTable;
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, Source, Type, Message, Detail, CreatedOn FROM ErrorLog WHERE ID IN ({0})", commaSeparatedKeys);
errorLogTable = database.Connection.RetrieveData(database.AdapterType, query);
errorLogList = new ErrorLog[errorLogTable.Rows.Count];
foreach (DataRow row in errorLogTable.Rows)
{
id = row.ConvertField("ID");
errorLogList[keys.IndexOf(id)] = new ErrorLog()
{
ID = id,
Source = row.Field("Source"),
Type = row.Field("Type"),
Message = row.Field("Message"),
Detail = row.Field("Detail"),
CreatedOn = row.Field("CreatedOn")
};
}
}
return new ObservableCollection(errorLogList ?? new ErrorLog[0]);
}
finally
{
if (createdConnection && database != null)
database.Dispose();
}
}
#endregion
}
}