Beispiel #1
0
        /// <summary>
        /// Validation, which depends on data retrieved from the database.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        private async Task <CreateUpdateProtocolStatus> PerformValidationStep2(CreateProtocolRequestDto request)
        {
            // Validating that ElementIds provided in the request correspond to existing Elements in the database
            var elementIds =
                request
                .ProtocolElements
                .Select(pe => pe.ElementId)
                .Distinct()
                .ToList();

            var existingElements = await protocolService.GetElementsByIds(elementIds);

            if (existingElements.Count != elementIds.Count)
            {
                return(CreateUpdateProtocolStatus.OneOfElementIdsInvalid);
            }

            // Validating that ProtocolElement associated with particular Element have conditions with appropriate Operand based on ElementType
            foreach (var protocolElement in request.ProtocolElements)
            {
                var alertConditions = protocolElement.Alerts != null && protocolElement.Alerts.Any()
                    ? protocolElement.Alerts.SelectMany(b => b.Conditions).ToList()
                    : new List <ConditionDto>();

                var branchConditions = protocolElement.Branches != null && protocolElement.Branches.Any()
                    ? protocolElement.Branches.SelectMany(b => b.Conditions).ToList()
                    : new List <ConditionDto>();

                var conditions = alertConditions.Concat(branchConditions).ToList();

                if (conditions.Any())
                {
                    var associatedElement = existingElements.SingleOrDefault(e => e.Id == protocolElement.ElementId);

                    if (associatedElement != null)
                    {
                        if (associatedElement is QuestionElement)
                        {
                            var questionElement = (QuestionElement)associatedElement;

                            if (questionElement.AnswerSet is SelectionAnswerSet)
                            {
                                Guid tempGuid;
                                var  selectionAnswerSet = questionElement.AnswerSet as SelectionAnswerSet;

                                if (conditions.Any(c => c.Operand != OperandType.SelectionAnswerChoice ||
                                                   !Guid.TryParse(c.Value, out tempGuid) ||
                                                   !selectionAnswerSet.SelectionAnswerChoices.Select(ac => ac.Id).Contains(Guid.Parse(c.Value)))
                                    )
                                {
                                    return(CreateUpdateProtocolStatus.OneOfBranchesOrAlertsInproperlyFilledIn);
                                }
                            }

                            if (questionElement.AnswerSet is ScaleAnswerSet)
                            {
                                if (conditions.Any(c => c.Operand != OperandType.ScaleAnswerSet))
                                {
                                    return(CreateUpdateProtocolStatus.OneOfBranchesOrAlertsInproperlyFilledIn);
                                }
                            }
                        }

                        if (associatedElement is MeasurementElement)
                        {
                            if (conditions.Any(c => c.Operand == OperandType.SelectionAnswerChoice || c.Operand == OperandType.ScaleAnswerSet))
                            {
                                return(CreateUpdateProtocolStatus.OneOfBranchesOrAlertsInproperlyFilledIn);
                            }
                        }

                        if (associatedElement is TextMediaElement)
                        {
                            return(CreateUpdateProtocolStatus.OneOfBranchesOrAlertsInproperlyFilledIn);
                        }
                    }
                }
            }

            return(CreateUpdateProtocolStatus.Success);
        }