Created
November 17, 2017 20:56
-
-
Save Tschrock/871f306a89fdc4a417baf7f2570a5f2e to your computer and use it in GitHub Desktop.
For Loop worflow action for Rock RMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <copyright> | |
// Copyright by the Spark Development Network | |
// | |
// Licensed under the Rock Community License (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.rockrms.com/license | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// </copyright> | |
// | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.ComponentModel.Composition; | |
using System.Linq; | |
using Rock; | |
using Rock.Attribute; | |
using Rock.Data; | |
using Rock.Model; | |
using Rock.Workflow; | |
namespace org.newpointe.RockU.Workflow.Action.RockU | |
{ | |
/// <summary> | |
/// Activates a new activity for a given activity type | |
/// </summary> | |
[ActionCategory( "RockU" )] | |
[Description( "Activates a new activity instance and all of its actions for each item in a list." )] | |
[Export( typeof( ActionComponent ) )] | |
[ExportMetadata( "ComponentName", "Activate Activities For Items" )] | |
[WorkflowTextOrAttribute("Item List", "Item List Attribute", "A \"|\" seperated list of values to activate activities for. <span class='tip tip-lava'></span>", true, "", "", 0, "ItemList", new string[] { "Rock.Field.Types.ValueListFieldType" } )] | |
[WorkflowActivityType( "Activity", "The activity type to activate", true, "", "", 1 )] | |
[TextField( "Activity Attribute Key", "The key of the attribute where the current item will be stored in each activated activity.", false, "", "", 2 )] | |
public class ActivateActivityForEach : ActionComponent | |
{ | |
/// <summary> | |
/// Executes the specified workflow. | |
/// </summary> | |
/// <param name="rockContext">The rock context.</param> | |
/// <param name="action">The action.</param> | |
/// <param name="entity">The entity.</param> | |
/// <param name="errorMessages">The error messages.</param> | |
/// <returns></returns> | |
public override bool Execute( RockContext rockContext, WorkflowAction action, object entity, out List<string> errorMessages ) | |
{ | |
errorMessages = new List<string>(); | |
var itemListValue = GetAttributeValue( action, "ItemList" ); | |
Guid itemListValueAsGuid = itemListValue.AsGuid(); | |
if(!itemListValueAsGuid.IsEmpty()) | |
{ | |
var workflowAttributeValue = action.GetWorklowAttributeValue( itemListValueAsGuid ); | |
if ( workflowAttributeValue != null ) | |
{ | |
itemListValue = workflowAttributeValue; | |
} | |
} | |
else | |
{ | |
itemListValue = itemListValue.ResolveMergeFields( GetMergeFields( action ) ); | |
} | |
if(string.IsNullOrWhiteSpace(itemListValue)) | |
{ | |
action.AddLogEntry( "List is empty, not activating any activities.", true ); | |
return true; | |
} | |
Guid guid = GetAttributeValue( action, "Activity" ).AsGuid(); | |
if ( guid.IsEmpty() ) | |
{ | |
action.AddLogEntry( "Invalid Activity Property", true ); | |
return false; | |
} | |
var workflow = action.Activity.Workflow; | |
var activityType = new WorkflowActivityTypeService( rockContext ).Queryable() | |
.Where( a => a.Guid.Equals( guid ) ).FirstOrDefault(); | |
if ( activityType == null ) | |
{ | |
action.AddLogEntry( "Invalid Activity Property", true ); | |
return false; | |
} | |
string activityAttributeKey = GetAttributeValue( action, "ActivityAttributeKey" ); | |
bool hasActivityAttributeKey = !string.IsNullOrWhiteSpace(activityAttributeKey); | |
foreach (var item in itemListValue.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries )) | |
{ | |
var activity = WorkflowActivity.Activate( activityType, workflow ); | |
if ( hasActivityAttributeKey ) { | |
activity.SetAttributeValue( activityAttributeKey, item ); | |
} | |
action.AddLogEntry( string.Format( "Activated new '{0}' activity", activityType.ToString() ) ); | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment