//===============================================================================
// PJM Interconnection, LLC. Sprego Application
// Comments provided by Glenn Long
//===============================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIComposition.Services;
using UIComposition.Views.RegoView;
using System.ComponentModel;
using System.Reflection;
namespace UIComposition.Views.RegoView
{
using System.Windows.Controls;
using System.Windows.Forms;
using Infragistics.Windows.DataPresenter;
using System.Windows.Data;
using System.Reflection;
///
/// Interaction logic for SpregoView.xaml
///
public partial class RegoView : IRegoView
{
private int index = 0;
private decimal unitscheduleid = 0;
private DateTime mktday;
private decimal segmentid = 0;
private decimal mw = 0;
private decimal price = 0;
XamDataGrid _Schedulesgrid;
XamDataGrid _Unitsgrid;
bool _isSynching;
Schedules d;
public int count = 0;
public static bool isViewCreated = false;
public static ICollectionView schedView = null;
#region Public Properties
public decimal Unitscheduleid
{
get { return unitscheduleid; }
set { unitscheduleid = value; }
}
public DateTime Mktday
{
get { return mktday; }
set { mktday = value; }
}
public decimal Segmentid
{
get { return segmentid; }
set { segmentid = value; }
}
public decimal MW
{
get { return mw; }
set { mw = value; }
}
public decimal Price
{
get { return price; }
set { price = value; }
}
#endregion
public RegoView()
{
// This is required to load the view
InitializeComponent();
this.RegoUnitsList.CellActivated += new EventHandler(RegoUnitsList_CellActivated);
}
public RegoPresentationModel Model
{
get { return this.DataContext as RegoPresentationModel; }
set { RegoUpdater(value); }
}
private void RegoUpdater(RegoPresentationModel value)
{
var x = (from p in typeof( RegoPresentationModel ).GetProperties() where p.PropertyType == typeof( RegoDislpayHelper ) select p);
this.DataContext = value;
this.RegoUnitsList.DataContext = value;
this.RegoUnitsList.DataSource = value.RegoUnitsMaster.UnitsList.ToList();
this.RegoSchedulesList.DataContext = value;
this.RegoSchedulesList.DataSource = (from r in value.RegoUnitsMaster.SchedulesList[0].SchedulesList orderby r.UnitID select new { Schedules = r.UnitID, r.UnitScheduleID, r.Unitschedulelongname, r.MinDown, r.MaxRun, r.MinRun, Segments = r.RegoSegments }).ToList();
}
private void RegoSchedulesList_FieldLayoutInitialized(object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e)
{
string xaml = GetXamlForFieldLayout(e.FieldLayout);
Console.WriteLine(xaml);
}
static string GetXamlForFieldLayout(FieldLayout fieldLayout)
{
StringBuilder buffer = new StringBuilder();
buffer.AppendLine("");
buffer.AppendLine(" ");
foreach (Field field in fieldLayout.Fields)
{
if (field.IsExpandableResolved == false)
{
buffer.AppendFormat(" ", field.Name);
buffer.AppendLine();
}
}
buffer.AppendLine(" ");
buffer.Append("");
return buffer.ToString();
}
// Filter Call
//void SynchronizeActiveRecordWithCurrentItem()
void RegoUnitsList_CellActivated(object sender, Infragistics.Windows.DataPresenter.Events.CellActivatedEventArgs e)
{
_Schedulesgrid = this.RegoSchedulesList;
_Unitsgrid = this.RegoUnitsList;
bool isCreated = false;
int hasRecords = 0;
//ICollectionView schedView = null;
var view = this.GetCurrentSchedulesView();
if (view == null)
{
return;
}
else
{
// Store the first set of records as we will need to call on them after the first search.
if (isViewCreated == false)
{
schedView = view;
isViewCreated = true;
}
}
var unitsview = this.GetCurrentUnitsView();
if (unitsview == null)
return;
Record activeRecord = _Unitsgrid.ActiveRecord;
if (activeRecord != null && activeRecord.IsSelected)
activeRecord.IsSelected = false;
// Postion the view so that it is even with the active Record
unitsview.MoveCurrentToPosition(activeRecord.Index);
// Get current Unit Record
var currentUnit = unitsview.CurrentItem;
if (currentUnit == null)
{
_Unitsgrid.ActiveRecord = null;
}
else
{
var currentUnitRec = _Unitsgrid.GetRecordFromDataItem(currentUnit, false);
if (currentUnitRec != null)
{
currentUnitRec.IsActive = true;
currentUnitRec.IsSelected = true;
//_Unitsgrid.BringRecordIntoView(currentUnitRec);
}
schedView.Filter = delegate(object item)
{
List MySegments = new List();
var stronglyTyped = GlennsTypeHack.Cast(item, new { Schedules = default(Nullable), UnitScheduleID = default(decimal), Unitschedulelongname = default(string), MinDown = default(string), MaxRun = default(string), MinRun = default(string), Segments = MySegments });
var SchedulesList = (new[] { stronglyTyped }).ToList();
isCreated = true;
// Reset view so we can add the new records
//this.RegoSchedulesList.DataSource = null;
if (stronglyTyped.Schedules.ToString() == currentUnitRec.Cells["UnitID"].Value.ToString() && stronglyTyped != null)
{
if (isCreated == true && (SchedulesList.Count >= 1))
{
hasRecords++;
if (hasRecords == 1)
{
// Don't add the current record, it's already there
// We do however need to set the DataSource with the first record if there is only one.
this.RegoSchedulesList.DataSource = SchedulesList;
}
else
{
SchedulesList.Add(new { stronglyTyped.Schedules, stronglyTyped.UnitScheduleID, stronglyTyped.Unitschedulelongname, stronglyTyped.MinDown, stronglyTyped.MaxRun, stronglyTyped.MinRun, stronglyTyped.Segments });
this.RegoSchedulesList.DataSource = SchedulesList;
}
}
return true;
}
else
{
return false;
}
};
view.Refresh();
}
}
ICollectionView GetCurrentSchedulesView()
{
return _Schedulesgrid.DataSource == null ? null : CollectionViewSource.GetDefaultView(_Schedulesgrid.DataSource);
}
ICollectionView GetCurrentUnitsView()
{
return _Unitsgrid.DataSource == null ? null : CollectionViewSource.GetDefaultView(_Unitsgrid.DataSource);
}
} // End Class
public class RegoDislpayHelper : INotifyPropertyChanged
{
public int Index { get; set; }
public RegoDislpayHelper() { }
public RegoDislpayHelper(ASEngine.BusinessObjects.Entities.ASRegoUnits units)
{
_units = units;
_schedules = new List();
foreach (ASEngine.BusinessObjects.Entities.ASRegoUnit unit in UnitsList)
unit.PropertyChanged += (sender, e) =>
{
// Raising PropertyChanged for the AllUnitsAreChecked
// property causes the data binding system to query its
// getter for the new value.
if (e.PropertyName == "Committed")
this.OnPropertyChanged("AllUnitsAreChecked");
};
this.Index = 0;
}
ASEngine.BusinessObjects.Entities.ASRegoUnits _units;
List _schedules;
public List SchedulesList { get { return _schedules; } }
public ASEngine.BusinessObjects.Entities.ASRegoUnits UnitsList { get { return _units; } }
public decimal? AllUnitsAreChecked
{
get
{
// Determine if all units have the same
// value for the IsChecked property.
decimal? value = null;
for (int i = 0; i < this.UnitsList.Count; ++i)
{
if (i == 0)
{
value = this._units[0].Committed;
}
else if (value != this._units[i].Committed)
{
value = null;
break;
}
}
return value;
}
set
{
if (value == null)
{
return;
}
else
{
foreach (ASEngine.BusinessObjects.Entities.ASRegoUnit unit in this.UnitsList)
{
unit.Committed = value.Value;
}
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string prop)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
#endregion // INotifyPropertyChanged Members
}
public class Schedules
{
public Schedules(ASEngine.BusinessObjects.Entities.ASRegoSchedules schedules)
{
_schedules = schedules;
}
ASEngine.BusinessObjects.Entities.ASRegoSchedules _schedules;
public ASEngine.BusinessObjects.Entities.ASRegoSchedules SchedulesList { get { return _schedules; } }
}
public class Schedule
{
private string maxrun;
private string mindown;
private string minrun;
private Nullable schedules;
private ASEngine.BusinessObjects.Entities.ASRegoSeqment segments;
private decimal unitscheduleid;
private string unitschedulelongname;
public Schedule()
{
}
public string MaxRun
{
get { return maxrun; }
}
public string MinDown
{
get { return mindown; }
}
public string MinRun
{
get { return minrun; }
}
public Nullable Schedules
{
get { return schedules; }
}
public ASEngine.BusinessObjects.Entities.ASRegoSeqment Segments
{
get { return segments; }
}
public decimal UnitScheduleID
{
get { return unitscheduleid; }
}
public string Unitschedulelongname
{
get { return unitschedulelongname; }
}
public Schedule(Nullable schedules, decimal unitscheduleid, string unitschedulelongname, string mindown, string maxrun,
string minrun, ASEngine.BusinessObjects.Entities.ASRegoSeqment segments)
{
this.maxrun = maxrun;
this.mindown = mindown;
this.minrun = minrun;
this.schedules = schedules;
this.segments = segments;
this.unitscheduleid = unitscheduleid;
this.unitschedulelongname = unitschedulelongname;
}
}
public class Segments
{
public Segments(ASEngine.BusinessObjects.Entities.ASRegoSeqments segments)
{
_segments = segments;
}
ASEngine.BusinessObjects.Entities.ASRegoSeqments _segments;
public ASEngine.BusinessObjects.Entities.ASRegoSeqments SegmentsList { get { return _segments; } }
}
static class GlennsTypeHack
{
internal static T Cast(object target, T example)
{
return (T)target;
}
}
public class ListRegoList : List
{
public ListRegoList()
{
{
RegoDislpayHelper l;
Schedules d;
List LSegments = new List();
// Load Data Structure
foreach (KeyValuePair units in RegoService.regoUnitsView)
{
foreach (ASEngine.BusinessObjects.Entities.ASRegoUnit unit in units.Value)
{
Add(l = new RegoDislpayHelper(units.Value));
foreach (KeyValuePair schedules in RegoService.regoSchedulesView)
{
// The Schedules record includes the segments which are loaded in the baseDAL object on the server.
l.SchedulesList.Add(d = new Schedules(schedules.Value));
}
}
}
}
}
public ListRegoList(decimal caseId)
{
{
RegoDislpayHelper l = null;
Schedules d = null;
// Load Data Structure per caseId
foreach (KeyValuePair units in RegoService.regoUnitsView)
{
if (units.Key == caseId)
{
foreach (ASEngine.BusinessObjects.Entities.ASRegoUnit unit in units.Value)
{
Add(l = new RegoDislpayHelper(units.Value));
foreach (KeyValuePair schedules in RegoService.regoSchedulesView)
{
if (schedules.Key == caseId)
{
l.SchedulesList.Add(d = new Schedules(schedules.Value));
} // end if
}
}
} //end if
}
}
}
}
}