Exemplo n.º 1
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, AttributeKeys.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, AttributeKeys.EntityIdGuid, 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, AttributeKeys.EntityIdGuid);
                entityObject = action.GetEntityFromAttributeValue(value, rockContext);
            }

            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, AttributeKeys.AttributeKey, true).ResolveMergeFields(mergeFields);
            string attributeValue = GetAttributeValue(action, AttributeKeys.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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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)
            {
                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, AttributeKey.EntityIdGuid, 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.EntityIdGuid);
                entityObject = action.GetEntityFromAttributeValue(value, rockContext);
            }

            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, AttributeKey.PropertyName, true).ResolveMergeFields(mergeFields);
            string propertyValue      = GetAttributeValue(action, AttributeKey.PropertyValue, true).ResolveMergeFields(mergeFields);
            string emptyValueHandling = GetAttributeValue(action, AttributeKey.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);
        }