|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using SchoolSample.Common;
using SchoolSample.EntityModel;
using SchoolSample.EntityModel.Resource;
namespace SchoolSample.ViewModel
{
[Export(ViewModelTypes.InstructorPageFilterViewModel, typeof (ViewModelBase))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class InstructorPageFilterViewModel : ViewModelBase
{
#region "Private Data Members"
private NotificationMessageAction<ClientFilter<Instructor>> _notification;
private int _currentMaxSortOrder;
#endregion "Private Data Members"
#region "Constructor"
public InstructorPageFilterViewModel()
{
// register for InstructorFilterMessage
AppMessages.InstructorFilterMessage.Register(this, OnInstructorFilterMessage);
// set enum type
ListSortDirectionCollection = new EnumCollection<ListSortDirection>(true,
SchoolModelResource.ResourceManager);
JointConditionCollection = new EnumCollection<JointConditionEnum>(false, SchoolModelResource.ResourceManager);
InstructorStatusCollection = new EnumCollection<StatusEnum>(false, SchoolModelResource.ResourceManager);
CompareOperator = new List<string> {">=", ">", "=", "<", "<="};
StatusCompareOperator = new List<string> {"=", "!="};
ResetAll();
}
#endregion "Constructor"
#region "Public Properties"
public EnumCollection<ListSortDirection> ListSortDirectionCollection { get; private set; }
public EnumCollection<JointConditionEnum> JointConditionCollection { get; private set; }
public EnumCollection<StatusEnum> InstructorStatusCollection { get; private set; }
public List<string> CompareOperator { get; private set; }
public List<string> StatusCompareOperator { get; private set; }
public string InstructorName
{
get { return _instructorName; }
set
{
if (!ReferenceEquals(_instructorName, value))
{
_instructorName = value;
RaisePropertyChanged("InstructorName");
}
}
}
private string _instructorName;
public int InstructorNameSortDirectionIndex
{
get { return _instructorNameSortDirectionIndex; }
set
{
if (_instructorNameSortDirectionIndex != value)
{
_instructorNameSortDirectionIndex = value;
RaisePropertyChanged("InstructorNameSortDirectionIndex");
}
}
}
private int _instructorNameSortDirectionIndex;
public int? InstructorNameSortOrder
{
get { return _instructorNameSortOrder; }
set
{
if (!ReferenceEquals(_instructorNameSortOrder, value))
{
_instructorNameSortOrder = value;
RaisePropertyChanged("InstructorNameSortOrder");
}
}
}
private int? _instructorNameSortOrder;
public DateTime? HireDate
{
get { return _hireDate; }
set
{
if (!ReferenceEquals(_hireDate, value))
{
_hireDate = value;
RaisePropertyChanged("HireDate");
}
}
}
private DateTime? _hireDate;
public int HireDateCompareOperatorIndex
{
get { return _hireDateCompareOperatorIndex; }
set
{
if (_hireDateCompareOperatorIndex != value)
{
_hireDateCompareOperatorIndex = value;
RaisePropertyChanged("HireDateCompareOperatorIndex");
}
}
}
private int _hireDateCompareOperatorIndex;
public int HireDateSortDirectionIndex
{
get { return _hireDateSortDirectionIndex; }
set
{
if (_hireDateSortDirectionIndex != value)
{
_hireDateSortDirectionIndex = value;
RaisePropertyChanged("HireDateSortDirectionIndex");
}
}
}
private int _hireDateSortDirectionIndex;
public int? HireDateSortOrder
{
get { return _hireDateSortOrder; }
set
{
if (!ReferenceEquals(_hireDateSortOrder, value))
{
_hireDateSortOrder = value;
RaisePropertyChanged("HireDateSortOrder");
}
}
}
private int? _hireDateSortOrder;
public StatusEnum? Status
{
get { return _status; }
set
{
if (!ReferenceEquals(_status, value))
{
_status = value;
RaisePropertyChanged("Status");
}
}
}
private StatusEnum? _status;
public int StatusCompareOperatorIndex
{
get { return _statusCompareOperatorIndex; }
set
{
if (_statusCompareOperatorIndex != value)
{
_statusCompareOperatorIndex = value;
RaisePropertyChanged("StatusCompareOperatorIndex");
}
}
}
private int _statusCompareOperatorIndex;
public int StatusSortDirectionIndex
{
get { return _statusSortDirectionIndex; }
set
{
if (_statusSortDirectionIndex != value)
{
_statusSortDirectionIndex = value;
RaisePropertyChanged("StatusSortDirectionIndex");
}
}
}
private int _statusSortDirectionIndex;
public int? StatusSortOrder
{
get { return _statusSortOrder; }
set
{
if (!ReferenceEquals(_statusSortOrder, value))
{
_statusSortOrder = value;
RaisePropertyChanged("StatusSortOrder");
}
}
}
private int? _statusSortOrder;
public decimal? Salary
{
get { return _salary; }
set
{
if (!ReferenceEquals(_salary, value))
{
_salary = value;
RaisePropertyChanged("Salary");
}
}
}
private decimal? _salary;
public int SalaryCompareOperatorIndex
{
get { return _salaryCompareOperatorIndex; }
set
{
if (_salaryCompareOperatorIndex != value)
{
_salaryCompareOperatorIndex = value;
RaisePropertyChanged("SalaryCompareOperatorIndex");
}
}
}
private int _salaryCompareOperatorIndex;
public int SalarySortDirectionIndex
{
get { return _salarySortDirectionIndex; }
set
{
if (_salarySortDirectionIndex != value)
{
_salarySortDirectionIndex = value;
RaisePropertyChanged("SalarySortDirectionIndex");
}
}
}
private int _salarySortDirectionIndex;
public int? SalarySortOrder
{
get { return _salarySortOrder; }
set
{
if (!ReferenceEquals(_salarySortOrder, value))
{
_salarySortOrder = value;
RaisePropertyChanged("SalarySortOrder");
}
}
}
private int? _salarySortOrder;
public int JointCondition1Index
{
get { return _jointCondition1Index; }
set
{
if (_jointCondition1Index != value)
{
_jointCondition1Index = value;
RaisePropertyChanged("JointCondition1Index");
}
}
}
private int _jointCondition1Index;
public int JointCondition2Index
{
get { return _jointCondition2Index; }
set
{
if (_jointCondition2Index != value)
{
_jointCondition2Index = value;
RaisePropertyChanged("JointCondition2Index");
}
}
}
private int _jointCondition2Index;
public int JointCondition3Index
{
get { return _jointCondition3Index; }
set
{
if (_jointCondition3Index != value)
{
_jointCondition3Index = value;
RaisePropertyChanged("JointCondition3Index");
}
}
}
private int _jointCondition3Index;
public bool? DialogResult
{
get { return _dialogResult; }
set
{
if (!ReferenceEquals(_dialogResult, value))
{
_dialogResult = value;
RaisePropertyChanged("DialogResult");
}
}
}
private bool? _dialogResult;
#endregion "Public Properties"
#region "Public Commands"
/// <summary>
/// Command for the OK button
/// </summary>
public RelayCommand OkCommand
{
get
{
if (_okCommand == null)
{
_okCommand = new RelayCommand(
OnOKCommand);
}
return _okCommand;
}
}
private RelayCommand _okCommand;
private void OnOKCommand()
{
if (_notification != null)
{
var clientFilter = ParseClientFilter();
_notification.Execute(clientFilter);
DialogResult = true;
}
}
/// <summary>
/// Command for the Cancel button
/// </summary>
public RelayCommand CancelCommand
{
get
{
if (_cancelCommand == null)
{
_cancelCommand = new RelayCommand(
OnCancelCommand);
}
return _cancelCommand;
}
}
private RelayCommand _cancelCommand;
private void OnCancelCommand()
{
DialogResult = false;
}
/// <summary>
/// Command for the Save as Default button
/// </summary>
public RelayCommand SaveDefaultCommand
{
get
{
if (_saveDefaultCommand == null)
{
_saveDefaultCommand = new RelayCommand(
OnSaveDefaultCommand);
}
return _saveDefaultCommand;
}
}
private RelayCommand _saveDefaultCommand;
private void OnSaveDefaultCommand()
{
var clientFilter = ParseClientFilter();
AppMessages.InstructorDefaultFilterMessage.Send(clientFilter);
DialogResult = true;
}
/// <summary>
/// Command for the Reset button
/// </summary>
public RelayCommand ResetCommand
{
get
{
if (_resetCommand == null)
{
_resetCommand = new RelayCommand(
OnResetCommand);
}
return _resetCommand;
}
}
private RelayCommand _resetCommand;
private void OnResetCommand()
{
ResetAll();
}
/// <summary>
/// Command for SelectionChanged event of InstructorName SortDirection
/// </summary>
public RelayCommand InstructorNameSortDirectionSelectionChangedCommand
{
get
{
if (_instructorNameSortDirectionSelectionChangedCommand == null)
{
_instructorNameSortDirectionSelectionChangedCommand = new RelayCommand(
OnInstructorNameSortDirectionSelectionChangedCommand);
}
return _instructorNameSortDirectionSelectionChangedCommand;
}
}
private RelayCommand _instructorNameSortDirectionSelectionChangedCommand;
private void OnInstructorNameSortDirectionSelectionChangedCommand()
{
if (InstructorNameSortDirectionIndex == 0)
{
if (InstructorNameSortOrder != null)
{
var currentInt = InstructorNameSortOrder;
InstructorNameSortOrder = null;
_currentMaxSortOrder = _currentMaxSortOrder - 1;
if (HireDateSortOrder != null && HireDateSortOrder > currentInt)
{
HireDateSortOrder = HireDateSortOrder - 1;
}
if (StatusSortOrder != null && StatusSortOrder > currentInt)
{
StatusSortOrder = StatusSortOrder - 1;
}
if (SalarySortOrder != null && SalarySortOrder > currentInt)
{
SalarySortOrder = SalarySortOrder - 1;
}
}
}
else
{
if (InstructorNameSortOrder == null)
{
_currentMaxSortOrder++;
InstructorNameSortOrder = _currentMaxSortOrder;
}
}
}
/// <summary>
/// Command for SelectionChanged event of HireDate SortDirection
/// </summary>
public RelayCommand HireDateSortDirectionSelectionChangedCommand
{
get
{
if (_hireDateSortDirectionSelectionChangedCommand == null)
{
_hireDateSortDirectionSelectionChangedCommand = new RelayCommand(
OnHireDateSortDirectionSelectionChangedCommand);
}
return _hireDateSortDirectionSelectionChangedCommand;
}
}
private RelayCommand _hireDateSortDirectionSelectionChangedCommand;
private void OnHireDateSortDirectionSelectionChangedCommand()
{
if (HireDateSortDirectionIndex == 0)
{
if (HireDateSortOrder != null)
{
var currentInt = HireDateSortOrder;
HireDateSortOrder = null;
_currentMaxSortOrder = _currentMaxSortOrder - 1;
if (InstructorNameSortOrder != null && InstructorNameSortOrder > currentInt)
{
InstructorNameSortOrder = InstructorNameSortOrder - 1;
}
if (StatusSortOrder != null && StatusSortOrder > currentInt)
{
StatusSortOrder = StatusSortOrder - 1;
}
if (SalarySortOrder != null && SalarySortOrder > currentInt)
{
SalarySortOrder = SalarySortOrder - 1;
}
}
}
else
{
if (HireDateSortOrder == null)
{
_currentMaxSortOrder++;
HireDateSortOrder = _currentMaxSortOrder;
}
}
}
/// <summary>
/// Command for SelectionChanged event of Status SortDirection
/// </summary>
public RelayCommand StatusSortDirectionSelectionChangedCommand
{
get
{
if (_statusSortDirectionSelectionChangedCommand == null)
{
_statusSortDirectionSelectionChangedCommand = new RelayCommand(
OnStatusSortDirectionSelectionChangedCommand);
}
return _statusSortDirectionSelectionChangedCommand;
}
}
private RelayCommand _statusSortDirectionSelectionChangedCommand;
private void OnStatusSortDirectionSelectionChangedCommand()
{
if (StatusSortDirectionIndex == 0)
{
if (StatusSortOrder != null)
{
var currentInt = StatusSortOrder;
StatusSortOrder = null;
_currentMaxSortOrder = _currentMaxSortOrder - 1;
if (InstructorNameSortOrder != null && InstructorNameSortOrder > currentInt)
{
InstructorNameSortOrder = InstructorNameSortOrder - 1;
}
if (HireDateSortOrder != null && HireDateSortOrder > currentInt)
{
HireDateSortOrder = HireDateSortOrder - 1;
}
if (SalarySortOrder != null && SalarySortOrder > currentInt)
{
SalarySortOrder = SalarySortOrder - 1;
}
}
}
else
{
if (StatusSortOrder == null)
{
_currentMaxSortOrder++;
StatusSortOrder = _currentMaxSortOrder;
}
}
}
/// <summary>
/// Command for SelectionChanged event of Salary SortDirection
/// </summary>
public RelayCommand SalarySortDirectionSelectionChangedCommand
{
get
{
if (_salarySortDirectionSelectionChangedCommand == null)
{
_salarySortDirectionSelectionChangedCommand = new RelayCommand(
OnSalarySortDirectionSelectionChangedCommand);
}
return _salarySortDirectionSelectionChangedCommand;
}
}
private RelayCommand _salarySortDirectionSelectionChangedCommand;
private void OnSalarySortDirectionSelectionChangedCommand()
{
if (SalarySortDirectionIndex == 0)
{
if (SalarySortOrder != null)
{
var currentInt = SalarySortOrder;
SalarySortOrder = null;
_currentMaxSortOrder = _currentMaxSortOrder - 1;
if (InstructorNameSortOrder != null && InstructorNameSortOrder > currentInt)
{
InstructorNameSortOrder = InstructorNameSortOrder - 1;
}
if (HireDateSortOrder != null && HireDateSortOrder > currentInt)
{
HireDateSortOrder = HireDateSortOrder - 1;
}
if (StatusSortOrder != null && StatusSortOrder > currentInt)
{
StatusSortOrder = StatusSortOrder - 1;
}
}
}
else
{
if (SalarySortOrder == null)
{
_currentMaxSortOrder++;
SalarySortOrder = _currentMaxSortOrder;
}
}
}
#endregion "Public Commands"
#region "Private Methods"
/// <summary>
/// Message handler for InstructorFilterMessage
/// </summary>
/// <param name="notification"></param>
private void OnInstructorFilterMessage(NotificationMessageAction<ClientFilter<Instructor>> notification)
{
_notification = notification;
}
/// <summary>
/// Reset all to initial settings
/// </summary>
private void ResetAll()
{
InstructorName = string.Empty;
HireDate = null;
Status = null;
Salary = null;
InstructorNameSortOrder = null;
HireDateSortOrder = null;
StatusSortOrder = null;
SalarySortOrder = null;
InstructorNameSortDirectionIndex = 0;
HireDateSortDirectionIndex = 0;
StatusSortDirectionIndex = 0;
SalarySortDirectionIndex = 0;
JointCondition1Index = 0;
JointCondition2Index = 0;
JointCondition3Index = 0;
HireDateCompareOperatorIndex = 0;
StatusCompareOperatorIndex = 0;
SalaryCompareOperatorIndex = 0;
_currentMaxSortOrder = 0;
}
/// <summary>
/// Parse and create ClientFilter object
/// </summary>
/// <returns></returns>
private ClientFilter<Instructor> ParseClientFilter()
{
var clientFilter = new ClientFilter<Instructor>();
// dynamically build WhereExpression
// filter by Instructor Name
if (!string.IsNullOrEmpty(InstructorName))
{
clientFilter = clientFilter.And(n => n.Name.Contains(InstructorName));
}
// filter by Instructor Hire Date
var clientFilter1 = new ClientFilter<Instructor>();
if (HireDate != null)
{
switch (HireDateCompareOperatorIndex)
{
case 0: // >=
clientFilter1 = clientFilter1.And(
n => n.HireDate >=
((DateTime) HireDate).Date);
break;
case 1: // >
clientFilter1 = clientFilter1.And(
n => n.HireDate >=
((DateTime) HireDate).Date.AddDays(1));
break;
case 2: // =
clientFilter1 = clientFilter1.And(
n => n.HireDate >=
((DateTime) HireDate).Date &&
n.HireDate < ((DateTime) HireDate).Date.AddDays(1));
break;
case 3: // <
clientFilter1 = clientFilter1.And(
n => n.HireDate <
((DateTime) HireDate).Date);
break;
case 4: // <=
clientFilter1 = clientFilter1.And(
n => n.HireDate <
((DateTime) HireDate).Date.AddDays(1));
break;
}
}
clientFilter = JointCondition1Index == 0
? clientFilter.And(clientFilter1.WhereExpression) // Joint Condition : And
: clientFilter.Or(clientFilter1.WhereExpression); // Joint Condition : Or
// filter by Instructor Status
clientFilter1 = new ClientFilter<Instructor>();
if (Status != null)
{
switch (StatusCompareOperatorIndex)
{
case 0: // =
clientFilter1 =
clientFilter1.And(n => n.Status == (StatusEnum) Status);
break;
case 1: // !=
clientFilter1 =
clientFilter1.And(n => n.Status != (StatusEnum) Status);
break;
}
}
clientFilter = JointCondition2Index == 0
? clientFilter.And(clientFilter1.WhereExpression) // Joint Condition : And
: clientFilter.Or(clientFilter1.WhereExpression); // Joint Condition : Or
// filter by Salary
clientFilter1 = new ClientFilter<Instructor>();
if (Salary != null)
{
switch (SalaryCompareOperatorIndex)
{
case 0: // >=
clientFilter1 = clientFilter1.And(
n => n.Salary >= Salary);
break;
case 1: // >
clientFilter1 = clientFilter1.And(
n => n.Salary > Salary);
break;
case 2: // =
clientFilter1 = clientFilter1.And(
n => n.Salary == Salary);
break;
case 3: // <
clientFilter1 = clientFilter1.And(
n => n.Salary < Salary);
break;
case 4: // <=
clientFilter1 = clientFilter1.And(
n => n.Salary <= Salary);
break;
}
}
clientFilter = JointCondition3Index == 0
? clientFilter.And(clientFilter1.WhereExpression) // Joint Condition : And
: clientFilter.Or(clientFilter1.WhereExpression); // Joint Condition : Or
// dynamically build SortExpressions
if (InstructorNameSortOrder != null && InstructorNameSortOrder == 1)
{
clientFilter = InstructorNameSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Name) // Ascending
: clientFilter.OrderByDescending(n => n.Name); // Descending
}
else if (HireDateSortOrder != null && HireDateSortOrder == 1)
{
clientFilter = HireDateSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.HireDate) // Ascending
: clientFilter.OrderByDescending(n => n.HireDate); // Descending
}
else if (StatusSortOrder != null && StatusSortOrder == 1)
{
clientFilter = StatusSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Status) // Ascending
: clientFilter.OrderByDescending(n => n.Status); // Descending
}
else if (SalarySortOrder != null && SalarySortOrder == 1)
{
clientFilter = SalarySortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Salary) // Ascending
: clientFilter.OrderByDescending(n => n.Salary); // Descending
}
if (InstructorNameSortOrder != null && InstructorNameSortOrder == 2)
{
clientFilter = InstructorNameSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Name) // Ascending
: clientFilter.OrderByDescending(n => n.Name); // Descending
}
else if (HireDateSortOrder != null && HireDateSortOrder == 2)
{
clientFilter = HireDateSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.HireDate) // Ascending
: clientFilter.OrderByDescending(n => n.HireDate); // Descending
}
else if (StatusSortOrder != null && StatusSortOrder == 2)
{
clientFilter = StatusSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Status) // Ascending
: clientFilter.OrderByDescending(n => n.Status); // Descending
}
else if (SalarySortOrder != null && SalarySortOrder == 2)
{
clientFilter = SalarySortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Salary) // Ascending
: clientFilter.OrderByDescending(n => n.Salary); // Descending
}
if (InstructorNameSortOrder != null && InstructorNameSortOrder == 3)
{
clientFilter = InstructorNameSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Name) // Ascending
: clientFilter.OrderByDescending(n => n.Name); // Descending
}
else if (HireDateSortOrder != null && HireDateSortOrder == 3)
{
clientFilter = HireDateSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.HireDate) // Ascending
: clientFilter.OrderByDescending(n => n.HireDate); // Descending
}
else if (StatusSortOrder != null && StatusSortOrder == 3)
{
clientFilter = StatusSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Status) // Ascending
: clientFilter.OrderByDescending(n => n.Status); // Descending
}
else if (SalarySortOrder != null && SalarySortOrder == 3)
{
clientFilter = SalarySortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Salary) // Ascending
: clientFilter.OrderByDescending(n => n.Salary); // Descending
}
if (InstructorNameSortOrder != null && InstructorNameSortOrder == 4)
{
clientFilter = InstructorNameSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Name) // Ascending
: clientFilter.OrderByDescending(n => n.Name); // Descending
}
else if (HireDateSortOrder != null && HireDateSortOrder == 4)
{
clientFilter = HireDateSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.HireDate) // Ascending
: clientFilter.OrderByDescending(n => n.HireDate); // Descending
}
else if (StatusSortOrder != null && StatusSortOrder == 4)
{
clientFilter = StatusSortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Status) // Ascending
: clientFilter.OrderByDescending(n => n.Status); // Descending
}
else if (SalarySortOrder != null && SalarySortOrder == 4)
{
clientFilter = SalarySortDirectionIndex == 1
? clientFilter.OrderBy(n => n.Salary) // Ascending
: clientFilter.OrderByDescending(n => n.Salary); // Descending
}
// the default sort order is PersonId
if (clientFilter.SortExpressions.Count == 0)
{
clientFilter = clientFilter.OrderBy(n => n.PersonId);
}
return clientFilter;
}
#endregion "Private Methods"
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET