namespace BCG { using Bts.DataStreamCore; using Spark.Core.Collections; using Spark.Core.Diagnostics; using System; using System.Collections; using System.Collections.Generic; using System.Linq; [DataStreamUserRespresentation("oldproject")] public class ProjectStream : ViewStream { private ProjectHelper? myHelper; readonly string? myColExp; readonly string? myTableName; readonly ViewStream? myVs; private IDictionary? myDic; IDisposable? subscription; /// /// /// stream to susbsribe to as the input stream /// expression defining the column mapping public ProjectStream (ViewStream vs, string colExp) { myVs = vs; myColExp = colExp; } /// /// /// stream to subscribe to as the input stream /// expression defining the column mapping /// name of the mapped table public ProjectStream (ViewStream vs, string colExp, string tableName) { if (colExp == "!") mySetRealSource (new ChangeTableNameStream (vs, tableName)); else { myVs = vs; myColExp = colExp; myTableName = tableName; } } /// /// /// stream to subscribe to as the input stream /// dictionary mapping the aliased to the original public ProjectStream (ViewStream vs, IDictionary dic) : this(vs, GenericDictionaryWrapper.Create(dic)) {} public ProjectStream (ViewStream vs, IEnumerable> dic) { myVs = vs; myDic = dic.ToDictionary(kv => kv.Value, kv => kv.Key); } protected override void myOnFirstSubscriber() => subscription = ExceptionAssert.MemberNotNull(myVs).Subscribe(myHandleChange); protected override void myOnLastSubscriber() { subscription?.Dispose(); subscription = null; } internal void myHandleChange (object newValue) { var dp = (IDataPacket)newValue; if (myHelper != null) { IBlueRowCollection dels, adds; BlueDataUtility.getTrueDeletesAndAdds (dp.deletedRows, dp.addedRows, out dels, out adds, myHelper.relevantOrigCols); if (dels.Count + adds.Count > 0) myFireEvent (new DataPacket (new ProjectedTable (dp.newRows, myHelper), new ProjectedTable (adds, myHelper), new ProjectedTable (dels, myHelper) ) ); } else { //first time a callback is received myHelper = myDic == null ? new ProjectHelper(dp.Schema, ExceptionAssert.MemberNotNull(myColExp), myTableName) : new ProjectHelper (dp.Schema, myDic, myTableName); IBlueRowCollection temp; myFireEvent (new DataPacket (temp = new ProjectedTable (dp.newRows, myHelper), temp, new BlueTable (temp.schema) ) ); } } public void myHandleChangeTest(object newValue) { myHandleChange(newValue); } } }