Click here to Skip to main content
15,886,873 members
Articles / Desktop Programming / Windows Forms

Three-tier .NET Application Utilizing Three ORM Technologies

Rate me:
Please Sign up or sign in to vote.
4.95/5 (118 votes)
30 Jan 2010CPOL109 min read 163.9K   4.4K   437  
LINQ to SQL, Entity Framework, and NHibernate used in a parallel fashion in a three-tier WinForms application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using s = System;
using du = DomainUtil;
using u = Util;
using uitb=UIType;
using gt=GlobalType;

namespace ClientUtil
{
	public class OwnershipDictionary
	{
		Dictionary<int, int[]> _inner = new Dictionary<int, int[]>();
		int _iCount;
		string CreateKey(int iNWAccntID, int iParticipantID) { return iNWAccntID.ToString() + ":" + iParticipantID.ToString(); }
		public OwnershipDictionary(u.ROArray<DTO.Cache.Ownership> lstOwnerships, du.Participants aParticipants)
		{
			_iCount = aParticipants.Count;
			Dictionary<string, int> dictShare = new Dictionary<string, int>();
			foreach (DTO.Cache.Ownership aOwnership in lstOwnerships)
			{
				string strKey = CreateKey(aOwnership.NWAccntID, aOwnership.ParticipantID);
				dictShare.Add(strKey, aOwnership.Share);
			}

			Dictionary<int, object> dictNWAccntIDs = new Dictionary<int, object>();
			foreach (DTO.Cache.Ownership aOwnership in lstOwnerships)
			{
				if(!dictNWAccntIDs.ContainsKey(aOwnership.NWAccntID))
					dictNWAccntIDs.Add(aOwnership.NWAccntID, null);
			}

			foreach (int iNWAccntID in dictNWAccntIDs.Keys)
			{
				int[] arrShares = new int[_iCount];
				for (int i = 0; i < aParticipants.Count; i++)
				{
					string strKey = CreateKey(iNWAccntID, aParticipants.IDFromIndex(i));
					int iShare;
					if(dictShare.TryGetValue(strKey, out iShare))
						arrShares[i] = iShare;
				}
				_inner.Add(iNWAccntID, arrShares);
			}
		}
		public int[] GetParticipantShares(int iNWAccntID)
		{
			int[] shares;
			_inner.TryGetValue(iNWAccntID, out shares);
			if(shares==null)
				return new int[_iCount];
			return shares;
		}
	}
	public class Summary : uitb.ISummary
	{

		uitb.NWAccntDltSum[] _arrNWAccntDltSums;
		uitb.DltAllocSum[] _arrDltAllocSums;
		uitb.SummaryItem[] _arrSummaryItems;
		uitb.BudgetReport _BudgetReport;

		public IEnumerable<uitb.NWAccntDltSum> GetNWAccntDltSums(	) { return _arrNWAccntDltSums; }
		public IEnumerable<uitb.DltAllocSum>   GetDltAllocSums(		) { return _arrDltAllocSums; }
		public IEnumerable<uitb.SummaryItem>   GetSummaryItems(		) { return _arrSummaryItems; }
		public uitb.BudgetReport TheBudgetReport{get{return _BudgetReport;}}

		private static uitb.SummaryItem CreateSummaryNWAccnts(uitb.NWAccntDltSum[] arrNWAccntDltSums, int iParticipantCount)
		{
			int[] arrNWAccntAmounts = new int[iParticipantCount];
			foreach (uitb.NWAccntDltSum aNWAccntDltSum in arrNWAccntDltSums)
			{
				for (int i = 0; i < iParticipantCount; i++)
				{
					arrNWAccntAmounts[i] += aNWAccntDltSum.GetParticipantAmount(i);
				}
			}
			return new uitb.SummaryItem("NWAccnts", arrNWAccntAmounts);
		}
		private static uitb.SummaryItem CreateSummaryDltAllocAccnts(uitb.DltAllocSum[] arrDltAllocSums, int iParticipantCount)
		{
			int[] arrDltAllocAccntAmounts = new int[iParticipantCount];
			foreach (uitb.DltAllocSum aDltAllocSum in arrDltAllocSums)
			{
				for (int i = 0; i < iParticipantCount; i++)
				{
					arrDltAllocAccntAmounts[i] += aDltAllocSum.GetParticipantAmount(i);
				}
			}
			return new uitb.SummaryItem("DltAllocAccnts", arrDltAllocAccntAmounts);
		}
		private static uitb.SummaryItem CreateSummaryNWAccntPluWash(uitb.SummaryItem aSummaryItemWash, uitb.SummaryItem aSummaryItemNWAccnt, int iParticipantCount)
		{
			int[] arrNWAccntPlusWashAmounts = new int[iParticipantCount];
			for (int i = 0; i < iParticipantCount; i++)
			{
				arrNWAccntPlusWashAmounts[i] = aSummaryItemWash.GetParticipantAmount(i) + aSummaryItemNWAccnt.GetParticipantAmount(i);
			}
			return new uitb.SummaryItem("Wash + NWAccnts", arrNWAccntPlusWashAmounts);
			
		}
		private static uitb.SummaryItem CreateSummaryError(uitb.SummaryItem aSummaryItemNWAccntPlusWash, uitb.SummaryItem aSummaryItemDltAllocAccnt, int iParticipantCount)
		{
			int[] arrErrorAmounts = new int[iParticipantCount];
			for (int i = 0; i < iParticipantCount; i++)
			{
				arrErrorAmounts[i] = aSummaryItemNWAccntPlusWash.GetParticipantAmount(i) - aSummaryItemDltAllocAccnt.GetParticipantAmount(i);
			}
			return new uitb.SummaryItem("Error", arrErrorAmounts);
		}

		private static uitb.NWAccntDltSum[] CreateNWAccntDltSums(
			u.ROArray<DTO.NWAccntDltSum> arrDomainDALNWAccntDltSums,
			Dictionary<int, string> dictNWAccntNameByID,
			OwnershipDictionary aOwnershipDictionary)
		{
			int iRecCount = arrDomainDALNWAccntDltSums.Count;
			uitb.NWAccntDltSum[] arrNWAccntDltSums = new uitb.NWAccntDltSum[iRecCount];
			for (int i = 0; i < iRecCount; i++)
			{
				DTO.NWAccntDltSum input = arrDomainDALNWAccntDltSums[i];
				int[] shares = aOwnershipDictionary.GetParticipantShares(input.NWAccntID);
				string name = dictNWAccntNameByID[input.NWAccntID];

				float[] arrParticipantShares2 = new float[shares.Length];
				for (int j = 0; j < shares.Length; j++)
				{
					arrParticipantShares2[j] = shares[j];
				}
				int[] arrParticipantAmounts = du.Misc.DistributeValue(input.Amount, arrParticipantShares2);


		//		arrNWAccntDltSums[i] = new uit.NWAccntDltSum(name, input.Amount, shares);
				arrNWAccntDltSums[i] = new uitb.NWAccntDltSum(name, input.Budgeted, arrParticipantAmounts);
			}
			return arrNWAccntDltSums;
		}
		
		private static uitb.DltAllocSum[] CreateDltAllocSums(
			u.ROArray<DTO.DltAllocSum> arrDomainDALDltAllocSums, 
			Dictionary<int, string> dictDltAllocAccntNameByID)
		{
			int iRecCount = arrDomainDALDltAllocSums.Count;
			uitb.DltAllocSum[] arrDltAllocSums = new uitb.DltAllocSum[iRecCount];
			for (int i = 0; i < iRecCount; i++)
			{
				DTO.DltAllocSum input = arrDomainDALDltAllocSums[i];
				string name = dictDltAllocAccntNameByID[input.DltAllocAccntID];
				arrDltAllocSums[i] = new uitb.DltAllocSum(name, input.Budgeted, input.ParticipantAmounts.ToArray());
			}
			return arrDltAllocSums;
		}

