private void BindLava(IQueryable <GroupMember> members, List <Person> familyMembers, RockContext rockContext)
        {
            var items = new List <LavaData>();
            AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);

            foreach (var person in familyMembers)
            {
                //Get all the camp group members for this person
                List <GroupMember> medicalMembers = members.Where(m => m.PersonId == person.Id).ToList();

                if (!medicalMembers.Any())
                {
                    continue;
                }

                person.LoadAttributes();
                var attribute       = person.GetAttributeValue(GetAttributeValue("MedicationMatrixKey"));
                var attributeMatrix = attributeMatrixService.Get(attribute.AsGuid());

                LavaData data = new LavaData
                {
                    Person      = person,
                    Groups      = medicalMembers.Select(m => m.Group).ToList(),
                    Medications = attributeMatrix
                };
                items.Add(data);
            }
            var mergeObjects = Rock.Lava.LavaHelper.GetCommonMergeFields(RockPage, CurrentPerson);

            mergeObjects.Add("Items", items);
            ltLava.Text = GetAttributeValue("LavaTemplate").ResolveMergeFields(mergeObjects);
        }
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            AttributeMatrixService     attributeMatrixService     = new AttributeMatrixService(rockContext);
            AttributeMatrixItemService attributeMatrixItemService = new AttributeMatrixItemService(rockContext);

            errorMessages = new List <string>();

            // Get all the attribute values
            var attributeMatrixGuid = action.GetWorklowAttributeValue(GetActionAttributeValue(action, "AttributeMatrix").AsGuid()).AsGuidOrNull();
            var itemGuid            = GetActionAttributeValue(action, "ItemGuid").ResolveMergeFields(GetMergeFields(action)).AsGuidOrNull();

            if (attributeMatrixGuid.HasValue && itemGuid.HasValue)
            {
                // Load the matrix
                AttributeMatrix matrix = attributeMatrixService.Get(attributeMatrixGuid.Value);

                AttributeMatrixItem item = matrix.AttributeMatrixItems.Where(i => i.Guid == itemGuid.Value).FirstOrDefault();

                if (item != null)
                {
                    matrix.AttributeMatrixItems.Remove(item);
                    attributeMatrixItemService.Delete(item);
                }
            }

            return(true);
        }
Exemplo n.º 3
0
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);

            errorMessages = new List <string>();

            // Get all the attribute values
            var attributeMatrixGuid = action.GetWorklowAttributeValue(GetActionAttributeValue(action, "AttributeMatrix").AsGuid()).AsGuidOrNull();
            var itemAttributes      = GetActionAttributeValue(action, "ItemAttributes").AsDictionaryOrNull();

            if (attributeMatrixGuid.HasValue && itemAttributes != null)
            {
                // Load the matrix
                AttributeMatrix matrix = attributeMatrixService.Get(attributeMatrixGuid.Value);

                // Create the new item
                AttributeMatrixItem item = new AttributeMatrixItem();
                item.AttributeMatrix = matrix;
                item.LoadAttributes();

                foreach (var attribute in item.Attributes)
                {
                    if (itemAttributes.ContainsKey(attribute.Key))
                    {
                        item.SetAttributeValue(attribute.Key, itemAttributes[attribute.Key].ResolveMergeFields(GetMergeFields(action)));
                    }
                }
                matrix.AttributeMatrixItems.Add(item);
                rockContext.SaveChanges();
                item.SaveAttributeValues(rockContext);
            }

            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="value">The value.</param>
        public override void SetEditValue(Control control, Dictionary <string, ConfigurationValue> configurationValues, string value)
        {
            AttributeMatrixEditor attributeMatrixEditor = control as AttributeMatrixEditor;

            if (attributeMatrixEditor != null)
            {
                var rockContext = new RockContext();
                AttributeMatrixTemplate attributeMatrixTemplate = null;
                if (attributeMatrixEditor.AttributeMatrixTemplateId.HasValue)
                {
                    attributeMatrixTemplate = new AttributeMatrixTemplateService(rockContext).Get(attributeMatrixEditor.AttributeMatrixTemplateId.Value);
                }

                if (attributeMatrixTemplate != null)
                {
                    var             attributeMatrixService = new AttributeMatrixService(rockContext);
                    AttributeMatrix attributeMatrix        = null;
                    Guid?           attributeMatrixGuid    = value.AsGuidOrNull();
                    if (attributeMatrixGuid.HasValue)
                    {
                        attributeMatrix = attributeMatrixService.Get(attributeMatrixGuid.Value);
                    }

                    if (attributeMatrix == null)
                    {
                        // Create the AttributeMatrix now and save it even though they haven't hit save yet. We'll need the AttributeMatrix record to exist so that we can add AttributeMatrixItems to it
                        // If this ends up creating an orphan, we can clean up it up later
                        attributeMatrix = new AttributeMatrix {
                            Guid = Guid.NewGuid()
                        };
                        attributeMatrix.AttributeMatrixTemplateId = attributeMatrixEditor.AttributeMatrixTemplateId.Value;
                        attributeMatrix.AttributeMatrixItems      = new List <AttributeMatrixItem>();
                        attributeMatrixService.Add(attributeMatrix);
                        rockContext.SaveChanges();
                    }

                    // If the AttributeMatrixTemplateId jwas changed since the last time the attributeMatrix was saved, change it and wipe out the items
                    if (attributeMatrix.AttributeMatrixTemplateId != attributeMatrixEditor.AttributeMatrixTemplateId.Value)
                    {
                        attributeMatrix.AttributeMatrixTemplateId = attributeMatrixEditor.AttributeMatrixTemplateId.Value;

                        var attributeMatrixItemService = new AttributeMatrixItemService(rockContext);

                        // If the AttributeMatrixTemplateId changed, all the values in the AttributeMatrixItems
                        // are referring to attributes from the old template, so wipe them out. All of them.
                        foreach (var attributeMatrixItem in attributeMatrix.AttributeMatrixItems.ToList())
                        {
                            attributeMatrixItemService.Delete(attributeMatrixItem);
                        }

                        attributeMatrix.AttributeMatrixItems.Clear();
                        rockContext.SaveChanges();
                    }

                    attributeMatrixEditor.AttributeMatrixGuid = attributeMatrix.Guid;
                }
            }
        }
