/// <summary> /// Find a duplicate in the resource and return it (only company and title are considered in the comparison) /// </summary> /// <param name="type">the object type</param> /// <param name="vacancy">the object to find a duplicate of</param> /// <returns></returns> private VacancyObject GetDuplicate(ResourceType type, VacancyObject vacancy) { return(new JsonResourceManager <VacancyObject>(type).Resources.Find(v => Equals(v, vacancy))); }
/// <summary> /// Try returning a Vacancy object list with the data and close the dialog. /// If the data is invalid, notify the user and leave the dialog open. /// If the data already exists in a resource, ask the user if he still wants to add the duplicate. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CmdAdd_Click(object sender, EventArgs e) { _askToClose = false; var errors = GetInputFeedback(); if (errors.Count == 0 && IsInputValid()) { var toCheck = new VacancyObject(comboCompanies.Text, txtVacancy.Text, DateTime.Now, string.Empty); string msg = null; if (IsDuplicate(ResourceType.Vacancies, toCheck) && msg == null) { msg = @"Vacancy already exists in vacancies list. Do you still want to add it? (Duplicate: " + GetDuplicate(ResourceType.Vacancies, toCheck).Added + @")"; } if (IsDuplicate(ResourceType.Done, toCheck) && msg == null) { msg = @"Vacancy already exists in completed vacancies list. Do you still want to add it? (Duplicate: " + GetDuplicate(ResourceType.Done, toCheck).Added + @")"; } if (IsDuplicate(ResourceType.Blacklist, toCheck) && msg == null) { msg = @"Vacancy already exists in blacklist. Do you still want to add it? (Duplicate: " + GetDuplicate(ResourceType.Blacklist, toCheck).Added + @")"; } if (ReturnVacancies.Contains(toCheck) && msg == null) // check if vacancy was already entered before in this session { msg = @"Vacancy already entered. Do you still want to add it?"; } if (msg != null) { var dialogResult = MessageBox.Show(msg, @"Found duplicate", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (dialogResult != DialogResult.Yes) { return; } } ReturnVacancies.Add(new VacancyObject( comboCompanies.Text, txtVacancy.Text, DateTime.Now, txtUrl.Text)); if (!checkAddMultiple.Checked) { this.DialogResult = DialogResult.OK; this.Close(); } else { ClearControls(); // Indicate on the close button how many vacancies are to be saved cmdClose.Text = @"Close and Save (" + ReturnVacancies.Count + @")"; } } else { var errorMsg = string.Empty; foreach (var error in errors) { errorMsg += error + Environment.NewLine; } MessageBox.Show(errorMsg, @"Invalid input", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
/// <summary> /// Check a new vacancy for local duplicates in all three vacancy lists (only company and title are considered in the comparison) /// <param name="type">the object type</param> /// <param name="vacancy">the object to find a duplicate of</param> /// </summary> /// <returns>if the vacancy already exists in a resource</returns> private bool IsDuplicate(ResourceType type, VacancyObject vacancy) { return(new JsonResourceManager <VacancyObject>(type).Resources.Contains(vacancy)); }