		private static uitb.SummaryItem[] CreateSummaryItems(
			int[]			arrPartcipantWashes,
			uitb.NWAccntDltSum[] arrNWAccntDltSums,
			uitb.DltAllocSum[] arrDltAllocSums,
			int iParticipantCount)
		{
			List<uitb.SummaryItem> lstSummaryItems = new List<uitb.SummaryItem>();
			
			uitb.SummaryItem aSummaryItemWash = new uitb.SummaryItem("Wash", arrPartcipantWashes);
			lstSummaryItems.Add(aSummaryItemWash);

			uitb.SummaryItem aSummaryItemNWAccnt = CreateSummaryNWAccnts(arrNWAccntDltSums, iParticipantCount);
			lstSummaryItems.Add(aSummaryItemNWAccnt);

			uitb.SummaryItem aSummaryItemNWAccntPlusWash = CreateSummaryNWAccntPluWash(aSummaryItemWash, aSummaryItemNWAccnt, iParticipantCount);
			lstSummaryItems.Add(aSummaryItemNWAccntPlusWash);

			uitb.SummaryItem aSummaryItemDltAllocAccnt = CreateSummaryDltAllocAccnts(arrDltAllocSums, iParticipantCount);
			lstSummaryItems.Add(aSummaryItemDltAllocAccnt);

			uitb.SummaryItem aSummaryItemError = CreateSummaryError(aSummaryItemNWAccntPlusWash, aSummaryItemDltAllocAccnt, iParticipantCount);
			lstSummaryItems.Add(aSummaryItemError);

			return lstSummaryItems.ToArray();
		}
		static uitb.BudgetDelta CreateBudgetDelta(DTO.BudgetDelta si_BudgetDelta)
		{
			if(si_BudgetDelta==null)
				return null;
			string strFormat = "#,###.00";
			int? iDays = si_BudgetDelta.Days;
			float? fAllocation = null;
			if(si_BudgetDelta.Allocation.HasValue)
				fAllocation=   ((float)si_BudgetDelta.Allocation	)/100F;
			float fBudgetExpensesMatchedTransactions			=-1*((float)si_BudgetDelta.DltAllocs	)/100F;
			float fFundingUsageMinusFundingMatchedTransactions	=-1*((float)si_BudgetDelta.NWDlts		)/100F;
			
			float fDeltaMatchedTransactions			= 
				fAllocation.GetValueOrDefault()
				- fBudgetExpensesMatchedTransactions 
				+ fFundingUsageMinusFundingMatchedTransactions;
			
			return new uitb.BudgetDelta(
				iDays.HasValue?iDays.Value.ToString():null,
				fAllocation.HasValue?fAllocation.Value.ToString(strFormat):null,
				fBudgetExpensesMatchedTransactions				.ToString(strFormat),
				fFundingUsageMinusFundingMatchedTransactions	.ToString(strFormat),
				fDeltaMatchedTransactions						.ToString(strFormat));
		}
		static uitb.BudgetReport CreateBudgetReport(DTO.BudgetReport aBudgetReport)
		{
			if(aBudgetReport==null)
				return null;
			return new uitb.BudgetReport(
				CreateBudgetDelta(aBudgetReport.ClippedFromStartDate),
				CreateBudgetDelta(aBudgetReport.ForMatchedTransactions));
		}
		void Init(
			DTO.Summary aDomainDALSummary, 
			Dictionary<int, string> dictNWAccntNameByID, 
			Dictionary<int, string> dictDltAllocAccntNameByID, 
			OwnershipDictionary aOwnershipDictionary)
		{
			int iParticipantCount = aDomainDALSummary.ParticipantWashes.Count;

			_arrNWAccntDltSums	= CreateNWAccntDltSums(	aDomainDALSummary.NWAccntDltSums,	dictNWAccntNameByID, aOwnershipDictionary);
			_arrDltAllocSums	= CreateDltAllocSums(	aDomainDALSummary.DltAllocSums,		dictDltAllocAccntNameByID);
			_arrSummaryItems	= CreateSummaryItems(   aDomainDALSummary.ParticipantWashes.ToArray(), _arrNWAccntDltSums, _arrDltAllocSums, iParticipantCount);
			_BudgetReport		= CreateBudgetReport(aDomainDALSummary.TheBudgetReport);
		}

		void Init(DTO.Summary aDomainDALSummary, DTO.Cache aCache, du.Participants aParticipants)
		{
			Dictionary<int, string> dictNWAccntNameByID = new Dictionary<int,string>();
			Dictionary<int, string> dictDltAllocAccntNameByID  = new Dictionary<int,string>();
			OwnershipDictionary aOwnershipDictionary  = new OwnershipDictionary(aCache.Ownerships, aParticipants);

			foreach (DTO.Cache.NWAccnt aNWAccnt in aCache.NWAccnts)
			{
				dictNWAccntNameByID.Add(aNWAccnt.ID, aNWAccnt.Name);
			}
			foreach (DTO.Cache.DltAllocAccnt aDltAllocAccnt in aCache.DltAllocAccnts)
			{
				dictDltAllocAccntNameByID.Add(aDltAllocAccnt.ID, aDltAllocAccnt.Name);
			}

			Init(aDomainDALSummary, dictNWAccntNameByID, dictDltAllocAccntNameByID, aOwnershipDictionary);		
		}
		public Summary(
			DTO.Summary aDomainDALSummary, 
			Dictionary<int, string> dictNWAccntNameByID, 
			Dictionary<int, string> dictDltAllocAccntNameByID, 
			OwnershipDictionary aOwnershipDictionary)
		{
			Init(aDomainDALSummary, dictNWAccntNameByID, dictDltAllocAccntNameByID, aOwnershipDictionary);
		}
		
