Example #1
0
        /// <summary>
        /// Gets the validity of a specified cell.
        /// </summary>
        /// <param name="cell">The cell.</param>
        /// <returns>
        /// The validity of the specified cell.
        /// </returns>
        public static Validity GetValidity(
            this IValidationCell cell)
        {
            if (cell == null)
            {
                throw new ArgumentNullException(nameof(cell));
            }

            var validationStatus = cell.GetValidationStatus();

            switch (validationStatus)
            {
            case ValidationStatus.ValidationMissing:
            case ValidationStatus.DeterminedSubjectIsValid:
                return(Validity.Valid);

            case ValidationStatus.DeemedNotApplicable:
                return(Validity.NotApplicable);

            case ValidationStatus.DeterminedSubjectIsInvalid:
                return(Validity.Invalid);

            case ValidationStatus.Aborted:
                return(Validity.Aborted);

            default:
                return(Validity.Unknown);
            }
        }
Example #2
0
        /// <summary>
        /// Gets the validation message for a specified cell or null if none exists.
        /// </summary>
        /// <param name="cell">The cell.</param>
        /// <returns>
        /// The validation message for the specified cell or null if none exists.
        /// </returns>
        public static string GetValidationMessageOrNull(
            this IValidationCell cell)
        {
            if (cell == null)
            {
                throw new ArgumentNullException(nameof(cell));
            }

            string result;

            var lastValidationEvent = cell.ValidationEvents?.LastOrDefault();

            if (lastValidationEvent == null)
            {
                result = null;
            }
            else if (lastValidationEvent is IHaveMessage haveMessageEvent)
            {
                result = haveMessageEvent.Message;
            }
            else
            {
                result = null;
            }

            return(result);
        }
        public ValidateCellIfNecessaryOp DeepCloneWithCell(IValidationCell cell)
        {
            var result = new ValidateCellIfNecessaryOp(
                cell);

            return(result);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ValidateCellIfNecessaryOp"/> class.
        /// </summary>
        /// <param name="cell">The cell.</param>
        public ValidateCellIfNecessaryOp(
            IValidationCell cell)
        {
            if (cell == null)
            {
                throw new ArgumentNullException(nameof(cell));
            }

            this.Cell = cell;
        }
Example #5
0
        /// <summary>
        /// Gets the validation status of a specified cell.
        /// </summary>
        /// <param name="cell">The cell.</param>
        /// <returns>
        /// The validation status of the specified cell.
        /// </returns>
        public static ValidationStatus GetValidationStatus(
            this IValidationCell cell)
        {
            if (cell == null)
            {
                throw new ArgumentNullException(nameof(cell));
            }

            ValidationStatus result;

            var lastValidationEvent = cell.ValidationEvents?.LastOrDefault();

            if (cell.Validation == null)
            {
                result = ValidationStatus.ValidationMissing;
            }
            else if (lastValidationEvent == null)
            {
                result = ValidationStatus.Unvalidated;
            }
            else if (lastValidationEvent is CellValidationClearedEvent)
            {
                result = ValidationStatus.Unvalidated;
            }
            else if (lastValidationEvent is CellValidationDeemedNotApplicableEvent)
            {
                result = ValidationStatus.DeemedNotApplicable;
            }
            else if (lastValidationEvent is CellValidationAbortedEvent)
            {
                result = ValidationStatus.Aborted;
            }
            else if (lastValidationEvent is CellValidationDeterminedCellValidEvent)
            {
                result = ValidationStatus.DeterminedSubjectIsValid;
            }
            else if (lastValidationEvent is CellValidationFailedEvent)
            {
                result = ValidationStatus.Failed;
            }
            else if (lastValidationEvent is CellValidationDeterminedCellInvalidEvent)
            {
                result = ValidationStatus.DeterminedSubjectIsInvalid;
            }
            else
            {
                throw new InvalidOperationException(Invariant($"Cannot determine the {nameof(ValidationStatus)} of the specified cell."));
            }

            return(result);
        }
Example #6
0
        private async Task ValidateCellIfNecessaryAsync(
            IValidationCell cell)
        {
            // NOTE: THIS CODE IS A NEAR DUPLICATE OF THE SYNC METHOD ABOVE; NO GOOD WAY TO D.R.Y. IT OUT
            var validationStatus = cell.GetValidationStatus();

            if (validationStatus == ValidationStatus.ValidationMissing)
            {
                // no-op
            }
            else if (validationStatus == ValidationStatus.Unvalidated)
            {
                CellValidationEventBase validationEvent;

                try
                {
                    var validation = cell.Validation;

                    var validationResult = await this.protocolFactory.GetProtocolAndExecuteViaReflectionAsync <ValidationResult>(validation.Operation);

                    string message = null;

                    if (validationResult.MessageOp != null)
                    {
                        message = await this.protocolFactory.GetProtocolAndExecuteViaReflectionAsync <string>(validationResult.MessageOp);
                    }

                    var validity = await this.protocolFactory.GetProtocolAndExecuteViaReflectionAsync <Validity>(validationResult.ValidityOp);

                    if (validity == Validity.Invalid)
                    {
                        validationEvent = new CellValidationDeterminedCellInvalidEvent(this.timestampUtc, null, message);
                    }
                    else if (validity == Validity.Valid)
                    {
                        validationEvent = new CellValidationDeterminedCellValidEvent(this.timestampUtc, null, message);
                    }
                    else if (validity == Validity.NotApplicable)
                    {
                        validationEvent = new CellValidationDeemedNotApplicableEvent(this.timestampUtc, null, message);
                    }
                    else if (validity == Validity.Aborted)
                    {
                        validationEvent = new CellValidationAbortedEvent(this.timestampUtc, null, message);
                    }
                    else
                    {
                        throw new NotSupportedException(Invariant($"This {nameof(Validity)} is not supported: {validity}."));
                    }
                }
                catch (OpExecutionAbortedExceptionBase ex)
                {
                    // Here are are purposefully setting message to null because we have no idea who the thrower is
                    // nor whether the report author wants to emit this message to the user.
                    validationEvent = new CellValidationAbortedEvent(this.timestampUtc, ex.ToString(), null);
                }
                catch (OpExecutionDeemedNotApplicableExceptionBase ex)
                {
                    // Here are are purposefully setting message to null because we have no idea who the thrower is
                    // nor whether the report author wants to emit this message to the user.
                    validationEvent = new CellValidationDeemedNotApplicableEvent(this.timestampUtc, ex.ToString(), null);
                }
                catch (Exception ex)
                {
                    // The "proper" exception for a protocol to throw is an OpExecutionFailedExceptionBase.
                    // Protocol authors might not comply.
                    validationEvent = new CellValidationFailedEvent(this.timestampUtc, ex.ToString());
                }

                cell.Record(validationEvent);
            }
            else if (cell.ValidationEvents.Last().TimestampUtc != this.timestampUtc)
            {
                throw new InvalidOperationException("Something went wrong.  The cell was validated, but the recorded timestamp doesn't match this timestamp.");
            }
        }