/// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            var workflow = action.Activity.Workflow;

            workflow.IsPersisted = true;

            if (GetAttributeValue(action, "PersistImmediately").AsBoolean(false))
            {
                var service = new WorkflowService(rockContext);
                if (workflow.Id == 0)
                {
                    service.Add(workflow);
                }

                rockContext.WrapTransaction(() =>
                {
                    rockContext.SaveChanges();
                    workflow.SaveAttributeValues(rockContext);
                    foreach (var activity in workflow.Activities)
                    {
                        activity.SaveAttributeValues(rockContext);
                    }
                });
            }

            action.AddLogEntry("Updated workflow to be persisted!");

            return(true);
        }
        /// <summary>
        /// Adds the person to group.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="person">The person.</param>
        /// <param name="workflowType">Type of the workflow.</param>
        /// <param name="groupMembers">The group members.</param>
        private void AddPersonToGroup(RockContext rockContext, Person person, WorkflowType workflowType, List <GroupMember> groupMembers)
        {
            if (person != null)
            {
                if (!_group.Members
                    .Any(m =>
                         m.PersonId == person.Id &&
                         m.GroupRoleId == _defaultGroupRole.Id))
                {
                    var groupMemberService = new GroupMemberService(rockContext);
                    var groupMember        = new GroupMember();
                    groupMember.PersonId          = person.Id;
                    groupMember.GroupRoleId       = _defaultGroupRole.Id;
                    groupMember.GroupMemberStatus = (GroupMemberStatus)GetAttributeValue("GroupMemberStatus").AsInteger();
                    groupMember.GroupId           = _group.Id;
                    groupMemberService.Add(groupMember);
                    rockContext.SaveChanges();

                    if (workflowType != null)
                    {
                        try
                        {
                            var workflowService = new WorkflowService(rockContext);
                            var workflow        = Workflow.Activate(workflowType, person.FullName);

                            List <string> workflowErrors;
                            if (workflow.Process(rockContext, groupMember, out workflowErrors))
                            {
                                if (workflow.IsPersisted || workflow.IsPersisted)
                                {
                                    if (workflow.Id == 0)
                                    {
                                        workflowService.Add(workflow);
                                    }

                                    rockContext.WrapTransaction(() =>
                                    {
                                        rockContext.SaveChanges();
                                        workflow.SaveAttributeValues(_rockContext);
                                        foreach (var activity in workflow.Activities)
                                        {
                                            activity.SaveAttributeValues(rockContext);
                                        }
                                    });
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ExceptionLogService.LogException(ex, this.Context);
                        }
                    }
                }
            }
        }
Пример #3
0
        private void LaunchWorkflow(Person person)
        {
            using (var rockContext = new RockContext())
            {
                var workflowType = WorkflowTypeCache.Read(_workflowTypeGuid.Value);
                if (workflowType != null && (workflowType.IsActive ?? true))
                {
                    var workflowService = new WorkflowService(rockContext);
                    var workflow        = Rock.Model.Workflow.Activate(workflowType, person.FullName, rockContext);
                    workflowService.Add(workflow);
                    rockContext.SaveChanges();

                    workflow.SetAttributeValue("Person", person.Guid);
                    workflow.SaveAttributeValues();
                }
            }
        }
Пример #4
0
        public virtual void Execute(IJobExecutionContext context)
        {
            JobDataMap dataMap = context.JobDetail.JobDataMap;

            var rockContext     = new RockContext();
            var workflowService = new WorkflowService(rockContext);
            WorkflowTypeCache workflowTypeCache = WorkflowTypeCache.Get(dataMap.GetString("WGuid").AsGuid());

            DataSet data = DbService.GetDataSet(dataMap.GetString("SQLQuery"), CommandType.Text, new Dictionary <string, object>());

            foreach (DataTable tbl in data.Tables)
            {
                foreach (DataRow row in tbl.Rows)
                {
                    Workflow workflow = Workflow.Activate(workflowTypeCache, "New " + workflowTypeCache.WorkTerm);

                    foreach (DataColumn col in tbl.Columns)
                    {
                        workflow.SetAttributeValue(col.ColumnName, row[col].ToString());
                    }

                    if (workflowService.Process(workflow, out List <string> errorMessages))
                    {
                        // If the workflow type is persisted, save the workflow
                        if (workflow.IsPersisted || workflowTypeCache.IsPersisted)
                        {
                            if (workflow.Id == 0)
                            {
                                workflowService.Add(workflow);
                            }

                            rockContext.WrapTransaction(() =>
                            {
                                rockContext.SaveChanges();
                                workflow.SaveAttributeValues(rockContext);
                                foreach (WorkflowActivity activity in workflow.Activities)
                                {
                                    activity.SaveAttributeValues(rockContext);
                                }
                            });
                        }
                    }
                }
            }
        }
        private void PersistWorkflow(Rock.Model.Workflow workflow)
        {
            if (workflow.Id == 0)
            {
                var workflowService = new WorkflowService(_rockContext);
                workflowService.Add(workflow);
            }

            _rockContext.WrapTransaction(() =>
            {
                _rockContext.SaveChanges();
                workflow.SaveAttributeValues(_rockContext);
                foreach (var activity in workflow.Activities)
                {
                    activity.SaveAttributeValues(_rockContext);
                }
            });
        }
Пример #6
0
        private void LaunchWorkflow(String firstName, String lastName, String email)
        {
            using (var rockContext = new RockContext())
            {
                var workflowType = WorkflowTypeCache.Read(_workflowTypeGuid.Value);
                if (workflowType != null && (workflowType.IsActive ?? true))
                {
                    var workflowService = new WorkflowService(rockContext);
                    var workflow        = Rock.Model.Workflow.Activate(workflowType, string.Format("{0} {1}", firstName, lastName), rockContext);
                    workflowService.Add(workflow);
                    rockContext.SaveChanges();

                    workflow.SetAttributeValue("FirstName", firstName);
                    workflow.SetAttributeValue("LastName", lastName);
                    workflow.SetAttributeValue("Email", email);
                    workflow.SaveAttributeValues();
                }
            }
        }
Пример #7
0
        private void SaveForProcessingLater(Rock.Model.Workflow newWorkflow, RockContext rockContext)
        {
            newWorkflow.IsPersisted = true;
            var service = new WorkflowService(rockContext);

            if (newWorkflow.Id == 0)
            {
                service.Add(newWorkflow);
            }

            rockContext.WrapTransaction(() =>
            {
                rockContext.SaveChanges();
                newWorkflow.SaveAttributeValues(rockContext);
                foreach (var activity in newWorkflow.Activities)
                {
                    activity.SaveAttributeValues(rockContext);
                }
            });
        }
Пример #8
0
        protected void LaunchWorkflow(Guid workflowGuid, Dictionary <string, string> attributes)
        {
            RockContext         _rockContext         = new RockContext();
            WorkflowService     _workflowService     = new WorkflowService(_rockContext);
            WorkflowTypeService _workflowTypeService = new WorkflowTypeService(_rockContext);
            WorkflowType        _workflowType        = _workflowTypeService.Get(workflowGuid);

            Workflow _workflow = Rock.Model.Workflow.Activate(_workflowType, "New Test" + _workflowType.WorkTerm);


            foreach (KeyValuePair <string, string> attribute in attributes)
            {
                _workflow.SetAttributeValue(attribute.Key, attribute.Value);
            }


            List <string> errorMessages;

            if (_workflowService.Process(_workflow, out errorMessages))
            {
                // If the workflow type is persisted, save the workflow
                if (_workflow.IsPersisted || _workflowType.IsPersisted)
                {
                    if (_workflow.Id == 0)
                    {
                        _workflowService.Add(_workflow);
                    }

                    _rockContext.WrapTransaction(() =>
                    {
                        _rockContext.SaveChanges();
                        _workflow.SaveAttributeValues(_rockContext);
                        foreach (var activity in _workflow.Activities)
                        {
                            activity.SaveAttributeValues(_rockContext);
                        }
                    });
                }
            }
        }
        /// <summary>
        /// Launches the workflow.
        /// </summary>
        /// <param name="workflowTypeGuid">The workflow type unique identifier.</param>
        /// <param name="family">The family.</param>
        private void LaunchWorkflow(Guid workflowTypeGuid, Group family)
        {
            int adultRoleId = GroupTypeCache.Read(SystemGuid.GroupType.GROUPTYPE_FAMILY.AsGuid()).Roles.Where(r => r.Guid == SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT.AsGuid()).FirstOrDefault().Id;

            var headOfHouse = family.Members.Where(m => m.GroupRoleId == adultRoleId).OrderBy(m => m.Person.Gender).FirstOrDefault();

            // don't launch a workflow if no adult is present in the family
            if (headOfHouse != null && headOfHouse.Person != null && headOfHouse.Person.PrimaryAlias != null)
            {
                var spouse = family.Members.Where(m => m.GroupRoleId == adultRoleId && m.PersonId != headOfHouse.Person.Id).FirstOrDefault();

                using (var rockContext = new RockContext())
                {
                    var workflowTypeService = new WorkflowTypeService(rockContext);
                    var workflowType        = workflowTypeService.Get(workflowTypeGuid);
                    if (workflowType != null)
                    {
                        var workflowService = new WorkflowService(rockContext);
                        var workflow        = Rock.Model.Workflow.Activate(workflowType, headOfHouse.Person.FullName, rockContext);
                        workflowService.Add(workflow);
                        rockContext.SaveChanges();

                        workflow.SetAttributeValue("Family", family.Guid);
                        workflow.SetAttributeValue("HeadOfHouse", headOfHouse.Person.PrimaryAlias.Guid);

                        if (family.Campus != null)
                        {
                            workflow.SetAttributeValue("Campus", family.Campus.Guid);
                        }

                        if (spouse != null && spouse.Person != null && spouse.Person.PrimaryAlias != null)
                        {
                            workflow.SetAttributeValue("Spouse", spouse.Person.PrimaryAlias.Guid);
                        }

                        workflow.SaveAttributeValues();
                    }
                }
            }
        }
Пример #10
0
        private bool HydrateObjects()
        {
            LoadWorkflowType();

            // Set the note type if this is first request
            if (!Page.IsPostBack)
            {
                var noteType = new NoteTypeService(_rockContext).Get(Rock.SystemGuid.NoteType.WORKFLOW_NOTE.AsGuid());
                ncWorkflowNotes.NoteTypeId = noteType.Id;
            }

            if (_workflowType == null)
            {
                ShowMessage(NotificationBoxType.Danger, "Configuration Error", "Workflow type was not configured or specified correctly.");
                return(false);
            }

            if (!_workflowType.IsAuthorized(Authorization.VIEW, CurrentPerson))
            {
                ShowMessage(NotificationBoxType.Warning, "Sorry", "You are not authorized to view this type of workflow.");
                return(false);
            }

            // If operating against an existing workflow, get the workflow and load attributes
            if (!WorkflowId.HasValue)
            {
                WorkflowId = PageParameter("WorkflowId").AsIntegerOrNull();
                if (!WorkflowId.HasValue)
                {
                    Guid guid = PageParameter("WorkflowGuid").AsGuid();
                    if (!guid.IsEmpty())
                    {
                        _workflow = _workflowService.Queryable()
                                    .Where(w => w.Guid.Equals(guid) && w.WorkflowTypeId == _workflowType.Id)
                                    .FirstOrDefault();
                        if (_workflow != null)
                        {
                            WorkflowId = _workflow.Id;
                        }
                    }
                }
            }

            if (WorkflowId.HasValue)
            {
                if (_workflow == null)
                {
                    _workflow = _workflowService.Queryable()
                                .Where(w => w.Id == WorkflowId.Value && w.WorkflowTypeId == _workflowType.Id)
                                .FirstOrDefault();
                }
                if (_workflow != null)
                {
                    _workflow.LoadAttributes();
                    foreach (var activity in _workflow.Activities)
                    {
                        activity.LoadAttributes();
                    }
                }
            }

            // If an existing workflow was not specified, activate a new instance of workflow and start processing
            if (_workflow == null)
            {
                string workflowName = PageParameter("WorkflowName");
                if (string.IsNullOrWhiteSpace(workflowName))
                {
                    workflowName = "New " + _workflowType.WorkTerm;
                }

                _workflow = Rock.Model.Workflow.Activate(_workflowType, workflowName);
                if (_workflow != null)
                {
                    // If a PersonId or GroupId parameter was included, load the corresponding
                    // object and pass that to the actions for processing
                    object entity   = null;
                    int?   personId = PageParameter("PersonId").AsIntegerOrNull();
                    if (personId.HasValue)
                    {
                        entity = new PersonService(_rockContext).Get(personId.Value);
                    }
                    else
                    {
                        int?groupId = PageParameter("GroupId").AsIntegerOrNull();
                        if (groupId.HasValue)
                        {
                            entity = new GroupService(_rockContext).Get(groupId.Value);
                        }
                    }

                    // Loop through all the query string parameters and try to set any workflow
                    // attributes that might have the same key
                    foreach (string key in Request.QueryString.AllKeys)
                    {
                        _workflow.SetAttributeValue(key, Request.QueryString[key]);
                    }

                    List <string> errorMessages;
                    if (_workflow.Process(_rockContext, entity, out errorMessages))
                    {
                        // If the workflow type is persisted, save the workflow
                        if (_workflow.IsPersisted || _workflowType.IsPersisted)
                        {
                            if (_workflow.Id == 0)
                            {
                                _workflowService.Add(_workflow);
                            }

                            _rockContext.WrapTransaction(() =>
                            {
                                _rockContext.SaveChanges();
                                _workflow.SaveAttributeValues(_rockContext);
                                foreach (var activity in _workflow.Activities)
                                {
                                    activity.SaveAttributeValues(_rockContext);
                                }
                            });

                            WorkflowId = _workflow.Id;
                        }
                    }
                }
            }

            if (_workflow == null)
            {
                ShowMessage(NotificationBoxType.Danger, "Workflow Activation Error", "Workflow could not be activated.");
                return(false);
            }

            if (_workflow.IsActive)
            {
                if (ActionTypeId.HasValue)
                {
                    foreach (var activity in _workflow.ActiveActivities)
                    {
                        _action = activity.Actions.Where(a => a.ActionTypeId == ActionTypeId.Value).FirstOrDefault();
                        if (_action != null)
                        {
                            _activity = activity;
                            _activity.LoadAttributes();

                            _actionType  = _action.ActionType;
                            ActionTypeId = _actionType.Id;
                            return(true);
                        }
                    }
                }

                var canEdit = IsUserAuthorized(Authorization.EDIT);

                // Find first active action form
                int personId = CurrentPerson != null ? CurrentPerson.Id : 0;
                foreach (var activity in _workflow.Activities
                         .Where(a =>
                                a.IsActive &&
                                (
                                    (!a.AssignedGroupId.HasValue && !a.AssignedPersonAliasId.HasValue) ||
                                    (a.AssignedPersonAlias != null && a.AssignedPersonAlias.PersonId == personId) ||
                                    (a.AssignedGroup != null && a.AssignedGroup.Members.Any(m => m.PersonId == personId))
                                )
                                )
                         .OrderBy(a => a.ActivityType.Order))
                {
                    if (canEdit || (activity.ActivityType.IsAuthorized(Authorization.VIEW, CurrentPerson)))
                    {
                        foreach (var action in activity.ActiveActions)
                        {
                            if (action.ActionType.WorkflowForm != null && action.IsCriteriaValid)
                            {
                                _activity = activity;
                                _activity.LoadAttributes();

                                _action      = action;
                                _actionType  = _action.ActionType;
                                ActionTypeId = _actionType.Id;
                                return(true);
                            }
                        }
                    }
                }
            }

            ShowNotes(false);
            ShowMessage(NotificationBoxType.Warning, string.Empty, "The selected workflow is not in a state that requires you to enter information.");
            return(false);
        }
        /// <summary>
        /// Handles the Click event of the lbSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void lbSave_Click(object sender, EventArgs e)
        {
            if (_group != null && _occurrence != null)
            {
                var rockContext        = new RockContext();
                var attendanceService  = new AttendanceService(rockContext);
                var personAliasService = new PersonAliasService(rockContext);

                var existingAttendees = attendanceService.Queryable()
                                        .Where(a =>
                                               a.GroupId == _group.Id &&
                                               a.ScheduleId == _group.ScheduleId &&
                                               a.StartDateTime >= _occurrence.StartDateTime &&
                                               a.StartDateTime < _occurrence.EndDateTime)
                                        .ToList();

                // If did not meet was selected and this was a manually entered occurrence (not based on a schedule)
                // then just delete all the attendance records instead of tracking a 'did not meet' value
                if (cbDidNotMeet.Checked && !_occurrence.ScheduleId.HasValue)
                {
                    foreach (var attendance in existingAttendees)
                    {
                        attendanceService.Delete(attendance);
                    }
                }
                else
                {
                    if (cbDidNotMeet.Checked)
                    {
                        // If the occurrence is based on a schedule, set the did not meet flags
                        foreach (var attendance in existingAttendees)
                        {
                            attendance.DidAttend   = null;
                            attendance.DidNotOccur = true;
                        }
                    }

                    foreach (var item in lvMembers.Items)
                    {
                        var hfMember = item.FindControl("hfMember") as HiddenField;
                        var cbMember = item.FindControl("cbMember") as CheckBox;

                        if (hfMember != null && cbMember != null)
                        {
                            int personId = hfMember.ValueAsInt();

                            var attendance = existingAttendees
                                             .Where(a => a.PersonAlias.PersonId == personId)
                                             .FirstOrDefault();

                            if (attendance == null)
                            {
                                int?personAliasId = personAliasService.GetPrimaryAliasId(personId);
                                if (personAliasId.HasValue)
                                {
                                    attendance               = new Attendance();
                                    attendance.GroupId       = _group.Id;
                                    attendance.ScheduleId    = _group.ScheduleId;
                                    attendance.PersonAliasId = personAliasId;
                                    attendance.StartDateTime = _occurrence.StartDateTime;
                                    attendanceService.Add(attendance);
                                }
                            }

                            if (attendance != null)
                            {
                                if (cbDidNotMeet.Checked)
                                {
                                    attendance.DidAttend   = null;
                                    attendance.DidNotOccur = true;
                                }
                                else
                                {
                                    attendance.DidAttend   = cbMember.Checked;
                                    attendance.DidNotOccur = null;
                                }
                            }
                        }
                    }
                }

                rockContext.SaveChanges();

                WorkflowType workflowType     = null;
                Guid?        workflowTypeGuid = GetAttributeValue("Workflow").AsGuidOrNull();
                if (workflowTypeGuid.HasValue)
                {
                    var workflowTypeService = new WorkflowTypeService(rockContext);
                    workflowType = workflowTypeService.Get(workflowTypeGuid.Value);
                    if (workflowType != null)
                    {
                        try
                        {
                            var workflowService = new WorkflowService(rockContext);
                            var workflow        = Workflow.Activate(workflowType, _group.Name);

                            workflow.SetAttributeValue("StartDateTime", _occurrence.StartDateTime.ToString("o"));
                            workflow.SetAttributeValue("Schedule", _group.Schedule.Guid.ToString());

                            List <string> workflowErrors;
                            if (workflow.Process(rockContext, _group, out workflowErrors))
                            {
                                if (workflow.IsPersisted || workflow.IsPersisted)
                                {
                                    if (workflow.Id == 0)
                                    {
                                        workflowService.Add(workflow);
                                    }

                                    rockContext.WrapTransaction(() =>
                                    {
                                        rockContext.SaveChanges();
                                        workflow.SaveAttributeValues(_rockContext);
                                        foreach (var activity in workflow.Activities)
                                        {
                                            activity.SaveAttributeValues(rockContext);
                                        }
                                    });
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ExceptionLogService.LogException(ex, this.Context);
                        }
                    }
                }

                NavigateToParentPage(new Dictionary <string, string> {
                    { "GroupId", _group.Id.ToString() }
                });
            }
        }
Пример #12
0
        private bool TriggerWorkflows(IEntity entity, WorkflowTriggerType triggerType, PersonAlias personAlias)
        {
            Dictionary <string, PropertyInfo> properties = null;

            using (var rockContext = new RockContext())
            {
                var workflowTypeService = new WorkflowTypeService(rockContext);
                var workflowService     = new WorkflowService(rockContext);

                foreach (var trigger in TriggerCache.Triggers(entity.TypeName, triggerType).Where(t => t.IsActive == true))
                {
                    bool match = true;

                    if (!string.IsNullOrWhiteSpace(trigger.EntityTypeQualifierColumn))
                    {
                        if (properties == null)
                        {
                            properties = new Dictionary <string, PropertyInfo>();
                            foreach (PropertyInfo propertyInfo in entity.GetType().GetProperties())
                            {
                                properties.Add(propertyInfo.Name.ToLower(), propertyInfo);
                            }
                        }

                        match = (properties.ContainsKey(trigger.EntityTypeQualifierColumn.ToLower()) &&
                                 properties[trigger.EntityTypeQualifierColumn.ToLower()].GetValue(entity, null).ToString()
                                 == trigger.EntityTypeQualifierValue);
                    }

                    if (match)
                    {
                        if (triggerType == WorkflowTriggerType.PreSave || triggerType == WorkflowTriggerType.PreDelete)
                        {
                            var workflowType = workflowTypeService.Get(trigger.WorkflowTypeId);

                            if (workflowType != null)
                            {
                                var workflow = Rock.Model.Workflow.Activate(workflowType, trigger.WorkflowName);

                                List <string> workflowErrors;
                                if (!workflow.Process(rockContext, entity, out workflowErrors))
                                {
                                    SaveErrorMessages.AddRange(workflowErrors);
                                    return(false);
                                }
                                else
                                {
                                    if (workflow.IsPersisted || workflowType.IsPersisted)
                                    {
                                        workflowService.Add(workflow);
                                        rockContext.SaveChanges();
                                    }
                                }
                            }
                        }
                        else
                        {
                            var transaction = new Rock.Transactions.WorkflowTriggerTransaction();
                            transaction.Trigger     = trigger;
                            transaction.Entity      = entity.Clone();
                            transaction.PersonAlias = personAlias;
                            Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
                        }
                    }
                }
            }

            return(true);
        }