		public Summary(DTO.Summary aDomainDALSummary, DTO.Cache aCache, du.Participants aParticipants)
		{
			Init(aDomainDALSummary, aCache, aParticipants);
		}
		public Summary(du.ITransaction aTransaction, DTO.Cache aCache, du.Participants aParticipants)
		{
			//DTO.Summary ddalSummary = new DTO.Summary();
			List<DTO.NWAccntDltSum>	lstNWAccntDltSums	= new List<DTO.NWAccntDltSum>();
			List<DTO.DltAllocSum>		lstDltAllocSums		= new List<DTO.DltAllocSum>();
			int iNWAccntDltBudget = 0;
			int iDltAllocBudget = 0;
			foreach (DTO.NWAccntDlt aNWAccntDlt in aTransaction.NWAccntDlts)
			{
				if(aNWAccntDlt.Budgeted)
					iNWAccntDltBudget += aNWAccntDlt.Amount;
				lstNWAccntDltSums.Add(new DTO.NWAccntDltSum(aNWAccntDlt.NWAccntID, aNWAccntDlt.Budgeted, aNWAccntDlt.Amount));
			}
			foreach (du.IDltAllocSum aDltAllocSum in aTransaction.DltAllocSums)
			{
				int[] arr = new int[aParticipants.Count];
				for(int i=0; i<aParticipants.Count; i++)
				{
					arr[i] = aDltAllocSum.GetParticipantAmount(i);
				}
				if(aDltAllocSum.Budgeted)
					iDltAllocBudget += arr.Sum();
				lstDltAllocSums.Add(new DTO.DltAllocSum(aDltAllocSum.DltAllocAccntID, aDltAllocSum.Budgeted, new u.ROArray<int>(arr)));
			}
			int[] arrWashAmounts = new int[aParticipants.Count];
			for(int i=0; i<aParticipants.Count; i++)
			{
				arrWashAmounts[i] = aTransaction.GetWashAmount(i);
			}
			DTO.BudgetDelta brmt = new DTO.BudgetDelta(
				null,
				null,
				iDltAllocBudget, 
				iNWAccntDltBudget);
			
			DTO.Summary ddalSummary = new DTO.Summary(
				new u.ROArray<DTO.NWAccntDltSum>(lstNWAccntDltSums), 
				new u.ROArray<DTO.DltAllocSum>(lstDltAllocSums), 
				new u.ROArray<int>(arrWashAmounts), 
				new DTO.BudgetReport(null, brmt));
			Init(ddalSummary, aCache, aParticipants);
		}
	}
	public class CentsFormatter : uitb.IIntFormatter
	{
		bool _bAsDollars;
		bool _bReverseSign;
		public bool AsDollars { get { return _bAsDollars; } /*set { _bAsDollars = value; }*/ }
		public bool ReverseSign { get { return _bReverseSign; } /*set { _bReverseSign = value; }*/ }
		public CentsFormatter(bool bAsDollars, bool bReverseSign)
		{
			_bAsDollars = bAsDollars;
			_bReverseSign = bReverseSign;
		}
		public CentsFormatter()
		:this(true, false)
		{
		}
		public int GetIntFromFormattedObject(object value)
		{
			if (value == null)
				return 0;
			if (value is string)
			{
				int i;
				if (_bAsDollars)
				{
					double d;
					try
					{
						d = s.Double.Parse((string)value);
					}
					catch
					{
						throw new s.Exception("Could not parse " + value.ToString() + ". The program is currently configured to expect dollar values.  To enter \"$1,234.56\" enter \"1234.56\".");
					}
					
					//"d*100" dould easily be 99.999999999,
					//if that is casted to an int, 
					//the result will be 99 instead of 100.
					//i= (int)(d * 100);

					i =s.Convert.ToInt32(d*100);
				}
				else
				{
					try
					{
						i= s.Int32.Parse((string)value);
					}
					catch
					{
						throw new s.Exception("Could not parse " + value.ToString() + ". The program is currently configured to expect cents.  To enter \"$1,234.56\" enter \"123456\".");
					}
				}
				if (_bReverseSign)
					i = -1 * i;
				return i;
			}
			throw new ArgumentException("");
		}
		public string FormatInt(int i)
		{
			if (_bReverseSign)
				i = -1 * i;
			if (_bAsDollars)
			{
				double d = i;
				d = d / 100;
				return d.ToString("#,###,##0.00");
			}
			else
			{
				return i.ToString();
			}
		}

	}
	public static class TransPredFactory
	{
		public static DTO.TransPred Create(uitb.Predicate aPredicate)
		{
			if(aPredicate is uitb.NotYetPostedPredicate)
			{
				return new DTO.UnPostedTransPred();
			}
			else if(aPredicate is uitb.PostedPredicate)
			{
				return new DTO.PostedTransPred();
			}
			else if(aPredicate is uitb.MostRecentPredicate)
			{
				var thePredicate = aPredicate as uitb.MostRecentPredicate;
				s.DateTime earliest = s.DateTime.Today.AddDays(-thePredicate.Days);
				return new DTO.OnOrAfterTransPred(earliest);
			}
			else if(aPredicate is uitb.AllBeforePredicate)
			{
				var thePredicate = aPredicate as uitb.AllBeforePredicate;
				return new DTO.BeforeTransPred(thePredicate.TheDateTime);
			}
			else if(aPredicate is uitb.TransactionIDPredicate)
			{
				var thePredicate = aPredicate as uitb.TransactionIDPredicate;
				return new DTO.IDTransPred(thePredicate.ID);
			}
			else if(aPredicate is uitb.TransactionRangePredicate)
			{
				var thePredicate = aPredicate as uitb.TransactionRangePredicate;
				return new DTO.IDRangeTransPred(thePredicate.First, thePredicate.Last);
			}
			else if(aPredicate is uitb.PostIDPredicate)
			{
				var thePredicate = aPredicate as uitb.PostIDPredicate;
				return new DTO.PostIDTransPred(thePredicate.ID);
			}
			else if(aPredicate is uitb.PostRangePredicate)
			{
				var thePredicate = aPredicate as uitb.PostRangePredicate;
				return new DTO.PostIDRangeTransPred(thePredicate.First, thePredicate.Last);
			}
			else if(aPredicate is uitb.MaxPostIDPredicate)
			{
				var thePredicate = aPredicate as uitb.MaxPostIDPredicate;
				return new DTO.MaxPostIDTransPred(thePredicate.ID);
			}
			else if(aPredicate is uitb.MinPostIDPredicate)
			{
				var thePredicate = aPredicate as uitb.MinPostIDPredicate;
				return new DTO.MinPostIDTransPred(thePredicate.ID);
			}
			else if(aPredicate is uitb.DateRangeAndParticipantPredicate)
			{
				var thePredicate = aPredicate as uitb.DateRangeAndParticipantPredicate;
				return new DTO.OptionalDateRangeTransPred(thePredicate.OnOrAfter, thePredicate.Before);
			}
			else
				throw new s.Exception();
		}
	}
	namespace Transactions
	{
		public class DataDisplayObject : uitb.Transactions.IDataDisplayObject
		{
			du.ITransaction _inner;
			du.Participants _Participants;
			CentsFormatter _CentsFormatter;
			public string	Participant		
			{
				get
				{
					if (_inner.ParticipantID.HasValue)	return _Participants.NameFromID(_inner.ParticipantID.Value);
					else								return null;
				} 
			}
			public string	Descrip				{get{return _inner.Descrip	;}}
			public DateTime Instant				{get{return _inner.Instant	;}}
			public int?		ID					{get{return _inner.ID		;}}
			public int?		PostID				{get{return _inner.PostID	;}}
			public string	TotalDltAllocAsStr	{get{return _CentsFormatter.FormatInt(_inner.GetTotalDltAllocsAmount());}}
			public int		TotalDltAllocAsNum	{get{return						  _inner.GetTotalDltAllocsAmount();}}
			public string	BudgetDeltAsStr		{get{return _CentsFormatter.FormatInt(_inner.GetBudgetDelt());}}
			public int		BudgetDeltAsNum		{get{return						  _inner.GetBudgetDelt();}}
			public string	IntField0			{get{return _CentsFormatter.FormatInt(_inner.GetTotalDltAllocAmountForParticipant(0));} }
			public string	IntField1			{get{return _CentsFormatter.FormatInt(_inner.GetTotalDltAllocAmountForParticipant(1));} }
			public string	IntField2			{get{return _CentsFormatter.FormatInt(_inner.GetTotalDltAllocAmountForParticipant(2));} }
			public string	IntField3			{get{return _CentsFormatter.FormatInt(_inner.GetTotalDltAllocAmountForParticipant(3));} }
			public string	IntField4			{get{return _CentsFormatter.FormatInt(_inner.GetTotalDltAllocAmountForParticipant(4));} }
			public string	IntField5			{get{return _CentsFormatter.FormatInt(_inner.GetTotalDltAllocAmountForParticipant(5));} }
			public string	IntField6			{get{return _CentsFormatter.FormatInt(_inner.GetTotalDltAllocAmountForParticipant(6));} }
			public string	IntField7			{get{return _CentsFormatter.FormatInt(_inner.GetTotalDltAllocAmountForParticipant(7));} }
			public string	IntField8			{get{return _CentsFormatter.FormatInt(_inner.GetTotalDltAllocAmountForParticipant(8));} }
			public string	IntField9			{get{return _CentsFormatter.FormatInt(_inner.GetTotalDltAllocAmountForParticipant(9));} }
			public int GetParticipantCount()
			{
				return _Participants.Count;
			}
			public DataDisplayObject(du.ITransaction aTrans, du.Participants aParticipants, CentsFormatter aAmountUI)
			{
				_inner = aTrans;
				_Participants = aParticipants;
				_CentsFormatter = aAmountUI;
			}
			public du.ITransaction Inner{get{return _inner;}}
		}
	}
	namespace DltAllocs
	{
		public class DataDisplayObject : uitb.DltAllocs.IDataDisplayObject
		{
			DTO.DltAlloc _inner;
			du.Participants _Participants;
			CentsFormatter _CentsFormatter;
			float[] _arrShares;
			public int		TransID				{get{return _inner.TransID		;}}
			public int?		PostID				{get{return _inner.PostID		;}}
			public string	TransDescrip		{get{return _inner.TransDescrip	;}}
			public DateTime TransInstant		{get{return _inner.TransInstant	;}}
			public int		ItemID				{get{return _inner.ItemID		;}}
			public string	ItemDescrip			{get{return _inner.ItemDescrip	;}}
			public string	AmountAsStr			{get{return _CentsFormatter.FormatInt(	_inner.ParticipantAmounts.Sum());}}
			public int		AmountAsNum			{get{return								_inner.ParticipantAmounts.Sum() ;}}
			public string	StrField0			{get{return _arrShares[0].ToString();} }
			public string	StrField1			{get{return _arrShares[1].ToString();} }
			public string	StrField2			{get{return _arrShares[2].ToString();} }
			public string	StrField3			{get{return _arrShares[3].ToString();} }
			public string	StrField4			{get{return _arrShares[4].ToString();} }
			public string	StrField5			{get{return _arrShares[5].ToString();} }
			public string	StrField6			{get{return _arrShares[6].ToString();} }
			public string	StrField7			{get{return _arrShares[7].ToString();} }
			public string	StrField8			{get{return _arrShares[8].ToString();} }
			public string	StrField9			{get{return _arrShares[9].ToString();} }

