Ejemplo n.º 1
0
        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.");
        }
Ejemplo n.º 2
0
        /// <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);
                }
            });
        }