public void TestCreateUpdateDeletePredefinedChoiceAndQuestion() { PredefinedQuestionDto pQuestion = new PredefinedQuestionDto(); pQuestion.InternalId = "TestQuestion" + Guid.NewGuid(); pQuestion.QuestionText = "Question text"; PredefinedQuestionDto cQuestion = null; PredefinedChoiceDto pChoice = new PredefinedChoiceDto(); pChoice.ChoiceLabel = "TestChoice"; pChoice.SortSequence = -1; pChoice.FixedValue = "c1"; PredefinedChoiceDto cChoice = null; cQuestion = FormRegistry.PredefinedQuestionDao.SaveQuestion(pQuestion, "new"); Assert.IsNotNull(cQuestion, "Predefined question wasn't created"); Assert.AreEqual(pQuestion.InternalId, cQuestion.InternalId, "Predefined question has invalid internal id"); Assert.AreEqual(pQuestion.QuestionText, cQuestion.QuestionText, "Predefined question has invalid question text"); Assert.AreEqual(0, cQuestion.StatusActive, "Predefined question has invalid status active"); Assert.AreNotEqual(0, cQuestion.QuestionId, "Predefined question has invalid question id"); pChoice.PredefinedQuestionDto = cQuestion; cChoice = FormRegistry.PredefinedQuestionDao.SaveChoice(pChoice, "new"); Assert.IsNotNull(cChoice, "Predefined choice wasn't created"); Assert.AreNotEqual(0, cChoice.ChoiceId, "Predefined choice has invalid choice id"); Assert.AreEqual(pChoice.ChoiceLabel, cChoice.ChoiceLabel, "Predefined choice has invalid choice label"); Assert.AreEqual(pChoice.FixedValue, cChoice.FixedValue, "Predefined choice has invalid fixed value"); Assert.IsNotNull(cChoice.PredefinedQuestionDto, "Predefined choice is not assign to any predefined question"); Assert.AreEqual(cQuestion.QuestionId, cChoice.PredefinedQuestionDto.QuestionId, "Predefined choice has invalid predefined questio id"); Assert.AreEqual(pChoice.SortSequence, cChoice.SortSequence, "Predefined choice has invalid sort sequence"); cQuestion.StatusActive = 1; cQuestion.QuestionText = "Question text = changed"; cQuestion = FormRegistry.PredefinedQuestionDao.SaveQuestion(cQuestion, "edit"); Assert.IsNotNull(cQuestion, "Predefined question wasn't updated"); Assert.AreEqual(1, cQuestion.StatusActive, "Status active of predefined question wasn't updated correctly"); Assert.AreEqual("Question text = changed", cQuestion.QuestionText, "Question text of predefined question wasn't updated correctly"); cChoice.PredefinedQuestionDto = cQuestion; cChoice.ChoiceLabel = "TestChoice - Changed"; cChoice = FormRegistry.PredefinedQuestionDao.SaveChoice(cChoice, "edit"); Assert.IsNotNull(cChoice, "Predefined choice wasn't updated"); Assert.AreEqual("TestChoice - Changed", cChoice.ChoiceLabel, "Choice label of predefined choice wasn't updated correctly"); FormRegistry.PredefinedQuestionDao.DeleteChoice(cChoice.ChoiceId); Assert.AreEqual(0, FormRegistry.PredefinedQuestionDao.GetPredefinedChoices(null, cChoice.ChoiceId).Count); cChoice = null; FormRegistry.PredefinedQuestionDao.DeletePredefinedQuestion(cQuestion.QuestionId); Assert.IsNull(FormRegistry.PredefinedQuestionDao.GetQuestionById(cQuestion.QuestionId)); cQuestion = null; }
public PredefinedQuestionDto SaveQuestion(PredefinedQuestionDto pQuestionDto, string mode) { PredefinedQuestionDto retval = null; DbCommand sp = null; DbConnection connection = null; IDataReader reader = null; try { connection = _dbLayer.GetConnection(); sp = connection.CreateCommand(); sp.CommandText = "update_predefined_question"; sp.CommandType = CommandType.StoredProcedure; if (mode == "edit") _dbLayer.AddParameter(sp, "@question_id", ParameterDirection.Input, DbType.Int32, pQuestionDto.QuestionId); _dbLayer.AddParameter(sp, "@internal_id", ParameterDirection.Input, DbType.String, pQuestionDto.InternalId); _dbLayer.AddParameter(sp, "@question_text", ParameterDirection.Input, DbType.String, pQuestionDto.QuestionText); _dbLayer.AddParameter(sp, "@hpp_id", ParameterDirection.Input, DbType.Int32, pQuestionDto.HppId); _dbLayer.AddParameter(sp, "@status_active", ParameterDirection.Input, DbType.Int16, pQuestionDto.StatusActive); _dbLayer.AddReturnParameter(sp); reader = sp.ExecuteReader(); if (reader.Read()) { retval = ReadPredefineQuestion(reader); } else { int err = _dbLayer.GetReturnValue(sp); Trace.WriteLine("PredefinedQuestionDao.SaveQuestion("+pQuestionDto + ", " + mode+") returned " + err); } } catch (DbException e) { Trace.WriteLine("PredefinedQuestionDao.SaveQuestion(" + pQuestionDto + ", " + mode + "): " + e.Message); retval = null; } finally { if (reader != null && !reader.IsClosed) reader.Close(); if (sp != null) sp.Dispose(); if (connection != null) { _dbLayer.ReturnConnection(connection); } else { _dbLayer.ReturnConnection(connection, true); } } return retval; }
protected PredefinedQuestionDto ReadPredefineQuestionByPageChoiceId(IDataReader reader, ref bool alreadyRead) { PredefinedQuestionDto retval = new PredefinedQuestionDto(); retval.QuestionId = reader.GetInt32(0); retval.InternalId = reader.GetString(1); retval.QuestionText = reader.GetString(2); alreadyRead = reader.Read(); return retval; }
protected PredefinedQuestionDto ReadPredefineQuestion(IDataReader reader) { PredefinedQuestionDto retval = new PredefinedQuestionDto(); retval.QuestionId = reader.GetInt32(0); retval.InternalId = reader.GetString(1); retval.QuestionText = reader.GetString(2); retval.HppId = null; if (GetInt32(reader, 3).HasValue) retval.HppId = GetInt32(reader, 3).Value; retval.StatusActive = reader.GetInt16(4); return retval; }
protected virtual void TearDown() { bool failed = false; foreach (int id in _createdMasterReports) { try { FormRegistry.MasterReportDao.DeleteMasterReport(id); } catch (Exception e) { Trace.WriteLine("Exception during master report deletion: " + e); failed = true; } } _createdMasterReports.Clear(); foreach (int pagePredefinedQuestionChoiceId in _createdPagePredefinedQuestionChoices) { try { FormRegistry.PredefinedQuestionDao.DeletePagePredefinedChoice(pagePredefinedQuestionChoiceId); } catch { failed = true; } } _createdPagePredefinedQuestionChoices.Clear(); foreach (int pagePredefinedQuestionId in _createdPagePredefinedQuestions) { try { FormRegistry.PredefinedQuestionDao.DeletePagePredefinedQuestion(pagePredefinedQuestionId); } catch { failed = true; } } _createdPagePredefinedQuestions.Clear(); foreach (int predefinedChoiceId in _createdPredefinedQuestionChoices) { try { FormRegistry.PredefinedQuestionDao.DeleteChoice(predefinedChoiceId); } catch { failed = true; } } _createdPredefinedQuestionChoices.Clear(); foreach (int predefinedQuestionId in _createdPredefinedQuestions) { try { FormRegistry.PredefinedQuestionDao.DeletePredefinedQuestion(predefinedQuestionId); } catch { failed = true; } } _createdPredefinedQuestions.Clear(); foreach (int questionnaireId in _createdQuestionnaires) { if (!FormRegistry.QuestionnaireDao.Delete(questionnaireId, TestUser)) { failed = true; } } _createdQuestionnaires.Clear(); foreach (string campaignCode in _createdCampaigns) { if (!FormRegistry.CampaignDao.Delete(campaignCode, _currentCampaign.CreatorEmailAddress)) { failed = true; } } _createdCampaigns.Clear(); _currentCampaign = null; _currentQuestionnaire = null; _currentPage = null; _currentPredefinedQuestion = null; _currentPredefinedChoices = null; _currentPagePredefinedQuestion = null; _currentPagePredefinedChoices = null; Assert.IsFalse(failed, "errors during deletion"); }
public PredefinedQuestionDto CreateNewPredefinedQuestion() { if (_currentPredefinedChoices == null) _currentPredefinedChoices = new List<PredefinedChoiceDto>(); PredefinedQuestionDto pQuestion = new PredefinedQuestionDto(); pQuestion.InternalId = "TestInternalId" + Guid.NewGuid(); pQuestion.QuestionText = "TestQuestionText"; _currentPredefinedQuestion = FormRegistry.PredefinedQuestionDao.SaveQuestion(pQuestion, "new"); _createdPredefinedQuestions.Add(_currentPredefinedQuestion.QuestionId); PredefinedChoiceDto pChoice = new PredefinedChoiceDto(); pChoice.ChoiceLabel = "TestChoiceLabel"; pChoice.PredefinedQuestionDto = _currentPredefinedQuestion; pChoice.FixedValue = "c1"; pChoice.SortSequence = -1; pChoice = FormRegistry.PredefinedQuestionDao.SaveChoice(pChoice, "new"); _createdPredefinedQuestionChoices.Add(pChoice.ChoiceId); _currentPredefinedChoices.Add(pChoice); PredefinedChoiceDto pChoiceOther = new PredefinedChoiceDto(); pChoiceOther.ChoiceLabel = "TestChoiceLabelOther"; pChoiceOther.OtherText = "TestOtherText"; pChoiceOther.ErrorMessage = "TestErrorMessage"; pChoiceOther.PredefinedQuestionDto = _currentPredefinedQuestion; pChoiceOther.FixedValue = "c2"; pChoiceOther.SortSequence = 999; pChoiceOther = FormRegistry.PredefinedQuestionDao.SaveChoice(pChoiceOther, "new"); _createdPredefinedQuestionChoices.Add(pChoiceOther.ChoiceId); _currentPredefinedChoices.Add(pChoiceOther); _currentPredefinedQuestion.StatusActive = 1; _currentPredefinedQuestion = FormRegistry.PredefinedQuestionDao.SaveQuestion(_currentPredefinedQuestion, "edit"); foreach (PredefinedChoiceDto choice in _currentPredefinedChoices) choice.PredefinedQuestionDto = _currentPredefinedQuestion; return _currentPredefinedQuestion; }
public static PredefinedQuestionDto SaveQuestion(PredefinedQuestionDto pQuestionDto, string mode) { return FormRegistry.PredefinedQuestionDao.SaveQuestion(pQuestionDto, mode); }
protected void Save_Predefined_Question_Edit() { try { //save question predefinedQuestionDto = PredefinedQuestionFacade.SaveQuestion(predefinedQuestionDto, Request.QueryString["content"]); //list of all existing in database choices (we could remove some of the or just add new choices) List<PredefinedChoiceDto> listOfPredefinedSavedChoices = PredefinedQuestionFacade.GetPredefinedChoicesByPredefinedQuestionId(predefinedQuestionDto.QuestionId); List<PredefinedChoiceDto> listOfNewChoices = null; //at first we add new choices if(listOfPredefinedChoices != null && listOfPredefinedChoices.Count > 0 && listOfPredefinedSavedChoices != null && listOfPredefinedSavedChoices.Count >0) listOfNewChoices = listOfPredefinedChoices.FindAll(pc => listOfPredefinedSavedChoices.Find(c => c.ChoiceId == pc.ChoiceId) == null); foreach (PredefinedChoiceDto choice in listOfNewChoices) { choice.PredefinedQuestionDto = predefinedQuestionDto; PredefinedChoiceDto pChoice = PredefinedQuestionFacade.SaveChoice(choice, "new"); choice.ChoiceId = pChoice.ChoiceId; } //it might happen that some choices were deleted List<PredefinedChoiceDto> listOfDeletedChoices = null; if (listOfPredefinedChoices != null && listOfPredefinedChoices.Count > 0 && listOfPredefinedSavedChoices != null && listOfPredefinedSavedChoices.Count > 0) listOfDeletedChoices = listOfPredefinedSavedChoices.FindAll(pc => listOfPredefinedChoices.Find(c => c.ChoiceId == pc.ChoiceId) == null); foreach (PredefinedChoiceDto choice in listOfDeletedChoices) { if(choice.SortSequence != 999 || (choice.SortSequence == 999 && String.IsNullOrEmpty(choice.OtherText) && String.IsNullOrEmpty(choice.ErrorMessage))) PredefinedQuestionFacade.DeleteChoice(choice.ChoiceId); } //and other choices that are on both lists we have to update List<PredefinedChoiceDto> listOfUpdatedChoices = null; if (listOfPredefinedChoices != null && listOfPredefinedChoices.Count > 0 && listOfPredefinedSavedChoices != null && listOfPredefinedSavedChoices.Count > 0) listOfUpdatedChoices = listOfPredefinedChoices.FindAll(pc => listOfPredefinedSavedChoices.FindAll(c => c.ChoiceId == pc.ChoiceId) != null); foreach (PredefinedChoiceDto choice in listOfUpdatedChoices) { PredefinedChoiceDto pChoice = PredefinedQuestionFacade.SaveChoice(choice, Request.QueryString["content"]); } //save other choice if (predefinedOtherChoice != null) { if (predefinedOtherChoice.SortSequence == 999 && !String.IsNullOrEmpty(predefinedOtherChoice.OtherText) && !String.IsNullOrEmpty(predefinedOtherChoice.ErrorMessage)) { PredefinedChoiceDto otherSaved = null; if(listOfPredefinedSavedChoices != null && listOfPredefinedSavedChoices.Count > 0) otherSaved = listOfPredefinedSavedChoices.Find(c => c.SortSequence == 999 && !String.IsNullOrEmpty(c.OtherText) && !String.IsNullOrEmpty(c.ErrorMessage)); if (otherSaved != null) { //we only need to update it PredefinedChoiceDto otherChoice = PredefinedQuestionFacade.SaveChoice(predefinedOtherChoice, Request.QueryString["content"]); } else { //we have to create new other choice predefinedOtherChoice.PredefinedQuestionDto = predefinedQuestionDto; PredefinedChoiceDto otherChoice = PredefinedQuestionFacade.SaveChoice(predefinedOtherChoice, "new"); predefinedOtherChoice.ChoiceId = otherChoice.ChoiceId; } } } else { PredefinedChoiceDto otherSaved = null; if(listOfPredefinedSavedChoices != null && listOfPredefinedSavedChoices.Count > 0) otherSaved = listOfPredefinedSavedChoices.Find(c => c.SortSequence == 999 && !String.IsNullOrEmpty(c.OtherText) && !String.IsNullOrEmpty(c.ErrorMessage)); if (otherSaved != null) { PredefinedQuestionFacade.DeleteChoice(otherSaved.ChoiceId); } } Response.Redirect("index.aspx"); } catch (Exception) { errorSavingDatabasePanel.Visible = true; } }
protected void Save_Predefined_Question_New() { try { //save question predefinedQuestionDto = PredefinedQuestionFacade.SaveQuestion(predefinedQuestionDto, Request.QueryString["content"]); //save choices foreach (PredefinedChoiceDto choice in listOfPredefinedChoices) { choice.PredefinedQuestionDto = predefinedQuestionDto; PredefinedChoiceDto pChoice = PredefinedQuestionFacade.SaveChoice(choice, Request.QueryString["content"]); choice.ChoiceId = pChoice.ChoiceId; } //save other choice if (predefinedOtherChoice != null && (predefinedOtherChoice.SortSequence == 999 && !String.IsNullOrEmpty(predefinedOtherChoice.OtherText) && !String.IsNullOrEmpty(predefinedOtherChoice.ErrorMessage))) { predefinedOtherChoice.PredefinedQuestionDto = predefinedQuestionDto; PredefinedChoiceDto otherChoice = PredefinedQuestionFacade.SaveChoice(predefinedOtherChoice, Request.QueryString["content"]); predefinedOtherChoice.ChoiceId = otherChoice.ChoiceId; } //after save all choices we set status of question as active predefinedQuestionDto.StatusActive = 1; predefinedQuestionDto = PredefinedQuestionFacade.SaveQuestion(predefinedQuestionDto, "edit"); foreach (PredefinedChoiceDto choice in listOfPredefinedChoices) { choice.PredefinedQuestionDto = predefinedQuestionDto; } if (predefinedOtherChoice != null && (predefinedOtherChoice.SortSequence == 999 && !String.IsNullOrEmpty(predefinedOtherChoice.OtherText) && !String.IsNullOrEmpty(predefinedOtherChoice.ErrorMessage))) { predefinedOtherChoice.PredefinedQuestionDto = predefinedQuestionDto; } Response.Redirect("index.aspx"); } catch (Exception) { errorSavingDatabasePanel.Visible = true; } }
protected void Page_Load_Edit() { page_utilities.Set_titles(this.Page, "Edit Pre-Defined Question"); if (!IsPostBack) { predefinedOtherChoice = new PredefinedChoiceDto(); predefinedQuestionDto = PredefinedQuestionFacade.GetPredefinedQuestionByQuestionId(int.Parse(Session["question_id"].ToString())); List<PredefinedChoiceDto> listOfAllChoices = PredefinedQuestionFacade.GetPredefinedChoicesByPredefinedQuestionId(int.Parse(Session["question_id"].ToString())); listOfPredefinedChoices = new List<PredefinedChoiceDto>(); foreach (PredefinedChoiceDto choice in listOfAllChoices) { if (choice.SortSequence == 999 && !String.IsNullOrEmpty(choice.OtherText) && !String.IsNullOrEmpty(choice.ErrorMessage)) predefinedOtherChoice = choice; else listOfPredefinedChoices.Add(choice); } delMsg.Text = @"*Deleting an existing predefined question is only possible if that question is not used on any form!"; LoadQuestion(); LoadListOfChoices(); LoadOtherChoice(); } }
protected void Page_Load_New() { page_utilities.Set_titles(this.Page, "Create Pre-Defined Question"); delete_question.Visible = false; if (!IsPostBack) { noChoicePanel.Visible = false; incorrectChoicePanel.Visible = false; incorrectQuestionTextPanel.Visible = false; predefinedQuestionDto = new PredefinedQuestionDto(); listOfPredefinedChoices = new List<PredefinedChoiceDto>(); predefinedOtherChoice = new PredefinedChoiceDto(); } }