|
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Input;
namespace SimpleAttendance
{
public class OverviewModel : INotifyPropertyChanged
{
public OverviewModel()
{
// Set the command properties
ComputeTotalsCommand = new DelegateCommand(ComputeTotals, CanComputeTotals);
}
#region ComputeTotalsCommand
public ICommand ComputeTotalsCommand { get; set; }
public void ComputeTotals(object param)
{
if (!(DesignerProperties.IsInDesignTool))
{
LoadData();
}
}
private bool CanComputeTotals(object param)
{
return true;
}
#endregion
// Operations
#region LoadData
public void LoadData()
{
bool hasKeys = false;
foreach (var item in SterlingService.Current.Database.Query<Student, int>())
{
hasKeys = true;
break;
}
if (hasKeys)
{
// Clear All Collections
Students.Clear();
Enrollments.Clear();
colAttendance.Clear();
colStudentOverview.Clear();
// Get the data
foreach (var item in SterlingService.Current.Database.Query<Student, int>())
{
Students.Add(item.LazyValue.Value);
}
foreach (var item in SterlingService.Current.Database.Query<Enrollment, int>())
{
Enrollments.Add(item.LazyValue.Value);
}
foreach (var item in SterlingService.Current.Database.Query<Attendance, string>())
{
colAttendance.Add(item.LazyValue.Value);
}
// Create the Query
var result = from Student in Students
select new StudentOverview
{
StudentId = Student.StudentId,
Name = Student.Name,
P = (from objEnrollment in Enrollments
from objAttendance in colAttendance
where objEnrollment.StudentId == Student.StudentId
where objEnrollment.EnrollmentId == objAttendance.EnrollmentId
where objAttendance.AttendanceStatus == "P"
select objAttendance).Count(),
T = (from objEnrollment in Enrollments
from objAttendance in colAttendance
where objEnrollment.StudentId == Student.StudentId
where objEnrollment.EnrollmentId == objAttendance.EnrollmentId
where objAttendance.AttendanceStatus == "T"
select objAttendance).Count(),
E = (from objEnrollment in Enrollments
from objAttendance in colAttendance
where objEnrollment.StudentId == Student.StudentId
where objEnrollment.EnrollmentId == objAttendance.EnrollmentId
where objAttendance.AttendanceStatus == "E"
select objAttendance).Count(),
U = (from objEnrollment in Enrollments
from objAttendance in colAttendance
where objEnrollment.StudentId == Student.StudentId
where objEnrollment.EnrollmentId == objAttendance.EnrollmentId
where objAttendance.AttendanceStatus == "U"
select objAttendance).Count(),
};
// Fill the final Collection
foreach (var Student in result)
{
colStudentOverview.Add(Student);
}
}
}
#endregion
// Properties (View Models)
#region MainPageModelVM
private MainPageModel _MainPageModelVM;
public MainPageModel MainPageModelVM
{
get { return _MainPageModelVM; }
set
{
if (MainPageModelVM == value)
{
return;
}
_MainPageModelVM = value;
this.NotifyPropertyChanged("MainPageModelVM");
}
}
#endregion
// Collections
#region colStudentOverview
private ObservableCollection<StudentOverview> _colStudentOverview
= new ObservableCollection<StudentOverview>();
public ObservableCollection<StudentOverview> colStudentOverview
{
get { return _colStudentOverview; }
private set
{
if (colStudentOverview == value)
{
return;
}
_colStudentOverview = value;
this.NotifyPropertyChanged("colStudentOverview");
}
}
#endregion
#region Students
private ObservableCollection<Student> _Students
= new ObservableCollection<Student>();
public ObservableCollection<Student> Students
{
get { return _Students; }
private set
{
if (Students == value)
{
return;
}
_Students = value;
this.NotifyPropertyChanged("Students");
}
}
#endregion
#region Enrollments
private ObservableCollection<Enrollment> _Enrollments
= new ObservableCollection<Enrollment>();
public ObservableCollection<Enrollment> Enrollments
{
get { return _Enrollments; }
private set
{
if (Enrollments == value)
{
return;
}
_Enrollments = value;
this.NotifyPropertyChanged("Enrollments");
}
}
#endregion
#region colAttendance
private ObservableCollection<Attendance> _colAttendance
= new ObservableCollection<Attendance>();
public ObservableCollection<Attendance> colAttendance
{
get { return _colAttendance; }
set
{
if (colAttendance == value)
{
return;
}
_colAttendance = value;
this.NotifyPropertyChanged("colAttendance");
}
}
#endregion
// Utility
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
}
|
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.