BExIS.Dlm.Services.Data.DatasetManager.getHistoricTuples C# (CSharp) Method

getHistoricTuples() private method

private getHistoricTuples ( DatasetVersion datasetVersion ) : List
datasetVersion BExIS.Dlm.Entities.Data.DatasetVersion
return List
        private List<AbstractTuple> getHistoricTuples(DatasetVersion datasetVersion)
        {
            //get previous versions including the version specified, because  the data tuples belong to all versions greater or equal to their original versions.
            List<Int64> versionIds = getPreviousVersionIds(datasetVersion);            //get all tuples from the main tuples table belonging to one of the previous versions + the current version
            List<DataTuple> tuples = DataTupleRepo.Get(p => versionIds.Contains(p.DatasetVersion.Id)).ToList();

            // get those history tuples that represent editedVersion versions of data tuples changed from at least one of the effective versions and not committed to them (them: the effective versions).
            // any single data tuple can be editedVersion by a specific version once at most.
            // it is possible for a tuple to have beed changed many times between any given two versions v(x) and v(y), so it is required to group the tuples based on their original ID and then select the record corresponding to the max version
            var editedTupleVersionsGrouped = DataTupleVerionRepo.Query(p => (p.TupleAction == TupleAction.Edited)
                                                                            && (versionIds.Contains(p.DatasetVersion.Id))
                                                                            && !(versionIds.Contains(p.ActingDatasetVersion.Id)))
                                                                .GroupBy(p => p.OriginalTuple.Id)
                                                                .Select(p => new { OriginalTupleId = p.Key, MaxVersionOfTheTuple = p.Max(l => l.DatasetVersion.Id) })
                                                                .ToList();

            IList<DataTupleVersion> editedTuples = new List<DataTupleVersion>();

            // having a list of original tuple id and related max version, now its time to build a proper query to fetch the actual data tuple versions from the database, the following block builds a dynamic predicate
            // to be passed to the where clause of the data retrieval method at: DataTupleVerionRepo.Query(...)
            if (editedTupleVersionsGrouped.Count >= 1)
            {
                var param1 = Expression.Parameter(typeof(DataTupleVersion), "p");
                var exp1 =
                    Expression.AndAlso(
                    Expression.Equal(
                        Expression.Property(Expression.Property(param1, "OriginalTuple"), "Id"),
                        Expression.Constant(editedTupleVersionsGrouped.First().OriginalTupleId)
                    ),
                    Expression.Equal(
                        Expression.Property(Expression.Property(param1, "DatasetVersion"), "Id"),
                        Expression.Constant(editedTupleVersionsGrouped.First().MaxVersionOfTheTuple)
                    )
                    );
                if (editedTupleVersionsGrouped.Count > 1)
                {
                    foreach (var item in editedTupleVersionsGrouped.Skip(1))
                    {
                        //var param = Expression.Parameter(typeof(DataTupleVersion), "p");
                        var exp =
                            Expression.AndAlso(
                            Expression.Equal(
                                Expression.Property(Expression.Property(param1, "OriginalTuple"), "Id"),
                                Expression.Constant(item.OriginalTupleId)
                            ),
                            Expression.Equal(
                                Expression.Property(Expression.Property(param1, "DatasetVersion"), "Id"),
                                Expression.Constant(item.MaxVersionOfTheTuple)
                            )
                            );
                       exp1 = Expression.OrElse(exp1, exp); ;

                    }
                }
                var typedExpression = Expression.Lambda<Func<DataTupleVersion, bool>>(exp1, new ParameterExpression[] { param1 });
                editedTuples = DataTupleVerionRepo.Query(typedExpression).ToList();
            }

            var deletedTuples = DataTupleVerionRepo.Get(p => (p.TupleAction == TupleAction.Deleted)
                                                            && (versionIds.Contains(p.DatasetVersion.Id))
                                                            && !(versionIds.Contains(p.ActingDatasetVersion.Id)))
                                                   .Cast<AbstractTuple>()
                                                   .ToList();

            List<AbstractTuple> result = tuples

                .Union(editedTuples.Cast<AbstractTuple>())
                .Union(deletedTuples)
                // there is no guarantee that the overall list is ordered as its original order! because 1: OrderNo is not set yet. 2: OrderNo is not managed during the changes and so on,
                // 3: The timestamp of the current tuples is indeed the timestamp of the change made by their latest acting version, but history record are carrying the original timestamp. but as there should be no overlap between the two table records
                // and history records have smaller timestamps, no side effect is expected. 4: I don't know why but ...
                .OrderBy(p => p.OrderNo).OrderBy(p => p.Timestamp)
                .ToList();
            return (result);
        }

Same methods

DatasetManager::getHistoricTuples ( DatasetVersion datasetVersion, int pageNumber, int pageSize ) : List