/* * CREATED: H. Conant MAR 13 2018 * MODIFIED: A. Valberg MAR 28 2018 * -Updated fields after adding 2 tables (agegroup & gender) * MODIFIED: H. Conant MAR 31 2018 * -Updated gender and age to repeaters and ODSes * * RetrieveSurveys_Click() * This method runs any code it contains when the "Retrieve Surveys" button is clicked. * * PARAMETERS: * object sender - object on the page that is being targeted * EventArgs e - event that has triggered the method * * RETURNS: * void * * ODEV METHOD CALLS: * MessageUserControl.ShowInfoMessage() * MessageUserControl.ShowInfoList() * SurveyController.GetSurveys() * MessageUserControl.ShowErrorMessage() */ protected void RetrieveSurveys_Click(object sender, EventArgs e) { ResultCountLabel.Text = "0"; List <string> message = new List <string>(); SurveyRepeater.DataSource = null; SurveyRepeater.DataBind(); List <int> unitIds = new List <int>(); List <int> genders = new List <int>(); List <int> respodentIds = new List <int>(); List <int> ages = new List <int>(); DateTime dateOne = new DateTime(); DateTime dateTwo = new DateTime(); string pattern = @"^$|^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/][0-9]{4}$"; Regex reg = new Regex(pattern); Match properStartDateFormat = reg.Match(dateStart.Value); Match properEndDateFormat = reg.Match(dateEnd.Value); if (!properStartDateFormat.Success || !properEndDateFormat.Success) { message.Add("Please enter valid date(s) using the pattern mm/dd/yyyy or leave the date fields blank."); } else { if (dateStart.Value != "") { if (DateTime.TryParse(dateStart.Value, out dateOne) == false) { message.Add("Please enter a valid start date value or leave it blank."); } } if (dateEnd.Value != "") { if (DateTime.TryParse(dateEnd.Value, out dateTwo) == false) { message.Add("Please enter a valid end date value or leave it blank."); } } } if (dateStart.Value != "" && dateEnd.Value != "" && dateOne > dateTwo) { message.Add("Please ensure that the provided end date occurs after or on the same day as the provided start date."); } if (dateTwo > DateTime.Today || dateOne > DateTime.Today) { message.Add("Please select dates that are on or before today's date."); } foreach (RepeaterItem item in UnitRepeater.Items) { CheckBox tempCheckBox = (CheckBox)item.FindControl("CheckBox"); HiddenField tempHiddenField = (HiddenField)item.FindControl("HiddenUnitID"); if (tempCheckBox.Text.Length > 10) { tempCheckBox = (CheckBox)item.FindControl("CheckBox1"); } if (tempCheckBox.Checked == true) { int tempUntId; int.TryParse(tempHiddenField.Value, out tempUntId); unitIds.Add(tempUntId); } } if (unitIds.Count < 1) { message.Add("Please select at least one unit to filter by."); } foreach (RepeaterItem item in GenderRepeater.Items) { CheckBox tempCheckBox = (CheckBox)item.FindControl("CheckBox"); HiddenField tempHiddenField = (HiddenField)item.FindControl("HiddenGenderID"); if (tempCheckBox.Text.Length > 10) { tempCheckBox = (CheckBox)item.FindControl("CheckBox1"); } if (tempCheckBox.Checked == true) { int tempGenderId; int.TryParse(tempHiddenField.Value, out tempGenderId); genders.Add(tempGenderId); } } if (genders.Count < 1) { message.Add("Please select at least one gender to filter by."); } foreach (RepeaterItem item in RespondentTypeRepeater.Items) { CheckBox tempCheckBox = (CheckBox)item.FindControl("CheckBox"); HiddenField tempHiddenField = (HiddenField)item.FindControl("HiddenRespondentID"); if (tempCheckBox.Text.Length > 10) { tempCheckBox = (CheckBox)item.FindControl("CheckBox1"); } if (tempCheckBox.Checked == true) { int tempTypeId; int.TryParse(tempHiddenField.Value, out tempTypeId); respodentIds.Add(tempTypeId); } } if (respodentIds.Count < 1) { message.Add("Please select at least one respondent type to filter by."); } foreach (RepeaterItem item in AgeRepeater.Items) { CheckBox tempCheckBox = (CheckBox)item.FindControl("CheckBox"); HiddenField tempHiddenField = (HiddenField)item.FindControl("HiddenAgeGroupID"); if (tempCheckBox.Text.Length > 10) { tempCheckBox = (CheckBox)item.FindControl("CheckBox1"); } if (tempCheckBox.Checked == true) { int tempAgeId; int.TryParse(tempHiddenField.Value, out tempAgeId); ages.Add(tempAgeId); } } if (ages.Count < 1) { message.Add("Please select at least one age range to filter by."); } if (message.Count == 1) { MessageUserControl.ShowInfoMessage(message[0]); } else if (message.Count > 1) { MessageUserControl.ShowInfoList("Please address the following errors and try again: ", message); } else { try { List <ExtendedSurveyDTO> surveys = new List <ExtendedSurveyDTO>(); if (dateStart.Value == "" && dateEnd.Value == "") { surveys = surveyController.GetSurveys(unitIds, genders, ages, respodentIds); } else if (dateStart.Value == "" && dateEnd.Value != "") { dateOne = DateTime.MinValue; surveys = surveyController.GetSurveys(unitIds, genders, ages, dateOne, dateTwo, respodentIds); } else if (dateStart.Value != "" && dateEnd.Value == "") { dateTwo = DateTime.Today; dateEnd.Value = DateTime.Today.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture); surveys = surveyController.GetSurveys(unitIds, genders, ages, dateOne, dateTwo, respodentIds); } else { surveys = surveyController.GetSurveys(unitIds, genders, ages, dateOne, dateTwo, respodentIds); } if (surveys.Count < 1) { MessageUserControl.ShowInfoMessage("No surveys found matching the entered search criteria."); } else { ResultCountLabel.Text = (surveys.Count).ToString(); SurveyRepeater.DataSource = surveys; SurveyRepeater.DataBind(); } } catch (Exception ex) { MessageUserControl.ShowErrorMessage("Retrieving surveys from the database failed. Please try again. If error persists, please contact your administrator.", ex); } } }
/* * CREATED: P. Chavez MAR 22 2018 * MODIFIED: C. Stanhope APR 5 2018 * - inputs now trim whitespace * MODIFIED: C. Stanhope APR 14 2018 * - user messages formatted the same as other pages * * AddAccountButton_Click() * This method creates a new account based on the input fields when the button is clicked. * * PARAMETERS: * object sender - references the object that raised the Page_Load event * EventArgs e - optional class that may be passed that inherits from EventArgs (usually empty) * * RETURNS: * void * * ODEV METHOD CALLS: * ErrorMessagesAndValidation.ErrorList() * MessageUserControl.ShowInfoMessage() * UserManager.GenerateNewPassword() * UserManager.AddAccountUser() * MessageUserControl.ShowSuccessMessage() * MessageUserControl.ShowErrorMessage() * MessageUserControl.ShowInfoList() */ protected void AddAccountButton_Click(object sender, EventArgs e) { //Retrieve the values from the controls string firstNameText = FirstNameTextBox.Text.Trim(); string lastNameText = LastNameTextBox.Text.Trim(); string emailText = EmailTextBox.Text.Trim(); string authLevelText = AuthorizationLevelRolesRadioList.SelectedValue; int careSiteID = int.Parse(CareSiteDDL.Visible == false ? "0" : CareSiteDDL.SelectedValue); List <string> errorList = new List <string>(); bool isValid = true; #region check if any inputs are blank if (string.IsNullOrWhiteSpace(firstNameText)) { errorList.Add("First Name"); isValid = false; } if (string.IsNullOrWhiteSpace(lastNameText)) { errorList.Add("Last Name"); isValid = false; } if (string.IsNullOrWhiteSpace(emailText)) { errorList.Add("Email"); isValid = false; } if (string.IsNullOrWhiteSpace(authLevelText)) { errorList.Add("Authorization Level"); isValid = false; } #endregion if (!isValid) { ErrorMessagesAndValidation errMessAndVal = new ErrorMessagesAndValidation(); string errorMessage = errMessAndVal.ErrorList(errorList); MessageUserControl.ShowInfoMessage(errorMessage); } else { bool matchRegex = true; List <string> regexErrorList = new List <string>(); Regex regexName = new Regex(@"^(?m)[A-Za-z][A-Za-z`. -]*$", RegexOptions.IgnoreCase); if (!regexName.Match(firstNameText).Success) { matchRegex = false; regexErrorList.Add("First Name can only contain letters, dashes, apostrophes, grave accents, spaces and periods."); } if (!regexName.Match(lastNameText).Success) { matchRegex = false; regexErrorList.Add("Last Name can only contain letters, dashes, apostrophes, grave accents, spaces and periods."); } if (!emailText.Contains("@")) { matchRegex = false; regexErrorList.Add("Email must include an '@' symbol."); } if (careSiteID == 0 && authLevelText == AuthorizationLevelRoles.User) { matchRegex = false; regexErrorList.Add("The Authorization Level User must have a Care Site associated with the account."); } if (matchRegex) { try { UserManager userManager = new UserManager(); string newPassword = userManager.GenerateNewPassword(); ApplicationUser newUserAccount = userManager.AddAccountUser(firstNameText, lastNameText, emailText, authLevelText, careSiteID, newPassword); string resultMessage = string.Format("The new account was created! UserName: {0} | Password: {1}", newUserAccount.UserName, newPassword); MessageUserControl.ShowSuccessMessage(resultMessage); //Reset the fields FirstNameTextBox.Text = ""; LastNameTextBox.Text = ""; EmailTextBox.Text = ""; AuthorizationLevelRolesRadioList.SelectedValue = AuthorizationLevelRoles.User; CareSiteDDL.SelectedValue = "0"; CareSiteDDL.Visible = true; CareSiteLabel.Visible = true; } catch (Exception ex) { MessageUserControl.ShowErrorMessage("Adding account failed. Please try again. If error persists, please contact your administrator.", ex); } } else // A regex didn't match { MessageUserControl.ShowInfoList("The following errors caused adding a management account to fail: ", regexErrorList); } } }
/* * CREATED: C. Stanhope MAR 23 2018 * MODIFIED: C. Stanhope MAR 24 2018 * - Added validation, finished method * MODIFIED: C. Stanhope APR 5 2018 * - trim whitespace on inputs * - validation now matches add care site * MODIFIED: C. Stanhope APR 6 2018 * - added try-catch for database access * MODIFIED: C. Stanhope APR 17 2018 * - fixed city regex to limit at 30 characters * - fixed care site regex to limit at 80 characters * * UpdateCareSiteButton_Click() * Used to update a care site in the database. Validates all input fields and updates only if valid. * * PARAMETERS: * object sender - references the object that raised the Page_Load event * EventArgs e - optional class that may be passed that inherits from EventArgs (usually empty) * * RETURNS: * void * * ODEV METHOD CALLS: * MessageUserControl.ShowInfoMessage() * CareSiteController.GetCareSiteByCareSiteID() * MessageUserControl.ShowErrorMessage() * ErrorMessagesAndValidation.ErrorList() */ protected void UpdateCareSiteButton_Click(object sender, EventArgs e) { if (selectedCareSite != null) { bool isValid = true; List <string> errorList = new List <string>(); #region regexes for validation Regex careSiteRegex = new Regex(@"^[A-z]{1}[A-z 0-9 .-]{4,79}$"); Regex addressRegex = new Regex(@"^[A-z 0-9 .#-]{1,40}$"); Regex cityRegex = new Regex(@"^[A-z]{1}[A-z .-]{0,29}$"); #endregion #region get values from page and validate string careSiteName = CareSiteNameTextBox.Text.Trim(); if (!careSiteRegex.IsMatch(careSiteName)) { errorList.Add("Care site must be a minimum of 5 characters and a maximum of 80 characters long. It must start with a letter and can contain letters, numbers, and the following symbols: . -"); isValid = false; } string address = AddressTextBox.Text.Trim(); if (!addressRegex.IsMatch(address)) { errorList.Add("Address must be a minimum of 1 character and a maximum of 40 characters long. It can contain letters, numbers, and the following symbols: # . -"); isValid = false; } string city = CityTextBox.Text.Trim(); if (!cityRegex.IsMatch(city)) { errorList.Add("City must be a minimum of 1 letter and a maximum of 30 characters long. It must start with a letter and can contain letters and the following symbols: . -"); isValid = false; } string province = ProvinceDDL.SelectedValue; #endregion if (isValid) { #region see if data changed bool dataChanged = false; if (selectedCareSite.caresitename != careSiteName) { dataChanged = true; } else if (selectedCareSite.address != address) { dataChanged = true; } else if (selectedCareSite.city != city) { dataChanged = true; } else if (selectedCareSite.province != province) { dataChanged = true; } #endregion if (dataChanged) { #region put valid data into selectedCareSite, add to database, show success message, change DDL selectedCareSite.caresitename = careSiteName; selectedCareSite.address = address; selectedCareSite.city = city; selectedCareSite.province = province; try { careSiteController.UpdateCareSite(selectedCareSite); MessageUserControl.ShowSuccessMessage("The " + selectedCareSite.caresitename + " care site was successfully updated."); CareSiteDDL.SelectedItem.Text = selectedCareSite.caresitename; } catch (Exception ex) { MessageUserControl.ShowErrorMessage("Updating care site failed. Please try again. If error persists, please contact your administrator.", ex); } #endregion } else { MessageUserControl.ShowInfoMessage("No changes to save."); } } else { #region show user message with "errors" ErrorMessagesAndValidation errMessAndVal = new ErrorMessagesAndValidation(); string errorMessage = errMessAndVal.ErrorList(errorList); //MessageUserControl.ShowInfoMessage(errorMessage); MessageUserControl.ShowInfoList("The following errors caused adding a care site to fail: ", errorList); #endregion } } else // no care site selected { MessageUserControl.ShowErrorMessage("No care site selected. The \"Update Care Site\" button should not be available if no care site is selected. Please try again. If error persists, please contact your administrator."); } }
/* * CREATED: A. Valberg MAR 19 2018 * * ProcessReport() * This method validates all user input & filters, and then processes the report. * * PARAMETERS: * None * * RETURNS: * void * * ODEV METHOD CALLS: * ControlValidation() * MessageUserControl.ShowInfoMessage() * MessageUserControl.ShowInfoList() * GetData() */ protected void ProcessReport() { // list to hold error msgs List <string> errors = new List <string>(); // local variables for page controls int questionID; questionID = QuestionDDL.SelectedValue != "" ? int.Parse(QuestionDDL.SelectedValue) : 0; List <string> unitIDs = new List <string>(); List <string> respondentTypes = new List <string>(); List <string> genders = new List <string>(); List <string> ages = new List <string>(); DateTime startDate = new DateTime(), endDate = new DateTime(); // regex to check date fields string pattern = @"^$|^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/][0-9]{4}$"; Regex reg = new Regex(pattern); // finding and retrieving page controls if (CareSiteDDL.SelectedValue != "") { ControlValidation(UnitRepeater, unitIDs, "HiddenUnitID"); } ControlValidation(RespondentTypeRepeater, respondentTypes, "HiddenRespondentTypeID"); ControlValidation(GenderRepeater, genders, "HiddenGenderID"); ControlValidation(AgeGroupRepeater, ages, "HiddenAgeGroupID"); // care site & unit validation if (CareSiteDDL.SelectedValue != "" && unitIDs.Count == 0) { errors.Add("Please select at least one unit."); } // date validation // checking if the data matches the regex if (!reg.IsMatch(dateStart.Text) || !reg.IsMatch(dateEnd.Text)) { errors.Add("Please enter valid date(s) using the pattern mm/dd/yyyy or leave the date fields blank."); } else { // checking if the data can be parsed if (dateStart.Text != "" && !DateTime.TryParse(dateStart.Text, out startDate)) { errors.Add("Please enter a valid start date value or leave it blank."); } if (dateEnd.Text != "" && !DateTime.TryParse(dateEnd.Text, out endDate)) { errors.Add("Please enter a valid start date value or leave it blank."); } // checking if the dates are valid if (startDate > DateTime.Today || endDate > DateTime.Today) { errors.Add("Please select dates that are on or before today's date."); } if (dateStart.Text != "" && dateEnd.Text != "" && startDate > endDate) { errors.Add("Please ensure that the provided end date occurs after or on the same day as the provided start date."); } else { if (dateStart.Text != "" && dateEnd.Text == "") { dateEnd.Text = DateTime.Today.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture); endDate = DateTime.Today; } } } // respondent type validation if (respondentTypes.Count == 0) { errors.Add("Please select at least one respondent type to filter by."); } // gender validation if (genders.Count == 0) { errors.Add("Please select at least one gender to filter by."); } // age group validation if (ages.Count == 0) { errors.Add("Please select at least one age group to filter by."); } // question validation if (questionID == 0) { errors.Add("Please select a question from the drop-down list."); } // check if there were errors found; if not, process the report if (errors.Count == 1) { MessageUserControl.ShowInfoMessage(errors[0]); ReportViewer1.Visible = false; } else { if (errors.Count > 1) { MessageUserControl.ShowInfoList("Please address the following errors and try again:", errors); ReportViewer1.Visible = false; } else // all data is assumed valid, create dataset { // these statements are to clear filters lists if all are selected; makes the query neater if (dateEnd.Text == "") { dateEnd.Text = DateTime.Today.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture); } if (respondentTypes.Count == RespondentTypeRepeater.Items.Count) { respondentTypes.Clear(); } if (ages.Count == AgeGroupRepeater.Items.Count) { ages.Clear(); } if (genders.Count == GenderRepeater.Items.Count) { genders.Clear(); } // creating a dataset and trying to fill it with the GetData method DataSet dt = new DataSet(); try { dt = GetData(questionID, unitIDs, respondentTypes, genders, ages, startDate, endDate); } catch (Exception ex) { MessageUserControl.ShowErrorMessage("An error occurred while processing the report. Please reload the page and try again. If the error persists, please contact your administrator.", ex); } // checking if 0 surveys are found - display "no data found" int count = 0; foreach (DataRow row in dt.Tables[0].Rows) { count += int.Parse(row.ItemArray[3].ToString()); } if (count == 0) { MessageUserControl.ShowInfoMessage("No data was found matching the entered search criteria."); ReportViewer1.Visible = false; } else // if you get here, the filters are valid! run the report. { ReportViewer1.Visible = true; ReportParameter param = new ReportParameter("QuestionID", QuestionDDL.SelectedItem.Text.Split(':')[0]); ReportDataSource rds = new ReportDataSource("ReportSource", dt.Tables[0]); ReportViewer1.LocalReport.DataSources.Clear(); ReportViewer1.LocalReport.ReportPath = "Reports/Report.rdlc"; ReportViewer1.LocalReport.SetParameters(param); ReportViewer1.LocalReport.DisplayName = "Reports_" + QuestionDDL.SelectedItem.Text.Split(':')[0] + "_" + DateTime.Now.ToString("MMMddyyyy_HHmm"); ReportViewer1.LocalReport.DataSources.Add(rds); ReportViewer1.LocalReport.Refresh(); } } } }
/* * CREATED: C. Stanhope MAR 23 2018 * MODIFIED: C. Stanhope MAR 24 2018 * - moved error list method to its own class and call it * - changed validation to account for care site name length * MODIFIED: C. Stanhope APR 5 2018 * - trim whitespace on inputs * MODIFIED: C. Stanhope APR 6 2018 * - added try-catch for database access * MODIFIED: C. Stanhope APR 17 2018 * - fixed city regex to limit at 30 characters * - fixed care site regex to limit at 80 characters * * AddCareSiteButton_Click() * Used to add a care site to the database. Validates all input fields. * * PARAMETERS: * object sender - references the object that raised the Page_Load event * EventArgs e - optional class that may be passed that inherits from EventArgs (usually empty) * * RETURNS: * void * * ODEV METHOD CALLS: * CareSiteController.AddCareSite() * MessageUserControl.ShowSuccessMessage() * MessageUserControl.ShowInfoMessage() * ErrorMessagesAndValidation.ErrorList() */ protected void AddCareSiteButton_Click(object sender, EventArgs e) { bool isValid = true; List <string> errorList = new List <string>(); #region regexes for validation Regex careSiteRegex = new Regex(@"^[A-z]{1}[A-z 0-9 .-]{4,79}$"); Regex addressRegex = new Regex(@"^[A-z 0-9 .#-]{1,40}$"); Regex cityRegex = new Regex(@"^[A-z]{1}[A-z .-]{0,29}$"); #endregion #region get values from page and validate string careSiteName = CareSiteNameTextBox.Text.Trim(); if (!careSiteRegex.IsMatch(careSiteName)) { errorList.Add("Care site must be a minimum of 5 characters and a maximum of 80 characters long. It must start with a letter and can contain letters, numbers, and the following symbols: . -"); isValid = false; } string address = AddressTextBox.Text.Trim(); if (!addressRegex.IsMatch(address)) { errorList.Add("Address must be a minimum of 1 letter and a maximum of 40 characters long. It can contain letters, numbers, and the following symbols: # . -"); isValid = false; } string city = CityTextBox.Text.Trim(); if (!cityRegex.IsMatch(city)) { errorList.Add("City must be a minimum of 1 letter and a maximum of 30 characters long. It must start with a letter and can contain letters and the following symbols: . -"); isValid = false; } string province = ProvinceDDL.SelectedValue; if (ProvinceDDL.SelectedIndex == 0) { errorList.Add("A province must be selected from the drop-down list"); isValid = false; } #endregion if (isValid) { #region put valid data into DTO, add to database, show success message, clear page CareSiteDTO newCareSite = new CareSiteDTO(); newCareSite.caresitename = careSiteName; newCareSite.address = address; newCareSite.city = city; newCareSite.province = province; CareSiteController careSiteController = new CareSiteController(); try { careSiteController.AddCareSite(newCareSite); // show message, clear page MessageUserControl.ShowSuccessMessage("New care site " + careSiteName + " was successfully added. Please navigate to the Manage Units page to add units to the new care site."); CareSiteNameTextBox.Text = ""; AddressTextBox.Text = ""; CityTextBox.Text = ""; ProvinceDDL.SelectedIndex = 0; } catch (Exception ex) { MessageUserControl.ShowErrorMessage("Adding care site failed. Please try again. If error persists, please contact your administrator.", ex); } #endregion } else { MessageUserControl.ShowInfoList("The following errors caused adding a care site to fail: ", errorList); } }