Beispiel #1
0
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List<string> errorMessages)
        {
            errorMessages = new List<string>();

            EntityTypeCache cachedEntityType = EntityTypeCache.Read(GetAttributeValue(action, "EntityType").AsGuid());
            var propertyValues = GetAttributeValue(action, "EntityProperties").Replace(" ! ", " | ").ResolveMergeFields(GetMergeFields(action)).TrimEnd('|').Split('|').Select(p => p.Split('^')).Select(p => new { Name = p[0], Value = p[1] });

            if (cachedEntityType != null)
            {
                Type entityType = cachedEntityType.GetEntityType();
                IEntity newEntity = (IEntity)Activator.CreateInstance(entityType);

                foreach (var prop in propertyValues)
                {
                    PropertyInfo propInf = entityType.GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance);
                    if (null != propInf && propInf.CanWrite)
                    {
                        if (!(GetAttributeValue(action, "EmptyValueHandling") == "IGNORE" && String.IsNullOrWhiteSpace(prop.Value)))
                        {
                            try
                            {
                                propInf.SetValue(newEntity, ObjectConverter.ConvertObject(prop.Value, propInf.PropertyType, GetAttributeValue(action, "EmptyValueHandling") == "NULL"), null);
                            }
                            catch (Exception ex) when (ex is InvalidCastException || ex is FormatException || ex is OverflowException)
                            {
                                errorMessages.Add("Invalid Property Value: " + prop.Name + ": " + ex.Message);
                            }
                        }
                    }
                    else
                    {
                        errorMessages.Add("Invalid Property: " + prop.Name);
                    }
                }

                rockContext.Set(entityType).Add(newEntity);
                rockContext.SaveChanges();

                // If request attribute was specified, requery the request and set the attribute's value
                Guid? entityAttributeGuid = GetAttributeValue(action, "EntityAttribute").AsGuidOrNull();
                if (entityAttributeGuid.HasValue)
                {
                    newEntity = (IEntity)rockContext.Set(entityType).Find(new object[] { newEntity.Id });
                    if (newEntity != null)
                    {
                        SetWorkflowAttributeValue(action, entityAttributeGuid.Value, newEntity.Guid.ToString());
                    }
                }

                return true;

            }
            else
            {
                errorMessages.Add("Invalid Entity Type");
            }
            return false;
        }
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List<string> errorMessages)
        {
            errorMessages = new List<string>();

            Guid workflowAttributeGuid = GetAttributeValue(action, "Entity").AsGuid();
            Guid entityGuid = action.GetWorklowAttributeValue(workflowAttributeGuid).AsGuid();

            if (!entityGuid.IsEmpty())
            {
                IEntity entityObject = null;
                EntityTypeCache cachedEntityType = EntityTypeCache.Read(GetAttributeValue(action, "EntityType").AsGuid());

                if (cachedEntityType != null)
                {
                    Type entityType = cachedEntityType.GetEntityType();
                    entityObject = rockContext.Set<IEntity>().AsQueryable().Where(e => e.Guid == entityGuid).FirstOrDefault();
                }
                else {
                    var field = AttributeCache.Read(workflowAttributeGuid).FieldType.Field;
                    entityObject = ((Rock.Field.IEntityFieldType)field).GetEntity(entityGuid.ToString(), rockContext);
                }

                var propertyValues = GetAttributeValue(action, "EntityProperties").Replace(" ! ", " | ").ResolveMergeFields(GetMergeFields(action)).TrimEnd('|').Split('|').Select(p => p.Split('^')).Select(p => new { Name = p[0], Value = p[1] });

                foreach (var prop in propertyValues)
                {
                    PropertyInfo propInf = entityObject.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance);
                    if (null != propInf && propInf.CanWrite)
                    {
                        if (!(GetAttributeValue(action, "EmptyValueHandling") == "IGNORE" && String.IsNullOrWhiteSpace(prop.Value)))
                        {
                            try
                            {
                                propInf.SetValue(entityObject, ObjectConverter.ConvertObject(prop.Value, propInf.PropertyType, GetAttributeValue(action, "EmptyValueHandling") == "NULL"), null);
                            }
                            catch (Exception ex) when (ex is InvalidCastException || ex is FormatException || ex is OverflowException)
                            {
                                errorMessages.Add("Invalid Property Value: " + prop.Name + ": " + ex.Message);
                            }
                        }
                    }
                    else
                    {
                        errorMessages.Add("Invalid Property: " + prop.Name);
                    }
                }

                rockContext.SaveChanges();
                return true;
            }
            else {
                errorMessages.Add("Invalid Entity Attribute");
            }
            return false;
        }
        /// <summary>
        /// Gets the expression.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entityIdProperty">The entity identifier property.</param>
        /// <param name="selection">The selection.</param>
        /// <returns></returns>
        public override Expression GetExpression( RockContext context, MemberExpression entityIdProperty, string selection )
        {
            // groupmembers
            var groupMembers = context.Set<GroupMember>();

            // m
            ParameterExpression groupMemberParameter = Expression.Parameter( typeof( GroupMember ), "m" );

            // m.PersonId
            MemberExpression memberPersonIdProperty = Expression.Property( groupMemberParameter, "PersonId" );

            // m.Group
            MemberExpression groupProperty = Expression.Property( groupMemberParameter, "Group" );

            // m.Group.GroupType
            MemberExpression groupTypeProperty = Expression.Property( groupProperty, "GroupType" );

            // m.Group.GroupType.Guid
            MemberExpression groupTypeGuidProperty = Expression.Property( groupTypeProperty, "Guid" );

            // family group type guid
            Expression groupTypeConstant = Expression.Constant( Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY.AsGuid() );

            // m.PersonId == p.Id
            Expression personCompare = Expression.Equal( memberPersonIdProperty, entityIdProperty );

            // m.Group.GroupType.Guid == GROUPTYPE_FAMILY guid
            Expression groupTypeCompare = Expression.Equal( groupTypeGuidProperty, groupTypeConstant );

            // m.PersonID == p.Id && m.Group.GroupType.Guid == GROUPTYPE_FAMILY guid
            Expression andExpression = Expression.And( personCompare, groupTypeCompare );

            // m => m.PersonID == p.Id && m.Group.GroupType.Guid == GROUPTYPE_FAMILY guid
            var compare = new Expression[] {
                Expression.Constant(groupMembers),
                Expression.Lambda<Func<GroupMember, bool>>(andExpression, new ParameterExpression[] { groupMemberParameter } )
            };

            // groupmembers.Where(m => m.PersonID == p.Id && m.Group.GroupType.Guid == GROUPTYPE_FAMILY guid)
            Expression whereExpression = Expression.Call( typeof( Queryable ), "Where", new Type[] { typeof( GroupMember ) }, compare );

            // m.Group.Name
            MemberExpression groupName = Expression.Property( groupProperty, "Name" );

            // m => m.Group.Name
            Expression groupNameLambda = Expression.Lambda( groupName, new ParameterExpression[] { groupMemberParameter } );

            // groupmembers.Where(m => m.PersonID == p.Id && m.Group.GroupType.Guid == GROUPTYPE_FAMILY guid).Select( m => m.Group.Name);
            Expression selectName = Expression.Call( typeof( Queryable ), "Select", new Type[] { typeof( GroupMember ), typeof( string ) }, whereExpression, groupNameLambda );

            // groupmembers.Where(m => m.PersonID == p.Id && m.Group.GroupType.Guid == GROUPTYPE_FAMILY guid).Select( m => m.Group.Name).FirstOrDefault();
            Expression firstOrDefault = Expression.Call( typeof( Queryable ), "FirstOrDefault", new Type[] { typeof( string ) }, selectName );

            return firstOrDefault;
        }
Beispiel #4
0
        /// <summary>
        /// Gets the expression.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entityIdProperty">The entity identifier property.</param>
        /// <param name="selection">The selection.</param>
        /// <returns></returns>
        public override Expression GetExpression( RockContext context, MemberExpression entityIdProperty, string selection )
        {
            // groupmembers
            var groupMembers = context.Set<Rock.Model.GroupMember>();

            // m
            ParameterExpression groupMemberParameter = Expression.Parameter( typeof( Rock.Model.GroupMember ), "m" );

            // m.PersonId
            MemberExpression memberPersonIdProperty = Expression.Property( groupMemberParameter, "PersonId" );

            // m.Group
            MemberExpression groupProperty = Expression.Property( groupMemberParameter, "Group" );
            MemberExpression groupCampusProperty = Expression.Property( groupProperty, "Campus" );

            // m.Group.GroupTypeId
            MemberExpression groupTypeProperty = Expression.Property( groupProperty, "GroupTypeID" );

            var groupTypeFamily = GroupTypeCache.GetFamilyGroupType();
            int groupTypeFamilyId = groupTypeFamily != null ? groupTypeFamily.Id : 0;

            // family group type Id
            Expression groupTypeConstant = Expression.Constant( groupTypeFamilyId );

            // m.PersonId == p.Id
            Expression personCompare = Expression.Equal( memberPersonIdProperty, entityIdProperty );

            // m.Group.GroupTypeId == GROUPTYPE_FAMILY Id
            Expression groupTypeCompare = Expression.Equal( groupTypeProperty, groupTypeConstant );

            // m.PersonID == p.Id && m.Group.GroupTypeId == GROUPTYPE_FAMILY Id
            Expression andExpression = Expression.And( personCompare, groupTypeCompare );

            // m => m.PersonID == p.Id && m.Group.GroupTypeId == GROUPTYPE_FAMILY Id
            var compare = new Expression[] {
                Expression.Constant(groupMembers),
                Expression.Lambda<Func<Rock.Model.GroupMember, bool>>(andExpression, new ParameterExpression[] { groupMemberParameter } )
            };

            // groupmembers.Where(m => m.PersonID == p.Id && m.Group.GroupTypeId == GROUPTYPE_FAMILY Id)
            Expression whereExpression = Expression.Call( typeof( Queryable ), "Where", new Type[] { typeof( Rock.Model.GroupMember ) }, compare );

            // m.Group.Campus.Name
            MemberExpression groupCampusName = Expression.Property( groupCampusProperty, "Name" );

            // m => m.Group.Campus.Name
            Expression groupCampusNameLambda = Expression.Lambda( groupCampusName, new ParameterExpression[] { groupMemberParameter } );

            // groupmembers.Where(m => m.PersonID == p.Id && m.Group.GroupTypeId == GROUPTYPE_FAMILY Id).Select( m => m.Group.Name);
            Expression selectName = Expression.Call( typeof( Queryable ), "Select", new Type[] { typeof( Rock.Model.GroupMember ), typeof( string ) }, whereExpression, groupCampusNameLambda );

            // groupmembers.Where(m => m.PersonID == p.Id && m.Group.GroupTypeId == GROUPTYPE_FAMILY Id).Select( m => m.Group.Name).FirstOrDefault();
            Expression firstOrDefault = Expression.Call( typeof( Queryable ), "FirstOrDefault", new Type[] { typeof( string ) }, selectName );

            return firstOrDefault;
        }
        /// <summary>
        /// Gets the expression.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entityIdProperty">The entity identifier property.</param>
        /// <param name="selection"></param>
        /// <returns></returns>
        public override Expression GetExpression( RockContext context, MemberExpression entityIdProperty, string selection )
        {
            // transactions
            var transactionDetails = context.Set<FinancialTransactionDetail>();

            // t
            ParameterExpression transactionDetailParameter = Expression.Parameter( typeof( FinancialTransactionDetail ), "t" );

            // t.Transaction
            MemberExpression transactionProperty = Expression.Property( transactionDetailParameter, "Transaction" );

            // t.Transaction.AuthorizedPersonAlias
            MemberExpression authorizedPersonAliasProperty = Expression.Property( transactionProperty, "AuthorizedPersonAlias" );

            // t.Transaction.AuthorizedPersonAlias.PersonId
            MemberExpression authorizedPersonIdProperty = Expression.Property( authorizedPersonAliasProperty, "PersonId" );

            // t.Transaction.AuthorizedPersonAlias.PersonId == Convert(p.Id)
            Expression whereClause = Expression.Equal( authorizedPersonIdProperty, Expression.Convert( entityIdProperty, typeof( int ) ) );

            // t.Transaction.TransactionTypeValueId
            MemberExpression transactionTypeValueIdProperty = Expression.Property( transactionProperty, "TransactionTypeValueId" );

            int transactionTypeContributionId = Rock.Web.Cache.DefinedValueCache.Read( Rock.SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION.AsGuid() ).Id;

            // t.Transaction.TransactionTypeValueId == transactionTypeContributionId
            whereClause = Expression.And( whereClause, Expression.Equal( transactionTypeValueIdProperty, Expression.Constant( transactionTypeContributionId ) ) );

            // get the selected AccountId(s).  If there are any, limit to transactions that for that Account
            if ( !string.IsNullOrWhiteSpace( selection ) )
            {
                // accountIds
                var selectedAccountIdList = selection.Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ).AsIntegerList();
                if ( selectedAccountIdList.Count() > 0 )
                {
                    // t.AccountId
                    MemberExpression accountIdProperty = Expression.Property( transactionDetailParameter, "AccountId" );

                    // accountIds.Contains(t.AccountId)
                    Expression selectedAccountIds = Expression.Constant( selectedAccountIdList );
                    Expression containsExpression = Expression.Call( selectedAccountIds, "Contains", new Type[] { }, accountIdProperty );

                    // t.authorizedPersonId == Convert(p.Id) && accountIds.Contains(t.AccountId)
                    whereClause = Expression.And( whereClause, containsExpression );
                }
            }

            // t => t.Transaction.AuthorizedPersonId == Convert(p.Id)
            var compare = new Expression[] {
                    Expression.Constant(transactionDetails),
                    Expression.Lambda<Func<FinancialTransactionDetail, bool>>( whereClause, new ParameterExpression[] { transactionDetailParameter } )
                };

            // transactions.Where( t => t.Transaction.AuthorizedPersonId == Convert(p.Id)
            Expression whereExpression = Expression.Call( typeof( Queryable ), "Where", new Type[] { typeof( FinancialTransactionDetail ) }, compare );

            // t.Transaction.TransactionDateTime
            MemberExpression transactionDateTime = Expression.Property( transactionProperty, "TransactionDateTime" );

            // t => t.Transaction.transactionDateTime
            var transactionDate = Expression.Lambda<Func<FinancialTransactionDetail, DateTime?>>( transactionDateTime, new ParameterExpression[] { transactionDetailParameter } );

            // transaction.Where( t => t.Transaction.AuthorizedPersonId == Convert(p.Id).Max( t => t.Transaction.transactionDateTime)
            string methodName = FirstOrLast == FirstLast.Last ? "Max" : "Min";
            Expression maxMinExpression = Expression.Call( typeof( Queryable ), methodName, new Type[] { typeof( FinancialTransactionDetail ), typeof( DateTime? ) }, whereExpression, transactionDate );

            return maxMinExpression;
        }
Beispiel #6
0
        /// <summary>
        /// Gets the expression.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entityIdProperty">The entity identifier property.</param>
        /// <param name="selection"></param>
        /// <returns></returns>
        public override Expression GetExpression( RockContext context, MemberExpression entityIdProperty, string selection )
        {
            // transactions
            var transactionDetails = context.Set<FinancialTransactionDetail>();

            // t
            ParameterExpression transactionDetailParameter = Expression.Parameter( typeof( FinancialTransactionDetail ), "t" );

            // t.Transaction
            MemberExpression transactionProperty = Expression.Property( transactionDetailParameter, "Transaction" );

            // t.Transaction.AuthorizedPersonId
            MemberExpression authorizedPersonIdProperty = Expression.Property( transactionProperty, "AuthorizedPersonId" );

            // t.Transaction.AuthorizedPersonId == Convert(p.Id)
            Expression whereClause = Expression.Equal(authorizedPersonIdProperty, Expression.Convert(entityIdProperty, typeof(int?)));

            // get the selected AccountId(s).  If there are any, limit to transactions that for that Account
            if ( !string.IsNullOrWhiteSpace( selection ) )
            {
                // accountIds
                var selectedAccountIdList = selection.Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ).Select( a => a.AsInteger() ).ToList();
                if ( selectedAccountIdList.Count() > 0 )
                {
                    // t.AccountId
                    MemberExpression accountIdProperty = Expression.Property( transactionDetailParameter, "AccountId" );

                    // accountIds.Contains(t.AccountId)
                    Expression selectedAccountIds = Expression.Constant(selectedAccountIdList);
                    Expression containsExpression = Expression.Call(selectedAccountIds, "Contains", new Type[] {}, accountIdProperty );

                    // t.authorizedPersonId == Convert(p.Id) && accountIds.Contains(t.AccountId)
                    whereClause = Expression.And(whereClause, containsExpression );
                }
            }

            // t => t.Transaction.AuthorizedPersonId == Convert(p.Id)
            var compare = new Expression[] {
                    Expression.Constant(transactionDetails),
                    Expression.Lambda<Func<FinancialTransactionDetail, bool>>( whereClause , new ParameterExpression[] { transactionDetailParameter } )
                };

            // transactions.Where( t => t.Transaction.AuthorizedPersonId == Convert(p.Id)
            Expression whereExpression = Expression.Call( typeof( Queryable ), "Where", new Type[] { typeof( FinancialTransactionDetail ) }, compare );

            // t.Transaction.TransactionDateTime
            MemberExpression transactionDateTime = Expression.Property( transactionProperty, "TransactionDateTime" );

            // t => t.Transaction.transactionDateTime
            var transactionDate = Expression.Lambda<Func<FinancialTransactionDetail, DateTime?>>( transactionDateTime, new ParameterExpression[] { transactionDetailParameter } );

            // transaction.Where( t => t.Transaction.AuthorizedPersonId == Convert(p.Id).Max( t => t.Transaction.transactionDateTime)
            Expression maxExpression = Expression.Call( typeof( Queryable ), "Max", new Type[] { typeof( FinancialTransactionDetail ), typeof( DateTime? ) }, whereExpression, transactionDate );

            return maxExpression;
        }