public ActEntryResolution IdsFor(HistoryRequest request, ActEntryOptions options, int[] actCodes)
        {
            var codeArg        = actCodes.Select(_ => _.ToString()).Join(",");
            var entryTimeArg   = request.EntryTimeArg();
            var orderDirection = request.SortOrder();

            var findByFocusTypeAndId = "focus_type = {0} AND focus_lowid = {1}".ToFormat(options.FocusType, options.FocusId);
            var focusArg             = findByFocusTypeAndId;
            var workflowObjectInfo   = WorkflowObjectInfo.GetObjectInfo(request.WorkflowObject.Type);

            if (workflowObjectInfo.UseParticipantActEntryModel)
            {
                focusArg = "(({0}) OR objid IN (SELECT participant2act_entry FROM table_participant WHERE {0}))".ToFormat(findByFocusTypeAndId);
            }

            var command = "SELECT COUNT(1) FROM table_act_entry WHERE act_code IN ({0}){1} AND {2}".ToFormat(codeArg, entryTimeArg, focusArg);
            var helper  = new SqlHelper(command);
            var count   = (int)helper.ExecuteScalar();

            if (count == 0)
            {
                return(new ActEntryResolution
                {
                    Count = 0,
                    Ids = new int[0]
                });
            }

            command = "SELECT TOP {0} objid, entry_time FROM table_act_entry WHERE act_code IN ({1}){2} AND {3} ORDER BY entry_time {4}, objid {4}".ToFormat(request.SqlLimit(), codeArg, entryTimeArg, focusArg, orderDirection);
            helper  = new SqlHelper(command);

            DateTime?last = null;
            var      ids  = new List <int>();

            using (var reader = helper.ExecuteReader())
            {
                while (reader.Read())
                {
                    var objid = reader.GetInt32(0);
                    ids.Add(objid);

                    last = reader.GetDateTime(reader.GetOrdinal("entry_time"));
                }
            }

            return(new ActEntryResolution
            {
                Count = count,
                Ids = ids,
                LastTimestamp = last
            });
        }
