//****************************************************************************************************** // ErrorLogViewModel.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: // ---------------------------------------------------------------------------------------------------- // 3/26/2012 - prasanthgs // Generated original version of source code. // 04/12/2012 - prasanthgs // Reworked as per the comments of codeplex reviewers. // Code Optimized. // 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.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Input; using System.Windows.Threading; using GSF.TimeSeries.UI.Commands; using GSF.TimeSeries.UI.DataModels; using GSF.TimeSeries.UI.Modal; namespace GSF.TimeSeries.UI.ViewModels { internal class ErrorLogViewModel : PagedViewModelBase { #region [ Members ] //private ErrorMonitor m_exMonitor; private Dispatcher m_dispatcher; private RelayCommand m_showCommand; //private string m_currentSortMemberPath; //private ListSortDirection m_currentSortDirection; #endregion #region [ Constructor] /// /// Creates a new instance of class. /// /// No of Maximum items in each page. /// Default value true. public ErrorLogViewModel(int itemsPerPage, bool autoSave = true) : base(itemsPerPage, autoSave) { m_dispatcher = Dispatcher.CurrentDispatcher; } #endregion #region [ Properties ] /// /// Checks for new item in Error list. /// Since this is an error viewer, no new records can be added. /// Always returns false. /// public override bool IsNewRecord { get { return false; } } /// /// Gets Show command. /// public ICommand ShowCommand { get { if (m_showCommand == null) m_showCommand = new RelayCommand(ShowErrorLog); return m_showCommand; } } #endregion #region [ Methods ] /// /// Initialization to be done before the initial call to . /// public override void Initialize() { base.Initialize(); SortMember = "ID"; SortDirection = "DESC"; } /// /// Sorts model data. /// /// Member path for sorting. /// Ascending or descending. public override void SortData(string sortMemberPath, ListSortDirection sortDirection) { SortMember = sortMemberPath; SortDirection = (sortDirection == ListSortDirection.Descending) ? "DESC" : "ASC"; ItemsKeys = null; Load(); } /// /// Show brief error log details. /// private void ShowErrorLog() { ErrorDetailDisplay logDisplay = new ErrorDetailDisplay(CurrentItem.Detail); logDisplay.ShowDialog(); } /// /// Overriden method /// public override void Load() { List pageKeys; try { if ((object)ItemsKeys == null) { ItemsKeys = ErrorLog.LoadKeys(null, SortMember, SortDirection); if ((object)SortSelector != null) { if (SortDirection == "ASC") ItemsKeys = ItemsKeys.OrderBy(SortSelector).ToList(); else ItemsKeys = ItemsKeys.OrderByDescending(SortSelector).ToList(); } } pageKeys = ItemsKeys.Skip((CurrentPageNumber - 1) * ItemsPerPage).Take(ItemsPerPage).ToList(); ItemsSource = ErrorLog.Load(null, pageKeys); } catch (Exception ex) { if (ex.InnerException != null) { Popup(ex.Message + Environment.NewLine + "Inner Exception: " + ex.InnerException.Message, "Load " + DataModelName + " Exception:", MessageBoxImage.Error); CommonFunctions.LogException(null, "Load " + DataModelName, ex.InnerException); } else { Popup(ex.Message, "Load " + DataModelName + " Exception:", MessageBoxImage.Error); CommonFunctions.LogException(null, "Load " + DataModelName, ex); } } finally { Mouse.OverrideCursor = null; } } /// /// Overriden method /// Use current item index as key for setting /// error list selected item after refresh. /// /// Returns current item index. public override int GetCurrentItemKey() { return CurrentItem.ID; } /// /// Overriden method /// Get current item index. /// /// Returns String representation of current index. public override string GetCurrentItemName() { return CurrentItem.ID.ToString(); } #endregion } }