private bool HydrateObjects() { LoadWorkflowType(); // Set the note type if this is first request if ( !Page.IsPostBack ) { var entityType = EntityTypeCache.Read( typeof( Rock.Model.Workflow ) ); var noteTypes = NoteTypeCache.GetByEntity( entityType.Id, string.Empty, string.Empty ); ncWorkflowNotes.NoteTypes = noteTypes; } if ( _workflowType == null ) { ShowNotes( false ); ShowMessage( NotificationBoxType.Danger, "Configuration Error", "Workflow type was not configured or specified correctly." ); return false; } if ( !_workflowType.IsAuthorized( Authorization.VIEW, CurrentPerson ) ) { ShowNotes( false ); 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 ( !_workflowService.Process( _workflow, entity, out errorMessages ) ) { ShowNotes( false ); ShowMessage( NotificationBoxType.Danger, "Workflow Processing Error(s):", "<ul><li>" + errorMessages.AsDelimited( "</li><li>" ) + "</li></ul>" ); return false; } if ( _workflow.Id != 0 ) { WorkflowId = _workflow.Id; } } } if ( _workflow == null ) { ShowNotes( false ); 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 = UserCanEdit || _workflow.IsAuthorized( Authorization.EDIT, CurrentPerson ); // Find first active action form int personId = CurrentPerson != null ? CurrentPerson.Id : 0; foreach ( var activity in _workflow.Activities .Where( a => a.IsActive && ( ( canEdit ) || ( !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; }
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); }
private bool HydrateObjects() { LoadWorkflowType(); 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 ) { _workflow = Rock.Model.Workflow.Activate( _workflowType, "Workflow" ); List<string> errorMessages; if ( _workflow.Process( _rockContext, out errorMessages ) ) { // If the workflow type is persisted, save the workflow if ( _workflow.IsPersisted || _workflowType.IsPersisted ) { _workflowService.Add( _workflow ); RockTransactionScope.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 ) { _activity = activity; _activity.LoadAttributes(); _action = action; _actionType = _action.ActionType; ActionTypeId = _actionType.Id; return true; } } } } } ShowMessage( NotificationBoxType.Warning, string.Empty, "The selected workflow is not in a state that requires you to enter information." ); return false; }
private bool HydrateObjects() { LoadWorkflowType(); // Set the note type if this is first request if (!Page.IsPostBack) { var entityType = EntityTypeCache.Get(typeof(Rock.Model.Workflow)); var noteTypes = NoteTypeCache.GetByEntity(entityType.Id, string.Empty, string.Empty); ncWorkflowNotes.NoteOptions.SetNoteTypes(noteTypes); } if (_workflowType == null) { ShowNotes(false); ShowMessage(NotificationBoxType.Danger, "Configuration Error", "Workflow type was not configured or specified correctly."); return(false); } if (!_workflowType.IsAuthorized(Authorization.VIEW, CurrentPerson)) { ShowNotes(false); ShowMessage(NotificationBoxType.Warning, "Sorry", "You are not authorized to view this type of workflow."); return(false); } if (!(_workflowType.IsActive ?? true)) { ShowNotes(false); ShowMessage(NotificationBoxType.Warning, "Sorry", "This type of workflow is not active."); 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) { hlblWorkflowId.Text = _workflow.WorkflowId; _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 (var param in RockPage.PageParameters()) { if (param.Value != null && param.Value.ToString().IsNotNullOrWhiteSpace()) { _workflow.SetAttributeValue(param.Key, param.Value.ToString()); } } List <string> errorMessages; if (!_workflowService.Process(_workflow, entity, out errorMessages)) { ShowNotes(false); ShowMessage(NotificationBoxType.Danger, "Workflow Processing Error(s):", "<ul><li>" + errorMessages.AsDelimited("</li><li>") + "</li></ul>"); return(false); } if (_workflow.Id != 0) { WorkflowId = _workflow.Id; } } } if (_workflow == null) { ShowNotes(false); ShowMessage(NotificationBoxType.Danger, "Workflow Activation Error", "Workflow could not be activated."); return(false); } var canEdit = UserCanEdit || _workflow.IsAuthorized(Authorization.EDIT, CurrentPerson); if (_workflow.IsActive) { if (ActionTypeId.HasValue) { foreach (var activity in _workflow.ActiveActivities) { _action = activity.ActiveActions.Where(a => a.ActionTypeId == ActionTypeId.Value).FirstOrDefault(); if (_action != null) { _activity = activity; _activity.LoadAttributes(); _actionType = _action.ActionTypeCache; ActionTypeId = _actionType.Id; return(true); } } } // Find first active action form int personId = CurrentPerson != null ? CurrentPerson.Id : 0; int?actionId = PageParameter("ActionId").AsIntegerOrNull(); foreach (var activity in _workflow.Activities .Where(a => a.IsActive && (!actionId.HasValue || a.Actions.Any(ac => ac.Id == actionId.Value)) && ( (canEdit) || (!a.AssignedGroupId.HasValue && !a.AssignedPersonAliasId.HasValue) || (a.AssignedPersonAlias != null && a.AssignedPersonAlias.PersonId == personId) || (a.AssignedGroup != null && a.AssignedGroup.Members.Any(m => m.PersonId == personId)) ) ) .ToList() .OrderBy(a => a.ActivityTypeCache.Order)) { if (canEdit || (activity.ActivityTypeCache.IsAuthorized(Authorization.VIEW, CurrentPerson))) { foreach (var action in activity.ActiveActions .Where(a => (!actionId.HasValue || a.Id == actionId.Value))) { if (action.ActionTypeCache.WorkflowForm != null && action.IsCriteriaValid) { _activity = activity; _activity.LoadAttributes(); _action = action; _actionType = _action.ActionTypeCache; ActionTypeId = _actionType.Id; return(true); } } } } lSummary.Text = string.Empty; } else { if (GetAttributeValue("ShowSummaryView").AsBoolean() && !string.IsNullOrWhiteSpace(_workflowType.SummaryViewText)) { var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson); mergeFields.Add("Action", _action); mergeFields.Add("Activity", _activity); mergeFields.Add("Workflow", _workflow); lSummary.Text = _workflowType.SummaryViewText.ResolveMergeFields(mergeFields, CurrentPerson); lSummary.Visible = true; } } if (lSummary.Text.IsNullOrWhiteSpace()) { if (_workflowType.NoActionMessage.IsNullOrWhiteSpace()) { ShowMessage(NotificationBoxType.Warning, string.Empty, "The selected workflow is not in a state that requires you to enter information."); } else { var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson); mergeFields.Add("Action", _action); mergeFields.Add("Activity", _activity); mergeFields.Add("Workflow", _workflow); ShowMessage(NotificationBoxType.Warning, string.Empty, _workflowType.NoActionMessage.ResolveMergeFields(mergeFields, CurrentPerson)); } } ShowNotes(false); return(false); }
public void Run() { // Open a "regular" db connection for non EF stuff var conn = new SqlConnection(arenaContext.Connection.ConnectionString); conn.Open(); var Assignments = arenaContext.asgn_assignments.AsQueryable().Where(a => a.assignment_type_id == ARENA_ASSIGNMENT_TYPE_ID); var i = 0; foreach (asgn_assignment assignment in Assignments) { i++; // Give us some feedback that we are still doing stuff. Console.WriteLine("Loading " + i + " of " + Assignments.Count()); PersonAlias requestorPersonAlias = personAliasService.Queryable().Where(pa => pa.AliasPersonId == assignment.requester_person_id).First(); // See if we already imported this one if (workflowService.Queryable().Where(w => w.ForeignId == assignment.assignment_id).Any()) { Console.WriteLine("Skipping assignment ID " + assignment.assignment_id + " since it was already imported."); continue; } // Do some preliminary checking var fieldValues = arenaContext.asgn_assignment_field_values.Where(v => v.assignment_id == assignment.assignment_id); if (fieldValues == null) { Console.WriteLine("Skipping assignment ID " + assignment.assignment_id + " since it has no custom field values."); continue; } Workflow workflow = new Workflow(); workflow.WorkflowTypeId = WORKFLOW_TYPE_ID; workflow.CreatedByPersonAliasId = workflow.InitiatorPersonAliasId = assignment.requester_person_id = requestorPersonAlias.Id; workflow.Name = assignment.title; workflow.Status = assignment.resolved_date > new DateTime(1900, 1, 1) ? "Completed" : "Active"; workflow.CreatedDateTime = workflow.ActivatedDateTime = assignment.date_created; workflow.LastProcessedDateTime = assignment.state_last_updated; if (assignment.resolved_date > new DateTime(1900, 1, 1)) { workflow.CompletedDateTime = assignment.resolved_date; } workflow.ForeignId = assignment.assignment_id; // Add it to EF workflowService.Add(workflow); workflow.LoadAttributes(rockContext); if (assignment.resolved_date > new DateTime(1900, 1, 1)) { workflow.SetAttributeValue("DischargeDate", assignment.resolved_date.ToString()); // Create a completed discharged activity WorkflowActivity workflowActivity = new WorkflowActivity(); workflowActivity.ActivityTypeId = DISCHARGE_ACTIVITY_ID; workflowActivity.CompletedDateTime = assignment.resolved_date; workflowActivity.ActivatedDateTime = assignment.resolved_date; workflowActivity.LastProcessedDateTime = assignment.resolved_date; workflowActivity.Workflow = workflow; workflowActivityService.Add(workflowActivity); } else { WorkflowActivity workflowActivity = WorkflowActivity.Activate(workflowActivityTypeService.Get(SUMMARY_ACTIVITY_ID), workflow, rockContext); workflowActivity.ActivatedDateTime = assignment.date_created; workflowActivity.LastProcessedDateTime = assignment.date_created; } // Set some attributes from data in the assignment workflow.SetAttributeValue("CloseNote", assignment.resolution_text); workflow.SetAttributeValue("HomeboundResidentDescription", assignment.description); workflow.SetAttributeValue("Requestor", requestorPersonAlias.Guid); // Set more attributes from custom fields SetPersonAliasAttribute(workflow, fieldValues, "HomeboundPerson"); SetDateAttribute(workflow, fieldValues, "StartDate"); SetAttribute(workflow, fieldValues, "NotifiedBy"); SetAttribute(workflow, fieldValues, "NotifiedOn"); SetYesNoAttribute(workflow, fieldValues, "Communion"); SetDateAttribute(workflow, fieldValues, "EndDate"); // Now load all the visits (Let's kick it old school with some sp fun!) SqlCommand visitCmd = new SqlCommand("cust_secc_sp_rept_pastoralVisit_migration", conn); visitCmd.Parameters.Add(new SqlParameter("@assignmentID", assignment.assignment_id)); visitCmd.CommandType = CommandType.StoredProcedure; SqlDataReader visitReader = visitCmd.ExecuteReader(); var dataTable = new DataTable(); dataTable.Load(visitReader); visitReader.Close(); if (dataTable.Rows.Count > 0) { // Reverse the dataTable and iterate foreach (DataRow dr in dataTable.AsEnumerable().Reverse().CopyToDataTable().Rows) { var visitorPersonId = dr["Visitor"].ToString().AsInteger(); var visitDate = dr["VisitDate"].ToString().AsDateTime(); var visitNote = dr["VisitNote"].ToString(); WorkflowActivity workflowActivity = new WorkflowActivity(); workflowActivity.ActivityTypeId = VISIT_ACTIVITY_ID; workflowActivity.CompletedDateTime = visitDate; workflowActivity.ActivatedDateTime = visitDate; workflowActivity.LastProcessedDateTime = visitDate; workflowActivity.Workflow = workflow; workflowActivityService.Add(workflowActivity); // Set the attributes workflowActivity.LoadAttributes(); workflowActivity.SetAttributeValue("Visitor", personAliasService.Queryable().Where(p => p.AliasPersonId == visitorPersonId).FirstOrDefault().Guid); workflowActivity.SetAttributeValue("VisitDate", visitDate); workflowActivity.SetAttributeValue("VisitNote", visitNote); } } rockContext.SaveChanges(); workflow.SaveAttributeValues(); // Save each activities attributes too foreach (WorkflowActivity activity in workflow.Activities) { activity.SaveAttributeValues(); } } Console.WriteLine("Loaded " + i + " Homebound Resident Workflows."); }
private bool HydrateObjects() { LoadWorkflowType(); if (_workflowType == null) { nbError.NotificationBoxType = NotificationBoxType.Danger; nbError.Text = "Configuration Error, Workflow type was not configured or specified correctly."; return(false); } if (!_workflowType.IsAuthorized(Authorization.VIEW, CurrentPerson)) { return(false); } if (!(_workflowType.IsActive ?? true)) { nbError.NotificationBoxType = NotificationBoxType.Danger; nbError.Text = "This type of workflow is not active."; return(false); } // If operating against an existing workflow, get the workflow and load attributes if (!WorkflowId.HasValue) { WorkflowId = parameter.AsIntegerOrNull(); if (!WorkflowId.HasValue) { Guid guid = parameter.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) { var 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; List <string> errorMessages; if (!_workflowService.Process(_workflow, entity, out errorMessages)) { //ShowMessage( NotificationBoxType.Danger, "Workflow Processing Error(s):", // "<ul><li>" + errorMessages.AsDelimited( "</li><li>" ) + "</li></ul>" ); return(false); } if (_workflow.Id != 0) { WorkflowId = _workflow.Id; } } } if (_workflow == null) { //ShowNotes( false ); //ShowMessage( NotificationBoxType.Danger, "Workflow Activation Error", "Workflow could not be activated." ); return(false); } var canEdit = UserCanEdit || _workflow.IsAuthorized(Authorization.EDIT, CurrentPerson); if (_workflow.IsActive) { if (ActionTypeId.HasValue) { foreach (var activity in _workflow.ActiveActivities) { _action = activity.ActiveActions.Where(a => a.ActionTypeId == ActionTypeId.Value).FirstOrDefault(); if (_action != null) { _activity = activity; _activity.LoadAttributes(); _actionType = _action.ActionTypeCache; ActionTypeId = _actionType.Id; return(true); } } } // Find first active action form int personId = CurrentPerson != null ? CurrentPerson.Id : 0; int?actionId = null; foreach (var activity in _workflow.Activities .Where(a => a.IsActive && (!actionId.HasValue || a.Actions.Any(ac => ac.Id == actionId.Value)) && ( (canEdit) || (!a.AssignedGroupId.HasValue && !a.AssignedPersonAliasId.HasValue) || (a.AssignedPersonAlias != null && a.AssignedPersonAlias.PersonId == personId) || (a.AssignedGroup != null && a.AssignedGroup.Members.Any(m => m.PersonId == personId)) ) ) .ToList() .OrderBy(a => a.ActivityTypeCache.Order)) { if (canEdit || (activity.ActivityTypeCache.IsAuthorized(Authorization.VIEW, CurrentPerson))) { foreach (var action in activity.ActiveActions .Where(a => (!actionId.HasValue || a.Id == actionId.Value))) { if (action.ActionTypeCache.WorkflowForm != null && action.IsCriteriaValid) { _activity = activity; _activity.LoadAttributes(); _action = action; _actionType = _action.ActionTypeCache; ActionTypeId = _actionType.Id; return(true); } } } } } return(false); }
protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (_rockContext == null) { _rockContext = new RockContext(); } if (_workflowService == null) { _workflowService = new WorkflowService(_rockContext); } var paramWorkflowId = PageParameter("WorkflowId"); WorkflowId = paramWorkflowId.AsIntegerOrNull(); if (!WorkflowId.HasValue) { Guid guid = PageParameter("WorkflowGuid").AsGuid(); if (!guid.IsEmpty()) { _workflow = _workflowService.Queryable() .Where(w => w.Guid.Equals(guid)) .FirstOrDefault(); if (_workflow != null) { WorkflowId = _workflow.Id; } } } if (WorkflowId.HasValue) { if (_workflow == null) { _workflow = _workflowService.Queryable() .Where(w => w.Id == WorkflowId.Value) .FirstOrDefault(); } //------------------------------- if (_workflow != null) { _workflow.LoadAttributes(); } } if (_workflow != null) { if (_workflow.IsActive) { 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 ((activity.ActivityType.IsAuthorized(Authorization.VIEW, CurrentPerson))) { foreach (var action in activity.ActiveActions) { if (action.ActionType.WorkflowForm != null && action.IsCriteriaValid) { _activity = activity; _activity.LoadAttributes(_rockContext); } } } } if (_activity != null) { var entryPage = _workflow.GetAttributeValue("EntryFormPage"); if (!String.IsNullOrEmpty(entryPage)) { var queryParams = new Dictionary <string, string>(); queryParams.Add("WorkflowTypeId", _activity.Workflow.WorkflowTypeId.ToString()); queryParams.Add("WorkflowId", _activity.WorkflowId.ToString()); var attrsToSend = GetAttributeValue("WorkflowAttributes"); if (!String.IsNullOrWhiteSpace(attrsToSend)) { foreach (var attr in attrsToSend.Split(',')) { var attrName = attr.Trim(); if (!String.IsNullOrEmpty(_activity.GetAttributeValue(attrName))) { queryParams.Add(attrName, _activity.GetAttributeValue(attrName)); } else if (!String.IsNullOrEmpty(_activity.Workflow.GetAttributeValue(attrName))) { queryParams.Add(attrName, _activity.Workflow.GetAttributeValue(attrName)); } } } var pageReference = new Rock.Web.PageReference(entryPage, queryParams); bool paramsDiffer = false; foreach (var pair in queryParams) { if (pair.Value != PageParameter(pair.Key)) { paramsDiffer = true; break; } } if (paramsDiffer || (pageReference.PageId != CurrentPageReference.PageId)) { Response.Redirect(pageReference.BuildUrl(), true); } } } } } }