			public DataDisplayObject(DTO.DltAlloc aDltAlloc, du.Participants aParticipants, CentsFormatter aAmountUI)
			{
				_inner = aDltAlloc;
				_Participants = aParticipants;
				_CentsFormatter = aAmountUI;

				int? iMultiplier;
				_arrShares = du.Misc.CreateSharesArray(_inner.ParticipantAmounts, out iMultiplier);
				if (iMultiplier.HasValue && iMultiplier.Value != 1)
					throw new s.Exception("");
			}
			public DTO.DltAlloc Inner{get{return _inner;}}
		}
	}
	namespace Items
	{
		public class Item : uitb.Items.IItem
		{
			DTO.ItemEx _inner;		
			string	_strDltAllocAccntNameNew	;
			int[]	_arrParticipantAmountsNew	;

			//external data
			uitb.IIntFormatter _IntFormatter;
			Dictionary<string, int> _dictOfDltAllocAccntIDsByName;

			//computed
			int			_iTotalOrMultiplierOrig		;
			float[]		_fSharesOrig				;
			int?		_iTotalOrMultiplierNew		;
			float[]		_fSharesNew					;

			public void SetExternalObjects(uitb.IIntFormatter aIntFormatter, Dictionary<string, int> dictOfDltAllocAccntIDsByName)
			{
				_IntFormatter = aIntFormatter;
				_dictOfDltAllocAccntIDsByName = dictOfDltAllocAccntIDsByName;
			}
			void ComputeOrig()
			{
				int? iMultiplier;
				_fSharesOrig = du.Misc.CreateSharesArray(_inner.ParticipantAmountsOrig, out iMultiplier);
				if (iMultiplier.HasValue)
					this._iTotalOrMultiplierOrig = iMultiplier.Value;
				else
					this._iTotalOrMultiplierOrig = _inner.ParticipantAmountsOrig.Aggregate((accum, i) => accum + i);
			}
			void ComputeNew()
			{
				if(_arrParticipantAmountsNew ==null)
				{
					_fSharesNew = null;
					_iTotalOrMultiplierNew = null;
					return;
				}
				int? iMultiplier;
				_fSharesNew = du.Misc.CreateSharesArray(_arrParticipantAmountsNew, out iMultiplier);
				if (iMultiplier.HasValue)
					this._iTotalOrMultiplierNew = iMultiplier.Value;
				else
					this._iTotalOrMultiplierNew = _arrParticipantAmountsNew.Aggregate((accum, i) => accum + i);
			}
			
