/// <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; } } }
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); }
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); }