示例#1
0
        public static void SaveAnswer(CampaignQuestionnaire oQuestionnaire, int pContactId = 0)
        {
            using (BrightPlatformEntities BPContext = new BrightPlatformEntities(UserSession.EntityConnection))
            {
                #region Member Variables
                var settings = oQuestionnaire.Form.Settings;
                var answerOptions = oQuestionnaire.Form.Settings.AnswerOptions;
                var binding = settings.DataBindings;
                sub_answers subanswer = null;
                //answer tempAnswer = null;
                //sub_answers tempSubAnswer = null;

                IDropbox iDropbox = null;
                ITextbox iTextbox = null;
                IMultipleChoice iMultiplechoice = null;
                ISchedule iSchedule = null;
                ISmartText iSmartText = null;
                IEnumerable<sub_answers> subanswersValues = null;
                List<MultipleChoiceValue> listMCV = null;
                List<OtherChoice> listOtherChoice = null;
                List<Attendie> listAttendie = null;
                List<SmartTextValue> listSmartText = null;
                bool IsNew = false;
                #endregion

                string answer_id = binding.answer_id;
                int int_answer_id = (!string.IsNullOrEmpty(answer_id) ? int.Parse(answer_id) : 0);
                var objAnswer = BPContext.answers.Include("sub_answers")
                    .FirstOrDefault(p => p.id == int_answer_id);
                if (objAnswer == null)
                {
                    #region Add Answer Details
                    IsNew = true;
                    //if not exists create new answer
                    objAnswer = new answer();
                    //set dummy values
                    //tempAnswer = BPContext.answers.OrderByDescending(a => a.id).FirstOrDefault();
                    //objAnswer.id = tempAnswer != null ? tempAnswer.id + 1 : 1;
                    objAnswer.account_level = binding.account_level;
                    objAnswer.questionlayout_id = int.Parse(binding.questionlayout_id);
                    objAnswer.accounts_id = int.Parse(binding.account_id);
                    //objAnswer.contact_id = !string.IsNullOrEmpty(binding.contact_id) ? int.Parse(binding.contact_id) : (int?) null;
                    objAnswer.campaigns_id = int.Parse(binding.campaign_id);
                    objAnswer.dialog_id = int.Parse(binding.dialog_id);
                    objAnswer.created_by = UserSession.CurrentUser.UserId;
                    objAnswer.created_timestamp = DateTime.Now;
                    objAnswer.modified_on = DateTime.Now;
                    objAnswer.modified_by = UserSession.CurrentUser.UserId;
                    //set settings
                    objAnswer.OwnershipBrightvision = settings.BVOwnership;
                    objAnswer.OwnershipAccount = settings.CustomerOwnership;
                    #endregion
                }
                //update the value of the schedule_id
                if (!string.IsNullOrEmpty(binding.schedule_id))
                    objAnswer.schedule_id = int.Parse(binding.schedule_id);

                //update account level question to currently selected contact id
                //if (binding.account_level)
                objAnswer.contact_id = !string.IsNullOrEmpty(binding.contact_id) ? int.Parse(binding.contact_id) : (int?)null;

                /**
                 * [@jeff 04.12.2013]: https://brightvision.jira.com/browse/PLATFORM-2742
                 * assign the proper contact id when saving answers.
                 */
                //objAnswer.contact_id = pContactId;

                //bool maxIDset = false;
                for (int x = 0; x < answerOptions.Count; ++x)
                {
                    switch (oQuestionnaire.Type.ToLower())
                    {
                        #region SubAnswer for Textbox Component
                        case QuestionTypeConstants.Textbox:
                            iTextbox = answerOptions[x] as ITextbox;
                            subanswer = objAnswer.sub_answers.ElementAtOrDefault(x);
                            if (subanswer == null)
                            {
                                //if not exists create new subanswer
                                subanswer = new sub_answers();
                                subanswer.answer_text = iTextbox.InputValue != null ? iTextbox.InputValue : string.Empty;
                                subanswer.sub_answer_index = -1;
                                objAnswer.sub_answers.Add(subanswer);
                            }
                            else
                            {
                                subanswer.answer_text = iTextbox.InputValue != null ? iTextbox.InputValue : string.Empty;
                                subanswer.sub_answer_index = -1;
                            }
                            break;
                        #endregion
                        #region SubAnswer for Dropbox Component
                        case QuestionTypeConstants.Dropbox:
                            iDropbox = answerOptions[x] as IDropbox;
                            subanswer = objAnswer.sub_answers.ElementAtOrDefault(x);
                            if (subanswer == null)
                            {
                                //if not exists create new subanswer
                                subanswer = new sub_answers();
                                subanswer.answer_text = new DropboxValue()
                                {
                                    SelectionValue = iDropbox.SelectionValue != null ? iDropbox.SelectionValue : string.Empty,
                                    OtherValue = iDropbox.DefaultSelectionValueIfOther != null ? iDropbox.DefaultSelectionValueIfOther : string.Empty
                                    //OtherValue = iDropbox.SelectionValueIfOther != null ? iDropbox.SelectionValueIfOther : string.Empty
                                }.ToJSONString();
                                subanswer.sub_answer_index = 1;
                                objAnswer.sub_answers.Add(subanswer);
                            }
                            else
                            {
                                subanswer.answer_text = new DropboxValue()
                                {
                                    SelectionValue = iDropbox.SelectionValue != null ? iDropbox.SelectionValue : string.Empty,
                                    OtherValue = iDropbox.DefaultSelectionValueIfOther != null ? iDropbox.DefaultSelectionValueIfOther : string.Empty
                                    //OtherValue = iDropbox.SelectionValueIfOther != null ? iDropbox.SelectionValueIfOther : string.Empty
                                }.ToJSONString();
                                subanswer.sub_answer_index = 1;
                            }
                            break;
                        #endregion
                        #region SubAnswer for MultipleChoice Component
                        case QuestionTypeConstants.MultipleChoice:
                            iMultiplechoice = answerOptions[x] as IMultipleChoice;
                            //Note: sub_answer_index != -1 if answer is in multiple choice values
                            //      sub_answer_index == -1 if answer is in other choice

                            //set multiplechoicevalues
                            listMCV = iMultiplechoice.MultipleChoiceValues;
                            subanswersValues =
                                from obj in objAnswer.sub_answers
                                where obj.sub_answer_index != -1
                                select obj;
                            if (subanswersValues != null && subanswersValues.Count() > 0)
                            {
                                for (int q = 0; q < subanswersValues.Count(); ++q)
                                {
                                    subanswer = subanswersValues.ElementAtOrDefault(q);
                                    if (subanswer != null)
                                    {
                                        subanswer.sub_answer_index = Convert.ToInt16(listMCV[q].Checked);
                                        subanswer.answer_text = listMCV[q].TextPrefix;
                                    }
                                }
                            }
                            else
                            {
                                for (int q = 0; q < iMultiplechoice.MultipleChoiceValues.Count; ++q)
                                {
                                    //if not exists create new subanswer
                                    subanswer = new sub_answers();
                                    subanswer.sub_answer_index = Convert.ToInt16(listMCV[q].Checked);
                                    subanswer.answer_text = listMCV[q].TextPrefix;
                                    objAnswer.sub_answers.Add(subanswer);
                                }
                            }
                            //set otherchoice
                            listOtherChoice = iMultiplechoice.OtherChoices;
                            subanswersValues =
                                from obj in objAnswer.sub_answers
                                where obj.sub_answer_index == -1
                                select obj;
                            if (subanswersValues != null && subanswersValues.Count() > 0)
                            {
                                for (int j = 0; j < subanswersValues.Count(); ++j)
                                {
                                    subanswer = subanswersValues.ElementAtOrDefault(j);
                                    if (subanswer != null)
                                    {
                                        subanswer.answer_text = listOtherChoice[j].InputValue;
                                        if (subanswer.answer_text == null)
                                            subanswer.answer_text = string.Empty;
                                        subanswer.sub_answer_index = -1;
                                    }
                                }
                            }
                            else
                            {
                                for (int j = 0; j < listOtherChoice.Count; ++j)
                                {
                                    //if not exists create new subanswer
                                    subanswer = new sub_answers();
                                    subanswer.answer_text = listOtherChoice[j].InputValue;
                                    if (subanswer.answer_text == null)
                                        subanswer.answer_text = string.Empty;
                                    subanswer.sub_answer_index = -1;
                                    objAnswer.sub_answers.Add(subanswer);
                                }
                            }
                            break;
                        #endregion
                        #region SubAnswer for Schedule Component
                        case QuestionTypeConstants.Schedule:
                            iSchedule = answerOptions[x] as ISchedule;
                            /*********************************************************************
                            * Change Log:
                            Note:   sub_answer_index = 1 if answer is CalendarSelectedValue => Booking Type
                                    sub_answer_index = 2 if answer is Schedule date InputValue => Sales Person / Resource
                                    sub_answer_index = 3 if answer is StartTime InputValue => ScheduleValue
                                    sub_answer_index = 4 if answer is EndTime InputValue => Null
                                    sub_answer_index = 5 if answer is Attendies => ContactAttendies
                                    sub_answer_index = 6 if answer is MeetingPlaceDetails => Null
                                    sub_answer_index = 7 if answer is OtherChoice => OtherChoice
                            *********************************************************************/

                            //set CalendarSelectedValue
                            if (iSchedule.ScheduleType != null)
                            {
                                subanswer = objAnswer.sub_answers.Where(sa => sa.sub_answer_index == 1).FirstOrDefault();
                                if (subanswer == null)
                                {
                                    //if not exists create new subanswer
                                    subanswer = new sub_answers();
                                    subanswer.answer_text = iSchedule.ScheduleType.ScheduleTypeSelectedValue;
                                    subanswer.sub_answer_index = 1;
                                    objAnswer.sub_answers.Add(subanswer);
                                }
                                else
                                {
                                    subanswer.answer_text = iSchedule.ScheduleType.ScheduleTypeSelectedValue;
                                    subanswer.sub_answer_index = 1;
                                }
                            }
                            if (iSchedule.ScheduleSalesPerson != null && iSchedule.ScheduleSalesPerson.SalesPersonSelectedValue != null)
                            {
                                //set Schedule date InputValue
                                subanswer = objAnswer.sub_answers.Where(sa => sa.sub_answer_index == 2).FirstOrDefault();
                                if (subanswer == null)
                                {
                                    //if not exists create new subanswer
                                    subanswer = new sub_answers();
                                    subanswer.answer_text = iSchedule.ScheduleSalesPerson.SalesPersonSelectedValue.ToJSONString();
                                    subanswer.sub_answer_index = 2;
                                    objAnswer.sub_answers.Add(subanswer);
                                }
                                else
                                {
                                    subanswer.answer_text = iSchedule.ScheduleSalesPerson.SalesPersonSelectedValue.ToJSONString();
                                    subanswer.sub_answer_index = 2;
                                }
                            }
                            //set StartTime InputValue
                            if (iSchedule.ScheduleValue != null)
                            {
                                subanswer = objAnswer.sub_answers.Where(sa => sa.sub_answer_index == 3).FirstOrDefault();
                                if (subanswer == null)
                                {
                                    //if not exists create new subanswer
                                    subanswer = new sub_answers();
                                    subanswer.answer_text = iSchedule.ScheduleValue.ToJSONString();
                                    subanswer.sub_answer_index = 3;
                                    objAnswer.sub_answers.Add(subanswer);
                                }
                                else
                                {
                                    subanswer.answer_text = iSchedule.ScheduleValue.ToJSONString();
                                    subanswer.sub_answer_index = 3;
                                }
                            }
                            //set EndTime InputValue
                            //subanswer = objAnswer.sub_answers.Where(sa => sa.sub_answer_index == 4).FirstOrDefault();
                            //if (subanswer == null) {
                            //    //if not exists create new subanswer
                            //    subanswer = new sub_answers();
                            //    //subanswer.answer_text = iSchedule.EndTime.InputValue != null ? iSchedule.EndTime.InputValue : string.Empty;
                            //    subanswer.sub_answer_index = 4;
                            //    objAnswer.sub_answers.Add(subanswer);
                            //} else {
                            //    //subanswer.answer_text = iSchedule.EndTime.InputValue != null ? iSchedule.EndTime.InputValue : string.Empty;
                            //    subanswer.sub_answer_index = 4;
                            //}
                            //set Attendies
                            listAttendie = iSchedule.Attendies;
                            subanswersValues = objAnswer.sub_answers.Where(k => k.sub_answer_index == 5);
                            int subCount = subanswersValues.Count();
                            int attCount = 0;
                            if (listAttendie == null || listAttendie.Count <= 0)
                            {
                                for (int d = 0; d < subCount; ++d)
                                {
                                    var ans = subanswersValues.ElementAtOrDefault(d);
                                    if (ans != null)
                                        BPContext.sub_answers.DeleteObject(ans);
                                }
                            }
                            else
                            {
                                attCount = listAttendie.Count;
                            }
                            subCount = subanswersValues.Count();
                            //remove existing excess
                            if (subCount > attCount)
                            {
                                int diff = subCount - attCount;
                                for (int j = diff - 1; j < subCount; ++j)
                                {
                                    var subAns = subanswersValues.ElementAtOrDefault(j);
                                    if (subAns != null)
                                        BPContext.sub_answers.DeleteObject(subAns);
                                }
                            }

                            if (attCount > 0)
                            {
                                for (int q = 0; q < attCount; ++q)
                                {
                                    subanswer = subanswersValues.ElementAtOrDefault(q);
                                    if (subanswer != null)
                                    {
                                        subanswer.sub_answer_index = 5;
                                        subanswer.answer_text = listAttendie[q].ToJSONString();
                                    }
                                    else
                                    {
                                        subanswer = new sub_answers();
                                        subanswer.sub_answer_index = 5;
                                        subanswer.answer_text = listAttendie[q].ToJSONString();
                                        objAnswer.sub_answers.Add(subanswer);
                                    }
                                }
                            }

                            //set otherchoice
                            listOtherChoice = iSchedule.OtherChoices;
                            subanswersValues = objAnswer.sub_answers.Where(k => k.sub_answer_index == 7);
                            if (subanswersValues != null && subanswersValues.Count() > 0)
                            {
                                for (int j = 0; j < subanswersValues.Count(); ++j)
                                {
                                    subanswer = subanswersValues.ElementAtOrDefault(j);
                                    if (subanswer != null)
                                    {
                                        subanswer.answer_text = listOtherChoice[j].InputValue;
                                        if (subanswer.answer_text == null)
                                            subanswer.answer_text = string.Empty;
                                        subanswer.sub_answer_index = 7;
                                    }
                                }
                            }
                            else
                            {
                                for (int j = 0; j < listOtherChoice.Count; ++j)
                                {
                                    //if not exists create new subanswer
                                    subanswer = new sub_answers();
                                    subanswer.answer_text = listOtherChoice[j].InputValue;
                                    if (subanswer.answer_text == null)
                                        subanswer.answer_text = string.Empty;
                                    subanswer.sub_answer_index = 7;
                                    objAnswer.sub_answers.Add(subanswer);
                                }
                            }
                            break;
                        #endregion
                        #region SubAnswer for SmartText
                        case QuestionTypeConstants.SmartText:
                            iSmartText = answerOptions[x] as ISmartText;
                            if (iSmartText.SmartTextValues == null || iSmartText.SmartTextValues.Count() < 1)
                                break;

                            subanswer = objAnswer.sub_answers.ElementAtOrDefault(x);
                            if (subanswer == null)
                            {
                                subanswer = new sub_answers();
                                subanswer.answer_text = SmartTextValue.ToJSONString(iSmartText.SmartTextValues);
                                subanswer.sub_answer_index = 0;
                                objAnswer.sub_answers.Add(subanswer);
                            }
                            else
                            {
                                subanswer.answer_text = SmartTextValue.ToJSONString(iSmartText.SmartTextValues);
                                subanswer.sub_answer_index = 0;
                            }
                            break;
                        #endregion
                    }
                }
                try
                {
                    //[@jeff 11.03.2011 #PLATFORM-710]: added modified by and dates
                    objAnswer.modified_on = DateTime.Now;
                    objAnswer.modified_by = UserSession.CurrentUser.UserId;

                    if (IsNew)
                    {
                        BPContext.answers.AddObject(objAnswer);
                    }
                    BPContext.SaveChanges();
                    binding.answer_id = objAnswer.id.ToString();
                    BPContext.FIUpdateContactDialogDate(objAnswer.contact_id, objAnswer.modified_by);
                }
                catch (Exception ex)
                {
                    var x = ex;
                }
            }
        }