using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using s = System;
using cub = ClientUtil;
using cu = ClientUtil.Items;
using du = DomainUtil;
using u = Util;
using ns = Presenter;
using ui=UIInterface.Items;
using uib=UIInterface;
using si=ServiceInterface;
using uitb=UIType;
using uit=UIType.Items;
namespace Presenter
{
public class Items : C, ui.IPresenter
{
si.IService _Service;
ui.IView _view;
uitb.Predicate _Predicate;
uit.Sort _Sort;
uib.IViewFactory _ViewFactory;
cub.CentsFormatter _CentsFormatter;
du.Participants _Participants;
DTO.Cache _Cache;
cub.OwnershipDictionary _OwnershipDictionary;
IList<uit.IItem> _data;
Dictionary<string, int> _dictOfDltAllocAccntIDsByName;
GlobalType.IParticipant[] _arrParticipants;
string[] _arrDltAllocAccntIDs;
protected override UIInterface.IView GetView(){return _view;}
public Items(uib.IViewFactory theViewFactory, si.IService theService)
{
_Service = theService;
_Participants = CreateDomainParticipants(_Service.GetParticipants());
_Cache = _Service.GetCache();
_OwnershipDictionary = new ClientUtil.OwnershipDictionary(_Cache.Ownerships, _Participants);
_ViewFactory = theViewFactory;
_Predicate = new uitb.NotYetPostedPredicate();
_Sort = uit.Sort.Natural;
_CentsFormatter = new cub.CentsFormatter();
_dictOfDltAllocAccntIDsByName = new Dictionary<string, int>();
foreach(DTO.Cache.DltAllocAccnt aDltAllocAccnt in _Cache.DltAllocAccnts)
{
_dictOfDltAllocAccntIDsByName.Add(aDltAllocAccnt.Name, aDltAllocAccnt.ID);
}
_view = theViewFactory.CreateViewForItems();
_view.SetPresenter(this);
_arrParticipants = _Participants.GetArryOfObjects();
_arrDltAllocAccntIDs = _dictOfDltAllocAccntIDsByName.Keys.ToArray();
}
public void Show()
{
_view.Show(_arrParticipants, _arrDltAllocAccntIDs);
}
static IList<uit.IItem> Sort(uit.Sort eSort, IList<uit.IItem> lst)
{
IEnumerable<uit.IItem> strm=null;
if ( eSort == uit.Sort.ParticipantShareAccnt ) strm = from aItem in lst orderby aItem.ParticipantName, aItem.ShareRatioOfFirstToAll, aItem.DltAllocAccntName, aItem.Descrip , aItem.ItemID select aItem;
else if ( eSort == uit.Sort.ShareAccnt ) strm = from aItem in lst orderby aItem.ShareRatioOfFirstToAll, aItem.DltAllocAccntName, aItem.Descrip , aItem.ItemID select aItem;
else if ( eSort == uit.Sort.AccntShare ) strm = from aItem in lst orderby aItem.DltAllocAccntName, aItem.ShareRatioOfFirstToAll, aItem.Descrip , aItem.ItemID select aItem;
else if ( eSort == uit.Sort.Natural ) strm = from aItem in lst orderby aItem.TransactionDate descending, aItem.TransactionID descending , aItem.ItemID select aItem;
else
throw new Exception("Unknown sort");
return strm.ToList();
}
void UpdateUIAndSet(uit.Sort aSort, uitb.Predicate aPredicate, IList<uit.IItem> data)
{
foreach(uit.IItem aItem in data)
{
aItem.SetExternalObjects(_CentsFormatter, _dictOfDltAllocAccntIDsByName);
}
_view.UpdateUI(data, aPredicate==null?"":aPredicate.ToString(), aSort);
_Sort = aSort;
_Predicate = aPredicate;
_data = data;
}
du.ITransaction GetTransaction(int i)
{
uit.IItem aItem = _data[i];
du.ITransaction aTransaction = du.TransactionFactory.CreateDomainTransation(_Service.GetTransaction(aItem.TransactionID));
return aTransaction;
}
void LLRefreshDesired(uitb.Predicate aPredicate, uit.Sort eSort)
{
if (aPredicate == null)
{
UpdateUIAndSet(eSort, null, null);
return;
}
if(aPredicate.ParticipantID.HasValue)
throw new NotSupportedException();
DTO.TransPred aTransPred = cub.TransPredFactory.Create(aPredicate);
IList<DTO.ItemEx> lstNaked = _Service.GetItemsInTransactions(aTransPred);
IList<uit.IItem> lst = lstNaked.Select(aNakedItem=>(uit.IItem)new cu.Item(aNakedItem)).ToList();
lst = Sort(eSort, lst);
UpdateUIAndSet(eSort, aPredicate, lst);
}
void LLEnsureSort(uit.Sort eSort)
{
if(_Sort == eSort)
return;
IList<uit.IItem> lst = Sort(eSort, _data);
UpdateUIAndSet(eSort, _Predicate, lst);
}
void LLExecuteTransactionActionOption(uitb.TransactionActionOption aTransactionActionOption, int iIndex)
{
if(aTransactionActionOption is uitb.TAODelete)
{
uit.IItem aItem = _data[iIndex];
if (aItem.PostID.HasValue)
{
_view.ShowSimpleMessage("Can't delete a posted transaction.");
return;
}
this._Service.DeleteTransaction(aItem.TransactionID,aItem.TransactionRowVersion);
LLRefreshDesired(_Predicate, _Sort);
}
else if(aTransactionActionOption is uitb.TAOViewEdit)
{
du.ITransaction aTransaction = GetTransaction(iIndex);
new Transaction(_ViewFactory, _Service, aTransaction);
}
else
throw new s.Exception();
}
void LLExecutePredicateOption(uitb.PredicateOption aPredicateOption)
{
uitb.Predicate aPredicate = GetPredicateFromPredicateOption(aPredicateOption, _ViewFactory, _Participants, _Predicate);
if(aPredicate==null)
return;
LLRefreshDesired(aPredicate, _Sort);
}
void LLDisplayOfTransactionActionOptionsDesired(int iDataIndex, object oViewData)
{
uit.IItem aItem = _data[iDataIndex];
List<uitb.TransactionActionOption> lstTransactionActionOptions = new List<uitb.TransactionActionOption>();
lstTransactionActionOptions.Add(new uitb.TAOViewEdit());
if(!aItem.PostID.HasValue)
lstTransactionActionOptions.Add(new uitb.TAODelete());
_view.DisplayTransactionActionOptions(lstTransactionActionOptions, iDataIndex, aItem.TransactionID, aItem.TransactionDescrip, oViewData);
}
void LLDisplayOfPredicateOptionsDesired(object view_data)
{
List<uitb.PredicateOption> lstPredicateOptions = new List<uitb.PredicateOption>();
lstPredicateOptions.Add(new uitb.PredOptNotYetPosted());
// lstPredicateOptions.Add(new uib.PredOptPosted());
// lstPredicateOptions.Add(new uib.PredOptionAllBefore());
lstPredicateOptions.Add(new uitb.PredOptPrevXDays(20));
lstPredicateOptions.Add(new uitb.PredOptPrevXDays(30));
lstPredicateOptions.Add(new uitb.PredOptPrevXDays(40));
lstPredicateOptions.Add(new uitb.PredOptPrevXMonths(2));
lstPredicateOptions.Add(new uitb.PredOptPrevXMonths(3));
lstPredicateOptions.Add(new uitb.PredOptPrevXMonths(4));
lstPredicateOptions.Add(new uitb.PredOptID());
lstPredicateOptions.Add(new uitb.PredOptIDRange());
// lstPredicateOptions.Add(new uitb.PredOptDatesAndPart());
_view.DisplayPredicateOptions(lstPredicateOptions, view_data);
}
void LLSaveAndRefreshDesired()
{
List<DTO.ItemUpdate> lst = new List<DTO.ItemUpdate>();
Dictionary<string, int> dictDltAllocAccntIDsByName = null;
foreach (uit.IItem aItem in this._data)
{
if (!aItem.Modified)
continue;
if(dictDltAllocAccntIDsByName ==null)
{
dictDltAllocAccntIDsByName = new Dictionary<string,int>();
foreach(DTO.Cache.DltAllocAccnt aDltAllocAccnt in _Cache.DltAllocAccnts)
{
dictDltAllocAccntIDsByName[aDltAllocAccnt.Name] = aDltAllocAccnt.ID;
}
}
var aItemUpdate = new DTO.ItemUpdate(
aItem.TransactionID,
aItem.ItemID,
dictDltAllocAccntIDsByName[aItem.DltAllocAccntName],
new u.ROArray<int>(aItem.GetAmountsArray()));
lst.Add(aItemUpdate);
}
if (lst.Count == 0)
{
_view.ClearBusyIndicator();
_view.ShowSimpleMessage("There are no items to update.");
return;
}
_Service.ResetItemsInTransactions(lst.ToArray());
LLRefreshDesired(_Predicate, _Sort);
}
public void EnsureSort(uit.Sort eSort)
{
u.ActionCommand<uit.Sort> cmd = new Util.ActionCommand<uit.Sort>(LLEnsureSort, eSort);
ExecuteWithErrorHandlingAndBusyIndicator(cmd);
}
public void RefreshDesired()
{
u.ActionCommand<uitb.Predicate, uit.Sort> cmd = new Util.ActionCommand<uitb.Predicate, uit.Sort>(LLRefreshDesired, _Predicate, _Sort);
ExecuteWithErrorHandlingAndBusyIndicator(cmd);
}
public void ExecuteTransactionActionOption(uitb.TransactionActionOption aTransactionActionOption, int iIndex)
{
var cmd = new Util.ActionCommand<uitb.TransactionActionOption, int>(LLExecuteTransactionActionOption, aTransactionActionOption, iIndex);
ExecuteWithErrorHandlingAndBusyIndicator(cmd);
}
public void ExecutePredicateOption(uitb.PredicateOption aPredicateOption)
{
var cmd = new Util.ActionCommand<uitb.PredicateOption>(LLExecutePredicateOption, aPredicateOption);
ExecuteWithErrorHandlingAndBusyIndicator(cmd);
}
public void DisplayOfTransactionActionOptionsDesired(int iTransactionIndex, object view_data)
{
var aActionCommand = new u.ActionCommand<int, object>(LLDisplayOfTransactionActionOptionsDesired, iTransactionIndex, view_data);
ExecuteWithErrorHandlingAndBusyIndicator(aActionCommand);
}
public void DisplayOfPredicateOptionsDesired(object view_data)
{
u.ActionCommand<object> cmd = new Util.ActionCommand<object>(LLDisplayOfPredicateOptionsDesired, view_data);
ExecuteWithErrorHandling(cmd);
}
public void SaveAndRefreshDesired()
{
ExecuteWithErrorHandlingAndBusyIndicator(LLSaveAndRefreshDesired);
}
}
}