//******************************************************************************************************
// ReportingProcessCollection.cs - Gbtc
//
// Copyright © 2015, 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://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:
// ----------------------------------------------------------------------------------------------------
// 06/09/2015 - J. Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections.ObjectModel;
using System.Text;
using GSF.TimeSeries.Adapters;
namespace GSF.TimeSeries.Reports
{
///
/// Represents a collection of items.
///
public class ReportingProcessCollection : Collection, IProvideStatus
{
#region [ Properties ]
string IProvideStatus.Name => GetType().Name;
///
/// Gets the descriptive status of this .
///
public virtual string Status
{
get
{
StringBuilder status = new StringBuilder();
// Show collection status
status.AppendFormat(" Total reporting processes: {0}", Count);
status.AppendLine();
if (Count > 0)
{
int index = 0;
status.AppendLine();
status.AppendFormat("Status of each reporting process:");
status.AppendLine();
status.Append(new string('-', 79));
status.AppendLine();
// Show the status of registered components.
lock (this)
{
foreach (IReportingProcess item in this)
{
status.AppendLine();
status.AppendFormat("Status of reporting process [{0}] \"{1}\":", ++index, item.Name);
status.AppendLine();
try
{
status.Append(item.Status);
}
catch (Exception ex)
{
status.AppendFormat("Failed to retrieve status due to exception: {0}", ex.Message);
status.AppendLine();
}
}
}
status.AppendLine();
status.Append(new string('-', 79));
status.AppendLine();
}
return status.ToString();
}
}
#endregion
#region [ Methods ]
///
/// Finds the for the specified name.
///
/// Name of the report type to find.
///
/// The for the specified name, if found;
/// otherwise, null if not found.
///
public IReportingProcess FindReportType(string reportType)
{
foreach (IReportingProcess reportingProcess in this)
{
if (reportType.Equals(reportingProcess.ReportType, StringComparison.OrdinalIgnoreCase))
return reportingProcess;
}
return null;
}
///
/// Inserts an element into the at the specified index.
///
/// The zero-based index at which should be inserted.
/// The object to insert. The value can be null for reference types.
///
/// is less than zero. -or-
/// is greater than .
///
protected override void InsertItem(int index, IReportingProcess item)
{
base.InsertItem(index, item);
item.LoadSettings();
}
///
/// Replaces the element at the specified index.
///
/// The zero-based index of the element to replace.
/// The new value for the element at the specified index. The value can be null for reference types.
///
/// is less than zero. -or-
/// is greater than .
///
protected override void SetItem(int index, IReportingProcess item)
{
base.SetItem(index, item);
item.LoadSettings();
}
#endregion
#region [ Static ]
// Static Methods
///
/// Loads available implementations.
///
/// Exception handler, if any, to call when type creation fails; otherwise, if null any exceptions will be thrown.
/// New collection of implementations.
public static ReportingProcessCollection LoadImplementations(Action exceptionHandler = null)
{
ReportingProcessCollection reportingProcesses = new ReportingProcessCollection();
IReportingProcess reportingProcess;
foreach (Type reportingProcessType in typeof(IReportingProcess).LoadImplementations())
{
reportingProcess = null;
try
{
// Try to load the reporting process implementation
reportingProcess = Activator.CreateInstance(reportingProcessType) as IReportingProcess;
}
catch (Exception ex)
{
if ((object)exceptionHandler != null)
exceptionHandler(ex);
else
throw;
}
if ((object)reportingProcess != null)
reportingProcesses.Add(reportingProcess);
}
return reportingProcesses;
}
#endregion
}
}