Esempio n. 2
0
        public ActEntryResolution IdsFor(HistoryRequest request, int[] actCodes)
        {
            var codeArg        = actCodes.Select(_ => _.ToString()).Join(",");
            var entryTimeArg   = request.EntryTimeArg();
            var orderDirection = request.SortOrder();

            var idArg = "";
            var workflowObjectInfo      = WorkflowObjectInfo.GetObjectInfo(request.WorkflowObject.Type);
            var inverseActivityRelation = workflowObjectInfo.ActivityRelation;

            if (inverseActivityRelation.IsEmpty())
            {
                throw new InvalidOperationException("Cannot traverse from {0} to act_entry".ToFormat(request.WorkflowObject.Type));
            }

            var activityRelation = _schema.GetRelation("act_entry", inverseActivityRelation).Name;

            if (workflowObjectInfo.IDFieldName.IsEmpty() || workflowObjectInfo.IDFieldName == "objid")
            {
                idArg = "{0} = {1}".ToFormat(activityRelation, request.WorkflowObject.Id);
            }
            else
            {
                var objId = (int)new SqlHelper("SELECT objid FROM table_{0} WHERE {1} = '{2}'".ToFormat(workflowObjectInfo.DatabaseTable, workflowObjectInfo.IDFieldName, request.WorkflowObject.Id)).ExecuteScalar();
                idArg = "{0} = {1}".ToFormat(activityRelation, objId);
            }

            var command = "SELECT COUNT(1) FROM table_act_entry WHERE act_code IN ({0}){1} AND {2}".ToFormat(codeArg, entryTimeArg, idArg);
            var helper  = new SqlHelper(command);
            var count   = (int)helper.ExecuteScalar();

            if (count == 0)
            {
                return(new ActEntryResolution
                {
                    Count = 0,
                    Ids = new int[0]
                });
            }

            command = "SELECT TOP {0} objid, entry_time FROM table_act_entry WHERE act_code IN ({1}){2} AND {3} ORDER BY entry_time {4}, objid {4}".ToFormat(request.SqlLimit(), codeArg, entryTimeArg, idArg, orderDirection);
            helper  = new SqlHelper(command);

            DateTime?last = null;
            var      ids  = new List <int>();

            using (var reader = helper.ExecuteReader())
            {
                while (reader.Read())
                {
                    var objid = reader.GetInt32(0);
                    ids.Add(objid);

                    last = reader.GetDateTime(reader.GetOrdinal("entry_time"));
                }
            }

            return(new ActEntryResolution
            {
                Count = count,
                Ids = ids,
                LastTimestamp = last
            });
        }
        private ActEntryResult resolveEntries(HistoryRequest request, int[] caseActCodes, int[] subcaseActCodes)
        {
            var actCodes = new List <int>(caseActCodes);

            actCodes.AddRange(subcaseActCodes);

            if (!actCodes.Any())
            {
                return(new ActEntryResult
                {
                    Count = 0,
                    Subcases = new int[0],
                    CaseEntries = new int[0],
                    SubcaseEntries = new int[0]
                });
            }

            var codeArg      = actCodes.Select(_ => _.ToString()).Join(",");
            var entryTimeArg = request.EntryTimeArg();
            var order        = request.SortOrder();

            var objId   = (int)new SqlHelper("SELECT objid FROM table_case WHERE id_number = '{0}'".ToFormat(request.WorkflowObject.Id)).ExecuteScalar();
            var command = "SELECT COUNT(1) FROM table_act_entry WHERE act_code IN ({0}){1} AND (act_entry2case = {2} OR act_entry2subcase IN (SELECT objid FROM table_subcase WHERE subcase2case = {2}))".ToFormat(codeArg, entryTimeArg, objId);
            var helper  = new SqlHelper(command);
            var count   = (int)helper.ExecuteScalar();

            if (count == 0)
            {
                return(new ActEntryResult
                {
                    Count = 0,
                    Subcases = new int[0],
                    CaseEntries = new int[0],
                    SubcaseEntries = new int[0]
                });
            }

            command = "SELECT objid FROM table_subcase WHERE subcase2case = {0}".ToFormat(objId);
            helper  = new SqlHelper(command);
            var ids = new List <int>();

            using (var reader = helper.ExecuteReader())
            {
                while (reader.Read())
                {
                    var objid = reader.GetInt32(0);
                    ids.Add(objid);
                }
            }

            command = new StringBuilder("SELECT TOP ")
                      .Append(request.SqlLimit())
                      .Append(" objid, act_entry2case, act_entry2subcase, entry_time FROM table_act_entry WHERE ")
                      .AppendFormat("((act_code IN ({0}) AND act_entry2case = {1})", caseActCodes.Select(_ => _.ToString()).Join(","), objId)
                      .Append(" OR ")
                      .AppendFormat("(act_code IN ({0}) AND act_entry2subcase IN (SELECT objid FROM table_subcase WHERE subcase2case = {1})))", subcaseActCodes.Select(_ => _.ToString()).Join(","), objId)
                      .Append(entryTimeArg)
                      .AppendFormat(" ORDER BY entry_time {0}, objid {0}", order)
                      .ToString();

            helper = new SqlHelper(command);

            var      caseIds       = new List <int>();
            var      subcaseIds    = new List <int>();
            DateTime?lastTimestamp = null;

            using (var reader = helper.ExecuteReader())
            {
                while (reader.Read())
                {
                    var objid = reader.GetInt32(0);
                    if (!reader.IsDBNull(reader.GetOrdinal("act_entry2subcase")))
                    {
                        subcaseIds.Add(objid);
                    }
                    else
                    {
                        caseIds.Add(objid);
                    }

                    lastTimestamp = reader.GetDateTime(reader.GetOrdinal("entry_time"));
                }
            }

            return(new ActEntryResult
            {
                Count = count,
                Subcases = ids.ToArray(),
                CaseEntries = caseIds.ToArray(),
                SubcaseEntries = subcaseIds.ToArray(),
                LastTimestamp = lastTimestamp
            });
        }