Exemplo n.º 5
0
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);

            errorMessages = new List <string>();

            // Get all the attribute values
            var attributeGuid       = GetActionAttributeValue(action, "AttributeMatrix").AsGuidOrNull();
            var attributeMatrixGuid = action.GetWorkflowAttributeValue(attributeGuid.HasValue ? attributeGuid.Value : Guid.Empty).AsGuidOrNull();
            var itemAttributes      = GetActionAttributeValue(action, "ItemAttributes").AsDictionaryOrNull();

            if (attributeGuid.HasValue && itemAttributes != null)
            {
                // Load the matrix
                AttributeMatrix matrix = attributeMatrixService.Get(attributeMatrixGuid.HasValue ? attributeMatrixGuid.Value : Guid.Empty);

                // If the matrix is null, create it first
                if (matrix == null)
                {
                    var attribute = AttributeCache.Get(GetActionAttributeValue(action, "AttributeMatrix").AsGuid());
                    matrix = new AttributeMatrix();
                    matrix.AttributeMatrixItems      = new List <AttributeMatrixItem>();
                    matrix.AttributeMatrixTemplateId = attribute.QualifierValues["attributematrixtemplate"]?.Value?.AsInteger() ?? 0;
                    attributeMatrixService.Add(matrix);

                    // Persist it and make sure it gets saved
                    rockContext.SaveChanges();
                    if (attribute.EntityTypeId == new Rock.Model.Workflow().TypeId)
                    {
                        action.Activity.Workflow.SetAttributeValue(attribute.Key, matrix.Guid.ToString());
                    }
                    else if (attribute.EntityTypeId == new WorkflowActivity().TypeId)
                    {
                        action.Activity.SetAttributeValue(attribute.Key, matrix.Guid.ToString());
                    }
                }

                // Create the new item
                AttributeMatrixItem item = new AttributeMatrixItem();
                item.AttributeMatrix = matrix;
                item.LoadAttributes();

                foreach (var attribute in item.Attributes)
                {
                    if (itemAttributes.ContainsKey(attribute.Key))
                    {
                        item.SetAttributeValue(attribute.Key, itemAttributes[attribute.Key].ResolveMergeFields(GetMergeFields(action)));
                    }
                }
                matrix.AttributeMatrixItems.Add(item);
                rockContext.SaveChanges();
                item.SaveAttributeValues(rockContext);
            }

            return(true);
        }
        private void BindGrid(IQueryable <GroupMember> members, List <Person> familyMembers, RockContext rockContext)
        {
            var gridData = new List <GridData>();
            AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);

            foreach (var person in familyMembers)
            {
                //Get all the camp group members for this person
                List <GroupMember> medicalMembers = members.Where(m => m.PersonId == person.Id).ToList();

                if (!medicalMembers.Any())
                {
                    continue;
                }

                GridData data = new GridData
                {
                    Id          = person.PrimaryAlias.Guid,
                    Name        = person.FullName,
                    Group       = string.Join("<br>", medicalMembers.Select(m => m.Group.Name)),
                    Medications = "No Medication Information"
                };
                person.LoadAttributes();
                var attribute       = person.GetAttributeValue(GetAttributeValue("MedicationMatrixKey"));
                var attributeMatrix = attributeMatrixService.Get(attribute.AsGuid());
                if (attributeMatrix != null)
                {
                    var lava     = attributeMatrix.AttributeMatrixTemplate.FormattedLava;
                    var template = attributeMatrix.AttributeMatrixTemplate;
                    template.LoadAttributes();
                    AttributeMatrixItem tempAttributeMatrixItem = new AttributeMatrixItem();
                    tempAttributeMatrixItem.AttributeMatrix = attributeMatrix;
                    tempAttributeMatrixItem.LoadAttributes();
                    Dictionary <string, object> mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, null, new Rock.Lava.CommonMergeFieldsOptions {
                        GetLegacyGlobalMergeFields = false
                    });
                    mergeFields.Add("AttributeMatrix", attributeMatrix);
                    mergeFields.Add("ItemAttributes", tempAttributeMatrixItem.Attributes.Select(a => a.Value).OrderBy(a => a.Order).ThenBy(a => a.Name));
                    mergeFields.Add("AttributeMatrixItems", attributeMatrix.AttributeMatrixItems.OrderBy(a => a.Order));
                    var medications = lava.ResolveMergeFields(mergeFields);
                    data.Medications = medications;
                }
                gridData.Add(data);
            }

            gGrid.DataSource = gridData;
            gGrid.DataBind();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Returns the field's current value(s)
        /// </summary>
        /// <param name="parentControl">The parent control.</param>
        /// <param name="value">Information about the value</param>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="condensed">Flag indicating if the value should be condensed (i.e. for use in a grid column)</param>
        /// <returns></returns>
        public override string FormatValue(Control parentControl, string value, Dictionary <string, ConfigurationValue> configurationValues, bool condensed)
        {
            var             rockContext            = new RockContext();
            var             attributeMatrixService = new AttributeMatrixService(rockContext);
            AttributeMatrix attributeMatrix        = null;
            Guid?           attributeMatrixGuid    = value.AsGuidOrNull();

            if (attributeMatrixGuid.HasValue)
            {
                attributeMatrix = attributeMatrixService.Get(attributeMatrixGuid.Value);
            }

            if (attributeMatrix != null)
            {
                if (configurationValues.ContainsKey(ATTRIBUTE_MATRIX_TEMPLATE))
                {
                    // set the AttributeMatrixTemplateId just in case it was changed since the last time the attributeMatrix was saved
                    int attributeMatrixTemplateId = configurationValues[ATTRIBUTE_MATRIX_TEMPLATE].Value.AsInteger();
                    if (attributeMatrix.AttributeMatrixTemplateId != attributeMatrixTemplateId)
                    {
                        attributeMatrix.AttributeMatrixTemplateId = attributeMatrixTemplateId;
                        attributeMatrix.AttributeMatrixTemplate   = new AttributeMatrixTemplateService(rockContext).Get(attributeMatrix.AttributeMatrixTemplateId);

                        // If the AttributeMatrixTemplateId changed, all the values in the AttributeMatrixItems
                        // are referring to attributes from the old template, so wipe them out. All of them.
                        attributeMatrix.AttributeMatrixItems.Clear();
                    }
                }

                // make a temp attributeMatrixItem to see what Attributes they have
                AttributeMatrixItem tempAttributeMatrixItem = new AttributeMatrixItem();
                tempAttributeMatrixItem.AttributeMatrix = attributeMatrix;
                tempAttributeMatrixItem.LoadAttributes();

                var lavaTemplate = attributeMatrix.AttributeMatrixTemplate.FormattedLava;
                Dictionary <string, object> mergeFields = Lava.LavaHelper.GetCommonMergeFields(parentControl?.RockBlock()?.RockPage, null, new Lava.CommonMergeFieldsOptions {
                    GetLegacyGlobalMergeFields = false
                });
                mergeFields.Add("AttributeMatrix", attributeMatrix);
                mergeFields.Add("ItemAttributes", tempAttributeMatrixItem.Attributes.Select(a => a.Value).OrderBy(a => a.Order).ThenBy(a => a.Name));
                mergeFields.Add("AttributeMatrixItems", attributeMatrix.AttributeMatrixItems.OrderBy(a => a.Order));
                return(lavaTemplate.ResolveMergeFields(mergeFields));
            }

            return(base.FormatValue(parentControl, value, configurationValues, condensed));
        }
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The workflow action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            var checkInState = GetCheckInState(entity, out errorMessages);
            var matrixAttributeMedicationKey   = GetAttributeValue(action, "MatrixAttributeMedicationKey");
            var matrixAttributeInstructionsKey = GetAttributeValue(action, "MatrixAttributeInstructionsKey");
            var matrixAttributeScheduleKey     = GetAttributeValue(action, "MatrixAttributeScheduleKey");

            AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);
            GroupMemberService     groupMemberService     = new GroupMemberService(rockContext);

            if (checkInState != null)
            {
                var family = checkInState.CheckIn.CurrentFamily;
                if (family != null)
                {
                    var commonMergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(null);

                    var people = family.GetPeople(true);
                    foreach (var person in people.Where(p => p.Selected))
                    {
                        foreach (var groupType in person.GroupTypes)
                        {
                            groupType.Labels = new List <CheckInLabel>();


                            foreach (var group in groupType.Groups)
                            {
                                List <string> medicationText   = (( string )GetAttributeValue(action, "MedicationText")).Split(',').ToList();
                                List <string> instructionsText = (( string )GetAttributeValue(action, "InstructionsText")).Split(',').ToList();

                                List <MedInfo> medInfos = new List <MedInfo>();
                                if (medicationText.Count == instructionsText.Count)
                                {
                                    for (int i = 0; i < medicationText.Count; i++)
                                    {
                                        medInfos.Add(new MedInfo {
                                            Medication = medicationText[i], Instructions = instructionsText[i]
                                        });
                                    }
                                }

                                group.Group.LoadAttributes();
                                var groupGuid         = group.Group.GetAttributeValue(GetAttributeValue(action, "GroupAttributeKey"));
                                var registrationGroup = new GroupService(rockContext).Get(groupGuid.AsGuid());
                                if (registrationGroup == null)
                                {
                                    continue;
                                }

                                var groupMember = groupMemberService.GetByGroupIdAndPersonId(registrationGroup.Id, person.Person.Id).FirstOrDefault();

                                var medicationKey    = GetAttributeValue(action, "MatrixAttributeKey");
                                var medicationMatrix = person.Person.GetAttributeValue(medicationKey);


                                var attributeMatrix = attributeMatrixService.Get(medicationMatrix.AsGuid());

                                var labelCache = KioskLabel.Get(new Guid(GetAttributeValue(action, "MedicationLabel")));

                                //Set up merge fields so we can use the lava from the merge fields
                                var mergeObjects = new Dictionary <string, object>();
                                foreach (var keyValue in commonMergeFields)
                                {
                                    mergeObjects.Add(keyValue.Key, keyValue.Value);
                                }

                                mergeObjects.Add("RegistrationGroup", registrationGroup);
                                mergeObjects.Add("RegistrationGroupMember", groupMember);
                                mergeObjects.Add("Group", group);
                                mergeObjects.Add("Person", person);
                                mergeObjects.Add("People", people);
                                mergeObjects.Add("GroupType", groupType);

                                if (attributeMatrix == null || attributeMatrix.AttributeMatrixItems.Count == 0)
                                {
                                    // Add a No Medication Information label for anyone without data
                                    var checkInLabel = new CheckInLabel(labelCache, mergeObjects);

                                    var index = 0;
                                    foreach (string mergeFieldText in medicationText)
                                    {
                                        checkInLabel.MergeFields.Add(mergeFieldText, index == 0 ? "No Medication Information Found" : "");
                                        checkInLabel.MergeFields.Add(instructionsText[index], "");
                                        index++;
                                    }
                                    addLabel(checkInLabel, checkInState, groupType, group, rockContext);
                                }
                                else
                                {
                                    var items = attributeMatrix.AttributeMatrixItems.ToList();
                                    var index = 0;

                                    while (index < items.Count)
                                    {
                                        var checkInLabel = new CheckInLabel(labelCache, mergeObjects);

                                        foreach (var med in medInfos)
                                        {
                                            if (items.Count > index)
                                            {
                                                items[index].LoadAttributes();

                                                string scheduleText = "";
                                                string separator    = "";
                                                var    schedule     = items[index].GetAttributeValue(matrixAttributeScheduleKey).SplitDelimitedValues();
                                                foreach (var scheduleGuid in schedule)
                                                {
                                                    scheduleText += separator + DefinedValueCache.Get(scheduleGuid);
                                                    separator     = ", ";
                                                }

                                                checkInLabel.MergeFields.Add(med.Medication,
                                                                             items[index].GetAttributeValue(matrixAttributeMedicationKey)
                                                                             + " - "
                                                                             + scheduleText
                                                                             );

                                                checkInLabel.MergeFields.Add(med.Instructions, items[index].GetAttributeValue(matrixAttributeInstructionsKey));
                                            }
                                            else
                                            {
                                                checkInLabel.MergeFields.Add(med.Medication, "");
                                                checkInLabel.MergeFields.Add(med.Instructions, "");
                                            }

                                            index++;
                                        }

                                        addLabel(checkInLabel, checkInState, groupType, group, rockContext);
                                    }
                                }
                            }
                        }
                    }
                }
                return(true);
            }

            errorMessages.Add($"Attempted to run {this.GetType().GetFriendlyTypeName()} in check-in, but the check-in state was null.");
            return(false);
        }