			public Item(DTO.ItemEx aNakedItem)
			{
				_inner = aNakedItem;

				ComputeOrig();
			}
			public int?			PostID				{ get { return _inner.PostID				; } }
			public int			TransactionID		{ get { return _inner.TransactionID			; } }
			public gt.RowVersion TransactionRowVersion	{ get { return _inner.TransactionRowVersion	; } }
			public string		TransactionDescrip	{ get { return _inner.TransactionDescrip	; } }
			public s.DateTime	TransactionDate		{ get { return _inner.TransactionDate		; } }
			public int?			ParticipantID		{ get { return _inner.ParticipantID			; } }
			public string		ParticipantName		{ get { return _inner.ParticipantName		; } }
			public int			ItemID				{ get { return _inner.ItemID				; } }
			public string		Descrip				{ get { return _inner.Descrip				; } }
			public bool			DltAllocAccountModified		{ get { return _strDltAllocAccntNameNew!=null; } }
			public bool			ParticipantAmountsModified	{ get { return _arrParticipantAmountsNew!=null; } }
			public bool			ParticipantAmountModified(int i)
			{
				if(_arrParticipantAmountsNew==null)
					return false;
				return (_inner.ParticipantAmountsOrig[i] != _arrParticipantAmountsNew[i]);
			}
			public bool			ParticipantShareModified(int i)
			{
				if(_fSharesNew == null)
					return false;
				return (_fSharesOrig[i] != _fSharesNew[i]);
			}
			public bool TotalOrMultiplierModified
			{
				get
				{
					if(_iTotalOrMultiplierNew == null)
						return false;
					return (_iTotalOrMultiplierNew.Value != _iTotalOrMultiplierOrig);
				}
			}
			public bool			Modified					{ get { return ParticipantAmountsModified || DltAllocAccountModified; } }
			public string GetAnyProblemWithUsing_AsAShare(string str)
			{
				float f;
				if(!float.TryParse(str, out f))
					return "Share must be parsable as a float.";
				return null;
			}
			public string GetAnyProblemWithUsing_AsDltAllocAccntName(string str)
			{
				if(!_dictOfDltAllocAccntIDsByName.ContainsKey(str))
					return "Not a valid account name.";
				return null;
			}
			public string DltAllocAccntName
			{
				get
				{
					if(_strDltAllocAccntNameNew!=null)
						return _strDltAllocAccntNameNew;
					return _inner.DltAllocAccntNameOrig;
				}
				set
				{
					string strProblem = GetAnyProblemWithUsing_AsDltAllocAccntName(value);
					if(null!=strProblem)
						throw new s.Exception(strProblem + ": " + value);
					if (_strDltAllocAccntNameNew == value)
						return;
					if(_inner.DltAllocAccntNameOrig == value)
						_strDltAllocAccntNameNew = null;
					else
						_strDltAllocAccntNameNew = value;

				}
			}
			public int			GetAmount(int iParticipantIndex)
			{
				if(_arrParticipantAmountsNew!=null)
					return _arrParticipantAmountsNew[iParticipantIndex];
				return _inner.ParticipantAmountsOrig[iParticipantIndex];
			}
			public float GetShare(int iParticipantIndex)
			{
				if(_fSharesNew!=null)
					return _fSharesNew[iParticipantIndex];
				return _fSharesOrig[iParticipantIndex];
			}
			public float[] GetSharesArray()
			{
				float[] mine;
				if(_fSharesNew!=null)
					mine = _fSharesNew;
				else
					mine = _fSharesOrig;

				float[] arrShares = new float[mine.Length];
				for(int i=0; i<mine.Length; i++)
				{
					arrShares[i] = mine[i];	
				}
				return arrShares;
			}
			public int[] GetAmountsArray()
			{
				int[] mine;
				if(_arrParticipantAmountsNew!=null)
					mine= _arrParticipantAmountsNew;
				else
					mine = _inner.ParticipantAmountsOrig.ToArray();
				
				int[] arrAmounts = new int[mine.Length];
				for (int i = 0; i < mine.Length; i++)
				{
					arrAmounts[i] = mine[i];
				}
				return arrAmounts;
			}
			public float ShareRatioOfFirstToAll
			{
				get
				{
					if(_fSharesNew!=null)
						return _fSharesNew[0] / _fSharesNew.Sum();
					else
						return _fSharesOrig[0] / _fSharesOrig.Sum();
				}
			}
			public int TotalOrMultiplier
			{
				get
				{
					if(_iTotalOrMultiplierNew.HasValue)
						return _iTotalOrMultiplierNew.Value;
					return _iTotalOrMultiplierOrig;
				}
			}
			public string TotalOrMultiplierAsString
			{
				get
				{
					return _IntFormatter.FormatInt(TotalOrMultiplier);
				}
			}
			public void SetShares(float[] arrShares)
			{
				if(arrShares.Length != _inner.ParticipantAmountsOrig.Count)
					throw new Exception();

				int[] arrParticipantAmounts = du.Misc.DistributeValue(TotalOrMultiplier, arrShares);
				bool bNewVal = false;
				for(int i=0; i<_inner.ParticipantAmountsOrig.Count; i++)
				{
					if(_inner.ParticipantAmountsOrig[i] == arrParticipantAmounts[i])
						continue;
					bNewVal = true;
					break;
				}
				if(!bNewVal)
				{
					_iTotalOrMultiplierNew = null;
					_fSharesNew = null;
					_arrParticipantAmountsNew = null;
				}
				else
				{
					_arrParticipantAmountsNew = arrParticipantAmounts;
					ComputeNew();
				}
			}
			public void SetShare(int iParticipantIndex, float fShare)
			{
				float[] arrShares = GetSharesArray();
				arrShares[iParticipantIndex] = fShare;
				SetShares(arrShares);
			}
			public float Share0{get{return GetShare(0);}set{SetShare(0, value);}}
			public float Share1{get{return GetShare(1);}set{SetShare(1, value);}}
			public float Share2{get{return GetShare(2);}set{SetShare(2, value);}}
			public float Share3{get{return GetShare(3);}set{SetShare(3, value);}}
			public float Share4{get{return GetShare(4);}set{SetShare(4, value);}}
			public float Share5{get{return GetShare(5);}set{SetShare(5, value);}}
			public float Share6{get{return GetShare(6);}set{SetShare(6, value);}}
			public float Share7{get{return GetShare(7);}set{SetShare(7, value);}}
			public float Share8{get{return GetShare(8);}set{SetShare(8, value);}}
			public float Share9{get{return GetShare(9);}set{SetShare(9, value);}}

