/// <summary> /// Activates and processes a workflow activity. If the workflow has not yet been activated, it will /// also be activated /// </summary> /// <param name="activityName">Name of the activity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> /// <exception cref="System.Exception"></exception> protected bool ProcessActivity(string activityName, out List <string> errorMessages) { errorMessages = new List <string>(); Guid?guid = GetAttributeValue("WorkflowType").AsGuidOrNull(); if (guid.HasValue) { using (var rockContext = new RockContext()) { var workflowTypeService = new WorkflowTypeService(rockContext); var workflowService = new WorkflowService(rockContext); var workflowType = workflowTypeService.Queryable("ActivityTypes") .Where(w => w.Guid.Equals(guid.Value)) .FirstOrDefault(); if (workflowType != null) { if (CurrentWorkflow == null) { CurrentWorkflow = Rock.Model.Workflow.Activate(workflowType, CurrentCheckInState.Kiosk.Device.Name, rockContext); if (Request["Override"] != null) { if (Request["Override"].ToString() == "True") { CurrentWorkflow.SetAttributeValue("Override", "True"); } } } var activityType = workflowType.ActivityTypes.Where(a => a.Name == activityName).FirstOrDefault(); if (activityType != null) { WorkflowActivity.Activate(activityType, CurrentWorkflow, rockContext); if (workflowService.Process(CurrentWorkflow, CurrentCheckInState, out errorMessages)) { // Keep workflow active for continued processing CurrentWorkflow.CompletedDateTime = null; return(true); } } else { errorMessages.Add(string.Format("Workflow type does not have a '{0}' activity type", activityName)); } } else { errorMessages.Add("Invalid Workflow Type"); } } } return(false); }
/// <summary> /// Saves the attribute value. /// </summary> /// <param name="workflow">The workflow.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <param name="fieldType">Type of the field.</param> /// <param name="rockContext">The rock context.</param> /// <param name="qualifiers">The qualifiers.</param> /// <returns></returns> private static bool SaveAttributeValue(Rock.Model.Workflow workflow, string key, string value, FieldTypeCache fieldType, RockContext rockContext, Dictionary <string, string> qualifiers = null) { bool createdNewAttribute = false; if (workflow.Attributes.ContainsKey(key)) { workflow.SetAttributeValue(key, value); } else { // Read the attribute var attributeService = new AttributeService(rockContext); var attribute = attributeService .Get(workflow.TypeId, "WorkflowTypeId", workflow.WorkflowTypeId.ToString()) .Where(a => a.Key == key) .FirstOrDefault(); // If workflow attribute doesn't exist, create it // ( should only happen first time a background check is processed for given workflow type) if (attribute == null) { attribute = new Rock.Model.Attribute(); attribute.EntityTypeId = workflow.TypeId; attribute.EntityTypeQualifierColumn = "WorkflowTypeId"; attribute.EntityTypeQualifierValue = workflow.WorkflowTypeId.ToString(); attribute.Name = key.SplitCase(); attribute.Key = key; attribute.FieldTypeId = fieldType.Id; attributeService.Add(attribute); if (qualifiers != null) { foreach (var keyVal in qualifiers) { var qualifier = new Rock.Model.AttributeQualifier(); qualifier.Key = keyVal.Key; qualifier.Value = keyVal.Value; attribute.AttributeQualifiers.Add(qualifier); } } createdNewAttribute = true; } // Set the value for this action's instance to the current time var attributeValue = new Rock.Model.AttributeValue(); attributeValue.Attribute = attribute; attributeValue.EntityId = workflow.Id; attributeValue.Value = value; new AttributeValueService(rockContext).Add(attributeValue); } return(createdNewAttribute); }
private void CopyAttributes(Rock.Model.Workflow newWorkflow, Rock.Model.WorkflowActivity currentActivity, RockContext rockContext) { if (currentActivity.Attributes == null) { currentActivity.LoadAttributes(rockContext); } if (currentActivity.Workflow.Attributes == null) { currentActivity.Workflow.LoadAttributes(rockContext); } // Pass attributes from current Workflow to new Workflow. foreach (string key in currentActivity.Workflow.AttributeValues.Keys) { newWorkflow.SetAttributeValue(key, currentActivity.Workflow.GetAttributeValue(key)); } // Pass attributes from current Activity to new Workflow. foreach (string key in currentActivity.AttributeValues.Keys) { newWorkflow.SetAttributeValue(key, currentActivity.GetAttributeValue(key)); } }
/// <summary> /// Activates and processes a workflow activity. If the workflow has not yet been activated, it will /// also be activated /// </summary> /// <param name="activityName">Name of the activity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> /// <exception cref="System.Exception"></exception> protected bool ProcessActivity( string activityName, out List<string> errorMessages ) { errorMessages = new List<string>(); Guid? guid = GetAttributeValue( "WorkflowType" ).AsGuidOrNull(); if ( guid.HasValue ) { using ( var rockContext = new RockContext() ) { var workflowTypeService = new WorkflowTypeService( rockContext ); var workflowService = new WorkflowService( rockContext ); var workflowType = workflowTypeService.Queryable( "ActivityTypes" ) .Where( w => w.Guid.Equals( guid.Value ) ) .FirstOrDefault(); if ( workflowType != null ) { if ( CurrentWorkflow == null ) { CurrentWorkflow = Rock.Model.Workflow.Activate( workflowType, CurrentCheckInState.Kiosk.Device.Name, rockContext ); if ( IsOverride ) { CurrentWorkflow.SetAttributeValue( "Override", "True" ); } } var activityType = workflowType.ActivityTypes.Where( a => a.Name == activityName ).FirstOrDefault(); if ( activityType != null ) { WorkflowActivity.Activate( activityType, CurrentWorkflow, rockContext ); if ( workflowService.Process( CurrentWorkflow, CurrentCheckInState, out errorMessages ) ) { // Keep workflow active for continued processing CurrentWorkflow.CompletedDateTime = null; return true; } } else { errorMessages.Add( string.Format( "Workflow type does not have a '{0}' activity type", activityName ) ); } } else { errorMessages.Add( "Invalid Workflow Type" ); } } } return false; }
/// <summary> /// Renders the specified context. /// </summary> /// <param name="context">The context.</param> /// <param name="result">The result.</param> public override void Render(Context context, TextWriter result) { // first ensure that entity commands are allowed in the context if (!this.IsAuthorized(context)) { result.Write(string.Format(RockLavaBlockBase.NotAuthorizedMessage, this.Name)); base.Render(context, result); return; } var attributes = new Dictionary <string, string>(); string parmWorkflowType = null; string parmWorkflowName = null; string parmWorkflowId = null; string parmActivityType = null; /* Parse the markup text to pull out configuration parameters. */ var parms = ParseMarkup(_markup, context); foreach (var p in parms) { if (p.Key.ToLower() == "workflowtype") { parmWorkflowType = p.Value; } else if (p.Key.ToLower() == "workflowname") { parmWorkflowName = p.Value; } else if (p.Key.ToLower() == "workflowid") { parmWorkflowId = p.Value; } else if (p.Key.ToLower() == "activitytype") { parmActivityType = p.Value; } else { attributes.AddOrReplace(p.Key, p.Value); } } /* Process inside a new stack level so our own created variables do not * persist throughout the rest of the workflow. */ context.Stack(() => { using (var rockContext = new RockContext()) { WorkflowService workflowService = new WorkflowService(rockContext); Rock.Model.Workflow workflow = null; WorkflowActivity activity = null; /* They provided a WorkflowType, so we need to kick off a new workflow. */ if (parmWorkflowType != null) { string type = parmWorkflowType; string name = parmWorkflowName ?? string.Empty; WorkflowTypeCache workflowType = null; /* Get the type of workflow */ if (type.AsGuidOrNull() != null) { workflowType = WorkflowTypeCache.Read(type.AsGuid()); } else if (type.AsIntegerOrNull() != null) { workflowType = WorkflowTypeCache.Read(type.AsInteger()); } /* Try to activate the workflow */ if (workflowType != null) { workflow = Rock.Model.Workflow.Activate(workflowType, parmWorkflowName); /* Set any workflow attributes that were specified. */ foreach (var attr in attributes) { if (workflow.Attributes.ContainsKey(attr.Key)) { workflow.SetAttributeValue(attr.Key, attr.Value.ToString()); } } if (workflow != null) { List <string> errorMessages; workflowService.Process(workflow, out errorMessages); if (errorMessages.Any()) { context["Error"] = string.Join("; ", errorMessages.ToArray()); } context["Workflow"] = workflow; } else { context["Error"] = "Could not activate workflow."; } } else { context["Error"] = "Workflow type not found."; } } /* They instead provided a WorkflowId, so we are working with an existing Workflow. */ else if (parmWorkflowId != null) { string id = parmWorkflowId.ToString(); /* Get the workflow */ if (id.AsGuidOrNull() != null) { workflow = workflowService.Get(id.AsGuid()); } else if (id.AsIntegerOrNull() != null) { workflow = workflowService.Get(id.AsInteger()); } if (workflow != null) { if (workflow.CompletedDateTime == null) { /* Currently we cannot activate an activity in a workflow that is currently * being processed. The workflow is held in-memory so the activity we would * activate would not show up for the processor and probably never run. */ if (!workflow.IsProcessing) { bool hasError = false; /* If they provided an ActivityType parameter then we need to activate * a new activity in the workflow. */ if (parmActivityType != null) { string type = parmActivityType.ToString(); WorkflowActivityTypeCache activityType = null; /* Get the type of activity */ if (type.AsGuidOrNull() != null) { activityType = WorkflowActivityTypeCache.Read(type.AsGuid()); } else if (type.AsIntegerOrNull() != null) { activityType = WorkflowActivityTypeCache.Read(type.AsInteger()); } if (activityType != null) { activity = WorkflowActivity.Activate(activityType, workflow); /* Set any workflow attributes that were specified. */ foreach (var attr in attributes) { if (activity.Attributes.ContainsKey(attr.Key)) { activity.SetAttributeValue(attr.Key, attr.Value.ToString()); } } } else { context["Error"] = "Activity type was not found."; hasError = true; } } /* Process the existing Workflow. */ if (!hasError) { List <string> errorMessages; workflowService.Process(workflow, out errorMessages); if (errorMessages.Any()) { context["Error"] = string.Join("; ", errorMessages.ToArray()); } context["Workflow"] = workflow; context["Activity"] = activity; } } else { context["Error"] = "Cannot activate activity on workflow that is currently being processed."; } } else { context["Error"] = "Workflow has already been completed."; } } else { context["Error"] = "Workflow not found."; } } else { context["Error"] = "Must specify one of WorkflowType or WorkflowId."; } RenderAll(NodeList, context, result); } }); }