Exemplo n.º 9
0
        protected override void OnLoad(EventArgs e)
        {
            nbAlert.Visible = false;

            if (CurrentPerson == null)
            {
                nbAlert.Visible = true;
                nbAlert.Text    = "Please log in to continue.";
                return;
            }
            if (!Page.IsPostBack)
            {
                RockContext rockContext   = new RockContext();
                var         familyMembers = CurrentPerson.GetFamilies().SelectMany(f => f.Members).Select(m => m.Person).ToList();

                AddCaretakees(familyMembers, rockContext);


                var groupTypeStrings = GetAttributeValue("GroupTypes").SplitDelimitedValues();
                var groupTypeIds     = new List <int>();
                foreach (var groupType in groupTypeStrings)
                {
                    var groupTypeCache = GroupTypeCache.Get(groupType.AsGuid());
                    if (groupTypeCache != null)
                    {
                        groupTypeIds.Add(groupTypeCache.Id);
                    }
                }

                var groups = new GroupService(rockContext).Queryable()
                             .Where(g => g.IsActive && !g.IsArchived && groupTypeIds.Contains(g.GroupTypeId));
                if (!groups.Any())
                {
                    nbAlert.Visible = true;
                    nbAlert.Text    = "Please configure this block.";
                    return;
                }

                var members = groups.SelectMany(g => g.Members);


                var gridData = new List <GridData>();
                AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);
                foreach (var person in familyMembers)
                {
                    //Get all the camp group members for this person
                    List <GroupMember> medicalMembers = members.Where(m => m.PersonId == person.Id).ToList();

                    if (!medicalMembers.Any())
                    {
                        continue;
                    }

                    GridData data = new GridData
                    {
                        Id          = person.PrimaryAlias.Guid,
                        Name        = person.FullName,
                        Group       = string.Join("<br>", medicalMembers.Select(m => m.Group.Name)),
                        Medications = "No Medication Information"
                    };
                    person.LoadAttributes();
                    var attribute       = person.GetAttributeValue(GetAttributeValue("MedicationMatrixKey"));
                    var attributeMatrix = attributeMatrixService.Get(attribute.AsGuid());
                    if (attributeMatrix != null)
                    {
                        var lava     = attributeMatrix.AttributeMatrixTemplate.FormattedLava;
                        var template = attributeMatrix.AttributeMatrixTemplate;
                        template.LoadAttributes();
                        AttributeMatrixItem tempAttributeMatrixItem = new AttributeMatrixItem();
                        tempAttributeMatrixItem.AttributeMatrix = attributeMatrix;
                        tempAttributeMatrixItem.LoadAttributes();
                        Dictionary <string, object> mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, null, new Rock.Lava.CommonMergeFieldsOptions {
                            GetLegacyGlobalMergeFields = false
                        });
                        mergeFields.Add("AttributeMatrix", attributeMatrix);
                        mergeFields.Add("ItemAttributes", tempAttributeMatrixItem.Attributes.Select(a => a.Value).OrderBy(a => a.Order).ThenBy(a => a.Name));
                        mergeFields.Add("AttributeMatrixItems", attributeMatrix.AttributeMatrixItems.OrderBy(a => a.Order));
                        var medications = lava.ResolveMergeFields(mergeFields);
                        data.Medications = medications;
                    }
                    gridData.Add(data);
                }

                gGrid.DataSource = gridData;
                gGrid.DataBind();
            }
        }
Exemplo n.º 10
0
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext);

            errorMessages = new List <string>();

            // Get all the attribute values
            var sourceMatrixAttributeGuid = action.GetWorkflowAttributeValue(GetActionAttributeValue(action, "SourceAttributeMatrix").AsGuid()).AsGuidOrNull();
            var targetMatrixAttributeGuid = action.GetWorkflowAttributeValue(GetActionAttributeValue(action, "TargetAttributeMatrix").AsGuid()).AsGuidOrNull();

            if (sourceMatrixAttributeGuid.HasValue)
            {
                // Load the source matrix
                AttributeMatrix sourceMatrix = attributeMatrixService.Get(sourceMatrixAttributeGuid.Value);
                AttributeMatrix targetMatrix = null;

                if (targetMatrixAttributeGuid.HasValue)
                {
                    // Just delete all the existing items and add new items from the source attribute
                    targetMatrix = attributeMatrixService.Get(targetMatrixAttributeGuid.Value);
                    targetMatrix.AttributeMatrixItems.Clear();
                }
                else
                {
                    targetMatrix = new AttributeMatrix();
                    targetMatrix.AttributeMatrixItems      = new List <AttributeMatrixItem>();
                    targetMatrix.AttributeMatrixTemplateId = sourceMatrix.AttributeMatrixTemplateId;
                    attributeMatrixService.Add(targetMatrix);
                }

                // Now copy all the items from the source to the target
                foreach (var sourceItem in sourceMatrix.AttributeMatrixItems)
                {
                    var targetItem = new AttributeMatrixItem();
                    sourceItem.LoadAttributes();
                    targetItem.AttributeMatrix = targetMatrix;
                    targetItem.LoadAttributes();
                    foreach (var attribute in sourceItem.AttributeValues)
                    {
                        targetItem.SetAttributeValue(attribute.Key, attribute.Value.Value);
                    }
                    targetMatrix.AttributeMatrixItems.Add(targetItem);
                    rockContext.SaveChanges(true);
                    targetItem.SaveAttributeValues(rockContext);
                }


                // Now store the target attribute
                var targetAttribute = AttributeCache.Get(GetActionAttributeValue(action, "TargetAttributeMatrix").AsGuid(), rockContext);
                if (targetAttribute.EntityTypeId == new Rock.Model.Workflow().TypeId)
                {
                    action.Activity.Workflow.SetAttributeValue(targetAttribute.Key, targetMatrix.Guid.ToString());
                }
                else if (targetAttribute.EntityTypeId == new WorkflowActivity().TypeId)
                {
                    action.Activity.SetAttributeValue(targetAttribute.Key, targetMatrix.Guid.ToString());
                }
            }

            return(true);
        }