			public string AmountAsString0{get{return _IntFormatter.FormatInt(this.GetAmount(0));}}
			public string AmountAsString1{get{return _IntFormatter.FormatInt(this.GetAmount(1));}}
			public string AmountAsString2{get{return _IntFormatter.FormatInt(this.GetAmount(2));}}
			public string AmountAsString3{get{return _IntFormatter.FormatInt(this.GetAmount(3));}}
			public string AmountAsString4{get{return _IntFormatter.FormatInt(this.GetAmount(4));}}
			public string AmountAsString5{get{return _IntFormatter.FormatInt(this.GetAmount(5));}}
			public string AmountAsString6{get{return _IntFormatter.FormatInt(this.GetAmount(6));}}
			public string AmountAsString7{get{return _IntFormatter.FormatInt(this.GetAmount(7));}}
			public string AmountAsString8{get{return _IntFormatter.FormatInt(this.GetAmount(8));}}
			public string AmountAsString9{get{return _IntFormatter.FormatInt(this.GetAmount(9));}}
		}
	}
	namespace NWAccntDlts
	{
		public class DataDisplayObject : uitb.NWAccntDlts.IDataDisplayObject
		{
			DTO.NWAccntDltEx _inner;
			CentsFormatter _CentsFormatter;
			public int		TransID				{get{return _inner.TransID		;}}
			public int?		PostID				{get{return _inner.PostID		;}}
			public string	TransDescrip		{get{return _inner.TransDescrip	;}}
			public DateTime TransInstant		{get{return _inner.TransInstant	;}}
			public string	AmountAsStr			{get{return _CentsFormatter.FormatInt(_inner.Amount);}}
			public int		AmountAsNum			{get{return						  _inner.Amount;}}

			public DataDisplayObject(DTO.NWAccntDltEx aNWAccntDlt, CentsFormatter aAmountUI)
			{
				_inner = aNWAccntDlt;
				_CentsFormatter = aAmountUI;
			}
			public DTO.NWAccntDltEx Inner{get{return _inner;}}
		}
	}
	namespace TransactionItems
	{
		public class RowData
		{
			public string Descrip;
			public int AmountOrMultiplier;
			public string Account;
			public float[] Shares;
			public static RowData DeepCopy(RowData other)
			{
				RowData aRowData = (RowData)other.MemberwiseClone();
				if (other.Shares != null)
				{
					aRowData.Shares = new float[other.Shares.Length];
					for (int i = 0; i < other.Shares.Length; i++)
					{
						aRowData.Shares[i] = other.Shares[i];
					}
				}
				return aRowData;
			}
		}
	}
	namespace TransactionNWAccntDlts
	{
		public class RowData
		{
			public string Account;
			public int Amount;
			public static RowData DeepCopy(RowData other)
			{
				RowData aRowData = (RowData)other.MemberwiseClone();
				return aRowData;
			}
		}
	}
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Austin Regional Clinic
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions