Ejemplo n.º 1
0
        public SurveyMessage GetSurveyByID(int surveyID)
        {
            var survey = new SurveyMessage();

            SqlParameter[] parameters =
            {
                new SqlParameter("@SurveyID", surveyID)
            };
            var dtSurvey = HaloDatabase.GetDataTable("dbo.GetSurveyDetails", parameters);

            if (dtSurvey.Rows.Count > 0)
            {
                survey = dtSurvey
                         .AsEnumerable()
                         .Select(s => new SurveyMessage
                {
                    SurveyID    = s.Field <int>("SurveyID"),
                    SurveyName  = s.IsNull("Survey") ? string.Empty : s.Field <string>("Survey"),
                    Description = string.Format("{0} (Started: {1})", s.Field <string>("Survey"), s.IsNull("StartDate") ? string.Empty : s.Field <DateTime>("StartDate").ToShortDateString()),
                    LogoFile    = s.IsNull("LogoFile") ? string.Empty : s.Field <string>("LogoFile"),
                    StartDate   = s.IsNull("StartDate") ? string.Empty : s.Field <DateTime>("StartDate").ToShortDateString()
                })
                         .FirstOrDefault();
            }
            return(survey);
        }
 public void InsertSurveyMessage(SurveyMessage entity)
 {
     _repository.GetRepository <SurveyMessage>().Insert(entity);
 }
 public void UpdateSurveyMessage(SurveyMessage entity)
 {
     _repository.GetRepository <SurveyMessage>().Update(entity);
 }
        private void UpdateSurveyMessageDetails(NotificationMessage message)
        {
            if (message.SurveyMessage == null)
            {
                return;
            }

            SurveyMessage survey = null;

            if (message.SurveyId.GetValueOrDefault(0) == 0)             // create NEW
            {
                survey = new SurveyMessage();
                _notificationMessageService.InsertSurveyMessage(survey);
                _unitOfWorkAsync.SaveChanges();
                message.SurveyId = survey.SurveyId;
                message.SurveyMessage.SurveyId = survey.SurveyId;
                message.SurveyMessage.Questions.ToList().ForEach(q =>
                {
                    q.SurveyId  = survey.SurveyId;
                    var answers = new List <SurveyQuestionAnswer>();
                    if (q.Answers != null)
                    {
                        answers   = new List <SurveyQuestionAnswer>(q.Answers);
                        q.Answers = null;
                    }
                    _notificationMessageService.InsertSurveyQuestion(q);
                    answers.ForEach(a => a.QuestionId = q.QuestionId);
                    _notificationMessageService.InsertSurveyAnswers(answers.Where(a => a.Answer != null));
                    q.Answers = answers;
                    survey.Questions.Add(q);
                });


                survey.MessageHeader           = message.SurveyMessage.MessageHeader;
                survey.MessageShortDescription = message.SurveyMessage.MessageShortDescription;
                survey.MessageDescription      = message.SurveyMessage.MessageDescription;
                message.SurveyMessage          = survey;
            }
            else             // edit existing
            {
                survey = _notificationMessageService.FindSurveyMessage(message.SurveyId.GetValueOrDefault(0));
                survey.MessageHeader           = message.SurveyMessage.MessageHeader;
                survey.MessageShortDescription = message.SurveyMessage.MessageShortDescription;
                survey.MessageDescription      = message.SurveyMessage.MessageDescription;

                foreach (var question in message.SurveyMessage.Questions)
                {
                    var originalQuestion = survey.Questions
                                           .SingleOrDefault(c => c.QuestionId == question.QuestionId && c.QuestionId != 0);
                    // Is original child item with same ID in DB?
                    if (originalQuestion != null)
                    {
                        // Yes -> Update scalar properties of child item
                        // lets first update answers
                        if (question.Answers != null)
                        {
                            foreach (var answer in question.Answers)
                            {
                                var originalAnswer = originalQuestion.Answers
                                                     .SingleOrDefault(c => c.AnswerId == answer.AnswerId && c.AnswerId != 0);
                                // Is original child item with same ID in DB?
                                if (originalAnswer != null)
                                {
                                    // Yes -> Update scalar properties of child item
                                    originalAnswer.ModifiedDate = DateTime.UtcNow;
                                    originalAnswer.Answer       = answer.Answer;
                                    originalAnswer.ObjectState  = ObjectState.Modified;
                                    _notificationMessageService.UpdateSurveyAnswer(originalAnswer);
                                }
                                else
                                {
                                    // No -> It's a new child item -> Insert
                                    answer.QuestionId  = question.QuestionId;
                                    answer.CreatedDate = DateTime.UtcNow;
                                    answer.ObjectState = ObjectState.Added;
                                    _notificationMessageService.InsertSurveyAnswer(answer);
                                }
                            }

                            //removing answers wich is not present in request (was deleter by user)
                            var originalAnswersIds = originalQuestion.Answers.Select(a => a.AnswerId).ToList();
                            foreach (var originalAnswerId in originalAnswersIds)
                            {
                                if (!question.Answers.Select(a => a.AnswerId).Contains(originalAnswerId))
                                {
                                    var answer = originalQuestion.Answers.First(a => a.AnswerId == originalAnswerId);
                                    answer.ObjectState = ObjectState.Deleted;
                                    _notificationMessageService.DeleteSurveyAnswer(answer);
                                }
                            }
                        }

                        originalQuestion.ModifiedDate = DateTime.UtcNow;
                        originalQuestion.ObjectState  = ObjectState.Modified;
                        originalQuestion.Question     = question.Question;
                        originalQuestion.QuestionType = question.QuestionType;                         //TODO: think about it

                        _notificationMessageService.UpdateSurveyQuestion(originalQuestion);
                    }
                    else
                    {
                        // No -> It's a new child item -> Insert
                        question.ObjectState = ObjectState.Added;
                        question.SurveyId    = survey.SurveyId;
                        var answers = new List <SurveyQuestionAnswer>();
                        if (question.Answers != null)
                        {
                            answers          = new List <SurveyQuestionAnswer>(question.Answers);
                            question.Answers = null;
                        }
                        _notificationMessageService.InsertSurveyQuestion(question);
                        answers.ForEach(a => a.QuestionId = question.QuestionId);
                        _notificationMessageService.InsertSurveyAnswers(answers.Where(a => a.Answer != null));
                        question.Answers = answers;
                    }
                }

                //removing questions with is not present in request
                var questionList = new List <SurveyQuestion>(survey.Questions);
                foreach (var question in questionList)
                {
                    if (!message.SurveyMessage.Questions.Select(q => q.QuestionId).Contains(question.QuestionId))
                    {
                        var answersList = new List <SurveyQuestionAnswer>(question.Answers);
                        answersList.ForEach(a =>
                        {
                            a.ObjectState = ObjectState.Deleted;
                            _notificationMessageService.DeleteSurveyAnswer(a);
                        });

                        var userAnswersList = new List <SurveyUserAnswer>(question.UserAnswers);
                        userAnswersList.ForEach(ua =>
                        {
                            ua.ObjectState = ObjectState.Deleted;
                            _notificationMessageService.DeleteSurveyUserAnswer(ua);
                        });

                        question.ObjectState = ObjectState.Deleted;
                        _notificationMessageService.DeleteSurveyQuestion(question);
                    }
                }


                message.SurveyMessage = survey;
            }

            _notificationMessageService.UpdateSurveyMessage(survey);
            _unitOfWorkAsync.SaveChanges();
        }