/// <summary>
        /// Gets the entity name
        /// </summary>
        /// <param name="entityTypeId">The Id of the Entity Type.</param>
        /// <param name="entityId">The Id of the Entity.</param>
        private string GetEntityName(int?entityTypeId, int?entityId, RockContext rockContext)
        {
            var retVal = string.Empty;

            if (entityTypeId.HasValue && entityId.HasValue)
            {
                var service = new EntityTypeService(rockContext);
                var entity  = service.GetEntity(( int )entityTypeId, ( int )entityId);
                if (entity is Group)
                {
                    retVal = (( Group )entity).Name;
                }
                else if (entity is GroupMember)
                {
                    retVal = (( GroupMember )entity).Person.FullName;
                }
                else if (entity is DefinedValue)
                {
                    retVal = (( DefinedValue )entity).Value;
                }
                else if (entity is RegistrationRegistrant)
                {
                    retVal = (( RegistrationRegistrant )entity).Person.FullName;
                }
            }

            return(retVal);
        }
示例#2
0
 private void AddEntityByGuid(EntityTypeService service, int entityTypeId, Guid?entityGuid, List <IEntity> entities)
 {
     if (entityGuid.HasValue)
     {
         var entity = service.GetEntity(entityTypeId, entityGuid.Value);
         if (entity != null)
         {
             entities.Add(entity);
         }
     }
 }
示例#3
0
        /// <summary>
        /// Gets the entity.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public IEntity GetEntity(RockContext rockContext, Message message)
        {
            if (!message.EntityTypeId.HasValue || !message.EntityId.HasValue)
            {
                return(null);
            }

            var entityTypeService = new EntityTypeService(rockContext);
            var entity            = entityTypeService.GetEntity(message.EntityTypeId.Value, message.EntityId.Value);

            return(entity);
        }
示例#4
0
        /// <summary>
        /// Gets the entity.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="workflowDetail">The workflow detail.</param>
        /// <returns></returns>
        public IEntity GetEntity(RockContext rockContext, WorkflowDetail workflowDetail)
        {
            if (!workflowDetail.EntityTypeId.HasValue || !workflowDetail.EntityId.HasValue)
            {
                return(null);
            }

            var entityTypeService = new EntityTypeService(rockContext);
            var entity            = entityTypeService.GetEntity(workflowDetail.EntityTypeId.Value, workflowDetail.EntityId.Value);

            return(entity);
        }
示例#5
0
        /// <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>();

            // Get the entity type
            EntityTypeCache entityType     = null;
            var             entityTypeGuid = GetAttributeValue(action, AttributeKey.EntityType).AsGuidOrNull();

            if (entityTypeGuid.HasValue)
            {
                entityType = EntityTypeCache.Get(entityTypeGuid.Value);
            }

            if (entityType == null)
            {
                var message = $"Entity Type could not be found for selected value ('{entityTypeGuid.ToString()}')!";
                errorMessages.Add(message);
                action.AddLogEntry(message, true);
                return(false);
            }

            var mergeFields  = GetMergeFields(action);
            var _rockContext = new RockContext();

            // Get the entity
            var     entityTypeService  = new EntityTypeService(_rockContext);
            IEntity entityObject       = null;
            var     entityIdGuidString = GetAttributeValue(action, AttributeKey.EntityIdOrGuid, true).ResolveMergeFields(mergeFields).Trim();
            var     entityId           = entityIdGuidString.AsIntegerOrNull();

            if (entityId.HasValue)
            {
                entityObject = entityTypeService.GetEntity(entityType.Id, entityId.Value);
            }
            else
            {
                var entityGuid = entityIdGuidString.AsGuidOrNull();
                if (entityGuid.HasValue)
                {
                    entityObject = entityTypeService.GetEntity(entityType.Id, entityGuid.Value);
                }
            }

            if (entityObject == null)
            {
                var value = GetActionAttributeValue(action, AttributeKey.EntityIdOrGuid);
                entityObject = action.GetEntityFromAttributeValue(value, rockContext);
            }

            if (entityObject == null)
            {
                var message = $"Entity could not be found for selected value ('{entityIdGuidString}')!";
                errorMessages.Add(message);
                action.AddLogEntry(message, true);
                return(false);
            }

            var attributeFilteredDocumentType = GetAttributeValue(action, AttributeKey.DocumentType).Split(',').Select(int.Parse).FirstOrDefault();
            var documentType = DocumentTypeCache.Get(attributeFilteredDocumentType);

            if (documentType == null)
            {
                var message = $"Document Type could not be found for selected value ('{attributeFilteredDocumentType}')!";
                errorMessages.Add(message);
                action.AddLogEntry(message, true);
                return(false);
            }

            var documentypesForContextEntityType = DocumentTypeCache.GetByEntity(entityType.Id, true);

            if (!documentypesForContextEntityType.Any(d => attributeFilteredDocumentType == d.Id))
            {
                var message = "The Document Type does not match the selected entity type.";
                errorMessages.Add(message);
                action.AddLogEntry(message, true);
                return(false);
            }

            var isDocumentTypeValid = IsDocumentTypeValidForEntity(entityObject, documentType);

            if (!isDocumentTypeValid)
            {
                var message = "The Document Type selected is not valid for the entity.";
                errorMessages.Add(message);
                action.AddLogEntry(message, true);
                return(false);
            }

            var binaryFile = new BinaryFileService(rockContext).Get(GetAttributeValue(action, AttributeKey.DocumentAttribute, true).AsGuid());

            if (binaryFile == null)
            {
                action.AddLogEntry("The document to add to the entity was not found.", true);

                // returning true here to allow the action to run 'successfully' without a document.
                // This allows the action to be easily used when the document is optional without a bunch of action filter tests.
                return(true);
            }

            var documentName = GetAttributeValue(action, AttributeKey.DocumentName).ResolveMergeFields(mergeFields);

            if (documentName.IsNullOrWhiteSpace())
            {
                documentName = Path.GetFileNameWithoutExtension(binaryFile.FileName);
            }

            var document = new Document();

            document.Name           = documentName;
            document.Description    = GetAttributeValue(action, AttributeKey.DocumentDescription).ResolveMergeFields(mergeFields);
            document.EntityId       = entityObject.Id;
            document.DocumentTypeId = documentType.Id;
            document.SetBinaryFile(binaryFile.Id, rockContext);

            if (!document.IsValidDocument(rockContext, out string errorMessage))
            {
                errorMessages.Add(errorMessage);
                action.AddLogEntry(errorMessage, true);
                return(false);
            }

            var documentService = new DocumentService(rockContext);

            documentService.Add(document);
            rockContext.SaveChanges();

            action.AddLogEntry("Added document to the Entity.");
            return(true);
        }
示例#6
0
        /// <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>();

            // Get the entity type
            EntityTypeCache entityType     = null;
            var             entityTypeGuid = GetAttributeValue(action, "EntityType").AsGuidOrNull();

            if (entityTypeGuid.HasValue)
            {
                entityType = EntityTypeCache.Get(entityTypeGuid.Value);
            }
            if (entityType == null)
            {
                errorMessages.Add(string.Format("Entity Type could not be found for selected value ('{0}')!", entityTypeGuid.ToString()));
                return(false);
            }

            var         mergeFields  = GetMergeFields(action);
            RockContext _rockContext = new RockContext();

            // Get the entity
            EntityTypeService entityTypeService  = new EntityTypeService(_rockContext);
            IEntity           entityObject       = null;
            string            entityIdGuidString = GetAttributeValue(action, "EntityIdGuid", true).ResolveMergeFields(mergeFields).Trim();
            var entityGuid = entityIdGuidString.AsGuidOrNull();

            if (entityGuid.HasValue)
            {
                entityObject = entityTypeService.GetEntity(entityType.Id, entityGuid.Value);
            }
            else
            {
                var entityId = entityIdGuidString.AsIntegerOrNull();
                if (entityId.HasValue)
                {
                    entityObject = entityTypeService.GetEntity(entityType.Id, entityId.Value);
                }
            }

            if (entityObject == null)
            {
                errorMessages.Add(string.Format("Entity could not be found for selected value ('{0}')!", entityIdGuidString));
                return(false);
            }

            // Get the property settings
            string propertyName       = GetAttributeValue(action, "PropertyName", true).ResolveMergeFields(mergeFields);
            string propertyValue      = GetAttributeValue(action, "PropertyValue", true).ResolveMergeFields(mergeFields);
            string emptyValueHandling = GetAttributeValue(action, "EmptyValueHandling");

            if (emptyValueHandling == "IGNORE" && String.IsNullOrWhiteSpace(propertyValue))
            {
                action.AddLogEntry("Skipping empty value.");
                return(true);
            }

            PropertyInfo propInf = entityObject.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);

            if (propInf == null)
            {
                errorMessages.Add(string.Format("Property does not exist ('{0}')!", propertyName));
                return(false);
            }

            if (!propInf.CanWrite)
            {
                errorMessages.Add(string.Format("Property is not writable ('{0}')!", entityIdGuidString));
                return(false);
            }

            try
            {
                propInf.SetValue(entityObject, ConvertObject(propertyValue, propInf.PropertyType, emptyValueHandling == "NULL"), null);
            }
            catch (Exception ex) when(ex is InvalidCastException || ex is FormatException || ex is OverflowException)
            {
                errorMessages.Add(string.Format("Could not convert property value ('{0}')! {1}", propertyValue, ex.Message));
                return(false);
            }

            if (!entityObject.IsValid)
            {
                errorMessages.Add(string.Format("Invalid property value ('{0}')! {1}", propertyValue, entityObject.ValidationResults.Select(r => r.ErrorMessage).ToList().AsDelimited(" ")));
                return(false);
            }

            try
            {
                _rockContext.SaveChanges();
            }
            catch (Exception ex)
            {
                errorMessages.Add(string.Format("Could not save value ('{0}')! {1}", propertyValue, ex.Message));
                return(false);
            }

            action.AddLogEntry(string.Format("Set '{0}' property to '{1}'.", propertyName, propertyValue));

            return(true);
        }
示例#7
0
        /// <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>();

            // Get the entity type
            EntityTypeCache entityType     = null;
            var             entityTypeGuid = GetAttributeValue(action, "EntityType").AsGuidOrNull();

            if (entityTypeGuid.HasValue)
            {
                entityType = EntityTypeCache.Get(entityTypeGuid.Value);
            }
            if (entityType == null)
            {
                errorMessages.Add(string.Format("Entity Type could not be found for selected value ('{0}')!", entityTypeGuid.ToString()));
                return(false);
            }

            var         mergeFields  = GetMergeFields(action);
            RockContext _rockContext = new RockContext();

            // Get the entity
            EntityTypeService entityTypeService  = new EntityTypeService(_rockContext);
            IEntity           entityObject       = null;
            string            entityIdGuidString = GetAttributeValue(action, "EntityIdGuid", true).ResolveMergeFields(mergeFields).Trim();
            var entityGuid = entityIdGuidString.AsGuidOrNull();

            if (entityGuid.HasValue)
            {
                entityObject = entityTypeService.GetEntity(entityType.Id, entityGuid.Value);
            }
            else
            {
                var entityId = entityIdGuidString.AsIntegerOrNull();
                if (entityId.HasValue)
                {
                    entityObject = entityTypeService.GetEntity(entityType.Id, entityId.Value);
                }
            }

            if (entityObject == null)
            {
                errorMessages.Add(string.Format("Entity could not be found for selected value ('{0}')!", entityIdGuidString));
                return(false);
            }

            var entityWithAttributes = entityObject as Attribute.IHasAttributes;

            if (entityWithAttributes == null)
            {
                errorMessages.Add(string.Format("Entity does not support attributes ('{0}')!", entityIdGuidString));
                return(false);
            }

            // Get the property settings
            string attributeKey   = GetAttributeValue(action, "AttributeKey", true).ResolveMergeFields(mergeFields);
            string attributeValue = GetAttributeValue(action, "AttributeValue", true).ResolveMergeFields(mergeFields);

            entityWithAttributes.LoadAttributes(_rockContext);
            entityWithAttributes.SetAttributeValue(attributeKey, attributeValue);

            try
            {
                entityWithAttributes.SaveAttributeValue(attributeKey, _rockContext);
            }
            catch (Exception ex)
            {
                errorMessages.Add(string.Format("Could not save value ('{0}')! {1}", attributeValue, ex.Message));
                return(false);
            }

            action.AddLogEntry(string.Format("Set '{0}' attribute to '{1}'.", attributeKey, attributeValue));

            return(true);
        }