//******************************************************************************************************
// RecordOperationsCache.cs - Gbtc
//
// Copyright © 2016, 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:
// ----------------------------------------------------------------------------------------------------
// 01/14/2016 - Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Reflection;
using GSF.Collections;
using GSF.Data.Model;
using GSF.Reflection;
using GSF.Web.Security;
using Microsoft.AspNet.SignalR.Hubs;
namespace GSF.Web.Hubs
{
///
/// Defines class that caches data hub operations.
///
public class RecordOperationsCache
{
#region [ Members ]
// Fields
private readonly Dictionary[]> m_recordOperations;
#endregion
#region [ Constructors ]
///
/// Creates a new .
///
/// Hub to analyze for record operations.
public RecordOperationsCache(Type hub)
{
int recordOperations = Enum.GetValues(typeof(RecordOperation)).Length;
m_recordOperations = new Dictionary[]>();
// Analyze and cache data hub methods that are targeted for record operations
foreach (MethodInfo method in hub.GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
AuthorizeHubRoleAttribute authorizeHubRoleAttribute;
RecordOperationAttribute recordOperationAttribute;
method.TryGetAttribute(out authorizeHubRoleAttribute);
if (method.TryGetAttribute(out recordOperationAttribute))
{
// Cache method name and any defined authorized roles for current record operation
m_recordOperations.GetOrAdd(recordOperationAttribute.ModelType,
type => new Tuple[recordOperations])[(int)recordOperationAttribute.Operation] =
new Tuple(method.Name, authorizeHubRoleAttribute?.Roles);
}
}
}
#endregion
#region [ Methods ]
///
/// Gets record operation methods for specified modeled table.
///
/// Modeled table.
/// record operation methods for specified modeled table.
public Tuple[] GetRecordOperations() where TModel : class, new()
{
return GetRecordOperations(typeof(TModel));
}
///
/// Gets record operation methods for specified modeled table type.
///
/// Model type.
/// record operation methods for specified modeled table type.
public Tuple[] GetRecordOperations(Type model)
{
return m_recordOperations[model];
}
#endregion
}
///
/// Defines class that caches data hub operations for the specified type .
///
/// Specific type for record operations cache.
public class RecordOperationsCache : RecordOperationsCache where T : IHub
{
///
/// Creates a new .
///
public RecordOperationsCache() : base(typeof(T))
{
}
}
}