Example #1
0
        private static AnswerQuestion GetAnswer(ApplicationAnswer answer)
        {
            if (string.IsNullOrEmpty(answer.Value))
            {
                return(null);
            }

            return(new AnswerQuestion
            {
                ID = answer.Question.Id,
                FormatID = int.Parse(answer.Question.FormatId),
                FormatIDSpecified = true,
                Items = new [] { answer is MultipleChoiceAnswer ? (object)int.Parse(answer.Value) : answer.Value },
            });
        }
        /// <summary>
        /// Creates an answer record with the given parameters
        /// </summary>
        /// <param name="applicationId">Application to link the answer to</param>
        /// <param name="appQuestionAndLine">Details of the question and line if applicable</param>
        public ApplicationAnswer CreateAnswerRecord(Guid applicationId, ApplicationQuestionsAndLines appQuestionAndLine)
        {
            ApplicationAnswer newAnswer = new ApplicationAnswer
            {
                ApplicationQuestionId = appQuestionAndLine.ApplicationQuestionId,
                ApplicationLineId     = appQuestionAndLine.ApplicationLineId
            };

            // Application answer does not exist, create it
            Entity newAnswerEntity = new Entity(defra_applicationanswer.EntityLogicalName);

            newAnswerEntity.Attributes.Add(defra_applicationanswer.Fields.defra_application,
                                           new EntityReference(defra_application.EntityLogicalName, applicationId));

            if (appQuestionAndLine.ApplicationQuestionId.HasValue)
            {
                newAnswerEntity.Attributes.Add(defra_applicationanswer.Fields.defra_question,
                                               new EntityReference(defra_applicationquestion.EntityLogicalName,
                                                                   appQuestionAndLine.ApplicationQuestionId.Value));
            }

            // If Question is at the Item level, link the answer to the corresponding application line
            if (appQuestionAndLine.ApplicationLineId.HasValue &&
                appQuestionAndLine.Scope.HasValue &&
                appQuestionAndLine.Scope.Value == (int)defra_application_task_scope.Item)
            {
                newAnswerEntity.Attributes.Add(
                    defra_applicationanswer.Fields.defra_applicationlineid,
                    new EntityReference(
                        defra_applicationline.EntityLogicalName,
                        appQuestionAndLine.ApplicationLineId.Value));
            }

            // Talk to CRM, create the answer record
            newAnswer.ApplicationAnswerId = OrganisationService.Create(newAnswerEntity);

            // Return answer
            return(newAnswer);
        }
        /// <summary>
        /// Function takes care of removing application answers that are no longer applicable
        /// to an application, and adds new answer records depending on the application lines
        /// linked to the application
        /// </summary>
        /// <param name="applicationId">Guid for the application to process</param>
        public void RefreshApplicationAnswers(Guid applicationId)
        {
            // Get the list of questions that should be there
            List <ApplicationQuestionsAndLines> applicableQuestionsAndLines = GetApplicableApplicationQuestions(applicationId) ?? new List <ApplicationQuestionsAndLines>();

            // And the list of questions that are linked to the application
            List <ApplicationAnswer> currentApplicationAnswers = GetApplicationAnswers(applicationId) ?? new List <ApplicationAnswer>();

            // Now work out which application answers should be removed
            /* WE -2269 - we no longer deactivate application answers, as the front end may create these ad-hoc */

            /*
             * foreach (ApplicationAnswer appAnswer in currentApplicationAnswers)
             * {
             *  // Check if the existing application answer still applies
             *  if (!HasMatchingApplicationQuestion(applicableQuestionsAndLines, appAnswer))
             *  {
             *      // Application answer no longer applies, deactivate it
             *      SetStatusAndState(
             *          new EntityReference(defra_applicationanswer.EntityLogicalName, appAnswer.ApplicationAnswerId),
             *          (int)defra_applicationanswerState.Inactive,
             *          (int)defra_applicationanswer_StatusCode.Inactive);
             *  }
             * }
             */

            // And work out which application answers should be created
            foreach (ApplicationQuestionsAndLines appQuestionAndLine in applicableQuestionsAndLines)
            {
                // Check if answer already exists, create the Answer record if it does not already exist
                if (!HasMatchingApplicationAnswer(currentApplicationAnswers, appQuestionAndLine))
                {
                    ApplicationAnswer newAppAnswer = CreateAnswerRecord(applicationId, appQuestionAndLine);

                    // Save the newly created application answer, so that we don't create a duplicate
                    currentApplicationAnswers.Add(newAppAnswer);
                }
            }
        }
 private static bool HasMatchingApplicationQuestion(List <ApplicationQuestionsAndLines> applicableQuestionsAndLines, ApplicationAnswer appAnswer)
 {
     return(applicableQuestionsAndLines.Any(
                q => q.ApplicationQuestionId == appAnswer.ApplicationQuestionId
                // If scope of question is at the Item level, also check the application line
                && (!q.Scope.HasValue || q.Scope.Value == (int)defra_application_task_scope.Item && q.ApplicationLineId == appAnswer.ApplicationLineId)));
 }