public HistoryResult HistoryFor(HistoryRequest request, IHistoryBuilder builder)
        {
            var activityCodes = new List <int>();
            var gatherer      = new ActEntryGatherer(activityCodes, request.ShowAllActivities, request.WorkflowObject, _services, _user, _privileges);
            var map           = _models.Find(request.WorkflowObject);

            map.Accept(gatherer);

            var options = new ActEntryOptions
            {
                FocusType = _schema.GetTableNumber(request.WorkflowObject.Type),
                FocusId   = int.Parse(request.WorkflowObject.Id)
            };
            var actEntries = IdsFor(request, options, activityCodes.ToArray());

            var items = actEntries.Ids.Any()
                                ? builder.GetAll(request, options, generic => generic.Filter(_ => _.IsIn("objid", actEntries.Ids.ToArray())))
                                : new ModelData[0];

            return(new HistoryResult
            {
                HistoryItemLimit = request.HistoryItemLimit,
                Since = request.Since,
                TotalResults = actEntries.Count,
                Items = request.ReverseOrder
                                        ? items.OrderBy(_ => _.Get <DateTime>("timestamp")).ThenBy(_ => _.Get <int>("id")).ToArray()
                                        : items.OrderByDescending(_ => _.Get <DateTime>("timestamp")).ThenByDescending(_ => _.Get <int>("id")).ToArray(),
                NextTimestamp = HistoryResult.DetermineNextTimestamp(request, actEntries)
            });
        }
        public HistoryResult HistoryFor(HistoryRequest request, IHistoryBuilder builder)
        {
            var activityCodes = new List <int>();
            var gatherer      = new ActEntryGatherer(activityCodes, request.ShowAllActivities, request.WorkflowObject, _services, _user, _privileges);
            var map           = _models.Find(request.WorkflowObject);

            map.Accept(gatherer);

            var policy     = _policies.LastOrDefault(_ => _.Matches(request)) ?? _default;
            var actEntries = policy.IdsFor(request, activityCodes.ToArray());

            var items = actEntries.Ids.Any()
                                ? builder.GetAll(request, generic => generic.Filter(_ => _.IsIn("objid", actEntries.Ids.ToArray())))
                                : new ModelData[0];

            return(new HistoryResult
            {
                HistoryItemLimit = request.HistoryItemLimit,
                Since = request.Since,
                TotalResults = actEntries.Count,
                Items = request.ReverseOrder
                                        ? items.OrderBy(_ => _.Get <DateTime>("timestamp")).ThenBy(_ => _.Get <int>("id")).ToArray()
                                        : items.OrderByDescending(_ => _.Get <DateTime>("timestamp")).ThenByDescending(_ => _.Get <int>("id")).ToArray(),
                NextTimestamp = HistoryResult.DetermineNextTimestamp(request, actEntries)
            });
        }
        public HistoryResult HistoryFor(HistoryRequest request, IHistoryBuilder builder)
        {
            var caseActivityCodes = determineActCodes(new WorkflowObject {
                Type = "case"
            }, request.ShowAllActivities);
            var subcaseActivityCodes = determineActCodes(new WorkflowObject {
                Type = "subcase", IsChild = true
            }, request.ShowAllActivities);

            var actEntries       = resolveEntries(request, caseActivityCodes.ToArray(), subcaseActivityCodes.ToArray());
            var caseHistoryItems = actEntries.CaseEntries.Any()
                                ? builder.GetAll(request, generic => generic.Filter(_ => _.IsIn("objid", actEntries.CaseEntries.ToArray())), workflowGeneric => workflowGeneric.Filter(_ => _.Equals("id_number", request.WorkflowObject.Id)))
                                : new ModelData[0];

            var childRequest = new HistoryRequest
            {
                WorkflowObject = new WorkflowObject {
                    Type = WorkflowObject.Subcase, IsChild = true
                },
                ShowAllActivities = request.ShowAllActivities
            };
            var subcaseHistoryItems = actEntries.SubcaseEntries.Any() && actEntries.Subcases.Any()
                                ? builder.GetAll(childRequest,
                                                 actEntryGeneric => actEntryGeneric.Filter(_ => _.IsIn("objid", actEntries.SubcaseEntries.ToArray())),
                                                 workflowGeneric => workflowGeneric.Filter(_ => _.IsIn("objid", actEntries.Subcases.ToArray())))
                                : new ModelData[0];

            var items = new List <ModelData>();

            items.AddRange(caseHistoryItems);
            items.AddRange(subcaseHistoryItems);

            var combinedItems = request.ReverseOrder
                                ? items.OrderBy(_ => _.Get <DateTime>("timestamp")).ThenBy(_ => _.Get <int>("id")).ToArray()
                                : items.OrderByDescending(_ => _.Get <DateTime>("timestamp")).ThenByDescending(_ => _.Get <int>("id")).ToArray();

            return(new HistoryResult
            {
                HistoryItemLimit = request.HistoryItemLimit,
                Since = request.Since,
                TotalResults = actEntries.Count,
                Items = combinedItems,
                NextTimestamp = request.HistoryItemLimit > (actEntries.CaseEntries.Length + actEntries.SubcaseEntries.Length)
                                        ? null
                                        : actEntries.LastTimestamp
            });
        }